Percentage Line From Previous Close For ThinkOrSwim

halcyonguy

Moderator - Expert
VIP
Lifetime
Is there a way to make this so it is Day time frame only, for example I use a 5 minute chart and the stock closed yesterday at $100, there will be a line at $103 and $97 that will not change for the day no matter what time frame I use?

Percentage Line From Previous Close For ThinkOrSwim
this version uses 2nd aggregation , default is day and close.
can choose to show all lines or just current day


Code:
# percent_lines_1

def na = double.nan;

input top_percent = 3.0;
input top_price_agg = AggregationPeriod.DAY;
def top_price = close(period = top_price_agg);
input top_price_offset = 1;
def top_p = top_price[top_price_offset];

input bottom_percent = 3.0;
def bottom_price_agg = AggregationPeriod.DAY;
def bottom_price = close(period = bottom_price_agg);
input bottom_price_offset = 1;
def bottom_p = bottom_price[bottom_price_offset];

def top_level = ((1 + (top_percent/100)) * top_p);
def bottom_level = ((1 - (bottom_percent/100)) * bottom_p);

input lines_today_only = yes;
def istoday = if GetLastDay() == GetDay() then 1 else 0;
def x = if lines_today_only then istoday else 1;


plot top = if x then top_level else na;
plot mid = if x then (top_p + bottom_p)/2 else na;
plot bottom = if x then bottom_level else na;

top.SetDefaultColor(Color.cyan);
top.setlineweight(1);
#top.hidebubble();
top.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

bottom.SetDefaultColor(Color.pink);
bottom.setlineweight(1);
#bottom.hidebubble();
bottom.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

mid.SetDefaultColor(Color.gray);
mid.setlineweight(1);
#mid.hidebubble();
mid.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

addlabel(1, "Top " + top_percent + "%", color.yellow);
addlabel(1, "Bottom " + bottom_percent + "%", color.yellow);
#
 
Last edited by a moderator:
Hi Guys.

New here and trying to figure out thinkscript. Basically I'm trying to draw a series of fixed horizontal lines based based on fixed percentages from the Retail Open. Please see attached as an example.


So the Yellow line will be drawn at the opening price at the retail open. Then once the retail open is known it will then draw other fixed lines based on the retail opening price. i.e.
The green line is drawn at Open + 1% and Open -1%
The orange line is drawn at Open +2% and Open -2%
etc etc

Also would it be possible to put an alert so if it breaches the line it provides an audible alert?

Maybe one indicator is enough as one could just copy it as many times as need and then just change the numbers.

Much appreciated and thanks in advance.
DJ
 
Hi Guys.

New here and trying to figure out thinkscript. Basically I'm trying to draw a series of fixed horizontal lines based based on fixed percentages from the Retail Open. Please see attached as an example.


So the Yellow line will be drawn at the opening price at the retail open. Then once the retail open is known it will then draw other fixed lines based on the retail opening price. i.e.
The green line is drawn at Open + 1% and Open -1%
The orange line is drawn at Open +2% and Open -2%
etc etc

Also would it be possible to put an alert so if it breaches the line it provides an audible alert?

Maybe one indicator is enough as one could just copy it as many times as need and then just change the numbers.

Much appreciated and thanks in advance.
DJ

You are correct, it is just a copy and paste for making your desired number of plots This has some options that you can choose to include and/or copy/paste.

Capture.jpg
Ruby:
input showtodayonly = yes;
def   c  = close;
input p1 = .005;
input p2 = .01;

plot o  = if showtodayonly and !IsNaN(close(period = AggregationPeriod.DAY)[-1])
          then Double.NaN
          else open(period = AggregationPeriod.DAY);

plot o1 = o + o * p1;
plot o2 = o + o * p2;

plot o1_ = o - o * p1;
plot o2_ = o - o * p2;

# Style
DefineGlobalColor("P", Color.GREEN);
DefineGlobalColor("N", Color.RED);

o1.SetDefaultColor(GlobalColor("P"));
o2.SetDefaultColor(GlobalColor("P"));

o1_.SetDefaultColor(GlobalColor("N"));
o2_.SetDefaultColor(GlobalColor("N"));

#Bubbles (Moveable)
input showbubbles = yes;
input bubblemover = 3;
def bm  = bubblemover;
def bm1 = bm + 1;
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o[bm], "Open", o.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o1[bm], p1, o1.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o2[bm], p2, o2.TakeValueColor());

AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o1_[bm], p1, o1_.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o2_[bm], p2, o2_.TakeValueColor());

