Plot Premarket Open Trendline / Range

nicktrader

New member
Looking for the correct TOS code to plot a trendline for the opening price based on premarket data (candle) starting at 4AM EST (0400). I have some code to plot the line, but Im having trouble with just plotting it for the current day only (not any prior days). I know my code is probably all messed up.. but it gives an idea.

Code:
input openingPMTime  = 0400.0; #hint OrMeanS: Begin Mean Period. Usually Market Open EST.
input openingTime  = 0930.0; #hint OrMeanS: Begin Mean Period. Usually Market Open EST.

def isDaily = If (GetAggregationPeriod() == AggregationPeriod.DAY, yes, no);
def isToday = If (GetDay() == GetLastDay() and SecondsFromTime(openingPMTime) >= 0, yes, no);
def isPreMarket = If (GetDay() == GetLastDay() and SecondsTillTime(openingPMTime) < 0, yes, no);

input pricePMOpen = open;

rec time_value = if(SecondsTillTime(openingPMTime) == 0, pricePMOpen, time_value[1]);

plot plotPMOpenLine = if(time_value == 0 and isToday, double.nan, time_value);
     plotPMOpenLine.SetDefaultColor(color.yellow);


Any assistance would be greatly appreciated. Thank You!! @BenTen
 

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

Thank you for this script.. I've been looking for pre-market open for a while.. is there a way to code it to display the same data point but from the prior day and display it as a line plot on today's intraday chart? I've tried unsuccessfully to code it myself.. thank you in advance
 
@nicktrader @horserider

This is fantastic! I’m not a coder so this site has been a great help to me. There is lots of scripts in UTS on opening range but I’m looking for a script for the pre-market range with the trendline for the 50% point of that premarket range. It can be drawn manually using TOS channel drawing tool but I’m looking for a script to do this. Can this script to plot the premarket open trendline and the Daily Open script in TOS shown below be combined to plot the 3 trendlines for a premarket range with the trendline for the 50% point of that premarket range? The 3 trendlines would be 1) This premarket open, 2) The 50% trendline between premarket market and market open and 3) market open). Thanks

Code:
#

# TD Ameritrade IP Company, Inc. (c) 2011-2020

#



input aggregationPeriod = AggregationPeriod.DAY;

input showOnlyLastPeriod = yes;



def prevPrice = open(period = aggregationPeriod)[-1];

def price = open(period = aggregationPeriod);

plot DailyOpen = if showOnlyLastPeriod and !IsNaN(prevPrice) then Double.NaN else price;



DailyOpen.SetDefaultColor(GetColor(2));

DailyOpen.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
 
Last edited by a moderator:
Hey guys if I use your code. How do I calculate, open to close of the premarket period. My code is terrible here

Code:
input openingPMTime = 0300.0; #Begin Premarket Opening Period. EST.
input openingTime = 0930.0; #Begin Regular market Opening Period. EST.

def na = Double.NaN;
def day = GetDay();
def isDaily = If (GetAggregationPeriod() == AggregationPeriod.DAY, yes, no);
def isBelowDaily = If (GetAggregationPeriod() < AggregationPeriod.DAY, yes, no);
def isToday = If (day == GetLastDay() and SecondsFromTime(openingPMTime) >= 0, yes, no);

def PMopenBar = day != day[1];
def PMOpen = if openingPMTime then open else na;
def PMClose = if openingTime then close else na;



plot PMC =  (PMOpen - PMClose);
PMC.assignvalueColor(if PMC >0 then color.green else color.red);
 
I was looking for this EXACT thing, glad I found it! However, the code is showing the opening price at the timestamp 00:00:01 and not 4AM EST. Is there a way to grab the price at a particular timestamp?

RNFE8bf.png
 
@markos This should be the full code. I believe a few lines were left off what nicktrader copied as the working study.


Code:
input openingPMTime  = 0400.0; #hint OrMeanS: Begin Mean Period. Usually Market Open EST.
input openingTime  = 0930.0; #hint OrMeanS: Begin Mean Period. Usually Market Open EST.

def isDaily = If (GetAggregationPeriod() == AggregationPeriod.DAY, yes, no);

def isPreMarket = If (GetDay() == GetLastDay() and SecondsTillTime(openingPMTime) < 0, yes, no);
input LineWidth = 1;
def na = Double.NaN;

