OPEN-SOURCE SCRIPT

Filtered Buy/Sell Signals After Stochastic Extreme Crossover

306
//version=5
indicator("Filtered Buy/Sell Signals After Stochastic Extreme Crossover", overlay=true)

// Define parameters
lr_period = 6
tma_period = 5
stoch_k = 8
stoch_d = 4
stoch_smooth = 4

// Compute Linear Regression Forecast (6, close)
linRegForecast = ta.linreg(close, lr_period, 0)

// Compute Triangular Moving Average (5)
tma = ta.sma(ta.sma(close, math.ceil(tma_period / 2)), tma_period)

// Compute the Average Line
averageLine = (linRegForecast + tma) / 2

// Compute Stochastic (8,4,4)
k = ta.sma(ta.stoch(close, high, low, stoch_k), stoch_smooth)
d = ta.sma(k, stoch_d)

// Track last confirmed Stochastic crossovers
var float lastBuyCrossover = na
var float lastSellCrossover = na

if ta.crossover(k, d) and k < 30
lastBuyCrossover := bar_index // Store the bar index of last Buy crossover below 30

if ta.crossunder(k, d) and k > 70
lastSellCrossover := bar_index // Store the bar index of last Sell crossover above 70

// Define Buy & Sell Signal Conditions with Confirmation
buySignal = ta.crossover(linRegForecast, tma) and lastBuyCrossover > lastSellCrossover
sellSignal = ta.crossunder(linRegForecast, tma) and lastSellCrossover > lastBuyCrossover

// Plot the indicators
plot(linRegForecast, color=color.blue, title="Linear Regression Forecast")
plot(tma, color=color.green, title="Triangular Moving Average (TMA)")
plot(averageLine, color=color.white, title="Average Line", linewidth=2) // White Average Line

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

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.