# Alerts
input usealerts = yes;

Alert(usealerts and c crosses o1, "+" + p1, Alert.BAR, Sound.Chimes);
Alert(usealerts and c crosses o2, "+" + p2, Alert.BAR, Sound.Chimes);

Alert(usealerts and c crosses o1_, "-" + p1, Alert.BAR, Sound.Ding);
Alert(usealerts and c crosses o2_, "-" + p2, Alert.BAR, Sound.Ding);
 
Hi again Sleepy. Another quick one on this. So I have been trying to modify the code so it can also give the option of plotting two days continuous instead of just one day. So right now the code we have is:

input showtodayonly = yes;
def c = close;
input p1 = .01;
input p2 = .02;
input p3 = .03;
input p4 = .04;

plot o = if showtodayonly and !IsNaN(close(period = AggregationPeriod.DAY)[-1])
then Double.NaN
else open(period = AggregationPeriod.DAY);

I am trying to modify this as below. It doesn't seem to want to compile with a second plot o and even when I changed the AggregationPeriod.DAY in the original code to AggregationPeriod.TWO_DAYS the lines did not show up. So basically I'm trying to add another option to todayonly but should be today only, all days and then include two days as well.

input showtodayonly = yes;
input showtwodaysonly = yes;
def c = close;
input p1 = .01;
input p2 = .02;
input p3 = .03;
input p4 = .04;

plot o = if showtodayonly and !IsNaN(close(period = AggregationPeriod.DAY)[-1])
then Double.NaN
else open(period = AggregationPeriod.DAY);

plot o = if showtwodaysonly and !IsNaN(close(period = AggregationPeriod.DAY)[-1])
then Double.NaN
else open(period = AggregationPeriod.TWO_DAYS);



Any help would be appreciated.
Thanks
DJ
 
Hi again Sleepy. Another quick one on this. So I have been trying to modify the code so it can also give the option of plotting two days continuous instead of just one day. So right now the code we have is:



I am trying to modify this as below. It doesn't seem to want to compile with a second plot o and even when I changed the AggregationPeriod.DAY in the original code to AggregationPeriod.TWO_DAYS the lines did not show up. So basically I'm trying to add another option to todayonly but should be today only, all days and then include two days as well.





Any help would be appreciated.
Thanks
DJ

I am not sure what exactly you are requesting, so I am assuming that your two_days is just the prior days open. This way you get two lines on each day for the current and prior days open. You can still limit the plot to showtodayonly or show all.

Ruby:
input showtodayonly = yes;
input showpriorday  = yes;
def c = close;
input p1 = .01;
input p2 = .02;
input p3 = .03;
input p4 = .04;

plot o = if showtodayonly and !IsNaN(close(period = AggregationPeriod.DAY)[-1])
then Double.NaN
else open(period = AggregationPeriod.DAY);
o.setpaintingStrategy(paintingStrategy.HORIZONTAL);

plot o1 = if showtodayonly and !IsNaN(close(period = AggregationPeriod.DAY)[-1])
then Double.NaN
else if showpriorday then open(period = AggregationPeriod.day)[1]
else double.nan;
o1.setpaintingStrategy(PaintingStrategy.DASHES);
o1.setlineWeight(3);
 
Hi Ruby. Sorry I should have attached a picture to make it clear. So the top picture is how it is now with the lines drawn on the current day. The second one is where I'm trying to get to. So these are both 2 day 1 minute chart.

So what I'm trying to do is have the lines drawn based on the opening of the prior day and lines fixed to continue through to the following day. So I'm measuring from the opening of the prior day only. So basically it is the same as now but instead of drawing from the start of the current day it should be drawing from the start of the prior day.

This is the first one.


This is the one I'm trying to get to for the two days.


Here is the code I am using.

input showtodayonly = yes;
def c = close;
input p1 = .01;
input p2 = .02;
input p3 = .03;
input p4 = .04;

plot o = if showtodayonly and !IsNaN(close(period = AggregationPeriod.DAY)[-1])
then Double.NaN
else open(period = AggregationPeriod.DAY);


plot o1 = o + o * p1;
plot o2 = o + o * p2;
plot o3 = o + o * p3;
plot o4 = o + o * p4;

plot o1_ = o - o * p1;
plot o2_ = o - o * p2;
plot o3_ = o - o * p3;
plot o4_ = o - o * p4;

# Style
DefineGlobalColor("P", Color.GREEN);
DefineGlobalColor("N", Color.RED);

