Assigning default calculated values conditionally

serendipity2020

Member
Plus
How do we initialize default value to an already assigned variable?

For example, I want to first assign some value to variable and then use that conditionally into another variable.

In Pine script, we do that like this below:
Code:
lowerBand = ohlc4 - ta.atr(12)
lowerBand := lowerBand > close ? lowerBand : high

So how would above code look like in ToS?
I tried below but that does not give correct answer:
Code:
def lBand = ohlc4 - atr(12);
def lowerBand = if lBand> close then lBand else high;

Here, I'm first assigning ohlc4 - atr(12) calculated value and then need to use it to the same variable lowerBand but because in ToS, I've used 2 different variables, its probably messing up result.

Any help is much appreciated!
 
How do we initialize default value to an already assigned variable?

For example, I want to first assign some value to variable and then use that conditionally into another variable.

In Pine script, we do that like this below:
Code:
lowerBand = ohlc4 - ta.atr(12)
lowerBand := lowerBand > close ? lowerBand : high

So how would above code look like in ToS?
I tried below but that does not give correct answer:
Code:
def lBand = ohlc4 - atr(12);
def lowerBand = if lBand> close then lBand else high;

Here, I'm first assigning ohlc4 - atr(12) calculated value and then need to use it to the same variable lowerBand but because in ToS, I've used 2 different variables, its probably messing up result.

Any help is much appreciated!

i don't quite understand. it looks like your 2 variable code would work.

here are 2 variations. maybe 1 will give you an idea.

ver1
Code:
def lowerBand = if ((ohlc4 - atr(12)) > close) then (ohlc4 - atr(12)) 
else high;


ver2
Code:
def bn = barnumber();
def na = double.nan;

def lowerband;
if bn == 1 then {
# initialize
lowerband = (ohlc4 - atr(12));
} else if lowerband[1] > close then {
lowerband = ohlc4 - atr(12); 
} else {
lowerband = high;
}

addchartbubble(1, low,
lowerband
, color.yellow, no);
#
 

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

i don't quite understand. it looks like your 2 variable code would work.

here are 2 variations. maybe 1 will give you an idea.

ver1
Code:
def lowerBand = if ((ohlc4 - atr(12)) > close) then (ohlc4 - atr(12))
else high;


ver2
Code:
def bn = barnumber();
def na = double.nan;

def lowerband;
if bn == 1 then {
# initialize
lowerband = (ohlc4 - atr(12));
} else if lowerband[1] > close then {
lowerband = ohlc4 - atr(12);
} else {
lowerband = high;
}

addchartbubble(1, low,
lowerband
, color.yellow, no);
#

Thanks for the reply. However, let me explain a bit. My actual condition in pine is far more complex and I just gave a sample in here.
So, pine has something like this.
Code:
    lowerBand = close - 1.2 * atr(14)
    prevLowerBand = nz(lowerBand[1])
    lowerBand := lowerBand > prevLowerBand or close[1] < prevLowerBand ? lowerBand : prevLowerBand

As you can see above, it first initializes lowerBand with some initial factor calculation and then reassigns lowerBand variable with some conditions in it and if you just compare barnumber with 1, it will never be true and hence may never get assigned. I tested your option 2 sample as well and its not matching with results. Sorry for not explaining it properly earlier. Hope if you can help here now.

Thanks in advance!

Note: Don't worry about nz as I've script nz written in ToS already. What I'm not able to do so far is to calculate lowerBand value with above condition.
 
Thanks for the reply. However, let me explain a bit. My actual condition in pine is far more complex and I just gave a sample in here.
So, pine has something like this.
Code:
    lowerBand = close - 1.2 * atr(14)
    prevLowerBand = nz(lowerBand[1])
    lowerBand := lowerBand > prevLowerBand or close[1] < prevLowerBand ? lowerBand : prevLowerBand

As you can see above, it first initializes lowerBand with some initial factor calculation and then reassigns lowerBand variable with some conditions in it and if you just compare barnumber with 1, it will never be true and hence may never get assigned. I tested your option 2 sample as well and its not matching with results. Sorry for not explaining it properly earlier. Hope if you can help here now.

Thanks in advance!

Note: Don't worry about nz as I've script nz written in ToS already. What I'm not able to do so far is to calculate lowerBand value with above condition.

before you say never, write some code to test a theory.
i use if bn == 1 then .. often , and it always seems to work.

here is a test code for initializing a value at barnumber 1

i will look at your codes in a day or 2

Code:
# test_init_cnt
declare lower;
def bn = barnumber();

def init = -40;
def step = 0.1;
def cnt;
if bn == 1 then {
cnt = init;
} else {
cnt = cnt[1] + step;
}

plot z = cnt;
#
 
before you say never, write some code to test a theory.
i use if bn == 1 then .. often , and it always seems to work.

here is a test code for initializing a value at barnumber 1

i will look at your codes in a day or 2

Code:
# test_init_cnt
declare lower;
def bn = barnumber();

def init = -40;
def step = 0.1;
def cnt;
if bn == 1 then {
cnt = init;
} else {
cnt = cnt[1] + step;
}

plot z = cnt;
#
@halcyonguy I apologize and do not mean to offend you and I'm still learning ToS. Appreciate when you get a chance to look at my code.
Thanks again for the reply.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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