// Define the length for the lookback period length = 8
// Calculate the highest high and lowest low of the previous 'length' candles, excluding the current candle highestHigh = ta.highest(high[1], length) lowestLow = ta.lowest(low[1], length)
// Initialize a variable to track the current position var int position = 0 // 0 = no position, 1 = buy, -1 = sell
// Generate Buy and Sell signals buySignal = (close > highestHigh) and (position != 1) sellSignal = (close < lowestLow) and (position != -1)
// Update the position variable if (buySignal) position := 1 if (sellSignal) position := -1
// Plot Buy and Sell signals on the chart plotshape(series=buySignal, location=location.belowbar, color=color.green, style=shape.labelup, text="BUY") plotshape(series=sellSignal, location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL")
// Debugging plots to check the highest high and lowest low plot(highestHigh, color=color.blue, title="Highest High") plot(lowestLow, color=color.orange, title="Lowest Low")
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 publications is governed by House rules. Você pode favoritá-lo para usá-lo em um gráfico.
As informações e publicações não devem ser e não constituem conselhos ou recomendações financeiras, de investimento, de negociação ou de qualquer outro tipo, fornecidas ou endossadas pela TradingView. Leia mais em Termos de uso.