Charts are laggy due to custom study

letsfingo

New member
First of all, this is not a computer performance issue. I have increased my memory allocation to TOS. Memory, CPU and GPU all have plenty to spare.

For some reason charts are lagging by a few seconds when I have this study enabled. The study checks to see if the chart symbol matches and if it does then it plots some lines.

It's a very simplistic study that basically does something similar to this for a bunch of symbols:
Code:
if GetSymbol() == "NVDA" { plotline = 250.00;}
else if GetSymbol() == "AAPL" { plotline = 150.00;}

I would understand if the performance of the chart was affected upon the initial loading of the chart, but it is being affected continuously while I have the chart open. The plot lines of the study are static and therefore don't need to be redrawn after the initial loading of the chart. Is there a way to stop this study from continuously affecting the performance of the charts?
 
Solution
You might be able to do something like the following, and it might speed up the code. It should only check the symbols on the first bar, then it should just continue to draw the line based on the line's own value. Although, it depends on how the platform interprets code in general, which is not something that is explained or documented. I am interested to hear the results.

Code:
def x;
if !x[1] {
    x = if getsymbol() == "NVDA" then 250
    else if getsymbol() == "AAPL" then 150
    else -1;
} else {
    x =  x[1];
}
plot y = x;
How many symbols is a bunch of symbols? They are redrawn, once per bar, every tick, whether they need to be or not. They are just redrawn in the same place. Declaring once_per_bar might help. I am curios why you don't just draw them manually if they are static? Moving the drawing when necessary is no more complicated than changing the value in the script.
 

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

Unfortunately, declaring once per bar didn't help. I understand that the lines are redrawn every bar, however, I think that it is the IF statements that are slowing it down. It is having to run through all of the IF and ELSE IF statements for every bar. The IF statements only serve to match the plotlines to the correct symbol. If it were possible to only run through the if statements once and then ignore them for subsequent runs, then this could fix the issue. I don't mind the lines being redrawn but I would like to have the variables for the lines already saved and avoid rerunning the IF statements, is that possible?
 
Last edited:
You might be able to do something like the following, and it might speed up the code. It should only check the symbols on the first bar, then it should just continue to draw the line based on the line's own value. Although, it depends on how the platform interprets code in general, which is not something that is explained or documented. I am interested to hear the results.

Code:
def x;
if !x[1] {
    x = if getsymbol() == "NVDA" then 250
    else if getsymbol() == "AAPL" then 150
    else -1;
} else {
    x =  x[1];
}
plot y = x;
 
Solution
According to a TOS rep I spoke with, custom scripts run slower than TOS built-in scripts, so for some lag I was experiencing, he suggested I replace custom scripts with built-in scripts where feasible (among other suggestions).

Not sure why, but there does seem to be some truth to this.
 
You might be able to do something like the following, and it might speed up the code. It should only check the symbols on the first bar, then it should just continue to draw the line based on the line's own value. Although, it depends on how the platform interprets code in general, which is not something that is explained or documented. I am interested to hear the results.

Code:
def x;
if !x[1] {
    x = if getsymbol() == "NVDA" then 250
    else if getsymbol() == "AAPL" then 150
    else -1;
} else {
    x =  x[1];
}
plot y = x;

thanks for posting that code in post#4.
i've never seen a variable set to a value, before bar #1.

i made this test version, to see what the variables are doing, but , still confused, ... heh
it displays bubbles with values.
at bar #1, x[1] has the desired value, 150 ...


Code:
# firstbar_stuff_00

def bn = barnumber();
def x;
if !x[1] {
    x = if getsymbol() == "NVDA" then 250
    else if getsymbol() == "AAPL" then 150
    else -1;
} else {
#    x =  x[1];
    x = 22;
}
plot y = x;

addchartbubble( ( bn < 4 ), low, "bn " + bn + "\nX " + x + "\n!x " + !x + "\nX1 " + x[1] + "\n!x1 " + !x[1], color.cyan, no);
#


uzjBgp2.jpg
 
You might be able to do something like the following, and it might speed up the code. It should only check the symbols on the first bar, then it should just continue to draw the line based on the line's own value. Although, it depends on how the platform interprets code in general, which is not something that is explained or documented. I am interested to hear the results.

Code:
def x;
if !x[1] {
    x = if getsymbol() == "NVDA" then 250
    else if getsymbol() == "AAPL" then 150
    else -1;
} else {
    x =  x[1];
}
plot y = x;
Thank you! I will give it a try and let you know how it goes.
 
thanks for posting that code in post#4.
i've never seen a variable set to a value, before bar #1.

i made this test version, to see what the variables are doing, but , still confused, ... heh

If you call an inline offset, like def X = X[30], it will expose data beyond the left edge of the chart, and even cause BarNumber() to run negative. Its just something that is hard to notice because you never actually see it with your own eye. However, if you were then to Fold X = 1 to BarNumber() it will throw an assertion because the first bar number, in this case, actually becomes -29.

C81UNrv.png
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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