Is any data structure supported?

muscleman

New member
I am planning to write an indicator that needs a sliding window. The most efficient way would need a stack or linked list to assist. I wonder if that's possible?
The naive approach would be to have an O(MN) running time by looping through the size of the sliding window every time I move to the next bar, but that's a little slow.
 
I am planning to write an indicator that needs a sliding window. The most efficient way would need a stack or linked list to assist. I wonder if that's possible?
The naive approach would be to have an O(MN) running time by looping through the size of the sliding window every time I move to the next bar, but that's a little slow.

hello and welcome

sorry, i have no idea what you are describing. none of your words make any sense.

what is a sliding window?
a stack of what?
there is no linking to data.
no idea what o(mn) is.

why do people make guesses using procedures that don't exist, instead of describing the situation generically...


look at a time chart. there are candles.
what do you want to see on those candles?


i'm guessing you want to define a range of bars and have something happen during those bars?

maybe this will help?

this will define a group of 12 bars, that is 4 bars from the last bar, on a chart.
it draws a yellow cloud over those 12 bars.


Code:
# xbars_nearlast_ybarsback_00

def na = Double.NaN;
def bn = BarNumber();

#def lastbn = HighestAll(If(IsNaN(close), 0, bn));
#def lastbar = if (bn == lastbn) then 1 else 0;
def lastbar = !IsNaN(close[0]) and IsNaN(close[-1]);

input x_bars_nearlast = 12;
input y_barsback = 4;
def x = (!IsNaN(close[-y_barsback]) and IsNaN(close[-(1 + x_bars_nearlast + y_barsback)]));

def top = if x then high * 1.006 else na;
def bot = if x then low * 0.994 else na;

addcloud(top, bot, color.yellow);
#

TfITFaV.jpg
 

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

Thank you halcyonguy. Sliding window, O(MN) running time, Stack etc. are all computer science terms. I am new to thinkscript and currently surprised by how little it supports..... I will have to hack the hell out of it to make it work. Looks like there is no decent way to implement it.
 
@muscleman

All windows in ThinkScript are sliding windows in the classical sense. I understand that you're new to the ThinkScript world, and I am saddened that you find the abilities restrictive. I am constantly surprised at the breadth and depth of things that can be done with the language.

Be aware from the outset that ThinkScript will expect there to be a constant value for periods (as for moving averages) and you cannot have a sliding window of variable length.

In general, we don't worry about time complexities when coding. Occasionally, ToS will throw an orange warning triangle at the top of the code-editing window to warn you of potentially overly complex scripts. Sometimes this can be mitigated with a declare once_per_bar; statement, though the warning triangle won't go away.

ThinkScript is a functional language and if you're coming from the perspective of Java or C or JavaScript (g-d help you) then there is a very steep learning curve. If you've worked in Haskell or other purely functional languages you'll have a leg up for sure.

As to your particular data structure, I'm unsure why you're working on an O(m*n) complexity. We don't have matrix constructions per se in ThinkScript. Everything is an array though; storing a value from a given point in the past as a variable becomes the exercise of storing the same value in an ongoing array and copying the value from the last into the current.

Stacks might be possible to implement, if you remember that we're working in a functional language and you need to push and pop things in the correct order. You would set up your x, y, z, and t registers as arrays and move values manually from one register to another, so that when you want to put a new value in x, you first move z to t, and then y to z, and then x to y and then put your new value in x. The reverse is also true for popping values from the x.

You can read up on the fold() function here:
https://tlc.thinkorswim.com/center/reference/thinkScript/Reserved-Words/fold
and it will open up a world of things you can do. To retrieve values inside a fold, be sure to check out GetValue() here:
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Others/GetValue

As a last thought, remember that ThinkOrSwim and ThinkScript do not provide trade automation. That is another topic entirely, and is possible through the TDA API should you want to code a strategy in python or java. You will not be doing High Frequency Trading on ThinkOrSwim. The fastest charts are 1m unless you want to go Tick-chart like some of us do (not for higher frequency candles mostly, though occasionally).

Welcome to the community, and feel free to come back with questions. We're here to help. ThinkScript is an awesome language once you come to terms with it.

-mashume
 
Last edited:
Thank you! This is very helpful!
Can I actually declare an array in thinkscript? I don't see any documentation for that, and once I def a variable and assign a value to it, I can never update the variable's value, which is a big bummer.
Also, I recently tried to build a scanner with all of the bullish and bearish reversal candlestick patterns. It gives me a "TooComplexException". It warns that this may not give accurate real time results. But I only run it once per day after market close, so it should be fine. But once it shows this error, it will not allow me to save the script to the scanner. Could you let me know if there is any work around?
 
