Plotting Market-Open Price Line

AstroBoy

New member
Hey everyone!
As I'm starting to learn ThinkScript, I wanted to make a basic script.
GOAL: The script will plot a price level line indicating the Market OPEN price... The plot will start from the end of the first market hour 10:30 AM EST and extend to the left towards first candle at 9:30 AM EST OR the plot will start from the 9:30 AM Market Open Price candle, and extend towards the 10:30 AM EST vertical line.
I took inspiration from a user here named Markos that helped others with defining a specific candle on the chart, but my plot is invalid. Can someone help me?

Code:
#This will draw a vertical line indicating the end of the first hour of trading
AddVerticalLine(SecondsTillTime(1030) == 0, “First Hour”, Color.GRAY, PaintingStrategy.horizontal);

#This defines the exact candle at 9:30 AM EST market open
def na = double.nan;
def MarketOpenCandle = if SecondsFromTime (0930) == 0 && SecondsTillTime(0930) == 0
                then barNumber()
                else na;

#This defines the Market OPEN price to then place the chart bubble
Def MarketOpenPrice = if barNumber() == MarketOpenCandle
                then OPEN
                else MarketOpenPrice[1];
addchartbubble(MarketOpenPrice,OPEN,”Market Open Price”);

#This plot is INVALID and doesn't work
Plot Opening_Price = MarketOpenPrice

Here's a PhotoShopped example of what I am trying to accomplish as an illustration
 
Solution
The code works and it plots the line correctly, but on multiple days, it carries the same line at the same price level to previous days.
I tried adding input so that the script knows to add the respective opening price line for its respective day, but it the script returns an error saying "No such function: MarketOpenPrice at 22:101" though I have already defined what MarketOpenPrice is?

I bolded the changes I added:
Code:
input aggregationPeriod = AggregationPeriod.DAY;
plot Opening_Price = if SecondsFromTime (0930) >= 0 and SecondsFromTime(1029) <= 0 then HighestAll (MarketOpenPrice(period = aggregationPeriod)) else Double.NaN;
The input aggregationPeriod = AggregationPeriod.DAY; on a chart with extended...
Hey everyone!
As I'm starting to learn ThinkScript, I wanted to make a basic script.
GOAL: The script will plot a price level line indicating the Market OPEN price... The plot will start from the end of the first market hour 10:30 AM EST and extend to the left towards first candle at 9:30 AM EST OR the plot will start from the 9:30 AM Market Open Price candle, and extend towards the 10:30 AM EST vertical line.
I took inspiration from a user here named Markos that helped others with defining a specific candle on the chart, but my plot is invalid. Can someone help me?

Code:
#This will draw a vertical line indicating the end of the first hour of trading
AddVerticalLine(SecondsTillTime(1030) == 0, “First Hour”, Color.GRAY, PaintingStrategy.horizontal);

#This defines the exact candle at 9:30 AM EST market open
def na = double.nan;
def MarketOpenCandle = if SecondsFromTime (0930) == 0 && SecondsTillTime(0930) == 0
                then barNumber()
                else na;

#This defines the Market OPEN price to then place the chart bubble
Def MarketOpenPrice = if barNumber() == MarketOpenCandle
                then OPEN
                else MarketOpenPrice[1];
addchartbubble(MarketOpenPrice,OPEN,”Market Open Price”);

#This plot is INVALID and doesn't work
Plot Opening_Price = MarketOpenPrice

Here's a PhotoShopped example of what I am trying to accomplish as an illustration
Here is one way

Ruby:
#This will draw a vertical line indicating the end of the first hour of trading
AddVerticalLine(SecondsTillTime(1030) == 0, “First Hour”, Color.GRAY, PaintingStrategy.HORIZONTAL);

#This defines the exact candle at 9:30 AM EST market open
def na = Double.NaN;
def MarketOpenCandle = if SecondsFromTime (0930) == 0 && SecondsTillTime(0930) == 0
                then BarNumber()
                else na;

#This defines the Market OPEN price to then place the chart bubble
def MarketOpenPrice = if BarNumber() == HighestAll(MarketOpenCandle)
                then open
                else MarketOpenPrice[1];

