The First 15 Min Bar

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;
 
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;
 

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

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!!
 
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).

Code:
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);
 
Is it possible to scan for a specific time range, ie 3-4pm?

Currently I know how to setup my scanner to scan on the hourly timeframe, and I can designate how many bars back to include, but this isn't reliable as there are sometime varying number of bars depending on the stock.

I'd like to be able to scan the previous 3-4pm block of time whether it's 8pm or 11pm, and not worry about how many bars to include.
 
Is it possible to scan for a specific time range, ie 3-4pm?

Currently I know how to setup my scanner to scan on the hourly timeframe, and I can designate how many bars back to include, but this isn't reliable as there are sometime varying number of bars depending on the stock.

I'd like to be able to scan the previous 3-4pm block of time whether it's 8pm or 11pm, and not worry about how many bars to include.
Hey! Did you figure out how to do it? Been searching for it but havent been able to find anything
 
I found some code to plot the first and last bar of the day on a chart and tried modifying it to scan for scenarios where the first 15-minute bar of the day for any stock is green (open < close). See below for the code I'm using. Unfortunately, it's not returning any results so I believe something is wrong but I'm not entirely sure what it is. I set the aggregation of the study to 15-minutes as well.

Code:
#

# logic

#

def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];

def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());

def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;

def condition = if firstbarofDay and open < close then 1 else 0;

plot scan = condition;
 
I fixed this issue myself: See below for the code I used:

Code:
# Condition for green bar                                                                            

def bn = if SecondsFromTime(0930) == 0 then 1 else bn[1] + 1;
def b1open = if bn==1 then open  else b1open[1];
def b1close = if bn==1 then close else b1close[1];
def b1high = if bn==1 then high  else b1high[1];
def b1low = if bn==1 then low   else b1low[1];

def green = if b1open < b1close then 1 else 0;

plot scan = green;

Change the "aggregation period" to the timeframe of your choice and it'll provide results for stocks where the first bar of the day in any timeframe is green (open greater than close). You can do b1open > b1close for red bars as well.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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