Hi,
I have written a simple EMA crossover strategy which I am now forward testing in demo mode. It is in the market close to 100% of the time and goes long when fast EMA crosses over the slow and vice versa for short positions and long exits. The strategy code is pasted below. The problem I am having is that it seems to be failing to close positions. E.g. Over night last night on the Nasdaq it when short at the appropriate place but failed to close out the position this morning when the fast EMA crossed above the slow (see attached image).
Does anyone have an idea as to why this is occurring and how it can be resolved?
Thanks,
Roj
//-------------------------------------------------------------------------
// Main code : EMA crossover NASDAQ m3
//-------------------------------------------------------------------------
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
EMAfast = 9
EMAslow = 30
//Risk Management
Capital = 5000
Risk = 0.01
StopLoss = abs(close-ExponentialAverage[EMAslow](close))
equity = Capital + StrategyProfit
maxrisk = round(equity*Risk)
PositionSize = abs(round((maxrisk/StopLoss)/PointValue)*pipsize)
// Conditions to enter long positions
indicator1 = ExponentialAverage[EMAfast](close)
indicator2 = ExponentialAverage[EMAslow](close)
c1 = (indicator1 CROSSES OVER indicator2)
IF c1 THEN
BUY PositionSize PERPOINT AT MARKET
ENDIF
// Conditions to exit long positions
indicator3 = ExponentialAverage[EMAfast](close)
indicator4 = ExponentialAverage[EMAslow](close)
c2 = (indicator3 CROSSES UNDER indicator4)
IF c2 THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
indicator5 = ExponentialAverage[EMAfast](close)
indicator6 = ExponentialAverage[EMAslow](close)
c3 = (indicator5 CROSSES UNDER indicator6)
IF c3 THEN
SELLSHORT PositionSize PERPOINT AT MARKET
ENDIF
// Conditions to exit short positions
indicator7 = ExponentialAverage[EMAfast](close)
indicator8 = ExponentialAverage[EMAslow](close)
c4 = (indicator7 CROSSES OVER indicator8)
IF c4 THEN
EXITSHORT AT MARKET
ENDIF
Most likely because of the 3 minute time frame that you are using. If the chart that you show was started at a different minute to the minute that the strategy was started at then you will get a different chart. In fact you can get three different charts of the 3 minute time frame depending upon which of the three minutes that you start the chart on!
Have to ask (quicker than thinking as I have to go out) … is the reasoning above the same for, for example, the 5 min Chart??
Five minute chart candles are always at 0000 0005 0010 0015 etc but a three minute candle is constructed from whenever the chart or strategy starts. If could be 0000 or 0001 or 0002. The open at 0000 is different to that at 0001 and so we can instantly see that a different three minute candle will be drawn depending on the start time. The close is different too and you are plotting an average of it so it stands to reason that the two different start times will not have the same average returned.
Thanks very much, I will try m5 or m1 and see how it goes. I chose m3 because it gave the best results in backtesting.