Extended Month Open/Close Lines

amangold

New member
Need Thinkscript code to plot horizontal lines for each Month Open/Close and extend all the way to right

Hi, I am trying to plot horizontal straight lines for monthly open and close values for the past 36 months, dynamically extending to the current day. Kindly help me write thinkscript code to achieve this. I have so far achieved this manually as follows (using a script I found online), but I noticed on other posts there is some way to do it with if/else conditions to create some kind of looping logic. Kindly advise. Thanks a lot!

input agg = AggregationPeriod.MONTH;

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

#######################--------------

def line1 = 1;
def p1 = if bn == 1 then na
else if ( isnan( close(period = agg)[-line1] ) and !isnan( close(period = agg)[-line1 +1] ))
then close(period = agg)
else p1[1];

plot p1b = p1;
p1b.setdefaultcolor(color.yellow);
p1b.setStyle(curve.long_dash);

#######################

def line2 = 1;
def p2 = if bn == 1 then na
else if ( isnan( open(period = agg)[-line2] ) and !isnan( open(period = agg)[-line2 +1] ))
then open(period = agg)
else p2[1];

plot p2b = p2;
p2b.setdefaultcolor(color.yellow);
p2b.setStyle(curve.long_dash);


#######################--------------

def line3 = 2;
def p3 = if bn == 2 then na
else if ( isnan( close(period = agg)[-line3] ) and !isnan( close(period = agg)[-line3 +1] ))
then close(period = agg)
else p3[1];

plot p3b = p3;
p3b.setdefaultcolor(color.yellow);
p3b.setStyle(curve.long_dash);

#######################

def line4 = 2;
def p4 = if bn == 2 then na
else if ( isnan( open(period = agg)[-line4] ) and !isnan( open(period = agg)[-line4 +1] ))
then open(period = agg)
else p4[1];

plot p4b = p4;
p4b.setdefaultcolor(color.yellow);
p4b.setStyle(curve.long_dash);
 

Attachments

  • monthslevel.jpg
    monthslevel.jpg
    124 KB · Views: 95
I test your script and it works very nice but just plot the last 2 bars (open and close), could be interesting that somebody can make this script to work with "N" number of bars in the aggregation period, I mean if we take let's say year-month then plot the desired number or months with in same period of time, (example I need to know how much the price up or drop in the last 3 months in one specific stock or ETF), so in this case just select in the setting of the indicator 3 bars or something like that.......
 
Last edited:
Need Thinkscript code to plot horizontal lines for each Month Open/Close and extend all the way to right

Hi, I am trying to plot horizontal straight lines for monthly open and close values for the past 36 months, dynamically extending to the current day. Kindly help me write thinkscript code to achieve this. I have so far achieved this manually as follows (using a script I found online), but I noticed on other posts there is some way to do it with if/else conditions to create some kind of looping logic. Kindly advise. Thanks a lot!

input agg = AggregationPeriod.MONTH;

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

#######################--------------

def line1 = 1;
def p1 = if bn == 1 then na
else if ( isnan( close(period = agg)[-line1] ) and !isnan( close(period = agg)[-line1 +1] ))
then close(period = agg)
else p1[1];

plot p1b = p1;
p1b.setdefaultcolor(color.yellow);
p1b.setStyle(curve.long_dash);

#######################

def line2 = 1;
def p2 = if bn == 1 then na
else if ( isnan( open(period = agg)[-line2] ) and !isnan( open(period = agg)[-line2 +1] ))
then open(period = agg)
else p2[1];

plot p2b = p2;
p2b.setdefaultcolor(color.yellow);
p2b.setStyle(curve.long_dash);


#######################--------------

def line3 = 2;
def p3 = if bn == 2 then na
else if ( isnan( close(period = agg)[-line3] ) and !isnan( close(period = agg)[-line3 +1] ))
then close(period = agg)
else p3[1];

plot p3b = p3;
p3b.setdefaultcolor(color.yellow);
p3b.setStyle(curve.long_dash);

#######################

def line4 = 2;
def p4 = if bn == 2 then na
else if ( isnan( open(period = agg)[-line4] ) and !isnan( open(period = agg)[-line4 +1] ))
then open(period = agg)
else p4[1];

plot p4b = p4;
p4b.setdefaultcolor(color.yellow);
p4b.setStyle(curve.long_dash);

Regrettably, each of extended lines will require a separate plot for each price.
The best way is to create those manually is to find a simple way to duplicate the code.

Below is how I usually do it through a script function, which can be referred to in plot statements and only have to change a few things. There are 2 scripts, 1 for each price. I did 10 plots and bubbles for each.

Using the 10, I duplicate them for the remainder of your 36 plots, colors, line formats and bubbles and just change the letters and numbers to those from 11 to 36 where there are 0 to 9.

Hope this helps!

Screenshot 2023-10-30 143109.png

Code:
#Extended Month Open/Close Lines

script o {
    input back  = 0;
    input price = FundamentalType.OPEN;
    input aggregation = AggregationPeriod.MONTH;
    def na = Double.NaN;
    def datacount = if GetMonth() != GetMonth()[1] and !IsNaN(close)
                    then datacount[1] + 1 else datacount[1];
    def thismonth = HighestAll(datacount) - datacount ;
    def extend    = if IsNaN(close)
                    then extend[1]
                    else if back == thismonth
                    then Fundamental(price, period = aggregation)
                    else extend[1];
    plot mp       = if thismonth > back then na else extend;
}