o1.SetDefaultColor(GlobalColor("P"));
o2.SetDefaultColor(GlobalColor("P"));
o3.SetDefaultColor(GlobalColor("P"));
o4.SetDefaultColor(GlobalColor("P"));

o1_.SetDefaultColor(GlobalColor("N"));
o2_.SetDefaultColor(GlobalColor("N"));
o3_.SetDefaultColor(GlobalColor("N"));
o4_.SetDefaultColor(GlobalColor("N"));

#Bubbles (Moveable)
input showbubbles = yes;
input bubblemover = 3;
def bm = bubblemover;
def bm1 = bm + 1;
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o[bm], "Open", o.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o1[bm], p1, o1.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o2[bm], p2, o2.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o3[bm], p3, o3.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o4[bm], p4, o4.TakeValueColor());

AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o1_[bm], p1, o1_.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o2_[bm], p2, o2_.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o3_[bm], p3, o3_.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o4_[bm], p4, o4_.TakeValueColor());

# Alerts
input usealerts = yes;

Alert(usealerts and c crosses o1, "+" + p1, Alert.BAR, Sound.Chimes);
Alert(usealerts and c crosses o2, "+" + p2, Alert.BAR, Sound.Chimes);
Alert(usealerts and c crosses o3, "+" + p3, Alert.BAR, Sound.Chimes);
Alert(usealerts and c crosses o4, "+" + p4, Alert.BAR, Sound.Chimes);

Alert(usealerts and c crosses o1_, "-" + p1, Alert.BAR, Sound.Ding);
Alert(usealerts and c crosses o2_, "-" + p2, Alert.BAR, Sound.Ding);
Alert(usealerts and c crosses o3_, "-" + p3, Alert.BAR, Sound.Ding);
Alert(usealerts and c crosses o4_, "-" + p4, Alert.BAR, Sound.Ding);

Thank you
DJ
 
Hi Ruby. Sorry I should have attached a picture to make it clear. So the top picture is how it is now with the lines drawn on the current day. The second one is where I'm trying to get to. So these are both 2 day 1 minute chart.

So what I'm trying to do is have the lines drawn based on the opening of the prior day and lines fixed to continue through to the following day. So I'm measuring from the opening of the prior day only. So basically it is the same as now but instead of drawing from the start of the current day it should be drawing from the start of the prior day.

This is the first one.


This is the one I'm trying to get to for the two days.


Here is the code I am using.



Thank you
DJ

@SleepyZ said,

Just input the daysback in this modification to plot any days open in the past. Zero would be today, 1 as the prior day to today, etc

Capture.jpg
Code:
input daysback      = 1;

input p1 = .005;
input p2 = .01;

def c        = close;
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) ;

def openday  = if thisday>daysback then double.nan else if thisday==daysback then open(period=aggregationPeriod.DAY) else openday[1];
plot o       = openday;

plot o1 = o + o * p1;
plot o2 = o + o * p2;

plot o1_ = o - o * p1;
plot o2_ = o - o * p2;

# Style
DefineGlobalColor("P", Color.GREEN);
DefineGlobalColor("N", Color.RED);

o.setdefaultColor(color.cyan);
o.setpaintingStrategy(paintingStrategy.HORIZONTAL);

o1.SetDefaultColor(GlobalColor("P"));
o2.SetDefaultColor(GlobalColor("P"));

o1_.SetDefaultColor(GlobalColor("N"));
o2_.SetDefaultColor(GlobalColor("N"));

#Bubbles (Moveable)
input showbubbles = yes;
input bubblemover = 3;
def bm  = bubblemover;
def bm1 = bm + 1;
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o[bm], "Open", o.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o1[bm], p1, o1.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o2[bm], p2, o2.TakeValueColor());

AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o1_[bm], p1, o1_.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o2_[bm], p2, o2_.TakeValueColor());

# Alerts
input usealerts = yes;

Alert(usealerts and c crosses o1, "+" + p1, Alert.BAR, Sound.Chimes);
Alert(usealerts and c crosses o2, "+" + p2, Alert.BAR, Sound.Chimes);

Alert(usealerts and c crosses o1_, "-" + p1, Alert.BAR, Sound.Ding);
Alert(usealerts and c crosses o2_, "-" + p2, Alert.BAR, Sound.Ding);
 
Thank you Sleepy. That worked perfectly! Nearly there with this one I think. This thinkscript seems very powerful. One other thing I'm grappling with is trying to add a bubble to the left next to each line similar what you did on the right. I'm not sure how you locate a bubble on the left and not the right. So what I'm trying to do is add preset numbers to the left of each line. I've put the code below and put in bold what I'm trying to add. For example the number 88 should appear in the bubble next to that line. input PChangeP1 = "88";

