Adding Bar Count for Histogram


Category:
0
0

Mr Hanhn,

 

I’ve looked at one of your previous posts and tried to follow what you were doing but it’s not transcribing the same for what I’m doing it seems. Would appreciate help if you were able

 

What I’m working on is inside the ## I’m trying to to count the bars, it’d great to add the bar count to my other labels. So basically, While there’s a bear div which I have printed with the label I’d also like to add to that how many bars have been printing that div.(it’s printing as histogram–hence bar count) I need it to flip however, so from bear div histogram print(let’s say it prints 3 bars, then 2, then 1 if 0 then NaN( I have labels set to Black no color or div printed if histogram = 0) but once it flips to bull div it needs to concat bar count again.

I think!! my problem is this

def upTrend = bulldiv[2] < bulldiv[1] and bulldiv[1] < bulldiv;
def downTrend = beardiv[2] > beardiv[1] and beardiv[1] > beardiv;

I tried interchanging scale instead of bulldiv/beardiv but couldn’t figure it out.

If you give it a try huge thanks to you.

input length = 20;

def indicator = mydivergence;
def myslope = reference LinearRegressionSlope(mydivergence, length);
def bullslope = reference LinearRegressionSlope(low, length);
def bearslope = reference LinearRegressionSlope(high, length);
def priceslope = (bullslope + bearslope) / 2;

def isbulldiv = if (bullslope < 0 and myslope >= 0) then 1 else 0;
def isbeardiv = if (bearslope > 0 and myslope <= 0) then 1 else 0;

def myBullDiv = if isbulldiv then myslope * bullslope else 0 ;
#plot Bulldiv=if mydiv-bullpricediv>0 then mydiv-bullpricediv else 0;
def myBearDiv = if isbeardiv then myslope * bearslope else 0;
#plot beardiv = if mydiv – bearpricediv<0 then mydiv-bearPricediv else 0;
plot zeroline = 0;

def scale = Max(AbsValue(LowestAll(myBullDiv)), AbsValue(LowestAll(myBearDiv)));

#

#

plot bulldiv = AbsValue(myBullDiv) / scale * 100;
plot beardiv = -(AbsValue(myBearDiv)) / scale * 100;
plot up = 100;
plot down = -100;
up.SetDefaultColor(Color.YELLOW);
down.SetDefaultColor(Color.YELLOW);

bulldiv.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
bulldiv.SetDefaultColor(Color.LIGHT_GREEN);
bulldiv.SetLineWeight(5);
beardiv.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
beardiv.SetDefaultColor(Color.LIGHT_RED);
beardiv.SetLineWeight(5);
zeroline.SetDefaultColor(Color.GRAY);

##################################

def upTrend = bulldiv[2] < bulldiv[1] and bulldiv[1] < bulldiv;
def downTrend = beardiv[2] > beardiv[1] and beardiv[1] > beardiv;

def isUpTrend = !upTrend[1] and upTrend;
def isDownTrend = !downTrend and downTrend;

rec upTrendCounter = CompoundValue(1, if isUpTrend then 1 else if upTrend then upTrendCounter[1] + 1 else 0, 0);
rec downTrendCounter = CompoundValue(1, if isDownTrend then 1 else if downTrend then downTrendCounter[1] + 1 else 0, 0);

AddLabel (yes, (Concat(“ —- “, Round(uptrendcounter, 1))));
AddLabel (yes, (Concat(“ —- “, Round(downtrendcounter, 1))));

##################################

input BuyLabelText = ” Buy “;

input SellLebelText = ” Sell “;
AddLabel (yes,
” /SMI/ “,
color.cyAN);

AddLabel(yes, ” /Bull/ ” , if bulldiv >= 50 then color.green else if bulldiv > 0 then color.light_GREEN else Color.blACK);
AddLabel(yes, ” /Bear/ ” , if beardiv <= -50 then color.red else if beardiv < -0 then color.pink else Color.blACK);

Attachments:
RESOLVED
Marked as spam
Posted by (Questions: 2, Answers: 1)
Asked on July 27, 2019 6:05 pm
185 views
0
Private answer

Your code is not complete. There is a reference to a variable named mydivergence but it is not included in your code. So the code you presented fails at line 3. Other than this, I can see that your AddLabel() statements are lacking the third parameter. Which is the color assignment. These will not work unless you provide a color. Such as this:

AddLabel (yes, (Concat(“ —- “, Round(upTrendCounter, 1))), Color.BLACK);
AddLabel (yes, (Concat(“ —- “, Round(downTrendCounter, 1))), Color.BLACK);

You can troubleshoot the code that counts the histogram bars by plotting them, like this:

plot test1 = upTrendCounter;
plot test2 = downTrendCounter;

If you don't get the values you expect from these two plots, then work your way further back in the code until you find the problem.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on July 27, 2019 7:01 pm