Scan based on value from specific time in past?


Category:
0
0

Hi Pete. I am trying to figure out how to capture an exact value from a very specific time in the past so I can use that captured/recorded value as an input later on in the same thinkscript for a scan. So here is an example….

What if I want to run a scan where I get the script to do this:

  1. Search all stocks where this next set of criteria is true..

    Criteria: Have the script lookup the historical value from today of ChaikinMoneyFlow at exactly 9:00am on the close of that 1 minute bar on the 1 minute chart (with exthours enabled)  and have the script somehow save that exact value with the “rec” command. Then ask the script to only show a list of certain stocks where the current ChaikinMoneyFlow today later in the same day (still on the 1 minute chart) is greater than the value which was recorded and stored from 4:00am earlier this morning ? Thanks!

RESOLVED
Marked as spam
Posted by (Questions: 3, Answers: 4)
Asked on December 14, 2020 12:49 pm
85 views
0
Private answer

The code for this can be written. However your goals are unrealistic. The list of stocks for which this plan will work is very limited. For the vast majority of stocks, there will be no 1 min bar at 9:00 Eastern. And this will break the code and the scan will deliver completely wrong results. Trading during extended hours session is very low and you cannot count on there being any data for the code to read at a specific time during extended hours session.

You may find this works for stocks like QQQ and SPY. But almost every other stock in the market is going to fail when you try to apply these conditions to a scan.

Edit: The request has been modified to get the value of the ChaikinMoneyFlow at 9:30 am Eastern and use hold that value until 9:29 am the following day. Then at any time you run scan, compare the stored valued form 9:30 am to the current value of ChaikinMoneyFlow.

So we copy the code straight from the built-in ChaikinMoneyFlow study included with Thinkorswim. Add one line of code at the top to contain the user input for time. Add two lines at the bottom, one to get and hold the value from the target time and the other to check if current value of ChaikinMoneyFlow is above the value from the target time.

input targetTime = 930;
input length = 21;
def tmp_var = if high == low then volume else (close - low - (high - close)) / (high - low) * volume;
def sum_close = sum(tmp_var, length);
def total = sum(volume, length);
def CMF = if total == 0 then 0 else sum_close / total;
rec targetValue = if SecondsTillTime(targetTime) == 0 then CMF else targetValue[1];
plot scan = CMF > targetValue;

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on December 14, 2020 2:58 pm
0
Thanks Pete. The type of stocks I trade are usually all stocks with very significant pre-market volume activity. These high volume premarket stocks will actually be a part of the scan as well which I already have figured out on how to find all stocks with significant premarket activity. What I am trying to do is once I get that list (which I already have working well) to add this additional time based scan criteria to it which I asked you about. So the probability is very high (almost guaranteed) that at 9:00am there will be a 1 minute bar and associated volume to base the lookup on. I tried using the method to lookup past bars but that is not as accurate as looking up specific time maybe using GetValue or something else
( at December 14, 2020 3:32 pm)
0
What would the code be then if I look at the 9:30am 1 minute bar to grab the value ?
( at December 14, 2020 4:16 pm)
0
I have updated my answer to include the modified solution you requested.
( at December 15, 2020 9:47 am)
0
Thanks Pete. So simple and it is incredibly helpful. I will try it out tomorrow.
( at December 15, 2020 6:53 pm)