There is also a picture with the numbers next to each line.

input daysback = 1;

input p1 = .01;
input p2 = .02;
input p3 = .03;
input p4 = .04;

def c = close;
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) ;

def openday = if thisday>daysback then double.nan else if thisday==daysback then open(period=aggregationPeriod.DAY) else openday[1];

plot o = openday;

plot o1 = o + o * p1;
plot o2 = o + o * p2;
plot o3 = o + o * p3;
plot o4 = o + o * p4;

plot o1_ = o - o * p1;
plot o2_ = o - o * p2;
plot o3_ = o - o * p3;
plot o4_ = o - o * p4;

# Style
DefineGlobalColor("P", Color.GREEN);
DefineGlobalColor("N", Color.RED);

o1.SetDefaultColor(GlobalColor("P"));
o2.SetDefaultColor(GlobalColor("P"));
o3.SetDefaultColor(GlobalColor("P"));
o4.SetDefaultColor(GlobalColor("P"));

o1_.SetDefaultColor(GlobalColor("N"));
o2_.SetDefaultColor(GlobalColor("N"));
o3_.SetDefaultColor(GlobalColor("N"));
o4_.SetDefaultColor(GlobalColor("N"));

#Bubbles (Moveable)
input showbubbles = yes;
input bubblemover = 3;
def bm = bubblemover;
def bm1 = bm + 1;

input bubblemover1 = 4;
def bm2 = bubblemover1;
def bm3 = bm2 - 20;


input PChangeP1 = "88";
input PChangeP2 = "92";
input PChangeP3 = "96";
input PChangeP4 = "98";

AddChartBubble(IsNaN(close[bm2]) and !IsNaN(close[bm3]), o[bm2], PChangeP1, O1.TakeValueColor());
AddChartBubble(IsNaN(close[bm2]) and !IsNaN(close[bm3]), o[bm2], PChangeP2, o2.TakeValueColor());
AddChartBubble(IsNaN(close[bm2]) and !IsNaN(close[bm3]), o[bm2], PChangeP3, o3.TakeValueColor());
AddChartBubble(IsNaN(close[bm2]) and !IsNaN(close[bm3]), o[bm2], PChangeP3, o4.TakeValueColor());



AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o[bm], "Open", o.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o1[bm], p1, o1.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o2[bm], p2, o2.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o3[bm], p3, o3.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o4[bm], p4, o4.TakeValueColor());

AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o1_[bm], p1, o1_.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o2_[bm], p2, o2_.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o3_[bm], p3, o3_.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o4_[bm], p4, o4_.TakeValueColor());

# Alerts
input usealerts = yes;

Alert(usealerts and c crosses o1, "+" + p1, Alert.BAR, Sound.Chimes);
Alert(usealerts and c crosses o2, "+" + p2, Alert.BAR, Sound.Chimes);
Alert(usealerts and c crosses o3, "+" + p3, Alert.BAR, Sound.Chimes);
Alert(usealerts and c crosses o4, "+" + p4, Alert.BAR, Sound.Chimes);

Alert(usealerts and c crosses o1_, "-" + p1, Alert.BAR, Sound.Ding);
Alert(usealerts and c crosses o2_, "-" + p2, Alert.BAR, Sound.Ding);
Alert(usealerts and c crosses o3_, "-" + p3, Alert.BAR, Sound.Ding);
Alert(usealerts and c crosses o4_, "-" + p4, Alert.BAR, Sound.Ding);



Thank you
DJ
 
Thank you Sleepy. That worked perfectly! Nearly there with this one I think. This thinkscript seems very powerful. One other thing I'm grappling with is trying to add a bubble to the left next to each line similar what you did on the right. I'm not sure how you locate a bubble on the left and not the right. So what I'm trying to do is add preset numbers to the left of each line. I've put the code below and put in bold what I'm trying to add. For example the number 88 should appear in the bubble next to that line. input PChangeP1 = "88";

There is also a picture with the numbers next to each line.





Thank you
DJ

Bubbles added indicating days back

Ruby:
input daysback = 1;

input p1 = .01;
input p2 = .02;
input p3 = .03;
input p4 = .04;

def c = close;
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) ;

def openday = if thisDay > daysback then Double.NaN else if thisDay == daysback then open(period = AggregationPeriod.DAY) else openday[1];

