How To Automate Script Writing

bigmit2011

New member
Hi,

Is there a way to iterate and create multiple conditions, programmatically?
For example I am interested in something as follows in one script:


Price Change Close is at least -8 percent less than 5 days ago

or

Price Change Close is at least -8 percent less than 6 days ago

or

Price Change Close is at least -8 percent less than 7 days ago

etc for a range of days I choose.

Is there a programmatic way to create multiple conditions via iteration?

So with Thinkscript, I imagine this statement

Code:
Price Change Close is at least -8 percent less than 5 days ago


translates as follows:

Code:
close[5] < .92 * close

So instead of manually writing out the conditions, I wonder if there is a programmatic way to do so.

Thank you.
 
Solution
Hi,

Is there a way to iterate and create multiple conditions, programmatically?
For example I am interested in something as follows in one script:


Price Change Close is at least -8 percent less than 5 days ago

or

Price Change Close is at least -8 percent less than 6 days ago

or

Price Change Close is at least -8 percent less than 7 days ago

etc for a range of days I choose.

Is there a programmatic way to create multiple conditions via iteration?

So with Thinkscript, I imagine this statement

Code:
Price Change Close is at least -8 percent less than 5 days ago


translates as follows:

Code:
close[5] < .92 * close

So instead of manually writing out the conditions, I wonder if there is a programmatic way to do so.

Thank...
So instead of manually writing out the conditions, I wonder if there is a programmatic way to do so.
Scripts need to be manually written out in ThinkOrSwim.

Some members favor using code editors.
An editor will automatically add parentheses, etc…
Editors also makes it immeasurably easier to replicate and mass replace lines.

Everybody has their favorite.
VS Code has Thinkscript extension (read more here: https://usethinkscript.com/threads/closest-related-programming-language-to-thinkscript.7218/)
Atom is a free app that members seem to like.
 
Last edited:
Hi,

Is there a way to iterate and create multiple conditions, programmatically?
For example I am interested in something as follows in one script:


Price Change Close is at least -8 percent less than 5 days ago

or

Price Change Close is at least -8 percent less than 6 days ago

or

Price Change Close is at least -8 percent less than 7 days ago

etc for a range of days I choose.

Is there a programmatic way to create multiple conditions via iteration?

So with Thinkscript, I imagine this statement

Code:
Price Change Close is at least -8 percent less than 5 days ago


translates as follows:

Code:
close[5] < .92 * close

So instead of manually writing out the conditions, I wonder if there is a programmatic way to do so.

Thank you.

maybe it depends what you want for a result.
if you are asking if there are loops in thinkscript, yes there is.
if you want to look at several formulas and end up with 1 number, then a fold loop may work.
a fold loop can be used to iterate through a formula, each time with a different number, a counter, between the start and stop numbers. the counting variable can be used as an offset, to look at different bars, before or after the current bar.

using this as an example,
close[5] < .92 * close

this will look at the past 5 bars to 22 bars. if the previous bar close is less than 98% of the current close, then it keeps looping. on the first time it is false, it stops looping. it counts how many consecutive times it is true.

Code:
def x = fold i = 5 to 22
  with p
  while getvalue(close, i) < ( 0.92 * close)
  do p + 1;

addchartbubble(1, low*0.992, x, color.yellow, no);

x will equal the offset number to the last bar it was true.


https://tlc.thinkorswim.com/center/reference/thinkScript/Reserved-Words/fold
 
Solution
maybe it depends what you want for a result.
if you are asking if there are loops in thinkscript, yes there is.
if you want to look at several formulas and end up with 1 number, then a fold loop may work.
a fold loop can be used to iterate through a formula, each time with a different number, a counter, between the start and stop numbers. the counting variable can be used as an offset, to look at different bars, before or after the current bar.

using this as an example,
close[5] < .92 * close

this will look at the past 5 bars to 22 bars. if the previous bar close is less than 98% of the current close, then it keeps looping. on the first time it is false, it stops looping. it counts how many consecutive times it is true.

Code:
def x = fold i = 5 to 22
  with p
  while getvalue(close, i) < ( 0.92 * close)
  do p + 1;

addchartbubble(1, low*0.992, x, color.yellow, no);

x will equal the offset number to the last bar it was true.


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


Thank you.
It possible to combine results (maybe to an array or so).

For example I want to get a list of stocks that are 15% greater than 15-45 days prior.
So I would need to loop from 15-45, and then add the scan results of each loop to some sort of list/array and then plot that total list.
 
Thank you.
It possible to combine results (maybe to an array or so).

For example I want to get a list of stocks that are 15% greater than 15-45 days prior.
So I would need to loop from 15-45, and then add the scan results of each loop to some sort of list/array and then plot that total list.
If you're scanning, you can create the first filter and save the scan results as a dynamic list. Then create the second filter and scan within the first saved dynamic list. That is, if you mean to be subsetting the first list with the second filter.

-mashume
 
Thank you.
It possible to combine results (maybe to an array or so).

For example I want to get a list of stocks that are 15% greater than 15-45 days prior.
So I would need to loop from 15-45, and then add the scan results of each loop to some sort of list/array and then plot that total list.

i'm guessing from your last post that a watchlist, that gets a list of stocks from a saved scan, may do what you want.

i don't use scans, so i am not sure if the code below will work.
maybe someone here can verify it.
---------------------------

make a scan and set it to day.
add this code in the code window.

# stocks that are 15% greater than 15-45 days prior.
def per = 15
def x = highest(close[15] , (45-15) )
plot y = ( close > ((1+(per*100))*x);

save scan
----
make a watchlist
pick the saved scan to source data to the watchlist

---------------------------
may i make an observation?

you keep guessing at what programming functions might work. iterations, loop, fold, arrays,.... before you list a complete set of rules you want a process/study to follow. doing this may cause others and yourself, to focus on something that may not be the best.

try to create a set of rules, that you want a study to follow. then mention what you want to see on a chart, or in a list.
 

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