Untitled

 avatar
unknown
plain_text
a month ago
2.0 kB
7
Indexable
//@version=5
strategy("MACD Breakout Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=1)

// Inputs for MACD
fastLength = input.int(12, title="Fast Length")
slowLength = input.int(26, title="Slow Length")
signalSmoothing = input.int(9, title="Signal Smoothing")

// Inputs for Breakout
lookbackPeriod = input.int(20, title="Lookback Period for Breakout")

// Risk Management Inputs
riskRewardRatio = input.float(2.0, title="Risk-Reward Ratio")
stopLossATR = input.float(1.5, title="Stop Loss Multiplier (ATR)")

// MACD Calculation
[macdLine, signalLine, _] = ta.macd(close, fastLength, slowLength, signalSmoothing)
macdCrossover = ta.crossover(macdLine, signalLine)

// Breakout Calculation
highestHigh = ta.highest(high, lookbackPeriod)
lowestLow = ta.lowest(low, lookbackPeriod)
breakoutLong = close > highestHigh
breakoutShort = close < lowestLow

// ATR Calculation for Stop Loss
atr = ta.atr(14)

// Entry Conditions
longCondition = macdCrossover and breakoutLong
shortCondition = ta.crossunder(macdLine, signalLine) and breakoutShort

// Calculate Stop Loss and Take Profit Levels
longStopLoss = highestHigh - (stopLossATR * atr)
longTakeProfit = highestHigh + (riskRewardRatio * (highestHigh - longStopLoss))
shortStopLoss = lowestLow + (stopLossATR * atr)
shortTakeProfit = lowestLow - (riskRewardRatio * (lowestLow - shortStopLoss))

// Execute Trades
if (longCondition)
    strategy.entry("Long", strategy.long)
    strategy.exit("Take Profit/Stop Loss", "Long", stop=longStopLoss, limit=longTakeProfit)

if (shortCondition)
    strategy.entry("Short", strategy.short)
    strategy.exit("Take Profit/Stop Loss", "Short", stop=shortStopLoss, limit=shortTakeProfit)

// Plotting
plot(highestHigh, title="Resistance Level", color=color.red, linewidth=1, style=plot.style_line)
plot(lowestLow, title="Support Level", color=color.green, linewidth=1, style=plot.style_line)
Leave a Comment