Simple Thinkscript code to plot highest and lowest price for a given timeframe

dog9355

New member
I am trying to create code that will automatically create a horizontal line at the highest and lowest price for the morning and afternoon sessions. So basically, between 9:35 AM and 10.05 AM, it would plot 2 lines at the highest and lowest price during that period. And then in the afternoon, it would do the same thing from 12:35 PM to 1:05 PM.

Can anyone help???

Basically something like this :

Solution
I am trying to create code that will automatically create a horizontal line at the highest and lowest price for the morning and afternoon sessions. So basically, between 9:35 AM and 10.05 AM, it would plot 2 lines at the highest and lowest price during that period. And then in the afternoon, it would do the same thing from 12:35 PM to 1:05 PM.

Can anyone help???

Basically something like this :

here is something i made , just change to start and stop times
@dog9355 , if you already creating something, show us what you got, it might be easier to fix it than coming up something from scratch.

-S

Sorry, I'm not a very good coder, right now I just have it picking the price as a manual input. Just can't figure out how to do it dynamically :

input Morning_Upper = 3000.1;
input Morning_Lower = 3000.1;
input Aft_Upper = 3000.1;
input Aft_Lower = 3000.1;

plot Mupper = Morning_Upper;
Mupper.SetDefaultColor(Color.YELLOW);
Mupper.SetStyle(Curve.SHORT_DASH);

plot Mlower = Morning_Lower;
Mlower.SetDefaultColor(Color.YELLOW);
Mlower.SetStyle(Curve.SHORT_DASH);

plot Aftupper = Aft_Upper;
Aftupper.SetDefaultColor(Color.BLUE);
Aftupper.SetStyle(Curve.SHORT_DASH);

plot Aftlower = Aft_Lower;
Aftlower.SetDefaultColor(Color.BLUE);
Aftlower.SetStyle(Curve.SHORT_DASH);

I am trying to create code that will automatically create a horizontal line at the highest and lowest price for the morning and afternoon sessions. So basically, between 9:35 AM and 10.05 AM, it would plot 2 lines at the highest and lowest price during that period. And then in the afternoon, it would do the same thing from 12:35 PM to 1:05 PM.

Can anyone help???

Basically something like this :

here is something i made , just change to start and stop times

@dog9355 pieced this together from another script of mine.

Ruby:
``````input Morning_Start_Time = 0935;
input Morning_End_Time = 1005;
input Afternoon_Start_Time = 1235;
input Afternoon_End_Time = 1305;
input End_Of_Day = 1630;
######################################
def Morning_Session = if SecondsFromTime(Morning_Start_Time) >= 0 and SecondsTillTime(Morning_End_Time) >= 0 then 1 else Double.NaN;
def Afternoon_Session = if SecondsFromTime(Afternoon_Start_Time) >= 0 and SecondsTillTime(Afternoon_End_Time) >= 0 then 1 else Double.NaN;
######################################
def Morning_Upper;
if SecondsTillTime(End_Of_Day) == 0 {
Morning_Upper = Double.NaN;
}else if secondsFromTime(Morning_Start_Time) == 0 {
Morning_Upper = high;
}else if Morning_Session and high>=Morning_Upper[1]{
Morning_Upper = high;
}else{
Morning_Upper = Morning_Upper[1];}

def Morning_Lower;
if SecondsTillTime(End_Of_Day) == 0 {
Morning_Lower = Double.NaN;
}else if secondsFromTime(Morning_Start_Time) == 0 {
Morning_Lower = low;
}else if Morning_Session and low<=Morning_Lower[1]{
Morning_Lower = low;
}else{
Morning_Lower = Morning_Lower[1];}

def Aft_Upper;
if SecondsTillTime(End_Of_Day) == 0 {
Aft_Upper = Double.NaN;
}else if secondsFromTime(Afternoon_Start_Time) == 0 {
Aft_Upper = high;
}else if Afternoon_Session and high>=Aft_Upper[1]{
Aft_Upper = high;
}else{
Aft_Upper = Aft_Upper[1];}