plot o = openday;

plot o1 = o + o * p1;
plot o2 = o + o * p2;
plot o3 = o + o * p3;
plot o4 = o + o * p4;

plot o1_ = o - o * p1;
plot o2_ = o - o * p2;
plot o3_ = o - o * p3;
plot o4_ = o - o * p4;

# Style
DefineGlobalColor("P", Color.GREEN);
DefineGlobalColor("N", Color.RED);

o1.SetDefaultColor(GlobalColor("P"));
o2.SetDefaultColor(GlobalColor("P"));
o3.SetDefaultColor(GlobalColor("P"));
o4.SetDefaultColor(GlobalColor("P"));

o1_.SetDefaultColor(GlobalColor("N"));
o2_.SetDefaultColor(GlobalColor("N"));
o3_.SetDefaultColor(GlobalColor("N"));
o4_.SetDefaultColor(GlobalColor("N"));

#Bubbles (Moveable)
input showbubbles = yes;
input bubblemover = 3;
def bm = bubblemover;
def bm1 = bm + 1;

input bubblemover1 = 4;
def bm2 = bubblemover1;
def bm3 = bm2 - 20;


input PChangeP1 = "88";
input PChangeP2 = "92";
input PChangeP3 = "96";
input PChangeP4 = "98";

AddChartBubble(IsNaN(close[bm2]) and !IsNaN(close[bm3]), o[bm2], PChangeP1, o1.TakeValueColor());
AddChartBubble(IsNaN(close[bm2]) and !IsNaN(close[bm3]), o[bm2], PChangeP2, o2.TakeValueColor());
AddChartBubble(IsNaN(close[bm2]) and !IsNaN(close[bm3]), o[bm2], PChangeP3, o3.TakeValueColor());
AddChartBubble(IsNaN(close[bm2]) and !IsNaN(close[bm3]), o[bm2], PChangeP3, o4.TakeValueColor());


AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o[bm], "Open", o.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o1[bm], p1, o1.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o2[bm], p2, o2.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o3[bm], p3, o3.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o4[bm], p4, o4.TakeValueColor());

AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o1_[bm], p1, o1_.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o2_[bm], p2, o2_.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o3_[bm], p3, o3_.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o4_[bm], p4, o4_.TakeValueColor());

#Bubble Day Indicaion
AddChartBubble(IsNaN(o[1]) and o, o, daysback, o.TakeValueColor());
AddChartBubble(IsNaN(o1[1]) and o1, o1, daysback, o1.TakeValueColor());
AddChartBubble(IsNaN(o2[1]) and o2, o2, daysback, o2.TakeValueColor());
AddChartBubble(IsNaN(o3[1]) and o3, o3, daysback, o3.TakeValueColor());
AddChartBubble(IsNaN(o4[1]) and o4, o4, daysback, o4.TakeValueColor());

AddChartBubble(IsNaN(o1_[1]) and o1_, o1_, daysback, o1_.TakeValueColor());
AddChartBubble(IsNaN(o2_[1]) and o2_, o2_, daysback, o2_.TakeValueColor());
AddChartBubble(IsNaN(o3_[1]) and o3_, o3_, daysback, o3_.TakeValueColor());
AddChartBubble(IsNaN(o4_[1]) and o4_, o4_, daysback, o4_.TakeValueColor());
# Alerts
input usealerts = yes;

Alert(usealerts and c crosses o1, "+" + p1, Alert.BAR, Sound.Chimes);
Alert(usealerts and c crosses o2, "+" + p2, Alert.BAR, Sound.Chimes);
Alert(usealerts and c crosses o3, "+" + p3, Alert.BAR, Sound.Chimes);
Alert(usealerts and c crosses o4, "+" + p4, Alert.BAR, Sound.Chimes);

Alert(usealerts and c crosses o1_, "-" + p1, Alert.BAR, Sound.Ding);
Alert(usealerts and c crosses o2_, "-" + p2, Alert.BAR, Sound.Ding);
Alert(usealerts and c crosses o3_, "-" + p3, Alert.BAR, Sound.Ding);
Alert(usealerts and c crosses o4_, "-" + p4, Alert.BAR, Sound.Ding);
 
Thanks Sleepy! I was able to figure that one out. One small thing I'm trying to modify is to plot the lines so instead of the zero line plotting from the current day open it starts from yesterdays close. I've just changed the "Open" in the code to "Close" in the def Openday line below. This has modified the chart to plot from yesterdays close ok but the problem is it's leaving lines in the prior day. For example in the attached graph I've set it to plot three days back but it is plotting the whole third day as well so trying to get rid of those lines. I've played around with the other code but not had much luck.

Any ideas would be appreciated. Thanks. DJ



Code:
input daysback      = 1;

input p1 = .01;
input p2 = .02;
input p3 = .03;
input p4 = .04;

def c        = close;
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) ;

#def openday  = if thisday>daysback then double.nan else if thisday==daysback then open(period=aggregationPeriod.DAY) else openday[1];
def openday  = if thisday>daysback then double.nan else if thisday==daysback then close(period=aggregationPeriod.DAY) else openday[1];

plot o       = openday;

plot o1 = o + o * p1;
plot o2 = o + o * p2;
plot o3 = o + o * p3;
plot o4 = o + o * p4;

plot o1_ = o - o * p1;
plot o2_ = o - o * p2;
plot o3_ = o - o * p3;
plot o4_ = o - o * p4;

# Style
DefineGlobalColor("P", Color.GREEN);
DefineGlobalColor("N", Color.RED);

o1.SetDefaultColor(GlobalColor("P"));
o2.SetDefaultColor(GlobalColor("P"));
o3.SetDefaultColor(GlobalColor("P"));
o4.SetDefaultColor(GlobalColor("P"));

o1_.SetDefaultColor(GlobalColor("N"));
o2_.SetDefaultColor(GlobalColor("N"));
o3_.SetDefaultColor(GlobalColor("N"));
o4_.SetDefaultColor(GlobalColor("N"));

#Bubbles (Moveable)
input showbubbles = yes;
input bubblemover = 3;
def bm  = bubblemover;
def bm1 = bm + 1;

input PercentileA1 = "65";
input PercentileA2 = "88";
input PercentileA3 = "95";
input PercentileA4 = "98";
input PercentileB1 = "61";
input PercentileB2 = "84";
input PercentileB3 = "94";
input PercentileB4 = "97";

AddCloud(o1_, o1, color.GREEN, color.GREEN, yes);
AddCloud(o1, o2, color.ORANGE, color.ORANGE, yes);
AddCloud(o1_, o2_, color.ORANGE, color.ORANGE, yes);
AddCloud(o2, o3, color.MAGENTA, color.MAGENTA, yes);
AddCloud(o2_, o3_, color.MAGENTA, color.MAGENTA, yes);
AddCloud(o3, o4, color.RED, color.RED, yes);
AddCloud(o3_, o4_, color.RED, color.RED, yes);


def x = barnumber();
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o1[bm], PercentileA1, o1.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o2[bm], PercentileA2, o2.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o3[bm], PercentileA3, o3.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o4[bm], PercentileA4, o4.TakeValueColor());

AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o1_[bm], PercentileB1, o1_.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o2_[bm], PercentileB2, o2_.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o3_[bm], PercentileB3, o3_.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o4_[bm], PercentileB4, o4_.TakeValueColor());



#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o[bm], "Open", o.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o1[bm], p1, o1.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o2[bm], p2, o2.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o3[bm], p3, o3.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o4[bm], p4, o4.TakeValueColor());

#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o1_[bm], p1, o1_.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o2_[bm], p2, o2_.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), #o3_[bm], p3, o3_.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o4_[bm], p4, o4_.TakeValueColor());

# Alerts
input usealerts = yes;

Alert(usealerts and c crosses o1, "+" + p1, Alert.BAR, Sound.Chimes);
Alert(usealerts and c crosses o2, "+" + p2, Alert.BAR, Sound.Chimes);
Alert(usealerts and c crosses o3, "+" + p3, Alert.BAR, Sound.Chimes);
Alert(usealerts and c crosses o4, "+" + p4, Alert.BAR, Sound.Chimes);

Alert(usealerts and c crosses o1_, "-" + p1, Alert.BAR, Sound.Ding);
Alert(usealerts and c crosses o2_, "-" + p2, Alert.BAR, Sound.Ding);
Alert(usealerts and c crosses o3_, "-" + p3, Alert.BAR, Sound.Ding);
Alert(usealerts and c crosses o4_, "-" + p4, Alert.BAR, Sound.Ding);
 
Thanks Sleepy! I was able to figure that one out. One small thing I'm trying to modify is to plot the lines so instead of the zero line plotting from the current day open it starts from yesterdays close. I've just changed the "Open" in the code to "Close" in the def Openday line below. This has modified the chart to plot from yesterdays close ok but the problem is it's leaving lines in the prior day. For example in the attached graph I've set it to plot three days back but it is plotting the whole third day as well so trying to get rid of those lines. I've played around with the other code but not had much luck.