AddChartBubble(BarNumber() == highestall( MarketOpenCandle), open, ”Market Open Price”);

#This plot is INVALID and doesn't work
plot Opening_Price = if SecondsFromTime (0930) >= 0 and SecondsFromTime(1030) <= 0 then HighestAll (MarketOpenPrice)  else Double.NaN;
Opening_Price.SetDefaultColor(Color.WHITE);
Opening_Price.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Opening_Price.SetLineWeight(3);
 

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

Here is one way
The code works and it plots the line correctly, but on multiple days, it carries the same line at the same price level to previous days.
I tried adding input so that the script knows to add the respective opening price line for its respective day, but it the script returns an error saying "No such function: MarketOpenPrice at 22:101" though I have already defined what MarketOpenPrice is?

I bolded the changes I added:
Code:
input aggregationPeriod = AggregationPeriod.DAY;
plot Opening_Price = if SecondsFromTime (0930) >= 0 and SecondsFromTime(1029) <= 0 then HighestAll (MarketOpenPrice(period = aggregationPeriod)) else Double.NaN;
 
The code works and it plots the line correctly, but on multiple days, it carries the same line at the same price level to previous days.
I tried adding input so that the script knows to add the respective opening price line for its respective day, but it the script returns an error saying "No such function: MarketOpenPrice at 22:101" though I have already defined what MarketOpenPrice is?

I bolded the changes I added:
Code:
input aggregationPeriod = AggregationPeriod.DAY;
plot Opening_Price = if SecondsFromTime (0930) >= 0 and SecondsFromTime(1029) <= 0 then HighestAll (MarketOpenPrice(period = aggregationPeriod)) else Double.NaN;
The input aggregationPeriod = AggregationPeriod.DAY; on a chart with extended hours, it will use the premarket open. The following will define the open at the start of regular trading hours at 0930 and extend it to the right. The plot will limit that line to the 0930-1030 timeframe. If you want to limit it to only show on the current day and not others then see the second code plot and showtodayonly as yes.

Ruby:
def MarketOpenPrice = if SecondsFromTime (0930) == 0 && SecondsTillTime(0930) == 0 then open else marketopenprice[1];

plot Opening_Price = if SecondsFromTime (0930) >= 0 and SecondsFromTime(1029) <= 0 then MarketOpenPrice else Double.NaN;
Ruby:
def MarketOpenPrice = if SecondsFromTime (0930) == 0 && SecondsTillTime(0930) == 0 then open else marketopenprice[1];

input showtodayonly = yes;

plot Opening_Price = if showtodayonly
                     then (if getday()==getlastday() and
                              SecondsFromTime (0930) >= 0 and
                              SecondsFromTime(1029) <= 0
                           then MarketOpenPrice
                           else Double.NaN)
                     else (if SecondsFromTime (0930) >= 0 and
                              SecondsFromTime(1029) <= 0
                           then MarketOpenPrice
                           else Double.NaN);
 
Solution
The input aggregationPeriod = AggregationPeriod.DAY; on a chart with extended hours, it will use the premarket open. The following will define the open at the start of regular trading hours at 0930 and extend it to the right. The plot will limit that line to the 0930-1030 timeframe. If you want to limit it to only show on the current day and not others then see the second code plot and showtodayonly as yes.
I really appreciate the assistance!! With the idea of defining a point within the time frame of 0930-1030, I tried testing my understanding of it and tried marking out the highest price of the 1st hour to plot a between 0930-1030 at that price level... and also the lowest price of the 1st hour, but it came out like this making the lines curve up and down, rather than just be one solid horizontal line like the Opening_Price line, do you have any advice on how to fix it?

Code:
def LowestMorningPrice = if SecondsFromTime(0930) >= 0 and SecondsTillTime(1030) >= 0 then LowestAll(low) else LowestMorningPrice[1];
def MorningLow = if LowestAll(low) then Lowest(low) else MorningLow[1];
plot FirstHourLow = if showtodayonly
    then (if getday()==getlastday() and
    SecondsFromTime (0930) >= 0 and SecondsFromTime (1030) <= 0
                            then MorningLow
                            else Double.NaN)
    else (if SecondsFromTime (0930) >= 0 and SecondsFromTime(1030) <= 0
                            then MorningLow
                            else Double.NaN);


