搜索
产品
社区
市场
新闻
经纪商
更多
CN
开始
社区
/
观点
/
pinescript //@version=5 strategy("Moving Average Crossover", ove
美元/日元
pinescript //@version=5 strategy("Moving Average Crossover", ove
由RockyRintharoSitanggang提供
关注
关注
2023年7月30日
0
2023年7月30日
pinescript
//
version
=5
strategy("Moving Average Crossover", overlay=true)
// Define inputs
length1 = input(20, "MA1 Length")
length2 = input(10, "MA2 Length")
stopLossOption = input("Fixed 5%", "Stop Loss Option", options=["Fixed 5%", "ATR 14 (2x SL)", "ATR 14 (2x SL) + Trail"])
// Calculate moving averages
ma1 = ta.sma(close, length1)
ma2 = ta.sma(close, length2)
// Define MACD variables
macdLine = ta.ema(close, 12) - ta.ema(close, 26)
signalLine = ta.ema(macdLine, 9)
// Define crossover and crossunder conditions
maCrossUp = ta.crossover(ma1, ma2)
maCrossDown = ta.crossunder(ma1, ma2)
macdCrossUp = ta.crossover(macdLine, signalLine)
macdCrossDown = ta.crossunder(macdLine, signalLine)
// Define stop loss and take profit levels
if stopLossOption == "Fixed 5%"
stopLoss = strategy.position_avg_price * 0.95
takeProfit = strategy.position_avg_price * 1.1
else if stopLossOption == "ATR 14 (2x SL)"
atr14 = ta.atr(14)
stopLoss = strategy.position_avg_price - (2 * atr14)
takeProfit = strategy.position_avg_price + (2 * atr14)
else if stopLossOption == "ATR 14 (2x SL) + Trail"
atr14 = ta.atr(14)
stopLoss = strategy.position_avg_price - (2 * atr14)
takeProfit = strategy.position_avg_price + (2 * atr14)
strategy.exit("Exit", "Buy", stop=stopLoss, trail_points=atr14)
// Define strategy entry and exit conditions
if (maCrossUp and macdCrossUp)
strategy.entry("Buy", strategy.long)
else if (maCrossDown and macdCrossDown)
strategy.entry("Sell", strategy.short)
// Plotting moving averages
plot(ma1, color=color.blue, title="MA1")
plot(ma2, color=color.red, title="MA2")
// Plotting MACD lines
plot(macdLine, color=color.green, title="MACD Line")
plot(signalLine, color=color.orange, title="Signal Line")
// Plotting strategy entry points
strategy.entry("Buy", strategy.long, when=maCrossUp and macdCrossUp)
strategy.entry("Sell", strategy.short, when=maCrossDown and macdCrossDown)
// Plotting stop loss and take profit levels
plot(stopLoss, color=color.red, title="Stop Loss")
plot(takeProfit, color=color.green, title="Take Profit")
Chart Patterns
Trend Analysis
RockyRintharoSitanggang
关注
免责声明
这些信息和出版物并不意味着也不构成TradingView提供或认可的金融、投资、交易或其它类型的建议或背书。请在
使用条款
阅读更多信息。