# Indicators
#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
)
#Rule/Triggers
#Pip calculation
pip10 = open * 0.010
pip5 = open * 0.005
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
series StopLoss:
once: open
for distance from 1 to (period - 1)
if low[distance] < StopLoss update StopLoss: low[distance]
# 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 StopRepeat
series InitialStop:
once: open
rest: StopLoss if (StopRepeat == 1) else InitialStop
series TakeProfit:
once: 0
rest : ((open - InitialStop) * 2 + open) if (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 < InitialStop or open < (ema50-pip10)
enter long when FinalLongEntryRule
exit long when FinalLongExitRule