def HighestMorningPrice = if SecondsFromTime(0930) >=0 and SecondsTillTime (1030) >= 0 then HighestAll(High) else HighestMorningPrice[1];
def MorningHigh = if HighestAll(high) then highest(high) else MorningHigh[1];
Plot FirstHourHigh = if showtodayonly
    then (if getday()==getlastday() and
    SecondsFromTime (0930) >= 0 and SecondsFromTime (1030) <= 0
                            then MorningHigh
                            else Double.NaN)
    else (if SecondsFromTime (0930) >=0 and SecondsFromTime(1030) <=0
                            then MorningHigh
                            else Double.NaN);
 
I really appreciate the assistance!! With the idea of defining a point within the time frame of 0930-1030, I tried testing my understanding of it and tried marking out the highest price of the 1st hour to plot a between 0930-1030 at that price level... and also the lowest price of the 1st hour, but it came out like this making the lines curve up and down, rather than just be one solid horizontal line like the Opening_Price line, do you have any advice on how to fix it?

Code:
def LowestMorningPrice = if SecondsFromTime(0930) >= 0 and SecondsTillTime(1030) >= 0 then LowestAll(low) else LowestMorningPrice[1];
def MorningLow = if LowestAll(low) then Lowest(low) else MorningLow[1];
plot FirstHourLow = if showtodayonly
    then (if getday()==getlastday() and
    SecondsFromTime (0930) >= 0 and SecondsFromTime (1030) <= 0
                            then MorningLow
                            else Double.NaN)
    else (if SecondsFromTime (0930) >= 0 and SecondsFromTime(1030) <= 0
                            then MorningLow
                            else Double.NaN);


def HighestMorningPrice = if SecondsFromTime(0930) >=0 and SecondsTillTime (1030) >= 0 then HighestAll(High) else HighestMorningPrice[1];
def MorningHigh = if HighestAll(high) then highest(high) else MorningHigh[1];
Plot FirstHourHigh = if showtodayonly
    then (if getday()==getlastday() and
    SecondsFromTime (0930) >= 0 and SecondsFromTime (1030) <= 0
                            then MorningHigh
                            else Double.NaN)
    else (if SecondsFromTime (0930) >=0 and SecondsFromTime(1030) <=0
                            then MorningHigh
                            else Double.NaN);
To create an horizontal highest high/ lowest low in a range is different than how was the open, which is plotted from the beginning of a range. I am not sure how you are using the highest high / lowest low, so I have 2 codes below. One with cyan/yellow is what you wanted, lines plotted from 0930 to 1030 at those levels. The other, green/gold/red is what is called the 'opening range' for that same period, but it is plotted from those levels.

Screenshot-2021-08-14-080244.jpg
Ruby:
input start           = 0930;
input numberofbars    = 60;     
input showtodayonly   = yes;     
def bars              = numberofbars / (GetAggregationPeriod() / 60000);
def highbars          = if SecondstillTime(start) == 0                               
                        then Highest(high[1], bars)[-bars]
                        else highbars[1];
def lowbars           = if SecondsfromTime(start) == 0                               
                        then Lowest(low[1], bars)[-bars]
                        else lowbars[1];
plot morninghigh      = if showtodayonly
                        then (if getday()==getlastday() and
                             !(secondstilltime(0930)>0 or
                               secondsfromTime(1030)>0)             
                              then highbars
                              else double.nan)
                       else if !(secondstilltime(0930)>0 or
                                 secondsfromTime(1030)>0)
                       then highbars
                       else double.nan;
morninghigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
plot morninglow      = if showtodayonly
                       then (if getday()==getlastday() and
                            !(secondstilltime(0930)>0 or
                              secondsfromTime(1030)>0)             
                             then lowbars
                             else double.nan)
                       else if !(secondstilltime(0930)>0 or
                                 secondsfromTime(1030)>0)
                       then lowbars
                       else double.nan;
morninglow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

