I’ve noticed that using the difference between two moving averages can provide a good exit point for a position when it gets over a certain value.
Some sample code:
MA5 = Average[5](close)
MA10 = Average[10](close)
MADIFF = abs(MA5-MA10)
MADIFFpoints = MADIFF >= 2.2*pointsize
IF MADIFFpoints=1 THEN
EXITSHORT AT MARKET
ENDIF
The problem I have is that depending on the stock price, the MA difference can be quite large between different stocks.
I would therefore like to be able to dynamically calculate the average MA difference over the last 24 hours and then do something with that.
My question is, how can I code that?
Many thanks
Robert
2 moving average difference is nothing more than an MACD.
Do you want to reset the MA difference average everyday at the market open?
Yes, of course but I should add though that I’m using EMAs and not MAs as shown above, my mistake.
If you look at the difference of EMA5 and EMA10 on the FTSE it’s normally in the range 0-6 but Alphabet Inc for example is more in the range 0-100 so it’s that difference I want to use programmatically if that makes sense.
No worry, I understood your request, but you are talking about “24 hours”, so is it 24 consecutive 1 hour bar for example, of is it reset each new day day?
I’ve just been looking at a few examples. It seems that generally the range on a daily basis is fairly predictable barring massive price spikes which are hard to code for.
Therefore I think if I could establish what the range was for the differences in the two EMAs over the last 24 hour period and then say use 30% of that range as a value then that would suffice. I guess what I’m saying is the range/value wouldn’t need to be reset each day (unless it’s easy to do?) as it’s generally the same.
I have attached a couple of screen shots of the range I’m trying to capture in case it explains it better than I do. I can catch go exits when the difference get to a certain value.
Thanks Nicolas.
So my explanation doesn’t match the pictures! Taking the Microsoft example, the normal range is 0-6 and I would maybe what the 40% value of that range so say 3.9 and for Alphabet 0-100 so 40. Hopefully that clears it up.
So basically, you want an average of the EMA difference. If you only need the average of the last 24 hours on a 5 minutes charts, you’ll need to adapt the period to 12*24 (12 bars of 5 minutes in an hour):
factor = 40 //percent factor of the average
MA5 = exponentialAverage[5](close)
MA10 = exponentialAverage[10](close)
DIFF = abs(MA5-MA10)
MADIFF = average[288](diff)*(factor/100)
return diff, MAdiff
I added a “factor” for you to adapt the trigger line (in percentage).
Great, I’ll have a look at that. Thank you.