What determines number of aggregation periods used in a scan using a script?


What determines number of aggregation periods used in a scan using a script? For example, if a custom script has selected  daily “D”, how many days data will be scanned?

A related question: Does the scanning script run from earliest period to the most recent or the other way? I’m guessing it the “earliest” so recursive variables and moving averages can be computed.


Marked as spam
Posted by (Questions: 3, Answers: 5)
Asked on May 20, 2019 4:29 pm
Private answer

Question one is answered by Thinkorswim here:


The answer to question 2 is: Left to Right, same exact way that code works on chart studies.

Marked as spam
Posted by (Questions: 36, Answers: 2045)
Answered on May 20, 2019 5:53 pm
"Question one" : Is the answer under the heading of "Data Limitations"? If true, 15 days of data will be pulled in if aggregation period is "From 1 min to 30 min". Please let me know if that is not the case and the answer is somewhere else on that page. Again, if true, will it always pull 15 days of data whether the script needs it or not? I'm also curious about how to debug scan scripts - something akin to debug logs? Thank you for all you do!
( at May 22, 2019 11:44 am)
That is 15 calendar days. So it includes weekends. Which we know are not included in the data stream. So at best you have 11 days of actual chart data available. There are no tools for debugging scans. I plot them on a chart so I can see what the scans are doing. If you have seen any of my free tutorials on scans you already know how to do this.
( at May 22, 2019 12:34 pm)
In your scan https://www.hahn-tech.com/ans/gap-to-previous-day-close-scan/ , I presume one would select "Day" aggregation period in which case scan would collect 730 astronomical days. Since scan starts evaluating from left to right, what is preventing the scan from selecting a stock based on 730 and 729 day because day 729 would return true for GetDay() GetDay[()1] and not today and yesterday? I must be missing something. Please help. Thank you.
( at May 22, 2019 5:18 pm)
No, that code is designed for intraday ONLY so you would NOT set it to daily time frame. Code runs on a scan exactly like it runs on a chart. If you have a chart study that displays a total of 10 signals over the last 20 bars, only the current bar can trigger the scan. Unless you specifically write the code to look back a specified number of bars the scan ALWAYS returns results based on the last bar on the chart.
( at May 22, 2019 6:36 pm)
Both, intraday ( 1 D, x min) and "last bar" correct my misconceptions. Your statement about a scan running the same as a study is very helpful. Even though the script is run for all bars, only it's running on last bar can trigger the scan and select a stock. Are there documents that provide such details because, AFAIK, Thinkorswim or Thinkscript official documentation don't. Thank You!
( at May 22, 2019 7:13 pm)
The exact number of bars used in a scan varies from symbol to symbol depending on aggregation period. To estimate it for an aggregation priod, determine the exact number for a few symbols as follows: 1) create a watch list with one symbol and limit the scan to that watch list 2) Use a technique as detailed in https://stackoverflow.com/questions/56518594/how-to-create-a-variable-that-retains-its-value input estimatedBarNumber = 250; plot scan = BarNumber() == estimatedBarNumber; You run the scan many times, varying estimatedBarNumber until your symbol is returned. Of course, you use an iterative approach with plot scan = BarNumber() > estimatedBarNumber; and plot scan = BarNumber() < estimatedBarNumber; to get there faster.
( at June 15, 2019 7:15 pm)