carrying a variable forward

Ecantor

New member
Plus
I am trying to save the high, low, open, close from the first 15 minute bar of the day. Here is a fragment of the code just showing preserving the open price. The code correctly captures the open for the first 15 minutes, but when the next bar begins, the value of firstbaropen goes to NaN. Please help!!! Ty. fyi. On my 15 min 5 day chart, the first bar is always bar number 131 so i could also have coded it as in 2nd example.

1st example

def na = Double.NaN;
def condition = if SecondsFromTime(0930) == 0 && SecondsTillTime(0930) == 0
then BarNumber()
else na;
def StartBar = condition;
def firstBarOpen = if BarNumber() == StartBar
then open
else firstBarOpen[1];


2nd example

input startbar = 131;
def firstbaropen = if barnumber() == startbar
then open
else firstbaropen[1];
 
Last edited by a moderator:
Solution
I am trying to save the high, low, open, close from the first 15 minute bar of the day. Here is a fragment of the code just showing preserving the open price. The code correctly captures the open for the first 15 minutes, but when the next bar begins, the value of firstbaropen goes to NaN. Please help!!! Ty. fyi. On my 15 min 5 day chart, the first bar is always bar number 131 so i could also have coded it as in 2nd example.

1st example

def na = Double.NaN;
def condition = if SecondsFromTime(0930) == 0 && SecondsTillTime(0930) == 0
then BarNumber()
else na;
def StartBar = condition;
def firstBarOpen = if BarNumber() == StartBar
then...
I am trying to save the high, low, open, close from the first 15 minute bar of the day. Here is a fragment of the code just showing preserving the open price. The code correctly captures the open for the first 15 minutes, but when the next bar begins, the value of firstbaropen goes to NaN. Please help!!! Ty. fyi. On my 15 min 5 day chart, the first bar is always bar number 131 so i could also have coded it as in 2nd example.

1st example

def na = Double.NaN;
def condition = if SecondsFromTime(0930) == 0 && SecondsTillTime(0930) == 0
then BarNumber()
else na;
def StartBar = condition;
def firstBarOpen = if BarNumber() == StartBar
then open
else firstBarOpen[1];


2nd example

input startbar = 131;
def firstbaropen = if barnumber() == startbar
then open
else firstbaropen[1];

In the first example, since def condition is set to be na after SecondsFromTime(0930) == 0 && SecondsTillTime(0930) == 0, then the value will then be na beyond that barnumber when you try to use it in firstbaropen..

By making Startbar a recursive. it retains the barnumber defined at the condition and makes it available for future use in finding the value of firstbaropen.

In the 2nd example, since startbar is defined by an input value, 131, then the resulting firstbaropen is retained by the recursive, firstbaropen[1]

In both examples, now that the startbar/startbar1's barnumber is retained, then high/low/close can be found as you did for the open.

Barnumbers, Labels and Bubbles were added and a normal method used for testing the coding.

Screenshot 2024-01-23 154733.png
Code:
#1st example

input hide_bn = yes;
input labels  = yes;
input bubble  = yes;

def na = Double.NaN;
plot bn = BarNumber();
bn.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
bn.SetHiding(hide_bn);

def condition = if SecondsFromTime(0930) == 0 && SecondsTillTime(0930) == 0
then BarNumber()
else condition[1];

def StartBar = condition;
def firstBarOpen = if BarNumber() == StartBar
then open
else firstBarOpen[1];

AddLabel(labels, firstBarOpen);

#2nd example

input startbar1 = 131;
def firstbaropen1 = if BarNumber() == startbar1
then open
else firstbaropen1[1];

AddLabel(labels, firstbaropen1);


AddChartBubble(bubble and bn == HighestAll(StartBar) or bn == startbar1, high,"O: " + open + "\n H: " + high + "\n L: " + low + "\n C: " + close);

#
 
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
431 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