Ruby:
input usealerts = no;
input ShowTodayOnly = { default "No", "Yes"};
def s = ShowTodayOnly;
input showbubbles = no;
input n = 5; #Hint n: Number (+ only) of bars to move bubble to the right in the expansion area
def n1 = n + 1;
input showclouds = no;
input period = AggregationPeriod.DAY;
input displace = 1;

def hh = high(period = period);
def ll = low(period = period);

input ORBegin = 0930;
input OREnd = 1030;

def ORActive = if SecondsTillTime(OREnd) > 0 and SecondsFromTime(ORBegin) >= 0 then 1 else if SecondsFromTime(ORBegin)[1] < 0 and SecondsFromTime(ORBegin) >= 0 then 1 else 0;
def today = if s == 0 or GetDay() == GetLastDay() and SecondsFromTime(ORBegin) >= 0 then 1 else 0;
rec ORHigh = if ORHigh[1] == 0 or ORActive[1] == 0 and ORActive == 1 then high else if ORActive and high > ORHigh[1] then high else ORHigh[1];
rec ORLow = if ORLow[1] == 0 or ORActive[1] == 0 and ORActive == 1 then low else if ORActive and low < ORLow[1] then low else ORLow[1];
def orhext = if IsNaN(close) then orhext[1] else ORHigh;
def orlext = if IsNaN(close) then orlext[1] else ORLow;
plot ORH = if ORActive or today < 1  then Double.NaN else orhext;
plot ORL = if ORActive or today < 1  then Double.NaN else orlext;
plot MID = (ORH + ORL) / 2;

ORH.SetDefaultColor(Color.GREEN);
ORH.SetStyle(Curve.MEDIUM_DASH);
ORH.SetLineWeight(2);
ORL.SetDefaultColor(Color.RED);
ORL.SetStyle(Curve.MEDIUM_DASH);
ORL.SetLineWeight(2);
MID.SetDefaultColor(Color.LIGHT_ORANGE);
MID.SetStyle(Curve.MEDIUM_DASH);
MID.SetLineWeight(2);
 
To create an horizontal highest high/ lowest low in a range is different than how was the open, which is plotted from the beginning of a range. I am not sure how you are using the highest high / lowest low, so I have 2 codes below. One with cyan/yellow is what you wanted, lines plotted from 0930 to 1030 at those levels. The other, green/gold/red is what is called the 'opening range' for that same period, but it is plotted from those levels.

I also meant to give you this code, which displays the developing lastday's morning high/low, with an option to display it just for the one hour or the whole day based upon the first hour. Th is script will only display on the lastday.

Ruby:
input start = 0930;
def       h = high;
def       l = low;
input displaytoendofday = yes;
def display      = if displaytoendofday
                   then SecondsTillTime(1600) > 0
                   else SecondsTillTime(1030) >= 0;
def openhigh     = if SecondsFromTime(start) == 0
                   then h
                   else Max(h,openhigh[1]);

plot morninghigh = if GetDay() == GetLastDay() and
                      SecondsFromTime(0930) >= 0 and
                      display
                   then HighestAll( if GetDay() == GetLastDay() and
                                    SecondsFromTime(start) >= 0 and
                                    SecondsTillTime(1030) >= 0
                                    then openhigh
                                    else Double.NaN)
                   else Double.NaN;
morninghigh.SetPaintingStrategy(PaintingStrategy.DASHES);
morninghigh.SetDefaultColor(Color.YELLOW);
morninghigh.SetLineWeight(3);

def openlow    = if SecondsFromTime(start) == 0
                   then l
                   else Min(l, openlow[1]);

plot morninglow = if GetDay() == GetLastDay() and
                   SecondsFromTime(0930) >= 0 and
                   display
                   then LowestAll( if GetDay() == GetLastDay() and
                                      SecondsFromTime(start) >= 0 and
                                      SecondsTillTime(1030) >= 0
                   then openlow
                   else Double.NaN) else Double.NaN;

morninglow.SetPaintingStrategy(PaintingStrategy.DASHES);
morninglow.SetDefaultColor(Color.MAGENTA);
morninglow.SetLineWeight(3);
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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