Saturday, July 30, 2011

I wonder where is a bug this time...

In April 2010 I've started working on a short-term forex trading strategy.

The initial results were amazing, just too good to be true.

Shortly it turned out that there was a bug in the code indeed :)

In the meantime, I had some other things to do, so the work on this promising strategy was stopped for nearly 1,5 years.

Recently I've came back to it.

I think, there still must be a bug somewhere...

Even that the generated strategy results are definitely less stable than before, they are still impressive for most of the tested periods:

Fig. Simulated results of the MD2 forex strategy, EURUSD
blue line - market
black line - strategy

The simulated strategy return for EURUSD over 501 sessions through July 22nd, 2011 is 53,81% vs 0,87% market return.

I'll hunt for bugs tomorrow :)

Thursday, July 28, 2011

Insignificance of RSI

RSI or Relative Strength Indicator is one of the popular technical analysis indicators.

It is assumed that RSI above 70 signals market is overbought, while its level below 30 suggests oversold condition.

Let's take a look at the relation between RSI(14) and historical daily changes of S&P500:

Fig. daily changes versus RSI

It doesn't gets any  better at different time scales...

Change of deviation from moving average seems neutral to returns

Relation between daily deviation from moving average to moving average of deviation seems to have no effect on asset returns:

Fig. daily deviation from moving average / average daily deviation from moving average: deviation from EMA15, EMA5 of deviation, return over next 3 days (15-5-3), EURUSD

Fig. daily deviation from moving average / average daily deviation from moving average, EURUSD 45-25-10

Fig. daily deviation from moving average / average daily deviation from moving average, EURUSD 250-25-5

Returns remain random at moderate deviations from moving average

It was not possible to spot any significant patterns when analyzing short term (3-10 days) price changes versus deviations from short- and mid-term (5, 10 ,50 ,100 days) moving averages for S&P500, EURUSD and gold futures:

Fig. returns versus deviation from moving average, S&P500

Fig. returns versus deviation from moving average, EURUSD

Fig. returns versus deviation from moving average, gold futures

Long term price deviations indicative for stock indexes, not so much for currencies or commodities

Fig. long term behavior of S&P500

The first potential buy signal is generated when price deviates more than 20-30% from the 250 days moving average.

There are two more indicators worth observing for long term signals:
  • relative standard deviation (SD) - i.e. standard deviation / average
  • long term nominal change - i.e. change of the price over 250 sessions (more or less one year)
In the case of relative SD, a buy signal is generated when the indicator exceeds 0.3.

Another buy signal emerges when price drops -40% / -50% over a year.

What's interesting, a weak signal also appears when a price increases by 50% over a year. There were very few instances of such a situation, though.

You can observe similar long term relations for other stock indexes, such as WIG20 (Warsaw Stock Exchange, Poland):

Fig. long term behavior of WIG20

The picture is not so obvious for currencies and commodities, though:

Fig. long term behavior of EURUSD

Fig. long term behavior of gold futures

Monday, July 25, 2011

The virtue of patience

Interesting: over the recent more or less 10 years (5000 sessions), anyone patient enough - i.e. ready to hold his position open over up to 250 sessions - who invested in the S&P500 index, would have lost a maximum of 4,1%, even that the market took a two huge dives in that period:

black line - return at the end of 250 days period
blue line - maximum return over a 250 days period
red line - maximum loss over a 250 days period

Of course, he would have to close his position at the right moment... ;)

A trillion dollars opportunity

The Energy Information Agency estimates technically recoverable shale gas resources in Poland at 5.295 trillion cubic meters (5.295e12 m^3).

Based on the current spot price of the natural gas ($4.3780/MMBtu) in the US where shale gas extraction shook the dynamics of the gas market, this translates into around 841 billion USD of potential revenues.

Given the current global economic situation and the idiosyncrasies of the European gas market this is a rather conservative price estimate (I will return to it later).

1 MMbtu / 1027 Btu/cf = 973.71 cf (cubic feet, feet^3)

973.71 cf = 27.572 m^3

1000 m^3 / 27.572 m^3 (1 MMbtu) = 36.2687

1000 m^3 * NG.F = $4.3780 * 36.2687 = $158.784 / 1000 m^3

5.295e12 m^3 * $158.784 / 1000 m^3 = $840 761 280 000

OK, so let's go back to some more pricing analysis.

The average price of the natural gas sold by Gazprom reached $306 at the end of 2010 and is expected to increase to $400 in 2011.

Meanwhile the average natural gas spot price over 10 years in the US was $5.69/Btu or $206.369/1000 m^3.

As the production of shale gas in Europe increases and LNG-based geographical arbitrage equalizes prices among regions, the European prices will come down over time.

Hence the long term average spot price is probably the better estimate of the value of reserves. Then the gas reserves of 5e12 m^3 translate into nearly 1,1 trillion USD.

If only reserves are as large as estimated by EIA...

Sunday, July 24, 2011

Simple return to mean as investment signal?

Financial instruments fluctuate around their means. Sometimes the price of an instrument is above this instrument's mean, and sometimes the other way. Price tend to return to the mean. But can you use this mechanism for buying an selling financial instruments?

Let's check the most simple average-based rule on the S&P500 index over the previous 10,000 sessions.

RULE: buy or short sell the index when the price deviates by X% from its mean.

Test 1: EMA=15, position holding period = 3

Buying at deviation from the mean equal to -0.2 (20%) would bring nearly a 10% gain, but there was just one such situation over the whole period being analyzed:

t= -0.2 n= 1 avg= 0.09904726 min= 0.09904726 max= 0.09904726 

Short term deviation doesn't look like a viable signal.

Test 2: EMA=200, position holding period = 3

No signals generated (testing only at 0.X, so 0.35 is not covered):

Large deviation from long term average does not influence short period movements.

Test 3: EMA=200, position holding period = 25

One weak signal: deviation = -0.3 increased probability of positive return. One could expect a 9,6% return, but there is also a risk of a loss of -7,1%.

t= -0.3 u= 13 d= 3 avg= 0.0964092 min= -0.07157303 max= 0.2284035 

Test 4: EMA=45, position holding period = 10

Another case with weak signal. This time at deviation = -0.2.

t= -0.2 u= 8 d= 2 avg= 0.05415763 min= -0.02528302 max= 0.1521247

Conclusion: Price deviations from the moving average (mean)  "larger" than -0.2 significantly increase probability of an increase in price in the case of S&P500, but such deviations are rare events.


Additional test: 35000+ quotations, MA=200, holding period = 25.

Both "strong" and "weak" signal has been generated. Average expected return at each deviation added to the chart:


It is getting to start getting more interesting when we consider long moving averages and long expected position holding period. Under such conditions, both the soft and hard signals give pretty solid expected gains with limited loss potential.

Test: EMA=250, holding period=250

soft signal:

t= -0.2 u= 178 d= 3 avg= 0.2415313 min= -0.1348127 max= 0.5206657 

hard signal:

t= -0.3 n= 29 avg= 0.3260642 min= 0.1503819 max= 0.5206657 

Test: EMA=100, holding period=250

t= -0.2 n= 41 avg= 0.2482144 min= 0.04344005 max= 0.5206657


Measuring price deviation from moving average in terms of standard deviations makes the picture more messy:

Test: SMA=250, holding period=250, deviation in terms of standard deviations: