so how come NO variables?

lewac1

New member
yeah NO VARIABLES. they call them variable but they're in fact CONSTANTS. Because once you assign and "alleged" variable to something YOU CAN NOT CHANGE IT! Thus its virtually impossible to write a complex script here.

now "they" say you define a "variable" using the "def" declaration. sorry. that ain't gonna work, folks. thus the workaround is we go NUTS drilling down because we only use the thing ONCE! happy coding? NOT! and I notice there's only THREE posts relating to the word "variable"? aren't they're any coders in this forum? yeah this is a humongous issue. sure. say I wanna write a trigger that displays on the chart when certain conditions are met. maybe I don't like big wicks and would like to avoid them when trading INTRADAY. wouldn't THAT be a prudent thing to be doing? sure small wicks get my attention, see? well its not gonna happen HERE!

Anyhow I'm a JAVA programmer of late but many, many others got my attention in what has become a very distant past...
 
Solution
This is just some random code for demonstration, it will check if the close is above the open. if it is, it will check the macd versus the previous bar. If they are both up, it will say buy. If the close is not above the open, it will not bother to check the macd at all.

Ruby:
declare lower;
def x;
def y;
def cond;
def z = macd();
if close > open {
    x = yes;
    if z > z[1] {
        y = yes;
    } else {
        y = no;
    }
} else {
    x = no;
    y = no;
}
cond = x and y;
plot output = cond;
AddLabel(
    yes,
    if cond then "BUY" else "DONT BUY",
    if cond then COLOR.GREEN else COLOR.ORANGE
);

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

Just wanted to say that I appreciated @lewac1 's post, and I can sympathize with the tone of it.
After spending time learning (i.e. beating my head against the wall with) ThinkScript for the last several weeks, it turns out that the scripting language has severe deficiencies.
While it supports some cool dashboard effects, it is impossible to use it to track a virtual portfolio, with a virtual fund that is used during back-testing.
It is also impossible to do variable re-assignment, unlike almost any other language I can think of.

For those starting to use ThinkScript, that means this doesn't work:
Code:
def TEST;
TEST = 10;
TEST = 15; #This produces an 'already assigned' error.

Elsewhere in the forum @tomsk mentioned "The only place where ThinkScript permits a def value to be changed is within an if-then-else structure or a switch construct.", but my attempts at this have failed. It looks like ThinkScript blocks any "re-assignment" (changing of value) for variables period, regardless of what construct they are used in. You can (initially) assign a variable in multiple ways, in an in if or switch statement, but you can't go back and update it later apparently.

What I am trying to do (among other things) is track the total amount spent over several sequential buys, or even simply track the number of sequential buys that have been made in a cycle.
The latter should be as easy as:
def NUM_BUYS = 0;
NUM_BUYS += 1;
NUM_BUYS += 1;
But there doesn't appear to be a way to track this without reassigning a variable.

If anyone has found a way around this I would like to know, as I am currently thinking I will need to switch away from TOS because of the lack of supported features in ThinkScript. :(
 
The script is iterated bar by bar. So even if thinkscript accepted that syntax, the result would always be 2. To continuously track a value, you have to call upon the previous iteration.

Code:
declare lower;
def buy_condition = close > open;
def numbuys = numbuys[1] + buy_condition;
#def numbuys = if buy_condition then numbuys[1] + 1 else numbuys[1];
plot buys = numbuys;
 
Just wanted to say that I appreciated @lewac1 's post, and I can sympathize with the tone of it.
After spending time learning (i.e. beating my head against the wall with) ThinkScript for the last several weeks, it turns out that the scripting language has severe deficiencies.
While it supports some cool dashboard effects, it is impossible to use it to track a virtual portfolio, with a virtual fund that is used during back-testing.
It is also impossible to do variable re-assignment, unlike almost any other language I can think of.

For those starting to use ThinkScript, that means this doesn't work:
Code:
def TEST;
TEST = 10;
TEST = 15; #This produces an 'already assigned' error.

Elsewhere in the forum @tomsk mentioned "The only place where ThinkScript permits a def value to be changed is within an if-then-else structure or a switch construct.", but my attempts at this have failed. It looks like ThinkScript blocks any "re-assignment" (changing of value) for variables period, regardless of what construct they are used in. You can (initially) assign a variable in multiple ways, in an in if or switch statement, but you can't go back and update it later apparently.

What I am trying to do (among other things) is track the total amount spent over several sequential buys, or even simply track the number of sequential buys that have been made in a cycle.
The latter should be as easy as:
def NUM_BUYS = 0;
NUM_BUYS += 1;
NUM_BUYS += 1;
But there doesn't appear to be a way to track this without reassigning a variable.

If anyone has found a way around this I would like to know, as I am currently thinking I will need to switch away from TOS because of the lack of supported features in ThinkScript. :(
yeah its bigtime bummer.. not only that the IF statement is absolutely atrocious as well. Can't even use the thing standalone! I'm trying to send a label to an indicator I wrote telling me to TRADE with a nice green label and I can't even do that! wanna know why? addlabel( ) is NOT ALLOWED within an IF statement!.. and what's with the required ELSE clause? well maybe I don't wanna do ELSE! but then I cant have a number of statements under a then or else either.. only ONE STATEMENT allowed, folks! this compiler requires some SERIOUS work, no doubt about it. between the IF and assignment anomalies its just about worthless. an IF statement SHOULD accept ANY reasonable statement within including multiple calculations to a variable.. the problem? there is no such thing as a VARIABLE.. the entities created are in fact, CONSTANTS! there's no such animal as a "dynamic" variable under thinkscript. absolutely absurd. One would think that a class act like TOS would have a class act to write scripts with as well. Unfortunately these two entities aren't even on the same planet. And there's little things like not being able to access the past in MACD. one can do that with RSI why not with MACD? nope all references are to the current bar only... referencing a prior bar is completely ignored. its stuff like that which makes the place stink. Look. we want a VARIABLE. you know.. an entity that can be MODIFIED? is that too difficult to grasp? and we want an IF statement that acts like an IF statement since when?
 
This is just some random code for demonstration, it will check if the close is above the open. if it is, it will check the macd versus the previous bar. If they are both up, it will say buy. If the close is not above the open, it will not bother to check the macd at all.

Ruby:
declare lower;
def x;
def y;
def cond;
def z = macd();
if close > open {
    x = yes;
    if z > z[1] {
        y = yes;
    } else {
        y = no;
    }
} else {
    x = no;
    y = no;
}
cond = x and y;
plot output = cond;
AddLabel(
    yes,
    if cond then "BUY" else "DONT BUY",
    if cond then COLOR.GREEN else COLOR.ORANGE
);
 
Last edited:
Solution

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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