#50 EMA and 100 EMA
ema50 = EMA(applyTo: close, period:50)
ema100 = EMA(applyTo: close, period:100)
#MACD and MACD signal.
macd = MACD(
applyTo: close,
fastPeriod: 12,
slowPeriod: 26
)
macdSignal = MACDSignal(
applyTo: close,
signalPeriod: 9,
fastPeriod: 12,
slowPeriod: 26
)
#Pip calculation
pip10 =
pip5 =
longEntryRule = open > (ema50 + pip10)
longEntryRule2 = open > (ema100 + pip10)
series longEntryRule3:
once: 0
rest: CrossesUpwards(value: macd,crosses: macdSignal)
#Final Long Entry Rule
FinalLongEntryRule = longEntryRule and longEntryRule2 and ( longEntryRule3 or longEntryRule3[1] or longEntryRule3[2] or longEntryRule3[3] or longEntryRule3[4] )
#Setting initial Stop
period = 5
once: open
rest: open
for distance from 1 to (period - 1)
InitialStop = low[distance] if isNewLow else InitialStop
# Signals
#StopRepeat is to protect Take profit and StopLoss base on open postions.
series StopRepeat:
once: 0
rest: 1 if (FinalLongEntryRule and StopRepeat == 0) else
series InitialStop:
once: open
rest: StopLoss if ((FinalLongEntryRule) and StopRepeat == 1) else InitialStop
series TakeProfit:
once: 0
rest : ((open - InitialStop) * 2 + open) if ((FinalLongEntryRule) and StopRepeat == 1) else TakeProfit
if (open > TakeProfit or open < InitialStop) update StopRepeat: 0
if (open <= TakeProfit or open >= InitialStop) update StopRepeat: 2
#Final Long Exit Rule
FinalLongExitRule = open > TakeProfit or open <
enter long when FinalLongEntryRule
exit long when FinalLongExitRule