I am trying to highlight in background of my chart, when the market is flat.
Obviously, I am doing something wrong, since the background is "completely" green.
Would appreciate your expertise.
// --- Flatmarket START --- // Calculate price range over a 10-bar period price_range = high - low // Set the threshold for flat market threshold = 0.5 * price_range // Check if the market is flat flatMarket = (price_range <= threshold) trendingMarket = (price_range >= threshold) // Check if the market is flat bgcolor(flatMarket ? color.red : trendingMarket ? color.green : na)
It doesn’t work
Your math is off here.
price_range = high - low will always be a positive number because
high is always greater than
low. Say it is 10.
threshold will always be less than
price_range because you do
threshold = 0.5 * price_range. In our example
threshold will be 5.
flatMarket will always be
trendingMarket will always be
flatMarket = (price_range <= threshold) // 10 <= 5 -> false trendingMarket = (price_range >= threshold) // 10 >= 5 -> true
This is why your background color is always green because
flatMarket is always
trendingMarket is always
true in your
bgcolor(flatMarket ? color.red : trendingMarket ? color.green : na)