plot o0 = o(0);
plot o1 = o(1);
plot o2 = o(2);
plot o3 = o(3);
plot o4 = o(4);
plot o5 = o(5);
plot o6 = o(6);
plot o7 = o(7);
plot o8 = o(8);
plot o9 = o(9);

o0.SetStyle(Curve.LONG_DASH);
o1.SetStyle(Curve.LONG_DASH);
o2.SetStyle(Curve.LONG_DASH);
o3.SetStyle(Curve.LONG_DASH);
o4.SetStyle(Curve.LONG_DASH);
o5.SetStyle(Curve.LONG_DASH);
o6.SetStyle(Curve.LONG_DASH);
o7.SetStyle(Curve.LONG_DASH);
o8.SetStyle(Curve.LONG_DASH);
o9.SetStyle(Curve.LONG_DASH);

DefineGlobalColor("O", Color.YELLOW);
o0.SetDefaultColor(GlobalColor("O"));
o1.SetDefaultColor(GlobalColor("O"));
o2.SetDefaultColor(GlobalColor("O"));
o3.SetDefaultColor(GlobalColor("O"));
o4.SetDefaultColor(GlobalColor("O"));
o5.SetDefaultColor(GlobalColor("O"));
o6.SetDefaultColor(GlobalColor("O"));
o7.SetDefaultColor(GlobalColor("O"));
o8.SetDefaultColor(GlobalColor("O"));
o9.SetDefaultColor(GlobalColor("O"));

input bubble_open = yes;
input mover_open  = 1;
def mo  = mover_open;
def mo1 = mo + 1;
def O_bubble = bubble_open and IsNaN(close[mo]) and !IsNaN(close[mo1]);

AddChartBubble(O_bubble, o0, "O0", o0.TakeValueColor());
AddChartBubble(O_bubble, o1, "O1", o1.TakeValueColor());
AddChartBubble(O_bubble, o2, "O2", o2.TakeValueColor());
AddChartBubble(O_bubble, o3, "O3", o3.TakeValueColor());
AddChartBubble(O_bubble, o4, "O4", o4.TakeValueColor());
AddChartBubble(O_bubble, o5, "O5", o5.TakeValueColor());
AddChartBubble(O_bubble, o6, "O6", o6.TakeValueColor());
AddChartBubble(O_bubble, o7, "O7", o7.TakeValueColor());
AddChartBubble(O_bubble, o8, "O8", o8.TakeValueColor());
AddChartBubble(O_bubble, o9, "O9", o9.TakeValueColor());


##################################
script c {
    input back  = 0;
    input price = FundamentalType.CLOSE;
    input aggregation = AggregationPeriod.MONTH;
    def na = Double.NaN;
    def datacount = if GetMonth() != GetMonth()[1] and !IsNaN(close)
                    then datacount[1] + 1 else datacount[1];
    def thismonth = HighestAll(datacount) - datacount ;
    def extend    = if IsNaN(close)
                    then extend[1]
                    else if back == thismonth
                    then Fundamental(price, period = aggregation)
                    else extend[1];
    plot cp       = if thismonth > back then na else extend;
}

plot c0 = c(0);
plot c1 = c(1);
plot c2 = c(2);
plot c3 = c(3);
plot c4 = c(4);
plot c5 = c(5);
plot c6 = c(6);
plot c7 = c(7);
plot c8 = c(8);
plot c9 = c(9);

c9.SetStyle(Curve.LONG_DASH);
c1.SetStyle(Curve.LONG_DASH);
c2.SetStyle(Curve.LONG_DASH);
c3.SetStyle(Curve.LONG_DASH);
c4.SetStyle(Curve.LONG_DASH);
c5.SetStyle(Curve.LONG_DASH);
c6.SetStyle(Curve.LONG_DASH);
c7.SetStyle(Curve.LONG_DASH);
c8.SetStyle(Curve.LONG_DASH);
c9.SetStyle(Curve.LONG_DASH);

DefineGlobalColor("C", Color.CYAN);
c0.SetDefaultColor(GlobalColor("C"));
c1.SetDefaultColor(GlobalColor("C"));
c2.SetDefaultColor(GlobalColor("C"));
c3.SetDefaultColor(GlobalColor("C"));
c4.SetDefaultColor(GlobalColor("C"));
c5.SetDefaultColor(GlobalColor("C"));
c6.SetDefaultColor(GlobalColor("C"));
c7.SetDefaultColor(GlobalColor("C"));
c8.SetDefaultColor(GlobalColor("C"));
c9.SetDefaultColor(GlobalColor("C"));


input bubble_close = yes;
input mover_close  = 10;
def mc  = mover_close;
def mc1 = mc + 1;
def c_bubble = bubble_close and IsNaN(close[mc]) and !IsNaN(close[mc1]);

AddChartBubble(c_bubble, c0, "C0", c0.TakeValueColor());
AddChartBubble(c_bubble, c1, "C1", c1.TakeValueColor());
AddChartBubble(c_bubble, c2, "C2", c2.TakeValueColor());
AddChartBubble(c_bubble, c3, "C3", c3.TakeValueColor());
AddChartBubble(c_bubble, c4, "C4", c4.TakeValueColor());
AddChartBubble(c_bubble, c5, "C5", c5.TakeValueColor());
AddChartBubble(c_bubble, c6, "C6", c6.TakeValueColor());
AddChartBubble(c_bubble, c7, "C7", c7.TakeValueColor());
AddChartBubble(c_bubble, c8, "C8", c8.TakeValueColor());
AddChartBubble(c_bubble, c9, "C9", c9.TakeValueColor());


#
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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