Highest and Lowest of the current quarter

DmitryOlk

New member
Plus
(thinkScript, Thinkorswim)

Please advise how to correctly finish the script,
which is almost ready.

In the code below,
it is necessary to draw horizontal lines (Highest and Lowest of the current quarter)
from mark to mark of drawing of the consecutive
quarter - from yellow to yellow vertical line,
based on my parameters;
in the code,
horizontal lines on the graph, drawing marks - "Kvartal_Bn"?


code:

Code:
def na  =  Double.NaN;
def bn  =  BarNumber ();

def is_real_Bar = !IsNaN(close) and IsNaN(close[-1]);               #  lastBar
def    real_Bar = if IsNaN(close[-1]) then  bn  else real_Bar [1];  #  lastBar


def  current_Date  =  GetYYYYMMDD ();       #  current  date
def  current_D     =  GetDay ();            #  number  current  day

def current_W  =  GetWeek();    #  number  current  week    in year
def current_M  =  GetMonth();   #  number  current  Month   in year
def current_Y  =  GetYear();    #          current  Year

def current_D_W  = GetDayOfWeek ( current_Date );    #  number  day  of week   ( 1 = Mon  ..  7 = Sun )
def current_D_M  = GetDayOfMonth( current_Date );    #  number  day  of month  ( 1  ..  31 )


def DayWeek = GetDayOfWeek( current_Date );


def secondFridayOffset =         #     ( 1 = Mon  ..  7 = Sun )
    if DayWeek <= 5 then 5 - DayWeek + 7
    else
       if  DayWeek == 6  then
          14      # Sat.
       else
          13  ;   # Sun.


def is_Friday  =  GetDayOfWeek( current_Date ) == 5;


def Kvartal_Date =
 ( current_M == 3  or  current_M == 6  or  current_M == 9  or  current_M == 12 )
     and
 ( current_D_M  ==  secondFridayOffset )
;


def Kvartal_Bn  =    if  Kvartal_Date  then  bn  else Kvartal_Bn[1];   



#   x lines :

#    Quartal  High
???

#    Quartal  Low
???


#  vertical Lines befor new Quartal
AddVerticalLine( (real_Bar < 1)  &&  Kvartal_Date , " " + Kvartal_Date , Color.ORANGE, Curve.SHORT_DASH);  #  , curve.MEDIUM_DASH);

#   end

Thank you in advance for your support in this decision.
 

Attachments

  • img.png
    img.png
    167.2 KB · Views: 81
Solution
Code:
#antall fredager. Inndataen må være et positivt heltall større enn null.
input Fridays = 1;
def FixIn =
    AbsValue(Max(Floor(Fridays),1));
def Date = GetYYYYMMDD();
def DoW  = GetDayOfWeek(Date);
def GM   = GetMonth();
def Fris =
    if (GM % 3) && !(GM[1] % 3) then
     GetDay() + (5 - Dow)
     + ((FixIn - 1) * 7)
    else Fris[1];
def isFri =
    GetDay() == Fris or
   (GetDay() != Fris &&
    Crosses(GetDay(),Fris,1)[-1]);
profile Q_High =
    DataProfile(high,PricePerRow.automatic,isFri,No);
    plot QH = Q_High.gethighest();
    QH.setpaintingStrategy(11);
    QH.setDefaultColor(color.red);
profile Q_Low =
    DataProfile(Low,PricePerRow.automatic,isFri,No);
    plot QL = Q_Low.getLowest();
    QL.SetpaintingStrategy(11)...
(thinkScript, Thinkorswim)

Please advise how to correctly finish the script,
which is almost ready.

In the code below,
it is necessary to draw horizontal lines (Highest and Lowest of the current quarter)
from mark to mark of drawing of the consecutive
quarter - from yellow to yellow vertical line,
based on my parameters;
in the code,
horizontal lines on the graph, drawing marks - "Kvartal_Bn"?


code:

Code:
def na  =  Double.NaN;
def bn  =  BarNumber ();

def is_real_Bar = !IsNaN(close) and IsNaN(close[-1]);               #  lastBar
def    real_Bar = if IsNaN(close[-1]) then  bn  else real_Bar [1];  #  lastBar


def  current_Date  =  GetYYYYMMDD ();       #  current  date
def  current_D     =  GetDay ();            #  number  current  day

def current_W  =  GetWeek();    #  number  current  week    in year
def current_M  =  GetMonth();   #  number  current  Month   in year
def current_Y  =  GetYear();    #          current  Year

def current_D_W  = GetDayOfWeek ( current_Date );    #  number  day  of week   ( 1 = Mon  ..  7 = Sun )
def current_D_M  = GetDayOfMonth( current_Date );    #  number  day  of month  ( 1  ..  31 )


def DayWeek = GetDayOfWeek( current_Date );