Thank you! This is very helpful!
Can I actually declare an array in thinkscript? I don't see any documentation for that, and once I def a variable and assign a value to it, I can never update the variable's value, which is a big bummer.
Also, I recently tried to build a scanner with all of the bullish and bearish reversal candlestick patterns. It gives me a "TooComplexException". It warns that this may not give accurate real time results. But I only run it once per day after market close, so it should be fine. But once it shows this error, it will not allow me to save the script to the scanner. Could you let me know if there is any work around?


1. The ToS platform does not support arrays.
2. Read more about the "too complex" error here:
https://usethinkscript.com/threads/answers-to-commonly-asked-questions.6006/#post-57834
 
Last edited:
OK. So calling them arrays was a stretch, but it is an apt way to think about the ToS data holders with some caveats.

you can only define a variable once. They are immutable, as this is really a functional paradigm language.
You can access past values of data holders using brackets... e.g. data_holder[3] will return the 3rd past value (it is zero indexed such that data_holder[0] is equivalent as far as I know to data_holder).
You cannot write to past values in the data_holder. Again, this is because immutability and functional language models etc...

Come to terms with functional programming and it all becomes simpler. If you try to shoehorn procedural thoughts into ThinkScript, you will be disappointed by the results and often frustrated.

-mashume
 
Thank you guys. Could you tell me if the scanner code also runs bar by bar like the indicators on the chart? If that's the case, then a scanner to check if current price is greater than 30 day moving average would be expensive as the scanner loads the last year's daily chart and runs bar by bar for the last 250 days, only to compare the current price with the last 30 day moving average. In that case, can I add an if block to say, only run the scanner if this bar is at most 30 bars from the most recent bar?
 
Thank you guys. Could you tell me if the scanner code also runs bar by bar like the indicators on the chart? If that's the case, then a scanner to check if current price is greater than 30 day moving average would be expensive as the scanner loads the last year's daily chart and runs bar by bar for the last 250 days, only to compare the current price with the last 30 day moving average. In that case, can I add an if block to say, only run the scanner if this bar is at most 30 bars from the most recent bar?

i don't scan, so this info is from what i have read, not experimented with.


yes, code can be added to limit the data used by formulas, but not needed.
the scanner can read from all the bars, but the output comes from the last bar.


if i am going to make a scan or watchlist study, i start with a lower chart study. that way i can see and verify the math is correct (by adding bubbles and labels and lines).

here is a lower study, that should work in a scanner.
it will be true when the current bar (last bar) close is > an average.
default is simple average

set scan time period to day

Code:
#declare lower;
input avg1_type = AverageType.SIMPLE;
input avg1_price = close;
input avg1_len = 30;
def avg = MovingAverage(avg1_type, avg1_price, avg1_len);
plot z = close > avg;


---------------------------

watchlist (column) study
copy the lower study and change it to be a column study

it displays above or below , green or red cell color.

i add a z to the beginning of my custom column study names, so they sort together at the end of the list.

zaboveavg
http://tos.mx/6YGaBmy


Code:
# zaboveavg
#  watchlist , day

input avg1_type = AverageType.SIMPLE;
input avg1_price = close;
input avg1_len = 30;
def avg = MovingAverage(avg1_type, avg1_price, avg1_len);
def z = close > avg;

addlabel(1, (if z then "above" else "below"), color.black);

assignbackgroundcolor(if z then color.green else color.red);

#

nNt2vQK.jpg



--------------------

=====================
=====================


not related, but topics that might be of interest,


------------
click on post number to get a url to that post

------------
think of all variables as a 1 dimensional array.
but variable addressing is relative, not absolute.
you can't address a specific variable, with an absolute index.

if the 10th bar on the chart is the current bar, and this is run,
def b = average(close, 10);
def a = b[1];
it won't read b at location 1. it reads b from 1 bar ago, the previous bar.
positive offsets read data in the past.

negative offsets read data from a future bar.
def c = b[-1];

https://tlc.thinkorswim.com/center/...nced/Chapter-10---Referencing-Historical-Data

https://tlc.thinkorswim.com/center/...dvanced/Chapter-12---Past-Offset-and-Prefetch


------------
sorting/ranking data.
this thread has several examples

sorting data from the last x bars
https://usethinkscript.com/threads/rank-volume-1-10.9504/#post-108403

------------
read the aggregation time of a chart ( or column,.. )

def chartagg = getAggregationPeriod();
def chartmin = (chartagg/1000)/60;
addlabel(1, "min " + chartmin, color.magenta);

------------

https://tlc.thinkorswim.com/center/reference/thinkScript/Constants/AverageType
..AverageTypes..
EXPONENTIAL
HULL
SIMPLE
WEIGHTED
WILDERS
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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