Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- strategy("EMA Trend Meter and SMI Strategy", overlay=true, pyramiding=5, commission_type=strategy.commission.percent, commission_value=0.02, initial_capital=1000, default_qty_type = strategy.cash, default_qty_value = 1000)
- // Inputs
- risk_ratio = input(2.0, title="Risk Management Ratio")
- prevent_short_below_ema = input(true, title="Prevent Short Trades Below EMA")
- prevent_long_above_ema = input(true, title="Prevent Long Trades Above EMA")
- ema_period_1 = input(13, title="EMA Period 1")
- ema_period_2 = input(21, title="EMA Period 2")
- ema_period_3 = input(34, title="EMA Period 3")
- ema_period_4 = input(55, title="EMA Period 4")
- ema_period_5 = input(89, title="EMA Period 5")
- smi_a = input(10, title="SMI Percent K Length")
- smi_b = input(3, title="SMI Percent D Length")
- smi_c = input(13, title="SMI EMA Signal Length")
- smi_smooth_period = input(5, title="SMI Smoothing Period")
- smi_ob = input(40, title="SMI Overbought")
- smi_os = input(-40, title="SMI Oversold")
- // EMA Trend Meter
- // Uses Exponential Moving Averages (EMAs) to identify trends.
- // Modified default settings to make it less sensitive and only trigger signals when a strong trend is likely.
- ema1 = ta.ema(close, ema_period_1)
- ema2 = ta.ema(close, ema_period_2)
- ema3 = ta.ema(close, ema_period_3)
- ema4 = ta.ema(close, ema_period_4)
- ema5 = ta.ema(close, ema_period_5)
- plot(ema1, color=color.green, title="EMA1")
- plot(ema2, color=color.green, title="EMA2")
- plot(ema3, color=color.green, title="EMA3")
- plot(ema4, color=color.green, title="EMA4")
- plot(ema5, color=color.green, title="EMA5")
- // Stochastic Momentum Index (SMI)
- // Measures the momentum of price movements.
- // Uses a custom version by "surjithctl" with adjusted colors for better visualization.
- // EMA Signal length = 13
- a = smi_a // Percent K Length
- b = smi_b // Percent D Length
- c = smi_c // EMA Signal Length
- smooth_period = smi_smooth_period // Smoothing Period
- ob = smi_ob // Overbought
- os = smi_os // Oversold
- // Range Calculation
- ll = ta.lowest(low, a)
- hh = ta.highest(high, a)
- diff = hh - ll
- rdiff = close - (hh+ll)/2
- avgrel = ta.ema(rdiff, b)
- avgdiff = ta.ema(diff, b)
- // SMI calculations
- SMI = avgdiff != 0 ? (avgrel/(avgdiff/2)*100) : 0.0
- // Apply smoothing to SMI
- SMI_smoothed = ta.sma(SMI, smooth_period)
- SMIsignal = ta.ema(SMI_smoothed, b)
- emasignal = ta.ema(SMI_smoothed, c)
- // Plotting
- plot(SMI_smoothed, "Stochastic", color=color.black)
- plot(emasignal, "EMA", color=color.red)
- h0 = hline(ob, "Overbought", color=color.gray)
- h1 = hline(os, "Oversold", color=color.gray)
- level_ob = ob
- level_obsmi = SMI_smoothed > level_ob ? SMI_smoothed : level_ob
- level_os = os
- level_ossmi = SMI_smoothed < level_os ? SMI_smoothed : level_os
- p1 = plot(level_ob, display=display.none)
- p2 = plot(level_obsmi, display=display.none)
- p3 = plot(level_os, display=display.none)
- p4 = plot(level_ossmi, display=display.none)
- fill(p1, p2, color=color.new(color.red, 60), title='OverBought')
- fill(p3, p4, color=color.new(color.green, 60), title='OverSold')
- // Long Entry Signal
- // All lines of the EMA Trend Meter turn green.
- // SMI white line dips below the blue line (potential pullback).
- // SMI white line crosses above the red line (bullish momentum).
- longEntry = ta.crossover(SMI_smoothed, emasignal) and ema1 > ema2 and ema2 > ema3 and ema3 > ema4 and ema4 > ema5
- if (prevent_long_above_ema)
- longEntry := longEntry and close < ema5
- // Short Entry Signal
- // All lines of the EMA Trend Meter turn red.
- // SMI white line goes above the blue line.
- // SMI white line crosses below the red line (bearish momentum).
- shortEntry = ta.crossunder(SMI_smoothed, emasignal) and ema1 < ema2 and ema2 < ema3 and ema3 < ema4 and ema4 < ema5
- if (prevent_short_below_ema)
- shortEntry := shortEntry and close > ema5
- // Risk Management
- // Use a 2:1 risk-to-reward ratio for take profit and stop loss levels.
- longStop = longEntry ? low - syminfo.mintick * 2 : na
- longTakeProfit = longEntry ? low + risk_ratio * (low - low[1]) : na
- shortStop = shortEntry ? high + syminfo.mintick * 2 : na
- shortTakeProfit = shortEntry ? high - risk_ratio * (high[1] - high) : na
- // Strategy Entry and Exit
- if (longEntry)
- strategy.entry("Long", strategy.long)
- if (shortEntry)
- strategy.entry("Short", strategy.short)
- if (strategy.position_size > 0)
- strategy.exit("Exit Long", "Long", stop=longStop, limit=longTakeProfit)
- if (strategy.position_size < 0)
- strategy.exit("Exit Short", "Short", stop=shortStop, limit=shortTakeProfit)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement