Background color for Heiken Ashi bars with no wick


Category:
0
0

Hi Pete.  May I ask for your help with creating a specific background color based on the Heiken Ashi bars?  I would like to have the background color appear as red if the current bar has no upper wick and the background color appear as green if the bar has no upper wick.  If the bar has both an upper and lower wick the background color would appear as black.  Thank you.

Marked as spam
Posted by (Questions: 19, Answers: 18)
Asked on December 13, 2019 8:54 pm
204 views
0
Private answer

We can take the code from a previous solution and add just a few lines to adapt it to a watchlist. Here is the link to the previous post where I am getting this code:

https://www.hahn-tech.com/ans/first-heikin-ashi-bullish-candlestick-with-no-lower-wick-after-2-or-more-red-candles/

Here is the solution. I pulled 6 lines of code from that previous solution and then added three lines to display a value in the watchlist column and dynamically adjust the color as you requested.

def haClose = ohlc4;
def haOpen = if haOpen[1] == 0 then haClose[1] else (haOpen[1] + haClose[1]) / 2;
def haHigh = Max(high, Max(haClose, haOpen));
def haLow = Min(low, Min(haClose, haOpen));
def noLowerWickGreen = haClose > haOpen and haOpen == haLow;
def noUpperWickRed = haClose < haOpen and haOpen == haHigh;
plot data = if noLowerWickGreen then 1 else if noUpperWickRed then -1 else 0;
data.AssignValueColor(if data <> 0 then Color.BLACK else Color.CURRENT);
AssignBackgroundColor(if data > 0 then Color.GREEN else if data < 0 then Color.RED else Color.CURRENT);

Please note, that in that previous solution the request was for green HA candle with no lower wick and red HA candle with no or upper wick.

If you wanted to remove the requirement for the candle color you can modify two lines of that code as follows:

def noLowerWickGreen = haOpen == haLow and haOpen <> haHigh;
def noUpperWickRed = haOpen == haHigh and haOpen <> haLow;

Note that modifying these lines as shown above will match your specification exactly.

Marked as spam
Posted by (Questions: 37, Answers: 4117)
Answered on December 14, 2019 8:42 am