I have always hated averages. They are horrible lagging things that can easily be curve fitted but I decided to code something to see a little more easily how bad they are and to see if any of the popular average types are better than others.
So I coded this simple code that uses arrays to record the average gain/loss of any candle if it is preceded by a candle that meets a preset average condition. This condition can be something like close > average or low > average or even fast average > slow average. This tests long conditions but you can also test short by using the reverse condition.
The 8 most popular average types are tested so that their performance can be compared. The result is the average gain per candle. A datum of every candle on the chart is also calculated so we can see how our average condition performance compares to buy and hold.
//Period setting for average
//p = 24
if barindex > 1 then
//Choose only 1 of these result conditions
//
c1 = close - open //for long
//c1 = open - close //for short
if islastbarupdate then
$lastopen[0]=open
endif
if open<>$lastopen[0] then
for t = 0 to 8
//Choose only 1 of these entry conditions
//
//Long Conditions
//avgcond = low[1] > average[p,t](customclose)[1] //Low is above average
//avgcond = close[1] > average[p,t](customclose)[1] //Close is above average
//avgcond = average[p,t](close[1]) > average[p,t](close[2]) //Average is rising
avgcond = average[p,t](close[1]) > average[p*2,t](close[1]) //Fast average is above slow average
//Short Conditions
//avgcond = high[1] < average[p,t](customclose[1]) //High is below average
//avgcond = close[1] < average[p,t](customclose[1]) //Close is below average
//avgcond = average[p,t](close[1]) < average[p,t](close[2]) //Average is falling
//avgcond = average[p,t](close[1]) < average[p*2,t](close[1]) //Fast average is below slow average
if avgcond then
$count[t] = $count[t] + 1
if c1 then
$total[t] = $total[t] + c1
$avg[t] = ($total[t]/$count[t])
endif
endif
next
endif
dcount = dcount + 1
dtotal = dtotal + c1
davg = (dtotal/dcount)
endif
return $avg[0] as "Simple", $avg[1] as "Exponential", $avg[2] as "Weighted", $avg[3] as "Wilder", $avg[4] as "Triangular", $avg[5] as "End Point", $avg[6] as "Time Series", $avg[7] as "Hull", $avg[8] as "Zero Lag", davg as "Datum"
The first attached image is of a test on the DJI daily chart. It tests long performance if the 24 day average > 48 day average. All averages have a lower average gain per candle than the buy and hold datum except for Hull, Zero Lag, Time Series and End Point averages where the performance is improved. So perhaps a simple filter like this can improve a strategy?
The second image tests if close > 24 day average and only the Time Series average beats buy and hold.