Any ideas would be appreciated. Thanks. DJ



Code:
input daysback      = 1;

input p1 = .01;
input p2 = .02;
input p3 = .03;
input p4 = .04;

def c        = close;
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) ;

#def openday  = if thisday>daysback then double.nan else if thisday==daysback then open(period=aggregationPeriod.DAY) else openday[1];
def openday  = if thisday>daysback then double.nan else if thisday==daysback then close(period=aggregationPeriod.DAY) else openday[1];

plot o       = openday;

plot o1 = o + o * p1;
plot o2 = o + o * p2;
plot o3 = o + o * p3;
plot o4 = o + o * p4;

plot o1_ = o - o * p1;
plot o2_ = o - o * p2;
plot o3_ = o - o * p3;
plot o4_ = o - o * p4;

# Style
DefineGlobalColor("P", Color.GREEN);
DefineGlobalColor("N", Color.RED);

o1.SetDefaultColor(GlobalColor("P"));
o2.SetDefaultColor(GlobalColor("P"));
o3.SetDefaultColor(GlobalColor("P"));
o4.SetDefaultColor(GlobalColor("P"));

o1_.SetDefaultColor(GlobalColor("N"));
o2_.SetDefaultColor(GlobalColor("N"));
o3_.SetDefaultColor(GlobalColor("N"));
o4_.SetDefaultColor(GlobalColor("N"));

#Bubbles (Moveable)
input showbubbles = yes;
input bubblemover = 3;
def bm  = bubblemover;
def bm1 = bm + 1;

input PercentileA1 = "65";
input PercentileA2 = "88";
input PercentileA3 = "95";
input PercentileA4 = "98";
input PercentileB1 = "61";
input PercentileB2 = "84";
input PercentileB3 = "94";
input PercentileB4 = "97";

AddCloud(o1_, o1, color.GREEN, color.GREEN, yes);
AddCloud(o1, o2, color.ORANGE, color.ORANGE, yes);
AddCloud(o1_, o2_, color.ORANGE, color.ORANGE, yes);
AddCloud(o2, o3, color.MAGENTA, color.MAGENTA, yes);
AddCloud(o2_, o3_, color.MAGENTA, color.MAGENTA, yes);
AddCloud(o3, o4, color.RED, color.RED, yes);
AddCloud(o3_, o4_, color.RED, color.RED, yes);


def x = barnumber();
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o1[bm], PercentileA1, o1.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o2[bm], PercentileA2, o2.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o3[bm], PercentileA3, o3.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o4[bm], PercentileA4, o4.TakeValueColor());

AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o1_[bm], PercentileB1, o1_.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o2_[bm], PercentileB2, o2_.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o3_[bm], PercentileB3, o3_.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o4_[bm], PercentileB4, o4_.TakeValueColor());



#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o[bm], "Open", o.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o1[bm], p1, o1.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o2[bm], p2, o2.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o3[bm], p3, o3.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o4[bm], p4, o4.TakeValueColor());

#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o1_[bm], p1, o1_.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o2_[bm], p2, o2_.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), #o3_[bm], p3, o3_.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o4_[bm], p4, o4_.TakeValueColor());

# Alerts
input usealerts = yes;

Alert(usealerts and c crosses o1, "+" + p1, Alert.BAR, Sound.Chimes);
Alert(usealerts and c crosses o2, "+" + p2, Alert.BAR, Sound.Chimes);
Alert(usealerts and c crosses o3, "+" + p3, Alert.BAR, Sound.Chimes);
Alert(usealerts and c crosses o4, "+" + p4, Alert.BAR, Sound.Chimes);

Alert(usealerts and c crosses o1_, "-" + p1, Alert.BAR, Sound.Ding);
Alert(usealerts and c crosses o2_, "-" + p2, Alert.BAR, Sound.Ding);
Alert(usealerts and c crosses o3_, "-" + p3, Alert.BAR, Sound.Ding);
Alert(usealerts and c crosses o4_, "-" + p4, Alert.BAR, Sound.Ding);

This identifies the bar and the close where the daysback selected closes for that day. Bars prior to the daysback close's bar number are not plotted. This should work for most symbols.

Capture.jpg
Ruby:
input daysback      = 1;

input p1 = .01;
input p2 = .02;
input p3 = .03;
input p4 = .04;

def c        = close;
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) ;