def secondFridayOffset =         #     ( 1 = Mon  ..  7 = Sun )
    if DayWeek <= 5 then 5 - DayWeek + 7
    else
       if  DayWeek == 6  then
          14      # Sat.
       else
          13  ;   # Sun.


def is_Friday  =  GetDayOfWeek( current_Date ) == 5;


def Kvartal_Date =
 ( current_M == 3  or  current_M == 6  or  current_M == 9  or  current_M == 12 )
     and
 ( current_D_M  ==  secondFridayOffset )
;


def Kvartal_Bn  =    if  Kvartal_Date  then  bn  else Kvartal_Bn[1];  



#   x lines :

#    Quartal  High
???

#    Quartal  Low
???


#  vertical Lines befor new Quartal
AddVerticalLine( (real_Bar < 1)  &&  Kvartal_Date , " " + Kvartal_Date , Color.ORANGE, Curve.SHORT_DASH);  #  , curve.MEDIUM_DASH);

#   end

Thank you in advance for your support in this decision.

This seems to work to plot the highest high/ lowest low for the last Quartal as you requested
Code:
def na  =  Double.NaN;
def bn  =  BarNumber ();

def is_real_Bar = !IsNaN(close) and IsNaN(close[-1]);               #  lastBar
def    real_Bar = if IsNaN(close[-1]) then  bn  else real_Bar [1];  #  lastBar


def  current_Date  =  GetYYYYMMDD ();       #  current  date
def  current_D     =  GetDay ();            #  number  current  day

def current_W  =  GetWeek();    #  number  current  week    in year
def current_M  =  GetMonth();   #  number  current  Month   in year
def current_Y  =  GetYear();    #          current  Year

def current_D_W  = GetDayOfWeek ( current_Date );    #  number  day  of week   ( 1 = Mon  ..  7 = Sun )
def current_D_M  = GetDayOfMonth( current_Date );    #  number  day  of month  ( 1  ..  31 )


def DayWeek = GetDayOfWeek( current_Date );


def secondFridayOffset =         #     ( 1 = Mon  ..  7 = Sun )
    if DayWeek <= 5 then 5 - DayWeek + 7
    else
       if  DayWeek == 6  then
          14      # Sat.
       else
          13  ;   # Sun.


def is_Friday  =  GetDayOfWeek( current_Date ) == 5;


def Kvartal_Date =
 ( current_M == 3  or  current_M == 6  or  current_M == 9  or  current_M == 12 )
     and
 ( current_D_M  ==  secondFridayOffset )
;


def Kvartal_Bn  =    if  Kvartal_Date  then  bn  else Kvartal_Bn[1];   



#   x lines :

#    Quartal  High
plot Quartal_High = if bn >= HighestAll(Kvartal_Bn)
then HighestAll(if bn >= HighestAll(Kvartal_Bn) then high else na)
else na;

#    Quartal  Low
plot Quartal_Low = if bn >= HighestAll(Kvartal_Bn)
then LowestAll(if bn >= HighestAll(Kvartal_Bn) then low else na)
else na;

#  vertical Lines befor new Quartal
AddVerticalLine( (real_Bar < 1)  &&  Kvartal_Date , " " + Kvartal_Date , Color.ORANGE, Curve.SHORT_DASH);  #  , curve.MEDIUM_DASH);

#   end
 

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

SleepyZ thank you for the answer.​


But the code you provided has some hidden meaning,
since there are no horizontal lines on the graph.

Based on the picture you provided,
I understood the following:

based on the horizontal coordinates of a certain quarter,
the presence of 2 points :

1) - "from" - Kvartal_Bn
2) - "to" - ?

accordingly,
the initial point of the coordinates is determined.

Please tell me
how to correctly calculate the second point of coordinates horizontally, of a certain current quarter?

Thank you in advance for your support in this decision.
 
(thinkScript, Thinkorswim)

Please advise how to correctly finish the script,
which is almost ready.

In the code below,
it is necessary to draw horizontal lines (Highest and Lowest of the current quarter)
from mark to mark of drawing of the consecutive
quarter - from yellow to yellow vertical line,
based on my parameters;
in the code,
horizontal lines on the graph, drawing marks - "Kvartal_Bn"?


code:

Code:
def na  =  Double.NaN;
def bn  =  BarNumber ();

def is_real_Bar = !IsNaN(close) and IsNaN(close[-1]);               #  lastBar
def    real_Bar = if IsNaN(close[-1]) then  bn  else real_Bar [1];  #  lastBar


def  current_Date  =  GetYYYYMMDD ();       #  current  date
def  current_D     =  GetDay ();            #  number  current  day

def current_W  =  GetWeek();    #  number  current  week    in year
def current_M  =  GetMonth();   #  number  current  Month   in year
def current_Y  =  GetYear();    #          current  Year

