Using the script function

Hi I am new to programming. I have a long script that gives me a buy or sell signal and I don't know how to use the script function and apply it to mutliple timeframes within one chart.

How would I program thinkscript to run a given script on various timeframes and put a label for each timeframe at the top? (i.e. SMA10 being greater than SMA50 on a 5m, 10, 1hr, 1d label at the top) using the script function in the thinkscript.
 
Solution
Hi I am new to programming. I have a long script that gives me a buy or sell signal and I don't know how to use the script function and apply it to mutliple timeframes within one chart.

How would I program thinkscript to run a given script on various timeframes and put a label for each timeframe at the top? (i.e. SMA10 being greater than SMA50 on a 5m, 10, 1hr, 1d label at the top) using the script function in the thinkscript.

This uses the script named sma to define the relationship between the 2 simple moving averages in a plot named cond. The script allows you to somewhat shorten the coding of the labels at the various timeframes you noted.

Ruby:
script sma {
    input agg = AggregationPeriod.FIVE_MIN...
Hi I am new to programming. I have a long script that gives me a buy or sell signal and I don't know how to use the script function and apply it to mutliple timeframes within one chart.

How would I program thinkscript to run a given script on various timeframes and put a label for each timeframe at the top? (i.e. SMA10 being greater than SMA50 on a 5m, 10, 1hr, 1d label at the top) using the script function in the thinkscript.

This uses the script named sma to define the relationship between the 2 simple moving averages in a plot named cond. The script allows you to somewhat shorten the coding of the labels at the various timeframes you noted.

Ruby:
script sma {
    input agg = AggregationPeriod.FIVE_MIN;
    plot cond  = SimpleMovingAvg(close(period = agg), 10) > SimpleMovingAvg(close(period = agg), 50);
}

AddLabel(1, 5 , if sma().cond == 1 then Color.GREEN else Color.RED);
AddLabel(1, 10, if sma(AggregationPeriod.TEN_MIN).cond == 1 then Color.GREEN else Color.RED);
AddLabel(1, 60, if sma(AggregationPeriod.HOUR).cond == 1 then Color.GREEN else Color.RED);
AddLabel(1, "DAY", if sma(AggregationPeriod.DAY).cond == 1 then Color.GREEN else Color.RED);
 
Solution
I am trying to use the script function with an indicator I written. I am trying to shorten the code but have found little in the Thinkscript discussion that adequately describes what goes inside the {}. Is there a complete discussion someplace?
 
Last edited:
I am trying to use the script function with an indicator I written. I am trying to shorten the code but have found little in the Thinkscript discussion that adequately describes what goes inside the {}. Is there a complete discussion someplace?

usual things within a script { }
1 or more inputs,
some formulas,
1 or more plots,


think of the script function, as creating your own custum function/study.
when you use a built in study, like
MovAvgExponential, there are 4 input parameters that can be entered.
price
length
displace
show breakout signals

to use it , you could supply all 4 parameters,
def k = MovAvgExponential( close, 22, 0, yes);
then it calculates a number and assigns to k.

there are 3 outputs, plots, from this study. the default output value is the first plot.


an example script,

script stuff {
input a = 0;
input z = 0;
def b = a*5+z;
plot c = b+3;
plot d = b/3;
}

to use a script, use its name, and some parameters,

def g = stuff( 4, 5 );
the parameter(s) used within ( ) , will each be assigned to an input variable, in the script.
input a , will become equal to 4
input z , will become equal to 5

g will be equal to the first plot, c.
this could also be written as,
def g = stuff( 4, 5).c;


not all parameters need to be used, but you have to start with the left ( first). if you want to use the 2nd.


plot is used to send a number out of the script. the first plot is the default output.

to read the 2nd output plot, add d to the end.
def u = stuff( 4,5 ).d;

although plot is used to generate output, a script only produces a number. it can't draw anything.


https://tlc.thinkorswim.com/center/reference/thinkScript/Reserved-Words/script

https://tlc.thinkorswim.com/center/reference/Tech-Indicators/studies-library/M-N/MovAvgExponential
 
Last edited:
usual things within a script { }
1 or more inputs,
some formulas,
1 or more plots,


think of the script function, as creating your own custum function/study.
when you use a built in study, like
MovAvgExponential, there are 4 input parameters that can be entered.
price
length
displace
show breakout signals

to use it , you could supply all 4 parameters,
def k = MovAvgExponential( close, 22, 0, yes);
then it calculates a number and assigns to k.

there are 3 outputs, plots, from this study. the default output value is the first plot.


an example script,

script stuff {
input a = 0;
input z = 0;
def b = a*5+z;
plot c = b+3;
plot d = b/3;
}

to use a script, use its name, and some parameters,

def g = stuff( 4, 5 );
the parameter(s) used within ( ) , will each be assigned to an input variable, in the script.
input a , will become equal to 4
input z , will become equal to 5

g will be equal to the first plot, c.
this could also be written as,
def g = stuff( 4, 5).c;


not all parameters need to be used, but you have to start with the left ( first). if you want to use the 2nd.


plot is used to send a number out of the script. the first plot is the default output.

to read the 2nd output plot, add d to the end.
def u = stuff( 4,5 ).d;

although plot is used to generate output, a script only produces a number. it can't draw anything.


https://tlc.thinkorswim.com/center/reference/thinkScript/Reserved-Words/script

https://tlc.thinkorswim.com/center/reference/Tech-Indicators/studies-library/M-N/MovAvgExponential

Thanks for the clarifying response Halcyonguy. Simply put, I now see that what goes inside the {} is another script.
 

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
543 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