# Help: Adding up numbers along with bars

##### Member
I can use a little help here. Basically I want to incrementally add up points with every bar. So if the first bar went up 3 points, the second up 5 points, and the third down 3 points, it would be 3 + 5 - 3 = 5 points.

I came up with a simple code to do this and it does its job. However, the first bar is not calculating the number correctly, so all the subsequent bars are off as well.

Here's my code and the chart. As you can see, inside the bubble are two numbers. The first one is the BH (body height) and the second is PL (total profit/loss).

To give you an example, consider the SECOND BAR. Here [9.25 : -29.5] means it's body size is 9.25 and the PL is -29.5 (-20.25 from the prior bar's PL and -9.25 from the current bar). However, the problem is with the FIRST BAR, where it says [4.25 : -20.25]. Here, 4.25 is correct but -20.25 is not. It should be the same for both values, eg [4.25 : 4.25]. I don't know where the -20.25 came from, and because of this all the bars that come after are off (obviously!). Any suggestion to get this fixed???

Code:
``````def BH = BodyHeight();
def diff = close - open;
def PL = IF IsNaN(BH[1]) THEN diff ELSE PL[1] + diff; ### Since BH[1] for the first bar will be NaN, Would this not work?

AddChartBubble(high + 0.2, BH + ":" + PL, Color.LIGHT_ORANGE, yes);``````

Solution
If you set the chart bubble to show only when the condition 'IsNaN(BH[1])' is true:
Ruby:
``AddChartBubble(IsNaN(BH[1]),high + 0.2, BH + ":" + PL, Color.LIGHT_ORANGE, yes);``
you will see a no bubble is ever shown.
This means the conditon ' IsNaN(BH[1])' is never true, so the first part of your IF/THEN/ELSE statement is never executing.

Ruby:
``````def BN = BarNumber();
def BH = BodyHeight();
def diff = close - open;
def PL = IF BN == 1 THEN diff ELSE PL[1] + diff; ### Since BH[1] for the first bar will be NaN, Would this not work?

AddChartBubble(1,high + 0.2, BH + ":" + PL, Color.LIGHT_ORANGE, yes);``````
If you set the chart bubble to show only when the condition 'IsNaN(BH[1])' is true:
Ruby:
``AddChartBubble(IsNaN(BH[1]),high + 0.2, BH + ":" + PL, Color.LIGHT_ORANGE, yes);``
you will see a no bubble is ever shown.
This means the conditon ' IsNaN(BH[1])' is never true, so the first part of your IF/THEN/ELSE statement is never executing.

Ruby:
``````def BN = BarNumber();
def BH = BodyHeight();
def diff = close - open;
def PL = IF BN == 1 THEN diff ELSE PL[1] + diff; ### Since BH[1] for the first bar will be NaN, Would this not work?

AddChartBubble(1,high + 0.2, BH + ":" + PL, Color.LIGHT_ORANGE, yes);``````

Thanks! That worked like a charm. Never even considered BarNumber(). Just goes to show what a noob I am.

87k+ Posts
373 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?