def current_D_W  = GetDayOfWeek ( current_Date );    #  number  day  of week   ( 1 = Mon  ..  7 = Sun )
def current_D_M  = GetDayOfMonth( current_Date );    #  number  day  of month  ( 1  ..  31 )


def DayWeek = GetDayOfWeek( current_Date );


def secondFridayOffset =         #     ( 1 = Mon  ..  7 = Sun )
    if DayWeek <= 5 then 5 - DayWeek + 7
    else
       if  DayWeek == 6  then
          14      # Sat.
       else
          13  ;   # Sun.


def is_Friday  =  GetDayOfWeek( current_Date ) == 5;


def Kvartal_Date =
 ( current_M == 3  or  current_M == 6  or  current_M == 9  or  current_M == 12 )
     and
 ( current_D_M  ==  secondFridayOffset )
;


def Kvartal_Bn  =    if  Kvartal_Date  then  bn  else Kvartal_Bn[1];  



#   x lines :

#    Quartal  High
???

#    Quartal  Low
???


#  vertical Lines befor new Quartal
AddVerticalLine( (real_Bar < 1)  &&  Kvartal_Date , " " + Kvartal_Date , Color.ORANGE, Curve.SHORT_DASH);  #  , curve.MEDIUM_DASH);

#   end

Thank you in advance for your support in this decision.

going by your first rule , and ignoring the rest,...
this will draw horizontal lines , at the highest and lowest, for each quarter period.

Code:
input agg = AggregationPeriod.quarter;

plot zhi = high(period = agg);
zhi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zhi.SetDefaultColor(Color.cyan);
zhi.hidebubble();

plot zlo = low(period = agg);
zlo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zhi.SetDefaultColor(Color.cyan);
zhi.hidebubble();
 
halcyonguy, thanks for the answer.

But the code you provided has some hidden meaning,
because the horizontal lines on the chart are drawn based on a constant hidden in the code, and in my example - the variable values of the days of a certain second or third Friday, according to the condition of the code (which is already there - "Kvartal_Bn" ) :

Based on the image you provided,
I understood the following:

Everything seems to be correct, but
Please tell me how to correctly "move" the lines to the left - from the yellow line to the yellow line, in accordance with the current quarter?

Thank you in advance for your support in this decision.
 

Attachments

  • img1.png
    img1.png
    184.8 KB · Views: 60
Code:
#antall fredager. Inndataen må være et positivt heltall større enn null.
input Fridays = 1;
def FixIn =
    AbsValue(Max(Floor(Fridays),1));
def Date = GetYYYYMMDD();
def DoW  = GetDayOfWeek(Date);
def GM   = GetMonth();
def Fris =
    if (GM % 3) && !(GM[1] % 3) then
     GetDay() + (5 - Dow)
     + ((FixIn - 1) * 7)
    else Fris[1];
def isFri =
    GetDay() == Fris or
   (GetDay() != Fris &&
    Crosses(GetDay(),Fris,1)[-1]);
profile Q_High =
    DataProfile(high,PricePerRow.automatic,isFri,No);
    plot QH = Q_High.gethighest();
    QH.setpaintingStrategy(11);
    QH.setDefaultColor(color.red);
profile Q_Low =
    DataProfile(Low,PricePerRow.automatic,isFri,No);
    plot QL = Q_Low.getLowest();
    QL.SetpaintingStrategy(11);
    QL.setdefaultColor(Color.green);
AddVerticalLine(
    isFri,"",Color.Yellow,3);
 
Last edited:
Solution
going by your first rule , and ignoring the rest,...
this will draw horizontal lines , at the highest and lowest, for each quarter period.

Code:
input agg = AggregationPeriod.quarter;

plot zhi = high(period = agg);
zhi.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zhi.SetDefaultColor(Color.cyan);
zhi.hidebubble();

plot zlo = low(period = agg);
zlo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zhi.SetDefaultColor(Color.cyan);
zhi.hidebubble();
You got about as far as I did last month with Dmitry.

https://usethinkscript.com/threads/quarterly-highs-and-lows.21357/#post-155475

I think the best strategy would be to have input dates for what he believes a quarter to start and end. He wants them to start in March, June, September and December. Unlike the 99.999999% of humans that understand that quarters start on January 1, April 1, July 1 and October 1.

Dmitry has yet to reply when he wants his quarter to end.
 
Dmitry.
Choose whatever date you want your quarter/quartval to start and end. You get to choose.
Code:
input Q1_Start = 20250101;
input Q1_End = 20250331;
input Q2_Start = 20250401;
input Q2_End = 20250630;
input Q3_Start = 20250701;
input Q3_End = 20250931;
input Q4_Start = 20251001;
input Q4_End = 20251231;

