Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=3
- strategy(title="55 EMA Trading Strategy", shorttitle="EMA55", overlay=true, default_qty_type=strategy.percent_of_equity, initial_capital=10000000000, default_qty_value=100, commission_value=0.15)
- trailingStopLevelLong = na
- trailingStopLevelShort = na
- sourceType = input("OHLC4 HA", title="Source type", options=["Close", "OHLC4", "OHLC4 HA"])
- len1 = input(defval = 3, title = "Ema 1 length", minval = 1)
- len2 = input(defval = 10, title = "Ema 2 length", minval = 2)
- len3 = input(defval = 20, title = "Ema 3 length", minval = 3)
- len4 = input(defval = 55, title = "Ema 4 length", minval = 4)
- len5 = input(defval = 100, title = "Ema 5 length", minval = 2)
- FromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)
- FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
- FromYear = input(defval = 2017, title = "From Year", minval = 2015)
- ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
- ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
- ToYear = input(defval = 9999, title = "To Year", minval = 2017)
- start = timestamp(FromYear, FromMonth, FromDay, 00, 00)
- finish = timestamp(ToYear, ToMonth, ToDay, 23, 59)
- window() => time >= start and time <= finish ? true : false
- handleHA = heikinashi(tickerid)
- openHA = security(handleHA, period, open)
- highHA = security(handleHA, period, high)
- lowHA = security(handleHA, period, low)
- closeHA = security(handleHA, period, close)
- src = sourceType == "Close" ? close : sourceType == "OHLC4" ? ohlc4 : (closeHA + highHA + lowHA + openHA) / 4
- ma8 = ema(src, len1)
- ma13 = ema(src, len2)
- ma21 = ema(src, len3)
- ma55 = ema(src, len4)
- ma200 = ema(src, len5)
- ma55crossover = crossover(ma21, ma55)
- ma55crossunder = crossunder(ma21, ma55)
- longEntry = ma8 > ma13 and ma21 > ma55 and (ma55crossover or ma55crossover[1] or ma55crossover[2] or ma55crossover[3] or ma55crossover[4]) and ma8 > ma200
- shortEntry = ma8 < ma13 and ma21 < ma55 and (ma55crossunder or ma55crossunder[1] or ma55crossunder[2] or ma55crossunder[3] or ma55crossunder[4]) and ma8 < ma200
- shortExit = ma8 > ma13 and ma21 > ma55 and (ma55crossover or ma55crossover[1] or ma55crossover[2] or ma55crossover[3] or ma55crossover[4])
- longExit = ma8 < ma13 and ma21 < ma55 and (ma55crossunder or ma55crossunder[1] or ma55crossunder[2] or ma55crossunder[3] or ma55crossunder[4])
- transp1 = 60
- transp2 = 48
- transp3 = 36
- transp4 = 24
- transp5 = 12
- transp6 = 0
- plot(ma8, color=aqua, transp=transp2, style=line, title="8", linewidth=1)
- plot(ma13, color=aqua, transp=transp3, style=line, title="13", linewidth=1)
- plot(ma21, color=aqua, transp=transp4, style=line, title="21", linewidth=1)
- plot(ma55, color=aqua, transp=transp5, style=line, title="55", linewidth=1)
- plot(ma200, color=aqua, transp=transp6, style=line, title="200", linewidth=1)
- longCondition = longEntry and not longEntry[1] and not longEntry[2] and not longEntry[3] and not longEntry[4]
- shortCondition = shortEntry and not shortEntry[1] and not shortEntry[2] and not shortEntry[3] and not shortEntry[4]
- longClose = longExit and not longExit[1] and not longExit[2] and not longExit[3] and not longExit[4]
- shortClose = shortExit and not shortExit[1] and not shortExit[2] and not shortExit[3] and not shortExit[4]
- strategy.entry("Long", strategy.long, when = window() and longCondition)
- strategy.close("Long", when=window() and longClose)
- strategy.entry("Short", strategy.short, when = window() and shortCondition)
- strategy.close("Short", when = window() and shortClose)
- slPercent = input(6.67, title='Stop Loss %', type=float, step=0.1) / 100
- tsPercent = input(0.0, title='Trailing Stop Loss %', type=float, step=0.1) / 100
- stopLevelLong = strategy.position_size > 0 ? strategy.position_avg_price * (1 - slPercent) : na
- stopLevelShort = strategy.position_size < 0 ? strategy.position_avg_price * (1 + slPercent) : na
- trailingStopLevelLong := strategy.position_size > 0 ? (na(trailingStopLevelLong[1]) ? high * (1 - tsPercent) : max(high * (1 - tsPercent), trailingStopLevelLong[1])) : na
- trailingStopLevelShort := strategy.position_size < 0 ? (na(trailingStopLevelShort[1]) ? low * (1 + tsPercent) : min(low * (1 + tsPercent), trailingStopLevelShort[1])) : na
- plotshape(slPercent > 0 ? stopLevelLong : na, location=location.absolute, style=shape.xcross, color=orange)
- plotshape(slPercent > 0 ? stopLevelShort : na, location=location.absolute, style=shape.xcross, color=orange)
- plotshape(tsPercent > 0 ? trailingStopLevelLong : na, location=location.absolute, style=shape.xcross, color=blue)
- plotshape(tsPercent > 0 ? trailingStopLevelShort : na, location=location.absolute, style=shape.xcross, color=blue)
- plotshape(strategy.position_avg_price, location=location.absolute, style=shape.circle, color=white)
- if slPercent > 0 and not (longCondition or shortCondition or longClose or shortClose)
- strategy.exit("Stop Loss (Long)", "Long", stop=stopLevelLong)
- strategy.exit("Stop Loss (Short)", "Short", stop=stopLevelShort)
- else
- strategy.cancel("Stop Loss (Long)")
- strategy.cancel("Stop Loss (Short)")
- if tsPercent > 0 and not (longCondition or shortCondition or longClose or shortClose)
- strategy.exit("Trailing Stop Loss (Long)", "Long", stop=trailingStopLevelLong)
- strategy.exit("Trailing Stop Loss (Short)", "Short", stop=trailingStopLevelShort)
- else
- strategy.cancel("Trailing Stop Loss (Long)")
- strategy.cancel("Trailing Stop Loss (Short)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement