OPEN-SOURCE SCRIPT

Advanced Strategy: Buy and Sell

//version=5
indicator("Advanced Strategy: Buy and Sell", overlay=true)

// User Inputs
ema_fast_length = input.int(12, title="EMA Fast Period")
ema_slow_length = input.int(26, title="EMA Slow Period")
rsi_period = input.int(14, title="RSI Period")
macd_fast = input.int(12, title="MACD Fast Period")
macd_slow = input.int(26, title="MACD Slow Period")
macd_signal = input.int(9, title="MACD Signal Period")
bb_length = input.int(20, title="Bollinger Bands Period")
bb_mult = input.float(2.0, title="Bollinger Bands Std Dev")
adx_period = input.int(14, title="ADX Period")
adx_threshold = input.int(20, title="Minimum ADX for Trend Validation")

// Indicator Calculations
ema_fast = ta.ema(close, ema_fast_length)
ema_slow = ta.ema(close, ema_slow_length)
rsi = ta.rsi(close, rsi_period)
[macd_line, signal_line, _] = ta.macd(close, macd_fast, macd_slow, macd_signal)
[bb_upper, bb_middle, bb_lower] = ta.bb(close, bb_length, bb_mult)

// ADX Calculation
true_range = ta.rma(ta.tr, adx_period)
plus_dm = ta.rma(ta.change(high) > ta.change(low) ? math.max(ta.change(high), 0) : 0, adx_period)
minus_dm = ta.rma(ta.change(low) > ta.change(high) ? math.max(ta.change(low), 0) : 0, adx_period)

plus_di = (plus_dm / true_range) * 100
minus_di = (minus_dm / true_range) * 100
dx = math.abs(plus_di - minus_di) / (plus_di + minus_di) * 100
adx = ta.rma(dx, adx_period)

// Buy Rules
buy_signal = ta.crossover(ema_fast, ema_slow) and rsi < 50 and macd_line > signal_line and close < bb_lower and adx > adx_threshold

// Sell Rules
sell_signal = ta.crossunder(ema_fast, ema_slow) and rsi > 50 and macd_line < signal_line and close > bb_upper and adx > adx_threshold

// Plot Indicators on the Chart
plot(ema_fast, color=color.green, title="EMA Fast")
plot(ema_slow, color=color.red, title="EMA Slow")
plot(bb_upper, color=color.orange, title="Bollinger Upper Band")
plot(bb_lower, color=color.orange, title="Bollinger Lower Band")

// Buy and Sell Signals
plotshape(buy_signal, style=shape.labelup, location=location.belowbar, color=color.green, size=size.small, title="Buy Signal")
plotshape(sell_signal, style=shape.labeldown, location=location.abovebar, color=color.red, size=size.small, title="Sell Signal")

// Alerts
alertcondition(buy_signal, title="Buy Alert", message="Buy Signal Detected!")
alertcondition(sell_signal, title="Sell Alert", message="Sell Signal Detected!")
Breadth IndicatorsCandlestick analysisChart 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