Hello. I need assistance in a code that basically scans (during premarket) that the current price is less than the previous day high? I know if I use the Daily Aggregation it wont use the premarket session data so im trying to get a code where it includes the premarket session. Thanks alot
def isOpen =
GetTime() >= RegularTradingStart(GetYYYYMMDD())
and GetTime() <= RegularTradingEnd(GetYYYYMMDD())
;
def pHigh =
if isOpen and !IsOpen[1] then high
else if isOpen then max(pHigh[1],High)
else pHigh[1]
;
def isPreMarket =
if isOpen then No
else if GetDay() != GetDay()[1] then Yes
else isPreMarket[1]
;
plot Scan = isPreMarket and Close < pHigh;
Anything to do with time in ToS is potentially tricky and gets wonky around weekends. You can use a combination of functions: https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Date---Time/GetYYYYMMDD
is good, and you can mod their example to give you the high from the previous day
then you can use SecondsTillTime(0930) > 0 to get results before the open
I don't have the bandwidth to sort it all out right now, but this may get you headed in the right direction. Good luck!
def isOpen =
GetTime() >= RegularTradingStart(GetYYYYMMDD())
and GetTime() <= RegularTradingEnd(GetYYYYMMDD())
;
def pHigh =
if isOpen and !IsOpen[1] then high
else if isOpen then max(pHigh[1],High)
else pHigh[1]
;
def isPreMarket =
if isOpen then No
else if GetDay() != GetDay()[1] then Yes
else isPreMarket[1]
;
plot Scan = isPreMarket and Close < pHigh;
def isOpen =
GetTime() >= RegularTradingStart(GetYYYYMMDD())
and GetTime() <= RegularTradingEnd(GetYYYYMMDD())
;
def pHigh =
if isOpen and !IsOpen[1] then high
else if isOpen then max(pHigh[1],High)
else pHigh[1]
;
def isPreMarket =
if isOpen then No
else if GetDay() != GetDay()[1] then Yes
else isPreMarket[1]
;
plot Scan = isPreMarket and Close < pHigh;
It is usually a good idea to avoid using the hourly aggregation with time sensitive code. The first bar contains that awkward thirty minutes of pre-market data. Use an aggregation that typically divides into market hours evenly; I would go with 30 minutes or less. Make sure it is set to is true, and extended hours is enabled in the scan.
It is usually a good idea to avoid using the hourly aggregation with time sensitive code. The first bar contains that awkward thirty minutes of pre-market data. Use an aggregation that typically divides into market hours evenly; I would go with 30 minutes or less. Make sure it is set to is true, and extended hours is enabled in the scan.
Here is another way to do your request. There were quite a few stocks where there was no premarket trading and showed up in the scan,, so I excluded those in the scan plot. You can set up the scan script in a chart to test the accuracy of the results.
Study script with debug
Ruby:
def ymd = GetYYYYMMDD();
def capture = !isnan(close) and ymd != ymd[1];
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
def thisDay = highestall(daycount) - daycount ;
def begin = if GetAggregationPeriod() == AggregationPeriod.HOUR then 0900 else 0930;
def phigh = if thisday == 1 and SecondsFromTime(begin)[1] < 0 and SecondsFromTime(begin) >= 0
then high
else if thisday == 1 and SecondsTillTime(1600) > 0
then Max(high, phigh[1]) else phigh[1];
def tclose = if thisDay == 0 and SecondsFromTime(0400)[1] < 0 and SecondsFromTime(0400) >= 0
then close else if thisDay == 0 and SecondsTillTime(begin) > 0
then Max(close, tclose[1]) else tclose[1];
plot scan = if thisday == 0 and tclose < phigh and tclose > 0 then 1 else 0;
input debug = no;
plot x = if thisday > 1 then double.nan else phigh;
x.SetPaintingStrategy(PaintingStrategy.hoRIZONTAL);
x.sethiding(!debug);
plot y = if thisday > 1 then double.nan else tclose;
y.SetPaintingStrategy(PaintingStrategy.horIZONTAL);
y.sethiding(!debug);
AddLabel(1, phigh + " " + tclose + " " + scan + " " + (dayCount) + " " + HighestAll(dayCount));
Scan script without debug
The picture has the scan results (190 run on SP500) run on 1 hour agg with a chart of one of the results. The red line is the previous high and the white line is the highest close in the current day's premarket
Ruby:
def ymd = GetYYYYMMDD();
def capture = !isnan(close) and ymd != ymd[1];
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
def thisDay = highestall(daycount) - daycount ;
def begin = if GetAggregationPeriod() == AggregationPeriod.HOUR then 0900 else 0930;
def phigh = if thisday == 1 and SecondsFromTime(begin)[1] < 0 and SecondsFromTime(begin) >= 0
then high
else if thisday == 1 and SecondsTillTime(1600) > 0
then Max(high, phigh[1]) else phigh[1];
def tclose = if thisDay == 0 and SecondsFromTime(0400)[1] < 0 and SecondsFromTime(0400) >= 0
then close else if thisDay == 0 and SecondsTillTime(begin) > 0
then Max(close, tclose[1]) else tclose[1];
plot scan = if thisday == 0 and tclose < phigh and tclose > 0 then 1 else 0;
@tradeking313 also be aware that this scans the current pre-market price, at any given moment, as requested. As time surpasses the pre-market, it depopulates, as there is no longer a current pre-market price.
Here is another way to do your request. There were quite a few stocks where there was no premarket trading and showed up in the scan,, so I excluded those in the scan plot. You can set up the scan script in a chart to test the accuracy of the results.
Study script with debug
Scan script without debug
The picture has the scan results (190 run on SP500) run on 1 hour agg with a chart of one of the results. The red line is the previous high and the white line is the highest close in the current day's premarket
So inside the scan script it has a aggregation period of a HOUR..any particular reason HOUR and not 30 min..and also do I match whatever aggregation inside the script.. to the aggregation on the scan filter (where it has extended hours) next to it?
So inside the scan script it has a aggregation period of a HOUR..any particular reason HOUR and not 30 min..and also do I match whatever aggregation inside the script.. to the aggregation on the scan filter (where it has extended hours) next to it?
The hour is for the code just to use the right start time of the first bar of the day, which is generally 0930, but the hour agg starts at 0900. I tested the hour as you indicated in a previous post an example of an hour. You can choose other aggregations than an hour.
If you are testing your scan to the chart study, you need to have both at the same aggregation.
Any one having a scanner to find out the STOCK which is trading 1-2 dollar below PRE Market HIGH for stock price above $50 ? Example: Lets assume TSLA premarket High was $100... Now it is trading it $98.. Scanner should pick it.
Scan script without debug
The picture has the scan results (190 run on SP500) run on 1 hour agg with a chart of one of the results. The red line is the previous high and the white line is the highest close in the current day's premarket
Ruby:
def ymd = GetYYYYMMDD();
def capture = !isnan(close) and ymd != ymd[1];
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
def thisDay = highestall(daycount) - daycount ;
def begin = if GetAggregationPeriod() == AggregationPeriod.HOUR then 0900 else 0930;
def phigh = if thisday == 1 and SecondsFromTime(begin)[1] < 0 and SecondsFromTime(begin) >= 0
then high
else if thisday == 1 and SecondsTillTime(1600) > 0
then Max(high, phigh[1]) else phigh[1];
def tclose = if thisDay == 0 and SecondsFromTime(0400)[1] < 0 and SecondsFromTime(0400) >= 0
then close else if thisDay == 0 and SecondsTillTime(begin) > 0
then Max(close, tclose[1]) else tclose[1];
plot scan = if thisday == 0 and tclose < phigh and tclose > 0 then 1 else 0;
def tclose = if thisDay == 0 and SecondsFromTime(0400)[1] < 0 and SecondsFromTime(0400) >= 0
then close else if thisDay == 0 and SecondsTillTime(begin) > 0
then Max(close, tclose[1]) else tclose[1];
@SleepyZ, can you please enlighten me once again or punt me to some books (lol)? You have this wired to check for highest close but I wanted lowest low. i tried for like 2 hours today but I know it's just a programming concept that I don't know:
I did
Ruby:
Min(low(), tclose[1])
but my scan doesn't work at all.
The concept i have trouble with is how ' tclose[1] ' is calculated? I think it's recursive but I am having trouble wrapping my brain around it. I even tried
I was also trying to change criteria towards the end, looking for % change. So I was looking at previous day's high, percent change to pre-market lows.
Ruby:
plot scan = if thisday == 0 and ((phigh - tclose[1]) / phigh) > percChange and tclose > 0 then 1 else 0;
@SleepyZ, can you please enlighten me once again or punt me to some books (lol)? You have this wired to check for highest close but I wanted lowest low. i tried for like 2 hours today but I know it's just a programming concept that I don't know:
I did
Ruby:
Min(low(), tclose[1])
but my scan doesn't work at all.
The concept i have trouble with is how ' tclose[1] ' is calculated? I think it's recursive but I am having trouble wrapping my brain around it. I even tried
I was also trying to change criteria towards the end, looking for % change. So I was looking at previous day's high, percent change to pre-market lows.
Ruby:
plot scan = if thisday == 0 and ((phigh - tclose[1]) / phigh) > percChange and tclose > 0 then 1 else 0;
def ScanActive = if SecondsTillTime(0930) >= 0
and SecondsFromTime(0001) > 0
then 1
else 0;
def tclose = if ScanActive and !ScanActive[1]
then low
else if !ScanActive
then double.nan
else if ScanActive and low < tclose[1]
then low else tclose[1];
plot scan = if thisday == 0 and (((tclose - phigh) / phigh) * 100) < -10 and tclose > .01 then 1 else 0;
@SleepyZ, can you please enlighten me once again or punt me to some books (lol)? You have this wired to check for highest close but I wanted lowest low. i tried for like 2 hours today but I know it's just a programming concept that I don't know:
I did
Ruby:
Min(low(), tclose[1])
but my scan doesn't work at all.
The concept i have trouble with is how ' tclose[1] ' is calculated? I think it's recursive but I am having trouble wrapping my brain around it. I even tried
I was also trying to change criteria towards the end, looking for % change. So I was looking at previous day's high, percent change to pre-market lows.
Ruby:
plot scan = if thisday == 0 and ((phigh - tclose[1]) / phigh) > percChange and tclose > 0 then 1 else 0;
The 'thisday' code identifies each day on the chart based upon yyyymmdd. The current day is 0 and previous days are numbered sequentially from 1.
The first part of each premarket variable was rewritten to be easier to understand and reliable using thisday and not the time code (secondsfromtime and secondstilltime. At the point when thisday==0 and thisday[1]==1, no matter the time, the variables are defined initially. As the premarket is sporadically traded, this method made initalizing the variables more reliable.
From the initial close, it was changed to be the developing close rather than the max close. From the inital low, it is the developing low which is the minimum of the current bars low or the prior developing low (tlow[1]). From the inital high, it is the developing high which is the maximum of the current bars high or the prior developing high (thigh[1]).
The picture below shows each of the developing plots for all the variables (previous days high and premarket (high, low and close)).
Hope this helps.
Ruby:
def ymd = GetYYYYMMDD();
def capture = !IsNaN(close) and ymd != ymd[1];
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
def thisDay = HighestAll(dayCount) - dayCount ;
def begin = if GetAggregationPeriod() == AggregationPeriod.HOUR then 0900 else 0930;
def phigh = if thisDay == 1 and SecondsFromTime(begin)[1] < 0 and SecondsFromTime(begin) >= 0
then high
else if thisDay == 1 and SecondsTillTime(1600) > 0
then Max(high, phigh[1]) else phigh[1];
def tclose = if thisDay == 0 and thisDay[1] == 1
then close else if thisDay == 0 and SecondsTillTime(begin) > 0
then close else tclose[1];
def tlow = if thisDay == 0 and thisDay[1] == 1
then low else if thisDay == 0 and SecondsTillTime(begin) > 0
then Min(low, tlow[1]) else tlow[1];
def thigh = if thisDay == 0 and thisDay[1] == 1
then high else if thisDay == 0 and SecondsTillTime(begin) > 0
then Max(high, thigh[1]) else thigh[1];
plot scan = if thisDay == 0 and tlow < phigh and tlow > 0 then 1 else 0;
AddLabel(1, begin);
plot xphigh = phigh;
xphigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
plot xlow = tlow;
xlow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
plot xhigh = thigh;
xhigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
plot xclose = tclose;
xclose.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
plot xthisday = thisDay;
xthisday.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
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.