0

I am trying to code the following strategy:

Buy: (1) signal one being the 50 ema crossing over the 200 ema. (2) signal two, wait for three red candles, with the final candle closing under the 50 ema. (3) signal three, next candle is immediately engulfing.

Only buy when all three signals are true.

The strategy is the same for short positions expect with reversed logic.

I have managed to input (2) and (3) with the following but I am struggling with the code required for signal 1.

Any help/tips would be appreciated.


//@version=5

//Indicators

strategy("Enculfing Candles Long", overlay=true, initial_capital = 2000, default_qty_value = 100, default_qty_type = strategy.percent_of_equity)

fastma = ta.ema(close, 50)
slowma = ta.ema(close, 200)
atr = ta.atr(14)
plot(fastma, "fastma", color=color.blue)
plot(slowma, "slowma", color= color.yellow)

//Buy Conditions

lowerClose1 = (close[1]<close[2]) and (close[1]<fastma)
lowerClose2 = (close[2]<close[3]) and (close[2]>fastma)
lowerClose3 = (close[3]<close[4]) and close[3]>fastma
higherClose = close>open[1]

buysignal1 = (fastma>slowma)
buysignal2 = close>fastma

longcond1 = buysignal1 and buysignal2 and lowerClose1 and lowerClose2 and lowerClose3 and higherClose

//Strategy Execution

timePeriod = time>= timestamp(syminfo.timezone, 2021, 01, 01, 0, 0)
InTrade = strategy.position_size > 0
notInTrade = strategy.position_size <= 0 

strategy.entry("buy", strategy.long, when= notInTrade and longcond1)
strategy.exit(id="buy", loss = (atr*200000), profit = (atr*300000))

0 Answers0