Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Highlight the background of my chart according to condition

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

// --- 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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading