previous day's high and low, including extended-hour sessions (pre- and post)

shakib3585

Active member
VIP
Hello,

Can anyone please help me with a script to find the previous session's high and low, including extended-hour sessions (pre and post)? I would like to find it based on a 1-hour aggregation period. I have modified a previous code made by @SleepyZ and adjusted the open and close session to get the highest high of the previous day. I plotted it against the ticker DUO but it was not working. Please help

Thank you

Code:
input daysback = 1;

def ymd      = GetYYYYMMDD();
def candles  = !IsNaN(close);
def capture  = candles and ymd != ymd[1];
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
def thisDay  = (HighestAll(dayCount) - dayCount) ;

#Prior Day HLC RegularTradingHours
input rthbegin = 0100;
input rthend   = 2000;
def pastOpen   = If(secondsfromTime(rthbegin)[1]<0 and (SecondsTillTime(rthbegin) >= 0), 0, 1);
def pastClose  = If(secondsfromTime(rthend)[1]<0 and (SecondsTillTime(rthend) >= 0), 0, 1);
def marketOpen = If(pastOpen and !pastClose, 1, 0);
def date       = GetYYYYMMDD();
def c = close;
def h = high;
def l = low;
def o = open;

#Highest High during RTH from Previous Day Determined
def begin      = CompoundValue(1, if thisday==daysback
                                  then if SecondsFromTime(rthbegin) == 0
                                       then h
                                       else if h > begin[1] and marketOpen
                                       then h
                                       else begin[1]
                                  else 0, begin[1]);
def beginbn    = if GetDay() == GetLastDay()
                 then 0
                 else if getday()==getday()[1]
                      then if h == begin and marketOpen
                           then BarNumber()
                           else beginbn[1]
                 else 0;
def beginprice = if getday()==getday()[1]
                 then if BarNumber() == HighestAll(beginbn)
                      then h
                      else beginprice[1]
                 else beginprice[1];

#Lowest Low During RTH from Previous Day Determined
def begin1      = CompoundValue(1, if thisday==daysback
                                   then if SecondsFromTime(rthbegin) == 0
                                        then l
                                        else if l < begin1[1] and marketOpen
                                        then l
                                        else begin1[1]
                                   else 0, l);
def beginbn1    = if GetDay() == GetLastDay()
                  then 0
                  else if l == begin1
                  then BarNumber()
                  else beginbn1[1];
def beginprice1 = if date == date[1]
                  then if BarNumber() == HighestAll(beginbn1)
                       then l
                       else beginprice1[1]
                  else beginprice1[1];

#Close During RTH from Previous Day Determined
def begin2      = CompoundValue(1, if thisday==daysback
                                   then if secondsfromTime(rthend)[1]<0 and SecondstillTime(rthend) >0
                                        then c
                                        else begin2[1]
                                   else begin2[1], c);
def beginbn2    = if GetDay() == GetLastDay()
                  then 0
                  else if c == begin2
                       then BarNumber()
                       else beginbn2[1];
def beginprice2 = if date == date[1]
                  then if BarNumber() == HighestAll(beginbn2)
                       then c
                       else beginprice2[1]
                  else beginprice2[1];

#Open During RTH from Previous Day Determined
def begin3      = CompoundValue(1, if thisday==daysback
                                   then if SecondsFromTime(rthbegin) == 0
                                        then o
                                        else begin3[1]
                                   else begin3[1], 0);
def beginbn3    = if GetDay() == GetLastDay()
                  then 0
                  else if date==date[1] and  o == (begin3)
                       then BarNumber()
                       else beginbn3[1];
def beginprice3 = if date == date[1]
                  then if BarNumber() == (beginbn3)
                       then o
                       else beginprice3[1]
                  else beginprice3[1];

def ph = if getday()!=getlastday() then double.nan else beginprice;
def pl = if getday()!=getlastday() then double.nan else beginprice1;
def pc = if getday()!=getlastday() then double.nan else beginprice2;
def po = if getday()!=getlastday() then double.nan else beginprice3;

plot a = ph;
 
Solution
Hello,

Can anyone please help me with a script to find the previous session's high and low, including extended-hour sessions (pre and post)? I would like to find it based on a 1-hour aggregation period. I have modified a previous code made by @SleepyZ and adjusted the open and close session to get the highest high of the previous day. I plotted it against the ticker DUO but it was not working. Please help

Thank you

Code:
input daysback = 1;

def ymd      = GetYYYYMMDD();
def candles  = !IsNaN(close);
def capture  = candles and ymd != ymd[1];
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
def thisDay  = (HighestAll(dayCount) - dayCount) ;

#Prior Day HLC RegularTradingHours
input rthbegin...
Hello,

Can anyone please help me with a script to find the previous session's high and low, including extended-hour sessions (pre and post)? I would like to find it based on a 1-hour aggregation period. I have modified a previous code made by @SleepyZ and adjusted the open and close session to get the highest high of the previous day. I plotted it against the ticker DUO but it was not working. Please help

Thank you

Code:
input daysback = 1;

def ymd      = GetYYYYMMDD();
def candles  = !IsNaN(close);
def capture  = candles and ymd != ymd[1];
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
def thisDay  = (HighestAll(dayCount) - dayCount) ;

