OPEN-SOURCE SCRIPT

Buy/Sell Signals for Natural Gas Futures

//version=5
indicator("Buy/Sell Signals for Natural Gas Futures", overlay=true)

// Input for Moving Averages
emaShortLength = input.int(20, title="Short EMA Length")
emaLongLength = input.int(50, title="Long EMA Length")

// Input for ATR (Stop-Loss, Take-Profit)
atrLength = input.int(14, title="ATR Length")
atrMultiplier = input.float(1.5, title="ATR Multiplier")
riskRewardRatio = input.float(2.0, title="Risk-Reward Ratio")

// Calculate Moving Averages
emaShort = ta.ema(close, emaShortLength)
emaLong = ta.ema(close, emaLongLength)

// Trend Detection
isUptrend = emaShort > emaLong
isDowntrend = emaShort < emaLong

// Average True Range (ATR) for Volatility-based Stop-Loss/Take-Profit
atr = ta.atr(atrLength)

// Breakout/Breakdown Levels (5-bar high/low for breakout/fall)
breakoutLevel = ta.highest(high, 5)
breakdownLevel = ta.lowest(low, 5)

// Buy Signal Condition
buySignal = close > breakoutLevel and isUptrend

// Sell Signal Condition
sellSignal = close < breakdownLevel and isDowntrend

// Stop-Loss and Take-Profit Levels (using ATR)
stopLossLong = close - (atr * atrMultiplier)
takeProfitLong = close + (atr * atrMultiplier * riskRewardRatio)
stopLossShort = close + (atr * atrMultiplier)
takeProfitShort = close - (atr * atrMultiplier * riskRewardRatio)

// Plot Buy/Sell Signals
plotshape(series=buySignal, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY")
plotshape(series=sellSignal, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL")

// Plot Stop-Loss and Take-Profit Levels for Buy and Sell
plotshape(series=buySignal ? stopLossLong : na, title="Stop-Loss Long", color=color.red, style=shape.triangledown, location=location.absolute, offset=-1, size=size.small)
plotshape(series=buySignal ? takeProfitLong : na, title="Take-Profit Long", color=color.green, style=shape.triangleup, location=location.absolute, offset=-1, size=size.small)

plotshape(series=sellSignal ? stopLossShort : na, title="Stop-Loss Short", color=color.red, style=shape.triangledown, location=location.absolute, offset=-1, size=size.small)
plotshape(series=sellSignal ? takeProfitShort : na, title="Take-Profit Short", color=color.green, style=shape.triangleup, location=location.absolute, offset=-1, size=size.small)

// Highlight the trend on the background (green for uptrend, red for downtrend)
bgcolor(isUptrend ? color.new(color.green, 90) : isDowntrend ? color.new(color.red, 90) : na)

// Alerts for Buy/Sell Signals
alertcondition(buySignal, title="Buy Signal Alert", message="Buy Signal Detected: Price has broken above resistance in an uptrend.")
alertcondition(sellSignal, title="Sell Signal Alert", message="Sell Signal Detected: Price has broken below support in a downtrend.")
Chart patterns

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?

Disclaimer