Help: Adding up numbers along with bars

rottentrade

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);

vTXfyYN.jpg
 
Solution
@rottentrade
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.

Try this instead:
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);
@rottentrade
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.

Try this instead:
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);
 
Solution

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
590 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

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?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top