OPEN-SOURCE SCRIPT

Buy/Sell Signals (Dynamic v2)

87
//version=5
indicator(title="Buy/Sell Signals (Dynamic v2)", shorttitle="Buy/Sell Dyn v2", overlay=true)

// Input for moving average lengths
lengthMA = input.int(20, title="Moving Average Length")
lengthEMA = input.int(5, title="Exponential Moving Average Length")

// Calculate Moving Averages
ma = ta.sma(close, lengthMA)
ema = ta.ema(close, lengthEMA)

// --- Buy Signal Conditions ---
buyMarketBelowMA = close < ma
buyMarketBelowEMA = close < ema
buyEMABelowMA = ema < ma
buyMarketCondition = buyMarketBelowMA and buyMarketBelowEMA and buyEMABelowMA

buyFollowingHighNotTouchedEMA = high[1] < ema[1]
buyCurrentCrossCloseAboveFollowingHigh = high > high[1] and close > high[1]
buySignalCondition = buyMarketCondition and buyFollowingHighNotTouchedEMA and buyCurrentCrossCloseAboveFollowingHigh

// Plot Buy Signal
plotshape(buySignalCondition, title="Buy Signal", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)

// --- Sell Signal Conditions (Occurring After a Buy Signal Sequence) ---
sellMarketAboveMA = close > ma
sellMarketAboveEMA = close > ema
sellEMAAboveMA = ema > ma
sellMarketConditionSell = sellMarketAboveMA and sellMarketAboveEMA and sellEMAAboveMA

var bool buySignalOccurredRecently = false
if buySignalCondition
buySignalOccurredRecently := true

sellSignalCondition = buySignalOccurredRecently and sellMarketConditionSell and close < close[1]

// Plot Sell Signal
plotshape(sellSignalCondition, title="Sell Signal", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small)

// Reset the buySignalOccurredRecently only after a sell signal
if sellSignalCondition
buySignalOccurredRecently := false

// Plot the Moving Averages for visual reference
plot(ma, color=color.blue, title="MA")
plot(ema, color=color.red, title="EMA")

Declinazione di responsabilità

Le informazioni ed i contenuti pubblicati non costituiscono in alcun modo una sollecitazione ad investire o ad operare nei mercati finanziari. Non sono inoltre fornite o supportate da TradingView. Maggiori dettagli nelle Condizioni d'uso.