How To Scan On Past Dates

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

@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):

thinkscript
plot Scan = high[20] == Highest(high[20], 252);
 
@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):

thinkscript
plot Scan = high[20] == Highest(high[20], 252);
let me play with that- thanks
 
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.
 
Id do that if I had the background and skills necessary - thanks for the info. :)
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.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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