def Aft_Lower;
if SecondsTillTime(End_Of_Day) == 0 {
Aft_Lower = Double.NaN;
}else if secondsFromTime(Afternoon_Start_Time) == 0 {
Aft_Lower = low;
}else if Afternoon_Session and low<=Aft_Lower[1]{
Aft_Lower = low;
}else{
Aft_Lower = Aft_Lower[1];}
######################################
def Morning_Session_StartBar = if SecondsFromTime(Morning_Start_Time) == 0 then BarNumber() else Morning_Session_StartBar[1];
def Morning_Session_EndBar = fold i = 0 to AbsValue(BarNumber()) while !IsNaN(GetValue(Morning_Session, -i)) do GetValue(BarNumber(), -i);
def Morning_Session_Length = 1 + (Morning_Session_EndBar - Morning_Session_StartBar);
def Morning_Session_High = fold ih = 0 to AbsValue(Morning_Session_Length) while !IsNaN(GetValue(Morning_Session, -ih)) do GetValue(Morning_Upper, -ih);
def Morning_Session_Low = fold il = 0 to AbsValue(Morning_Session_Length) while !IsNaN(GetValue(Morning_Session, -il)) do GetValue(Morning_Lower, -il);

def Afternoon_Session_StartBar = if SecondsFromTime(Afternoon_Start_Time) == 0 then BarNumber() else Afternoon_Session_StartBar[1];
def Afternoon_Session_EndBar = fold j = 0 to AbsValue(BarNumber()) while !IsNaN(GetValue(Afternoon_Session, -j)) do GetValue(BarNumber(), -j);
def Afternoon_Session_Length = 1 + (Afternoon_Session_EndBar - Afternoon_Session_StartBar);
def Afternoon_Session_High = fold jh = 0 to AbsValue(Afternoon_Session_Length) while !IsNaN(GetValue(Afternoon_Session, -jh)) do GetValue(Aft_Upper, -jh);
def Afernoon_Session_Low = fold jl = 0 to AbsValue(Afternoon_Session_Length) while !IsNaN(GetValue(Afternoon_Session, -jl)) do GetValue(Aft_Lower, -jl);

######################################
plot Mupper = if !IsNaN(Morning_Session) then Morning_Session_High else Double.NaN;
Mupper.SetDefaultColor(Color.YELLOW);
Mupper.SetStyle(Curve.SHORT_DASH);

plot Mlower = if !IsNaN(Morning_Session) then Morning_Session_Low else Double.NaN;
Mlower.SetDefaultColor(Color.YELLOW);
Mlower.SetStyle(Curve.SHORT_DASH);

plot Aftupper = if !IsNaN(Afternoon_Session) then Afternoon_Session_High else Double.NaN;
Aftupper.SetDefaultColor(Color.BLUE);
Aftupper.SetStyle(Curve.SHORT_DASH);

plot Aftlower = if !IsNaN(Afternoon_Session) then Afernoon_Session_Low else Double.NaN;
Aftlower.SetDefaultColor(Color.BLUE);
Aftlower.SetStyle(Curve.SHORT_DASH);

plot Mupper_Ext = if IsNaN(Morning_Session) then Morning_Upper else Double.NaN;
Mupper_Ext.SetDefaultColor(Color.YELLOW);
Mupper_Ext.SetStyle(Curve.SHORT_DASH);

plot Mlower_Ext = if IsNaN(Morning_Session) then Morning_Lower else Double.NaN;
Mlower_Ext.SetDefaultColor(Color.YELLOW);
Mlower_Ext.SetStyle(Curve.SHORT_DASH);

plot Aftupper_Ext = if IsNaN(Afternoon_Session) then Aft_Upper else Double.NaN;
Aftupper_Ext.SetDefaultColor(Color.BLUE);
Aftupper_Ext.SetStyle(Curve.SHORT_DASH);

plot Aftlower_Ext = if IsNaN(Afternoon_Session) then Aft_Lower else Double.NaN;
Aftlower_Ext.SetDefaultColor(Color.BLUE);
Aftlower_Ext.SetStyle(Curve.SHORT_DASH);``````

Last edited:
@dog9355 pieced this together from another script of mine.

@dog9355 pieced this together from another script of mine.

