Script is CPU intensive. Is there a cure?

U

USptcom

Guest
Script in question is one I compiled back in 2015 and since then have forgotten the little I learned about scripting (took a break from TDA/TOS). I'm hoping someone can find the reason for its creating a 35% increase in CPU load or maybe even offer a more efficient way of scripting.

Thanks!

Script provides in-chart labels for futures index trend tracking.
input length1 = 140;
input length2 = 100;
input length3 = 60;
input length4 = 30;
input length5 = 10;

def ESclose = close ("/ES", period = AggregationPeriod.DAY);
def NQclose = close ("/NQ", period = AggregationPeriod.DAY);
def YMclose = close ("/YM", period = AggregationPeriod.DAY);

def ESchange = ESclose - ESclose[1];
def NQchange = NQclose - NQclose[1];
def YMchange = YMclose - YMclose[1];

def EScloseMin = close ("/ES", period = AggregationPeriod.MIN);
def NQcloseMin = close ("/NQ", period = AggregationPeriod.MIN);
def YMcloseMin = close ("/YM", period = AggregationPeriod.MIN);

def ESchangePct = (ESclose - ESclose[1]) / ESclose[1] * 100;
def NQchangePct = (NQclose - NQclose[1]) / NQclose[1] * 100;
def YMchangePct = (YMclose - YMclose[1]) / YMclose[1] * 100;

def EScloseMin1 = if EScloseMin >= EScloseMin[length1] then 1 else 0;
def EScloseMin2 = if EScloseMin >= EScloseMin[length2] then 1 else 0;
def EScloseMin3 = if EScloseMin >= EScloseMin[length3] then 1 else 0;
def EScloseMin4 = if EScloseMin >= EScloseMin[length4] then 1 else 0;
def EScloseMin5 = if EScloseMin >= EScloseMin[length5] then 1 else 0;
def NQcloseMin1 = if NQcloseMin >= NQcloseMin[length1] then 1 else 0;
def NQcloseMin2 = if NQcloseMin >= NQcloseMin[length2] then 1 else 0;
def NQcloseMin3 = if NQcloseMin >= NQcloseMin[length3] then 1 else 0;
def NQcloseMin4 = if NQcloseMin >= NQcloseMin[length4] then 1 else 0;
def NQcloseMin5 = if NQcloseMin >= NQcloseMin[length5] then 1 else 0;
def YMcloseMin1 = if YMcloseMin >= YMcloseMin[length1] then 1 else 0;
def YMcloseMin2 = if YMcloseMin >= YMcloseMin[length2] then 1 else 0;
def YMcloseMin3 = if YMcloseMin >= YMcloseMin[length3] then 1 else 0;
def YMcloseMin4 = if YMcloseMin >= YMcloseMin[length4] then 1 else 0;
def YMcloseMin5 = if YMcloseMin >= YMcloseMin[length5] then 1 else 0;

AddLabel (1, (" ES " + Round(ESchange, 0)), if ESchange <= -.5 then Color.DARK_RED else if ESchange >= .5 then Color.DARK_GREEN else Color.BLACK);
AddLabel (1, AsText(ESchangePct, NumberFormat.TWO_DECIMAL_PLACES) + "%", if ESchangePct < 0 then Color.DARK_RED else if ESchangePct > 0 then Color.DARK_GREEN else Color.BLACK);
AddLabel (1, if EScloseMin1 then " " else " ", if EScloseMin1 then Color.GREEN else Color.RED);
AddLabel (1, if EScloseMin2 then " " else " ", if EScloseMin2 then Color.GREEN else Color.RED);
AddLabel (1, if EScloseMin3 then " " else " ", if EScloseMin3 then Color.GREEN else Color.RED);
AddLabel (1, if EScloseMin4 then " " else " ", if EScloseMin4 then Color.GREEN else Color.RED);
AddLabel (1, if EScloseMin5 then " " else " ", if EScloseMin5 then Color.GREEN else Color.RED);
AddLabel (1, " NQ " + Round(NQchange, 0), if NQchange <= -.5 then Color.DARK_RED else if NQchange >= .5 then Color.DARK_GREEN else Color.BLACK);
AddLabel (1, AsText(NQchangePct, NumberFormat.TWO_DECIMAL_PLACES) + "%", if NQchangePct < 0 then Color.DARK_RED else if NQchangePct > 0 then Color.DARK_GREEN else Color.BLACK);
AddLabel (1, if NQcloseMin1 then " " else " ", if NQcloseMin1 then Color.GREEN else Color.RED);
AddLabel (1, if NQcloseMin2 then " " else " ", if NQcloseMin2 then Color.GREEN else Color.RED);
AddLabel (1, if NQcloseMin3 then " " else " ", if NQcloseMin3 then Color.GREEN else Color.RED);
AddLabel (1, if NQcloseMin4 then " " else " ", if NQcloseMin4 then Color.GREEN else Color.RED);
AddLabel (1, if NQcloseMin5 then " " else " ", if NQcloseMin5 then Color.GREEN else Color.RED);
AddLabel (1, " YM " + Round(YMchange, 0), if YMchange <= -.5 then Color.DARK_RED else if YMchange >= .5 then Color.DARK_GREEN else Color.BLACK);
AddLabel (1, AsText(YMchangePct, NumberFormat.TWO_DECIMAL_PLACES) + "%", if YMchangePct < 0 then Color.DARK_RED else if YMchangePct > 0 then Color.DARK_GREEN else Color.BLACK);
AddLabel (1, if YMcloseMin1 then " " else " ", if YMcloseMin1 then Color.GREEN else Color.RED);
AddLabel (1, if YMcloseMin2 then " " else " ", if YMcloseMin2 then Color.GREEN else Color.RED);
AddLabel (1, if YMcloseMin3 then " " else " ", if YMcloseMin3 then Color.GREEN else Color.RED);
AddLabel (1, if YMcloseMin4 then " " else " ", if YMcloseMin4 then Color.GREEN else Color.RED);
AddLabel (1, if YMcloseMin5 then " " else " ", if YMcloseMin5 then Color.GREEN else Color.RED);
 
Solution
you might save a few processor cycles by changing this part of the labels:
Code:
 if EScloseMin1 then " " else " "
to be simply
Code:
" "
since your if statement always resolves to that anyway.

You may also (and I haven't tested this) be able to save some cycles using the AsPercent() built in function in your labels rather than calculating and rounding yourself.
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Others/AsPercent

Otherwise, that's just a lot of calculation and a long ton of labels -- any room left on your screen for the chart?!? ;-)

-mashume
you might save a few processor cycles by changing this part of the labels:
Code:
 if EScloseMin1 then " " else " "
to be simply
Code:
" "
since your if statement always resolves to that anyway.

You may also (and I haven't tested this) be able to save some cycles using the AsPercent() built in function in your labels rather than calculating and rounding yourself.
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Others/AsPercent

Otherwise, that's just a lot of calculation and a long ton of labels -- any room left on your screen for the chart?!? ;-)

-mashume
 
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
342 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