Boundary Condition. Starting a counter at Bar 1

sbhyde

New member
This is quite simple, I thought .. but this one is making be think this is VB6 all over again.

I am running a counter starting at bar 1. I don't always increment it but for this example I am. This is an isolated and simplifier example.
It did not include in the images, but the close price is different for each bar. You can look at the bars and see that. This should be easy to reproduce,

Code:
# MDLN  < use this symbol to easily get to bar #1

def pC = if BarNumber() == 1 then 1 else if (close == close[1]) then pC[1] else pC[1] + 1;

AddChartBubble(yes, high, BarNumber() + "  " + pC + "  " + isNAN(pC), Color.WHITE);

Despite the line assigning the counter the value of 1 when the Barnumber is 1 (BarNumber() == 1 then 1), the value in the counter is 0. The value is not NaN.
Attached is an image showing the code and the result.
Troubleshooting isolated the problem to (close == close[1]). If I replaced that expression with (1 == 2), I get the expected result.
Apparently even though the first condition of the if statement is true, it still evaluates close[1] which does not exist. But it does not produce an error.

I tried using CompoundValue like this

Code:
# MDLN  < use this symbol to easily get to bar #1

def pC = CompoundValue(1, pC[1] + 1, 1);

AddChartBubble(yes, high, BarNumber() + "  " + pC + "  " + isNAN(pC), Color.WHITE);

The counter initialized properly on bar 1, but it did not increment on bar 2. Proper incrementing started on bar 3. See images
Any thoughts would be appreciated
 

Attachments

  • BadCounterCV.jpg
    BadCounterCV.jpg
    127.8 KB · Views: 7
  • BadCounter.jpg
    BadCounter.jpg
    227.9 KB · Views: 6

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
806 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