def in_Q1 = GetYYYYMMDD() >= Q1_Start AND GetYYYYMMDD()<= Q1_End;
def in_Q2 = GetYYYYMMDD() >= Q2_Start AND GetYYYYMMDD()<= Q2_End;
def in_Q3 = GetYYYYMMDD() >= Q3_Start AND GetYYYYMMDD()<= Q3_End;
def in_Q4 = GetYYYYMMDD() >= Q4_Start AND GetYYYYMMDD()<= Q4_End;

def Q1_High = if in_Q1 then high else double.NaN;
def Q1_Highest = highestall(Q1_High);
plot Q1H = if in_Q1 then Q1_Highest else double.NaN;
Q1H.setpaintingStrategy(11);
Q1H.setDefaultColor(color.red);

def Q1_Low = if in_Q1 then low else double.NaN;
def Q1_Lowest = lowestall(Q1_Low);
plot Q1L = if in_Q1 then Q1_Lowest else double.NaN;
Q1L.SetpaintingStrategy(11);
Q1L.setdefaultColor(color.red);

def Q2_High = if in_Q2 then high else double.NaN;
def Q2_Highest = highestall(Q2_High);
plot Q2H = if in_Q2 then Q2_Highest else double.NaN;
Q2H.setpaintingStrategy(11);
Q2H.setDefaultColor(color.red);

def Q2_Low = if in_Q2 then low else double.NaN;
def Q2_Lowest = lowestall(Q2_Low);
plot Q2L = if in_Q2 then Q2_Lowest else double.NaN;
Q2L.SetpaintingStrategy(11);
Q2L.setdefaultColor(color.red);

def Q3_High = if in_Q3 then high else double.NaN;
def Q3_Highest = highestall(Q3_High);
plot Q3H = if in_Q3 then Q3_Highest else double.NaN;
Q3H.setpaintingStrategy(11);
Q3H.setDefaultColor(color.red);

def Q3_Low = if in_Q3 then low else double.NaN;
def Q3_Lowest = lowestall(Q3_Low);
plot Q3L = if in_Q3 then Q3_Lowest else double.NaN;
Q3L.SetpaintingStrategy(11);
Q3L.setdefaultColor(color.red);

def Q4_High = if in_Q4 then high else double.NaN;
def Q4_Highest = highestall(Q4_High);
plot Q4H = if in_Q4 then Q4_Highest else double.NaN;
Q4H.setpaintingStrategy(11);
Q4H.setDefaultColor(color.red);

def Q4_Low = if in_Q4 then low else double.NaN;
def Q4_Lowest = lowestall(Q4_Low);
plot Q4L = if in_Q4 then Q4_Lowest else double.NaN;
Q4L.SetpaintingStrategy(11);
Q4L.setdefaultColor(color.red);

addVerticalLine(GetYYYYMMDD() == Q1_Start, "Q1 Start", color.yellow);
addVerticalLine(GetYYYYMMDD() == Q2_Start, "Q2 Start", color.yellow);
addVerticalLine(GetYYYYMMDD() == Q3_Start, "Q3 Start", color.yellow);
addVerticalLine(GetYYYYMMDD() == Q4_Start, "Q4 Start", color.yellow);
 
Last edited:
He wants them to start in March, June, September and December. Unlike the 99.999999% of humans that understand that quarters start on January 1, April 1, July 1 and October 1.

I can barely follow what he's asking either. I thought he wanted to shift it by a variable number of Fridays.

This will also shift it back by a variable number of months. Friday = 0, 1st of the month. Friday = 1, first Friday, etc. Months = 1, Dec, March, June, Sept, etc. That's it for me though, after this he's on his own.

Code:
input Fridays = 0;
def   FixIn =
      AbsValue(Max(Floor(Fridays),0));

input Months = 1;
def   FixIn2 =
      AbsValue(Max(Floor(Months),0));

def Date = GetYYYYMMDD();
def DoW  = GetDayOfWeek(Date);
def GM   = GetMonth();
def Fris =
    if ((GM + FixIn2) % 3) && !((GM[1] + FixIn2) % 3) then
     GetDay() + ((5 - Dow) * Fridays != 0)
     + ((FixIn - 1) * 7)
    else Fris[1];
def isFri =
    GetDay() == Fris or
   (GetDay() != Fris &&
    Crosses(GetDay(),Fris,1)[-1]);   
profile Q_High =
    DataProfile(high,PricePerRow.automatic,isFri,No);
    plot QH = Q_High.gethighest();
    QH.setpaintingStrategy(11);
    QH.setDefaultColor(color.red);
profile Q_Low =
    DataProfile(Low,PricePerRow.automatic,isFri,No);
    plot QL = Q_Low.getLowest();
    QL.SetpaintingStrategy(11);
    QL.setdefaultColor(Color.green);
AddVerticalLine(
    isFri,"",Color.Yellow,3);
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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