def isBelowDaily = If (GetAggregationPeriod() < AggregationPeriod.DAY, yes, no);
def isToday = If (GetDay() == GetLastDay() and SecondsFromTime(openingPMTime) >= 0, yes, no);
def day = GetDay();

def PMopenBar = day != day[1];
def PMOpen = if PMopenBar then open else PMOpen[1];

plot PlotPMOLine = if isToday and isBelowDaily then PMOpen else na;
     PlotPMOLine.SetDefaultColor(CreateColor(77, 166, 255));
     PlotPMOLine.SetLineWeight(LineWidth);
     PlotPMOLine.SetPaintingStrategy(PaintingStrategy.DASHES);
     PlotPMOLine.HideTitle();
     PlotPMOLine.HideBubble();
Hi, I have noticed that the values for this on the spy can be off. Today for instance at 4:00 it should be 416 but the script draws the line at 413.33, is there any reason or a way to correct this? Thanks
 
Hi, is there a simple opening range indicator out there? I simply want a horizontal line plotted at the opening range high and a horizontal line at the opening range low, nothing else. I know there are several ORB indicators here on the forums but every one I can find is pretty involved, with lots of extra bells and whistles, and I suspect a bit resource-intensive. My old computer is stretched to the max as it is, and don't even get me started on my brain!
 
Hi, is there a simple opening range indicator out there? I simply want a horizontal line plotted at the opening range high and a horizontal line at the opening range low, nothing else. I know there are several ORB indicators here on the forums but every one I can find is pretty involved, with lots of extra bells and whistles, and I suspect a bit resource-intensive. My old computer is stretched to the max as it is, and don't even get me started on my brain!
http://tos.mx/glhBRfI
Ruby:
input openingRangeMinutes = 30;
input Market_Open_Time = 0930;
input Market_Close_Time = 1430;

def day = getDay();
def pastOpen = if((secondsTillTime(Market_Open_Time) > 0), 0, 1);
def pastClose = if((secondsTillTime(Market_Close_Time) > 0), 0, 1);
def marketOpen = if(pastOpen and !pastClose, 1, 0);
def firstBar = if (day[1] != day, day - 1, 0);

def secondsUntilOpen = secondsTillTime(Market_Open_Time);
def regularHours = secondsTillTime(Market_Close_Time);

def secondsFromOpen = secondsFromTime(Market_Open_Time);
def pastOpeningRange = if(secondsFromOpen >= (openingRangeMinutes *
60) + 1, 1, 0);

REC displayedHigh = if(high > displayedHigh[1] and marketOpen, high,
if(marketOpen and !firstBar, displayedHigh[1], high));
REC displayedLow = if(low < displayedLow[1] and marketOpen, low,
if(marketOpen and !firstBar, displayedLow[1], low));

rec ORHigh = if(pastOpeningRange, ORHigh[1], displayedHigh);
rec ORLow = if(pastOpeningRange, ORLow[1], displayedLow);

plot Opening_Range_High = if(pastOpeningRange and marketOpen, ORHigh,
double.nan);
plot Opening_Range_Low = if(pastOpeningRange and marketOpen, ORLow,
double.nan);
Opening_Range_High.SetDefaultColor(color.green);
Opening_Range_High.SetLineWeight(2);
Opening_Range_High.SetStyle(curve.POINTS);
Opening_Range_Low.SetDefaultColor(color.red);
Opening_Range_Low.SetLineWeight(2);
Opening_Range_Low.SetStyle(curve.POINTS); addOrder(OrderType.BUY_AUTO, no);
 
Last edited by a moderator:
Hi, is there a simple opening range indicator out there? I simply want a horizontal line plotted at the opening range high and a horizontal line at the opening range low, nothing else. I know there are several ORB indicators here on the forums but every one I can find is pretty involved, with lots of extra bells and whistles, and I suspect a bit resource-intensive. My old computer is stretched to the max as it is, and don't even get me started on my brain!
The ToS platform does not provide a way to 'simply' define 'opening' time. @gdlesley script is as simple as what is possible.
 
here is a simple orb study.
it will work with extended hours turned off, but not when on.
pick an aggregation time for the orb time.

Code:
# orb_simple_00

input agg = AggregationPeriod.thirty_min;
def orbhi = high( period = agg);
def orblo = low( period = agg);

def newday = getday() <> getday()[1];
def hi = if newday then orbhi else hi[1];
def lo = if newday then orblo else lo[1];

