WOW great question and I see where you are going with that - I do not know if TOS has that capability. A good question for one of the TOS experts.... @merryDay
@antwerks - Here’s what I got from a Google search, and since I don’t have good coding skills, I’m hoping that you can take this example and apply it to your scan that will allow me to look at past dates:
Select thinkScript Editor instead of the wizard
Use array indexing to tell the scanner how many bars in the past to look at. For example, close[20]represents the closing price exactly 20 days ago. [, 2]
Example Script (Scanning for a 52-week high 20 days ago):
@antwerks - Here’s what I got from a Google search, and since I don’t have good coding skills, I’m hoping that you can take this example and apply it to your scan that will allow me to look at past dates:
Select thinkScript Editor instead of the wizard
Use array indexing to tell the scanner how many bars in the past to look at. For example, close[20]represents the closing price exactly 20 days ago. [, 2]
Example Script (Scanning for a 52-week high 20 days ago):
scans work on data on the last bar.
so... you could make a study that , reads old data, and if the data is what you want, set a plot variable to true, on the last bar.
then add that study to the scan rules.
scans work on data on the last bar.
so... you could make a study that , reads old data, and if the data is what you want, set a plot variable to true, on the last bar.
then add that study to the scan rules.
Three useful historical-scan patterns
1. Condition happened within the last X bars
This is the simplest and usually best:
Code:
input lookbackBars = 10;
def condition =
close crosses above ExpAverage(close, 21);
plot scan =
Highest(
if condition then 1 else 0,
lookbackBars
) > 0;
This returns symbols where the condition occurred at least once during the last 10 bars.
The equivalent scanner concept is “condition was true within X bars,” which ThinkOrSwim also supports through its condition tools.
2. Condition happened previously and remains valid now
This is probably more useful for your swing scanner.
Example:
EMA reclaim occurred during the last 10 bars.
Price is still above the EMA now.
Trend has not failed.
Code:
input emaLength = 21;
input lookbackBars = 10;
def ema21 = ExpAverage(close, emaLength);
def freshReclaim =
close > ema21 and
close[1] <= ema21[1];
def reclaimOccurred =
Highest(
if freshReclaim then 1 else 0,
lookbackBars
) > 0;
def stillValid =
close > ema21 and
ema21 > ema21[1];
plot scan =
reclaimOccurred and
stillValid;
This does not merely find an old signal. It checks that the historical setup is still tradable today.
3). Record a historical event and carry its state forward
Use a recursive variable when the signal should remain active until invalidated:
Code:
input emaLength = 21;
def ema21 = ExpAverage(close, emaLength);
def trigger =
close crosses above ema21;
def invalidate =
close crosses below ema21;
rec setupActive =
if BarNumber() == 1 then 0
else if trigger then 1
else if invalidate then 0
else setupActive[1];
plot scan = setupActive;
This creates memory:
Trigger occurs.
setupActive remains true.
It stays true until the invalidation occurs.
That is especially useful for forex trading tool because it can remember that a tradable condition began several bars ago without requiring the trigger to happen on the current bar. It works for all tradable tickers.
For all of these you will have to input your criteria you are looking for in the script.
Three useful historical-scan patterns
1. Condition happened within the last X bars
This is the simplest and usually best:
Code:
input lookbackBars = 10;
def condition =
close crosses above ExpAverage(close, 21);
plot scan =
Highest(
if condition then 1 else 0,
lookbackBars
) > 0;
This returns symbols where the condition occurred at least once during the last 10 bars.
The equivalent scanner concept is “condition was true within X bars,” which ThinkOrSwim also supports through its condition tools.
2. Condition happened previously and remains valid now
This is probably more useful for your swing scanner.
Example:
EMA reclaim occurred during the last 10 bars.
Price is still above the EMA now.
Trend has not failed.
Code:
input emaLength = 21;
input lookbackBars = 10;
def ema21 = ExpAverage(close, emaLength);
def freshReclaim =
close > ema21 and
close[1] <= ema21[1];
def reclaimOccurred =
Highest(
if freshReclaim then 1 else 0,
lookbackBars
) > 0;
def stillValid =
close > ema21 and
ema21 > ema21[1];
plot scan =
reclaimOccurred and
stillValid;
This does not merely find an old signal. It checks that the historical setup is still tradable today.
3). Record a historical event and carry its state forward
Use a recursive variable when the signal should remain active until invalidated:
Code:
input emaLength = 21;
def ema21 = ExpAverage(close, emaLength);
def trigger =
close crosses above ema21;
def invalidate =
close crosses below ema21;
rec setupActive =
if BarNumber() == 1 then 0
else if trigger then 1
else if invalidate then 0
else setupActive[1];
plot scan = setupActive;
This creates memory:
Trigger occurs.
setupActive remains true.
It stays true until the invalidation occurs.
That is especially useful for forex trading tool because it can remember that a tradable condition began several bars ago without requiring the trigger to happen on the current bar. It works for all tradable tickers.
For all of these you will have to input your criteria you are looking for in the script.
A couple of questions:
1 - do I add one of these at the bottom of an existing scan’s script?
2 - how can I verify it was on the date intended to look back at?
A couple of questions:
1 - do I add one of these at the bottom of an existing scan’s script?
2 - how can I verify it was on the date intended to look back at?
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.