Trend Following Triple MA Strategy

hold-and-hope · strategy · 42 行 · 点赞 227 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=5
strategy("Triple EMA Crossover Strategy", overlay=true)
 
// Define the input lengths for the EMAs
shortEmaLength = input(9, title="Short EMA Length")
mediumEmaLength = input(21, title="Medium EMA Length")
longEmaLength = input(55, title="Long EMA Length")
 
// Define the risk/reward ratios for SL and TP
riskRewardRatio = input(1.2, title="Risk/Reward Ratio")  // Example: risk 1 to gain 1.2
atrMultiplier = input(1.5, title="ATR Multiplier for SL") // ATR multiplier for stop loss
 
// Calculate EMAs
ema9 = ta.ema(close, shortEmaLength)
ema21 = ta.ema(close, mediumEmaLength)
ema55 = ta.ema(close, longEmaLength)
 
// Plot EMAs on the chart
plot(ema9, color=color.blue, title="9 EMA")
plot(ema21, color=color.orange, title="21 EMA")
plot(ema55, color=color.red, title="55 EMA")
 
// Define Long and Short Conditions
longCondition = ta.crossover(ema9, ema21) and ema21 > ema55
shortCondition = ta.crossunder(ema9, ema21) and ema21 < ema55
 
// Calculate the Average True Range (ATR) for better stop loss positioning
atr = ta.atr(14)  // Using a 14-period ATR for dynamic SL
 
// Execute Long trades
if (longCondition)
    // Set stop loss and take profit prices
    stopLoss = close - (atr * atrMultiplier)
    takeProfit = close + ((close - stopLoss) * riskRewardRatio)
    strategy.entry("Long", strategy.long, stop=stopLoss, limit=takeProfit)
 
// Execute Short trades
if (shortCondition)
    // Set stop loss and take profit prices
    stopLoss = close + (atr * atrMultiplier)
    takeProfit = close - ((stopLoss - close) * riskRewardRatio)
    strategy.entry("Short", strategy.short, stop=stopLoss, limit=takeProfit)

← 返回列表