#def openday  = if thisday>daysback then double.nan else if thisday==daysback then open(period=aggregationPeriod.DAY) else openday[1];
def rth = Between(GetTime(), RegularTradingStart(GetYYYYMMDD()), RegularTradingEnd(GetYYYYMMDD()));
def openday  = if thisDay > daysback then Double.NaN else if thisDay == daysback and  rth[1] == 1 and rth == 0 then close else openday[1];

def openbar  = if close == openday then BarNumber() else openbar[1];

plot o       = if BarNumber() < openbar then Double.NaN else openday;

plot o1 = o + o * p1;
plot o2 = o + o * p2;
plot o3 = o + o * p3;
plot o4 = o + o * p4;

plot o1_ = o - o * p1;
plot o2_ = o - o * p2;
plot o3_ = o - o * p3;
plot o4_ = o - o * p4;

# Style
DefineGlobalColor("P", Color.GREEN);
DefineGlobalColor("N", Color.RED);

o1.SetDefaultColor(GlobalColor("P"));
o2.SetDefaultColor(GlobalColor("P"));
o3.SetDefaultColor(GlobalColor("P"));
o4.SetDefaultColor(GlobalColor("P"));

o1_.SetDefaultColor(GlobalColor("N"));
o2_.SetDefaultColor(GlobalColor("N"));
o3_.SetDefaultColor(GlobalColor("N"));
o4_.SetDefaultColor(GlobalColor("N"));

#Bubbles (Moveable)
input showbubbles = yes;
input bubblemover = 3;
def bm  = bubblemover;
def bm1 = bm + 1;

input PercentileA1 = "65";
input PercentileA2 = "88";
input PercentileA3 = "95";
input PercentileA4 = "98";
input PercentileB1 = "61";
input PercentileB2 = "84";
input PercentileB3 = "94";
input PercentileB4 = "97";

AddCloud(o1_, o1, Color.GREEN, Color.GREEN, yes);
AddCloud(o1, o2, Color.ORANGE, Color.ORANGE, yes);
AddCloud(o1_, o2_, Color.ORANGE, Color.ORANGE, yes);
AddCloud(o2, o3, Color.MAGENTA, Color.MAGENTA, yes);
AddCloud(o2_, o3_, Color.MAGENTA, Color.MAGENTA, yes);
AddCloud(o3, o4, Color.RED, Color.RED, yes);
AddCloud(o3_, o4_, Color.RED, Color.RED, yes);


def x = BarNumber();
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o1[bm], PercentileA1, o1.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o2[bm], PercentileA2, o2.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o3[bm], PercentileA3, o3.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o4[bm], PercentileA4, o4.TakeValueColor());

AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o1_[bm], PercentileB1, o1_.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o2_[bm], PercentileB2, o2_.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o3_[bm], PercentileB3, o3_.TakeValueColor());
AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o4_[bm], PercentileB4, o4_.TakeValueColor());



#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o[bm], "Open", o.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o1[bm], p1, o1.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o2[bm], p2, o2.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o3[bm], p3, o3.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o4[bm], p4, o4.TakeValueColor());

#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o1_[bm], p1, o1_.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o2_[bm], p2, o2_.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), #o3_[bm], p3, o3_.TakeValueColor());
#AddChartBubble(IsNaN(close[bm]) and !IsNaN(close[bm1]), o4_[bm], p4, o4_.TakeValueColor());

# Alerts
input usealerts = yes;

Alert(usealerts and c crosses o1, "+" + p1, Alert.BAR, Sound.Chimes);
Alert(usealerts and c crosses o2, "+" + p2, Alert.BAR, Sound.Chimes);
Alert(usealerts and c crosses o3, "+" + p3, Alert.BAR, Sound.Chimes);
Alert(usealerts and c crosses o4, "+" + p4, Alert.BAR, Sound.Chimes);

Alert(usealerts and c crosses o1_, "-" + p1, Alert.BAR, Sound.Ding);
Alert(usealerts and c crosses o2_, "-" + p2, Alert.BAR, Sound.Ding);
Alert(usealerts and c crosses o3_, "-" + p3, Alert.BAR, Sound.Ding);
Alert(usealerts and c crosses o4_, "-" + p4, Alert.BAR, Sound.Ding);
 
Last edited:
Thanks Sleepy!

This one I had a slight problem with as am getting N/A error. I noticed you had it working with extended hours and it certainly works ok with that. Would it possible to have it working with Retail hours and not extended? I had a look at the code and nothing stands out.

Thanks
DJ

 

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
397 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