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);
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);