Ruby:
``````input Morning_Start_Time = 0935;
input Morning_End_Time = 1005;
input Afternoon_Start_Time = 1235;
input Afternoon_End_Time = 1305;
######################################
def Morning_Session = SecondsFromTime(Morning_Start_Time) >= 0 and SecondsTillTime(Morning_End_Time) >= 0;
def Afternoon_Session = SecondsFromTime(Afternoon_Start_Time) >= 0 and SecondsTillTime(Afternoon_End_Time) >= 0;
######################################
def Morning_Upper;
if secondsFromTime(Morning_Start_Time)==0 {
Morning_Upper = high;
}else if Morning_Session and high>=Morning_Upper[1]{
Morning_Upper = high;
}else{
Morning_Upper = Morning_Upper[1];}

def Morning_Lower;
if secondsFromTime(Morning_Start_Time)==0 {
Morning_Lower = low;
}else if Morning_Session and low<=Morning_Lower[1]{
Morning_Lower = low;
}else{
Morning_Lower = Morning_Lower[1];}

def Aft_Upper;
if secondsFromTime(Afternoon_Start_Time)==0 {
Aft_Upper = high;
}else if Afternoon_Session and high>=Aft_Upper[1]{
Aft_Upper = high;
}else{
Aft_Upper = Aft_Upper[1];}

def Aft_Lower;
if secondsFromTime(Afternoon_Start_Time)==0 {
Aft_Lower = low;
}else if Afternoon_Session and low<=Aft_Lower[1]{
Aft_Lower = low;
}else{
Aft_Lower = Aft_Lower[1];}
######################################
def Morning_Session_StartBar = if SecondsFromTime(Morning_Start_Time) == 0 then BarNumber() else Morning_Session_StartBar[1];
def Morning_Session_EndBar = fold i = 0 to AbsValue(BarNumber()) while !IsNaN(GetValue(Morning_Session, -i)) do GetValue(BarNumber(), -i);
def Morning_Session_Length = 1 + (Morning_Session_EndBar - Morning_Session_StartBar);
def Morning_Session_High = fold iah = 0 to AbsValue(Morning_Session_Length) while Morning_Session do GetValue(Morning_Upper, -iah);
def Morning_Session_Low = fold ial = 0 to AbsValue(Morning_Session_Length) while Morning_Session do GetValue(Morning_Lower, -ial);

def Afternoon_Session_StartBar = if SecondsFromTime(Afternoon_Start_Time) == 0 then BarNumber() else Afternoon_Session_StartBar[1];
def Afternoon_Session_EndBar = fold j = 0 to AbsValue(BarNumber()) while !IsNaN(GetValue(Afternoon_Session, -j)) do GetValue(BarNumber(), -j);
def Afternoon_Session_Length = 1 + (Afternoon_Session_EndBar - Afternoon_Session_StartBar);
def Afternoon_Session_High = fold jah = 0 to AbsValue(Afternoon_Session_Length) while Afternoon_Session do GetValue(Aft_Upper, -jah);
def Afernoon_Session_Low = fold jal = 0 to AbsValue(Afternoon_Session_Length) while Afternoon_Session do GetValue(Aft_Lower, -jal);

######################################
plot Mupper = if Morning_Session and Morning_Session_High > 0 then Morning_Session_High else Double.NaN;
Mupper.SetDefaultColor(Color.YELLOW);
Mupper.SetStyle(Curve.SHORT_DASH);

plot Mlower = if Morning_Session and Morning_Session_Low > 0 then Morning_Session_Low else Double.NaN;
Mlower.SetDefaultColor(Color.YELLOW);
Mlower.SetStyle(Curve.SHORT_DASH);

plot Aftupper = if Afternoon_Session and Afternoon_Session_High > 0 then Afternoon_Session_High else Double.NaN;
Aftupper.SetDefaultColor(Color.BLUE);
Aftupper.SetStyle(Curve.SHORT_DASH);

plot Aftlower = if Afternoon_Session and Afernoon_Session_Low > 0 then Afernoon_Session_Low else Double.NaN;
Aftlower.SetDefaultColor(Color.BLUE);
Aftlower.SetStyle(Curve.SHORT_DASH);``````

