Trend following strategies all work similarly. I’ll use the simplest example. I need coding for the following.
Condition 1: EMA20 crosses over EMA50
After condition 1 is met, I want a counter for condition 2.
Condition 2 means stochastic crosses over 20. There we buy at market.
So AFTER the EMACross, a pullback of the stochastic should be bought, whereby this pullback includes a counter. I want to know how many pullbacks are profitable after an EMACross. Often only the first pullback is profitable, sometimes there are 2 or 3. This counter should therefore be optimisable.
Can someone code this for me please?
There you go:
DEFPARAM CumulateOrders = False
ONCE Winners = 0
ONCE Losers = 0
ONCE CrossOVER = 0
ONCE CrossUNDER = 0
Ema20 = average[20,1](close)
Ema50 = average[50,1](close)
myST = Stochastic[10,6](close)
//
IF Not OnMarket AND OnMarket[1] THEN
IF StrategyProfit > StrategyProfit[1] THEN
Winners = Winners + 1
ELSE
Losers = Losers + 1
ENDIF
ENDIF
//
IF CrossOVER = 0 THEN
CrossOVER = Ema20 CROSSES OVER Ema50
IF CrossOVER THEN
CrossUNDER = 0
Winners = 0
Losers = 0
ENDIF
ENDIF
IF CrossUNDER = 0 THEN
CrossUNDER = Ema20 CROSSES UNDER Ema50
IF CrossUNDER THEN
CrossOVER = 0
ENDIF
ENDIF
//
STcross = myST CROSSES OVER 20
IF STcross AND CrossOVER THEN
IF LongOnMarket THEN
IF PositionPerf > 0 THEN
Winners = Winners + 1
ELSE
Losers = Losers + 1
ENDIF
SELL AT Market
ENDIF
BUY AT Market
ENDIF
//
graph Winners coloured("Blue")
graph Losers coloured("Red")