plot h2 = hi;
plot l2 = lo;
h2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
h2.SetDefaultColor(Color.light_gray);
h2.hidebubble();
l2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l2.SetDefaultColor(Color.light_gray);
l2.hidebubble();

#h2.SetStyle(Curve.MEDIUM_DASH);
#h2.setlineweight(1);
#
 
Hey Everyone,
I use this code i found here to display ORH and ORL. It works great on time charts but for some reason it doesn't on a tick chart.
Any way to adjust it?
Thanks!

Code:
# Simple Opening Range
# Mobius
# V01.01.2011
input Start = 0930;
input End = 0945;

def h = high; def l = low; def c = close;
def x = barNumber();
def nan = double.nan;
def Active = SecondsFromTime(Start) >= 0 and
SecondsTillTime(End) >= 0;

def RTH = getTime() >= RegularTradingStart(getYYYYMMDD()) and
getTime() <= RegularTradingEnd(getYYYYMMDD());
def ORx1 = if Active and !Active[1]
then x
else ORx1[1];

def ORx2 = if !Active and Active[1]
then x-1
else nan;

def ORxHalf = Floor((HighestAll(ORx2) - HighestAll(ORx1))/2);
def ORh;
def ORl;

if Active and !Active[1]
{
ORh = h;
ORl = l;
}
else if Active and
h > ORh[1]
{
ORh = h;
ORl = ORl[1];
}
else if Active and
l < ORl[1]
{
ORh = ORh[1];
ORl = l;
}
else
{
ORh = ORh[1];
ORl = ORl[1];
}

plot ORh_ = if x >= HighestAll(ORx1)
then HighestAll(if isNaN(c[-1])
then ORh
else nan)
else nan;

ORh_.SetStyle(Curve.Long_Dash);
ORh_.SetLineWeight(3);
ORh_.SetDefaultColor(Color.GREEN);

plot ORl_ = if barNumber() >= HighestAll(ORx1)
then HighestAll(if isNaN(c[-1])
then ORl
else nan)
else nan;

ORl_.SetStyle(Curve.Long_Dash);
ORl_.SetLineWeight(3);
ORl_.SetDefaultColor(Color.RED);

# End Code
 
Last edited by a moderator:
Hey Everyone,
I use this code i found here to display ORH and ORL. It works great on time charts but for some reason it doesn't on a tick chart.
Any way to adjust it?
Thanks!

Code:
# Simple Opening Range

# Mobius

# V01.01.2011

input Start = 0930;

input End = 0945;

def h = high;

def l = low;

def c = close;

def x = barNumber();

def nan = double.nan;

def Active = SecondsFromTime(Start) >= 0 and

SecondsTillTime(End) >= 0;

def RTH = getTime() >= RegularTradingStart(getYYYYMMDD()) and

getTime() <= RegularTradingEnd(getYYYYMMDD());

def ORx1 = if Active and !Active[1]

then x

else ORx1[1];

def ORx2 = if !Active and Active[1]

then x-1

else nan;

def ORxHalf = Floor((HighestAll(ORx2) - HighestAll(ORx1))/2);

def ORh;

def ORl;

if Active and !Active[1]

{

ORh = h;

ORl = l;

}

else if Active and

h > ORh[1]

{

ORh = h;

ORl = ORl[1];

}

else if Active and

l < ORl[1]

{

ORh = ORh[1];

ORl = l;

}

else

{

ORh = ORh[1];

ORl = ORl[1];

}

plot ORh_ = if x >= HighestAll(ORx1)

then HighestAll(if isNaN(c[-1])

then ORh

else nan)

else nan;

ORh_.SetStyle(Curve.Long_Dash);

ORh_.SetLineWeight(3);

ORh_.SetDefaultColor(Color.GREEN);

plot ORl_ = if barNumber() >= HighestAll(ORx1)

then HighestAll(if isNaN(c[-1])

then ORl

else nan)

else nan;

ORl_.SetStyle(Curve.Long_Dash);

ORl_.SetLineWeight(3);

ORl_.SetDefaultColor(Color.RED);

# End Code

Scripts that utilize time (like this script, which uses time to define opening levels) cannot be used in tick charts. As there is no time associated with tick charting which are volume based.
read more:
https://usethinkscript.com/threads/...tick-range-renko-charts-in-thinkorswim.18400/
 

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
236 Online
Create Post

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