pinescript //@version=5 strategy("Moving Average Crossover", ove

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 PatternsTrend Analysis

Отказ от ответственности