How to Increase Value By 1

dantrader$

New member
Dear Forum,

This will come off as one of the most trivial questions in this group, however I have not been able to find a valid way to get a variable to to increase its value by one. I have tried: i=i+1; , i+=1; , i++; however all of these methods are throwing up the error "Already Assigned i". If anyone has solutions on how to resolve this issue, let me know, otherwise I am happy to provide the explicit code where the error is occurring. Thank you for your time and I look forward to hearing back soon.
 
Dear Forum,

This will come off as one of the most trivial questions in this group, however I have not been able to find a valid way to get a variable to to increase its value by one. I have tried: i=i+1; , i+=1; , i++; however all of these methods are throwing up the error "Already Assigned i". If anyone has solutions on how to resolve this issue, let me know, otherwise I am happy to provide the explicit code where the error is occurring. Thank you for your time and I look forward to hearing back soon.


https://tlc.thinkorswim.com/center/reference/thinkScript/tutorials/Advanced
ch 10

use an offset of 1 , [1] , to read a previous value of a variable and add 1 to it.
def a = a[1] + 1;
 

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

https://tlc.thinkorswim.com/center/reference/thinkScript/tutorials/Advanced
ch 10

use an offset of 1 , [1] , to read a previous value of a variable and add 1 to it.
def a = a[1] + 1;
Dear Halcyonguy,

Thank you for the reference! Unfortunately after implementing the following code, it is still not able to update the value inside of an if statement. Is there a work around or syntactical edit I can make in order for the following code to work? :
def z=0;
if (z == 0)
then {
def z = z[1] + 1;
}
else {}
plot a=z;
 
Dear Halcyonguy,

Thank you for the reference! Unfortunately after implementing the following code, it is still not able to update the value inside of an if statement. Is there a work around or syntactical edit I can make in order for the following code to work? :
def z=0;
if (z == 0)
then {
def z = z[1] + 1;
}
else {}
plot a=z;

answers would come quicker to people who post complete info and questions up front.


can't use a def within a if then.
can't use an output function within an if then. (plot, addlabel,...)
when using a def before an if then, it can't be set to a value.

Code:
def bn = barnumber();
def z;
if bn == 1 then {
z = 1;
} else if !isnan(close) then {
z = z[1] + 1;
} else {
 z = z[1];
}

plot a=z;
 
answers would come quicker to people who post complete info and questions up front.


can't use a def within a if then.
can't use an output function within an if then. (plot, addlabel,...)
when using a def before an if then, it can't be set to a value.

Code:
def bn = barnumber();
def z;
if bn == 1 then {
z = 1;
} else if !isnan(close) then {
z = z[1] + 1;
} else {
 z = z[1];
}

plot a=z;
Dear Halcyonguy,

I appreciate the followup! I have found that
def z;
if (z==0)
{z=z[1]+1;}
else{z=z[1];}
works as well!
Unfortunately, I am now running into a similar problem with updating values within nested if statements. My apologies for the double follow up, but if you have any insight as to how I can solve the following problem, it would be truly appreciated:

def m;
def z;

if ('condition')
then {
if ('subcondition1') {
m = m[1] + 1;
} else {
}
if ('subcondition2') {
z = z[1] + 1;
} else {
}
}
else {
}

The problem is that I cannot update the values of m and z. Thank you so much for your time, and I look forward to reading your response!
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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