#Prior Day HLC RegularTradingHours
input rthbegin = 0100;
input rthend   = 2000;
def pastOpen   = If(secondsfromTime(rthbegin)[1]<0 and (SecondsTillTime(rthbegin) >= 0), 0, 1);
def pastClose  = If(secondsfromTime(rthend)[1]<0 and (SecondsTillTime(rthend) >= 0), 0, 1);
def marketOpen = If(pastOpen and !pastClose, 1, 0);
def date       = GetYYYYMMDD();
def c = close;
def h = high;
def l = low;
def o = open;

#Highest High during RTH from Previous Day Determined
def begin      = CompoundValue(1, if thisday==daysback
                                  then if SecondsFromTime(rthbegin) == 0
                                       then h
                                       else if h > begin[1] and marketOpen
                                       then h
                                       else begin[1]
                                  else 0, begin[1]);
def beginbn    = if GetDay() == GetLastDay()
                 then 0
                 else if getday()==getday()[1]
                      then if h == begin and marketOpen
                           then BarNumber()
                           else beginbn[1]
                 else 0;
def beginprice = if getday()==getday()[1]
                 then if BarNumber() == HighestAll(beginbn)
                      then h
                      else beginprice[1]
                 else beginprice[1];

#Lowest Low During RTH from Previous Day Determined
def begin1      = CompoundValue(1, if thisday==daysback
                                   then if SecondsFromTime(rthbegin) == 0
                                        then l
                                        else if l < begin1[1] and marketOpen
                                        then l
                                        else begin1[1]
                                   else 0, l);
def beginbn1    = if GetDay() == GetLastDay()
                  then 0
                  else if l == begin1
                  then BarNumber()
                  else beginbn1[1];
def beginprice1 = if date == date[1]
                  then if BarNumber() == HighestAll(beginbn1)
                       then l
                       else beginprice1[1]
                  else beginprice1[1];

#Close During RTH from Previous Day Determined
def begin2      = CompoundValue(1, if thisday==daysback
                                   then if secondsfromTime(rthend)[1]<0 and SecondstillTime(rthend) >0
                                        then c
                                        else begin2[1]
                                   else begin2[1], c);
def beginbn2    = if GetDay() == GetLastDay()
                  then 0
                  else if c == begin2
                       then BarNumber()
                       else beginbn2[1];
def beginprice2 = if date == date[1]
                  then if BarNumber() == HighestAll(beginbn2)
                       then c
                       else beginprice2[1]
                  else beginprice2[1];

#Open During RTH from Previous Day Determined
def begin3      = CompoundValue(1, if thisday==daysback
                                   then if SecondsFromTime(rthbegin) == 0
                                        then o
                                        else begin3[1]
                                   else begin3[1], 0);
def beginbn3    = if GetDay() == GetLastDay()
                  then 0
                  else if date==date[1] and  o == (begin3)
                       then BarNumber()
                       else beginbn3[1];
def beginprice3 = if date == date[1]
                  then if BarNumber() == (beginbn3)
                       then o
                       else beginprice3[1]
                  else beginprice3[1];

def ph = if getday()!=getlastday() then double.nan else beginprice;
def pl = if getday()!=getlastday() then double.nan else beginprice1;
def pc = if getday()!=getlastday() then double.nan else beginprice2;
def po = if getday()!=getlastday() then double.nan else beginprice3;

plot a = ph;

that code seems to work , but.... it is way overly complicated,

you need to learn to look at a chart and describe what you want to see. if you are asking questions, don't guess at how to do it. mentioning 1 hour aggregation is irrelevant and confusing and has no bearing on finding a high of a day. focus on just what you want.

since you want data from a previous day, you don't need aggregation. just use a simple comparison to find the highest.
this only works for stocks. if you pick futures, it won't work because their period spans over midnight.
(if you want that, search for session, to find studies that find high and low for asia and europe trading times (and change it).


Code:
#prev_day_hilo3

def na = double.nan;
def bn = barnumber();

def d = getday();
def istoday = getday() == getlastday();

def hi = if istoday then hi[1] else if d != d[1] then high else max(hi[1], high);
def lo = if istoday then lo[1] else if d != d[1] then low else min(lo[1], low);

plot zhi = if istoday then hi else na;
plot zlo = if istoday then lo else na;

#plot y = hi;
#y.SetStyle(Curve.MEDIUM_DASH);
#
 
Solution

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

that code seems to work , but.... it is way overly complicated,

you need to learn to look at a chart and describe what you want to see. if you are asking questions, don't guess at how to do it. mentioning 1 hour aggregation is irrelevant and confusing and has no bearing on finding a high of a day. focus on just what you want.

since you want data from a previous day, you don't need aggregation. just use a simple comparison to find the highest.
this only works for stocks. if you pick futures, it won't work because their period spans over midnight.
(if you want that, search for session, to find studies that find high and low for asia and europe trading times (and change it).


Code:
#prev_day_hilo3

def na = double.nan;
def bn = barnumber();

def d = getday();
def istoday = getday() == getlastday();

def hi = if istoday then hi[1] else if d != d[1] then high else max(hi[1], high);
def lo = if istoday then lo[1] else if d != d[1] then low else min(lo[1], low);

plot zhi = if istoday then hi else na;
plot zlo = if istoday then lo else na;

#plot y = hi;
#y.SetStyle(Curve.MEDIUM_DASH);
#
Thank you @halcyonguy . One question please, can it be modified such that it can do the same for any previous day (i.e., 1 day before, 2 days before etc). Thanks a bunch!
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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