How to specify time period in stock scan filter?

M

Mamba10

New member
I was wondering if it's possible to make a scan limited to a certain time period in the day. Specifically the first 15 mins after open. I want the scan to only output stocks that are exhibiting the filters I've set only at that exact time period in the day.

So I have a MTF MACD indicator that Hahn Tech publicly released. The histogram turns red or green if the Current, Mid, and Long term time frames are in conjunction. It took me some time to make a scan for it, but I got around TOS's "second aggregation not allowed". But I only want the scan to be limited to the first 15 mins of market open.

Could this thinkScript work, if I entered it correctly to the scan code? Or is there something better/easier?:

Code:
input OpenTime = 930;
input DurationMinutes = 15;
def durationSec = DurationMinutes * 60;
def secondsPassed = SecondsFromTime(OpenTime);
def within = if secondsPassed >= 0 and secondsPassed <= durationSec then close else  Double.NaN;
 
T

tomsk

Well-known member
VIP
Here is a GENERIC scan with a time bracket for the first 15 minutes of the trading day
Describe your scan condition via the variable "condition"
Here's the snippet which evaluates a boolean value

Code:
def condition = <DESCRIBE_YOUR_SCAN_CONDITION_HERE>

def Active = secondsTillTime(0930) > 0 and secondsFromTime(0945) > 0;

plot scan = condition and Active;
 
M

Mamba10

New member
Here is a GENERIC scan with a time bracket for the first 15 minutes of the trading day
Describe your scan condition via the variable "condition"
Here's the snippet which evaluates a boolean value

def condition = <DESCRIBE_YOUR_SCAN_CONDITION_HERE>
def Active = secondsTillTime(0930) > 0 and secondsFromTime(0945) > 0;
plot scan = condition and Active;
Thanks!!
 
S

sigmaikem

New member
@tomsk where do I enter this code? I am new to thinkorswim
 
BenTen

BenTen

Administrative
Staff
VIP
@sigmaikem Go to the Scan tab and setup your scanner there. You can also read this thread if you don't know how to setup your own scanner.
 
M

msammons

New member
I tried the above code but there were no executions. I am trying to get trades to execute only from 6:00 am EST to 8:00 pm EST (regular day + normal extended hours). Lots of executions with this code but it does not eliminate the executions outside the extended hours (during backtesting). And when trading SPY thinkorswim extended hours are apparently 24/5.

Can anyone help? where is my code wrong? (or perhaps there is an easier way to restrict trades to only reg hours plus normal extended hours (8 am - 8 pm EST).


def x = ExpAverage(close, 30);
def y = ExpAverage(close,10);

input DayTimeStart=0600; # 8AM EST
input NightTimeEnd=1959; # 8PM EST

def CountfromStart=SecondsfromTime(DayTimeStart);
def CounttilEnd= Secondstilltime(NightTimeEnd);

def dayperiod=If(CountfromStart >0 && CounttilEnd >0,1,-1); # then 1 else 0;

def long = y crosses above x and dayperiod==1;
def short = y crosses below x and dayperiod == 1;

plot a = long;
plot b = short;

AddOrder(OrderType.BUY_TO_OPEN, long, open[-1], 100);
AddOrder(OrderType.SELL_TO_OPEN, short, open[-1], 100);
 

Similar threads

Top