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.
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.
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.
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.
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?
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.
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
# 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);
#
--------------------
=====================
=====================
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];
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.
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.