Ruby:
``````input Morning_Start_Time = 0935;
input Morning_End_Time = 1005;
input Afternoon_Start_Time = 1235;
input Afternoon_End_Time = 1305;
######################################
def Morning_Session = SecondsFromTime(Morning_Start_Time) >= 0 and SecondsTillTime(Morning_End_Time) >= 0;
def Afternoon_Session = SecondsFromTime(Afternoon_Start_Time) >= 0 and SecondsTillTime(Afternoon_End_Time) >= 0;
######################################
def Morning_Upper;
if secondsFromTime(Morning_Start_Time)==0 {
Morning_Upper = high;
}else if Morning_Session and high>=Morning_Upper[1]{
Morning_Upper = high;
}else{
Morning_Upper = Morning_Upper[1];}

def Morning_Lower;
if secondsFromTime(Morning_Start_Time)==0 {
Morning_Lower = low;
}else if Morning_Session and low<=Morning_Lower[1]{
Morning_Lower = low;
}else{
Morning_Lower = Morning_Lower[1];}

def Aft_Upper;
if secondsFromTime(Afternoon_Start_Time)==0 {
Aft_Upper = high;
}else if Afternoon_Session and high>=Aft_Upper[1]{
Aft_Upper = high;
}else{
Aft_Upper = Aft_Upper[1];}

def Aft_Lower;
if secondsFromTime(Afternoon_Start_Time)==0 {
Aft_Lower = low;
}else if Afternoon_Session and low<=Aft_Lower[1]{
Aft_Lower = low;
}else{
Aft_Lower = Aft_Lower[1];}
######################################
def Morning_Session_StartBar = if SecondsFromTime(Morning_Start_Time) == 0 then BarNumber() else Morning_Session_StartBar[1];
def Morning_Session_EndBar = fold i = 0 to AbsValue(BarNumber()) while !IsNaN(GetValue(Morning_Session, -i)) do GetValue(BarNumber(), -i);
def Morning_Session_Length = 1 + (Morning_Session_EndBar - Morning_Session_StartBar);
def Morning_Session_High = fold iah = 0 to AbsValue(Morning_Session_Length) while Morning_Session do GetValue(Morning_Upper, -iah);
def Morning_Session_Low = fold ial = 0 to AbsValue(Morning_Session_Length) while Morning_Session do GetValue(Morning_Lower, -ial);

def Afternoon_Session_StartBar = if SecondsFromTime(Afternoon_Start_Time) == 0 then BarNumber() else Afternoon_Session_StartBar[1];
def Afternoon_Session_EndBar = fold j = 0 to AbsValue(BarNumber()) while !IsNaN(GetValue(Afternoon_Session, -j)) do GetValue(BarNumber(), -j);
def Afternoon_Session_Length = 1 + (Afternoon_Session_EndBar - Afternoon_Session_StartBar);
def Afternoon_Session_High = fold jah = 0 to AbsValue(Afternoon_Session_Length) while Afternoon_Session do GetValue(Aft_Upper, -jah);
def Afernoon_Session_Low = fold jal = 0 to AbsValue(Afternoon_Session_Length) while Afternoon_Session do GetValue(Aft_Lower, -jal);

######################################
plot Mupper = if Morning_Session and Morning_Session_High > 0 then Morning_Session_High else Double.NaN;
Mupper.SetDefaultColor(Color.YELLOW);
Mupper.SetStyle(Curve.SHORT_DASH);

plot Mlower = if Morning_Session and Morning_Session_Low > 0 then Morning_Session_Low else Double.NaN;
Mlower.SetDefaultColor(Color.YELLOW);
Mlower.SetStyle(Curve.SHORT_DASH);

plot Aftupper = if Afternoon_Session and Afternoon_Session_High > 0 then Afternoon_Session_High else Double.NaN;
Aftupper.SetDefaultColor(Color.BLUE);
Aftupper.SetStyle(Curve.SHORT_DASH);

plot Aftlower = if Afternoon_Session and Afernoon_Session_Low > 0 then Afernoon_Session_Low else Double.NaN;
Aftlower.SetDefaultColor(Color.BLUE);
Aftlower.SetStyle(Curve.SHORT_DASH);``````
This is great.... thank you so much.

Is there any way to extend the dotted lines so that they go across the whole day's range???

@dog9355 updated code in previous post.

Not the exact question you're looking for?

87k+ Posts
309 Online

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?