Script Troubleshooting Needed
Budget: $30 – $250 USD
I am trying to create a script that counts stock chart candles that are above or below a certain value.
specifically, I want my script to sequentially count candles once they are above the 9 simple moving average. When the candle eventually closes below the 9 sma, I want those to be counted also
my current script isnt perfect and occasionally give repeating numbers
plot(close)
indicator("Candle Count", overlay=true)
// Source for SMA calculation
source = close
// Calculate the 9-period SMA
sma9 = ta.sma(source, 9)
// Variables for counters and tracking state
var int aboveCount = 0
var int belowCount = 0
var bool isAboveSMA = false
var bool isBelowSMA = false
// Check if the body of the candle (open and close) is above or below SMA and handle counting
if close > sma9 and open > sma9 // Both open and close above SMA
if not isAboveSMA
isAboveSMA := true
isBelowSMA := false
aboveCount := 1 // Reset to 1 when we start a new count above SMA
else
aboveCount := aboveCount + 1 // Increment if we're already counting above SMA
belowCount := 0 // Reset below count
else if close < sma9 and open < sma9 // Both open and close below SMA
if not isBelowSMA
isBelowSMA := true
isAboveSMA := false
belowCount := 1 // Reset to 1 when we start a new count below SMA
else
belowCount := belowCount + 1 // Increment if we're already counting below SMA
aboveCount := 0 // Reset above count
// Plot the 9 SMA for reference
plot(sma9, color=color.blue, linewidth=2, title="9 SMA")
// Label each candle above the high with slight offset, using brilliant white text
if isAboveSMA
label.new(bar_index, high + (high * 0.001), text=str.tostring(aboveCount), color=color.new(color.green, 80), textcolor=color.white, style=label.style_none)
else if isBelowSMA
label.new(bar_index, high + (high * 0.001), text=str.tostring(belowCount), color=color.new(color.red, 80), textcolor=color.white, style=label.style_none)
specifically, I want my script to sequentially count candles once they are above the 9 simple moving average. When the candle eventually closes below the 9 sma, I want those to be counted also
my current script isnt perfect and occasionally give repeating numbers
plot(close)
indicator("Candle Count", overlay=true)
// Source for SMA calculation
source = close
// Calculate the 9-period SMA
sma9 = ta.sma(source, 9)
// Variables for counters and tracking state
var int aboveCount = 0
var int belowCount = 0
var bool isAboveSMA = false
var bool isBelowSMA = false
// Check if the body of the candle (open and close) is above or below SMA and handle counting
if close > sma9 and open > sma9 // Both open and close above SMA
if not isAboveSMA
isAboveSMA := true
isBelowSMA := false
aboveCount := 1 // Reset to 1 when we start a new count above SMA
else
aboveCount := aboveCount + 1 // Increment if we're already counting above SMA
belowCount := 0 // Reset below count
else if close < sma9 and open < sma9 // Both open and close below SMA
if not isBelowSMA
isBelowSMA := true
isAboveSMA := false
belowCount := 1 // Reset to 1 when we start a new count below SMA
else
belowCount := belowCount + 1 // Increment if we're already counting below SMA
aboveCount := 0 // Reset above count
// Plot the 9 SMA for reference
plot(sma9, color=color.blue, linewidth=2, title="9 SMA")
// Label each candle above the high with slight offset, using brilliant white text
if isAboveSMA
label.new(bar_index, high + (high * 0.001), text=str.tostring(aboveCount), color=color.new(color.green, 80), textcolor=color.white, style=label.style_none)
else if isBelowSMA
label.new(bar_index, high + (high * 0.001), text=str.tostring(belowCount), color=color.new(color.red, 80), textcolor=color.white, style=label.style_none)