Advertisements
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[20] - low[20]
// 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
>Solution :
Your math is off here.
price_range = high[20] - low[20]
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.
Therefore, flatMarket
will always be false
and trendingMarket
will always be true
.
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 false
and trendingMarket
is always true
in your bgcolor()
call.
bgcolor(flatMarket ? color.red : trendingMarket ? color.green : na)