Plot a trend line within 10% of another line?

If you guys can help me with this example instead of me giving you my script and just giving me the fix I need, I want to try to get the fix using the correct answer from the example so I can learn through trial and error. Using this example from thinkscript glossary, this plots a line at the current time frames High and Low.

Code:
def onExpansion = if IsNaN(close) then yes else no;
plot HighestClose = if onExpansion then HighestAll(close) else double.NaN;
plot LowestClose = if onExpansion then LowestAll(close) else double.NaN;

I want to plot:

input number_of_days_low = 10% above the Lowest Close.
input number_of_days_high = 10% below the Highest Close.

I'm having trouble finding how to do 10%. I see a few different ways to do this on this website but they don't plot the line within 10%. they are plotting the line at 1.0 exactly.I used this post #14 and this post #19 and it doesn't work when I adjusted them to my parameters. Another post had a moving average, and it had --> high (high + low) / 1000; to get the % which didn't make sense.

I'm currently working on a chart that shows the Implied volatility high and low, with Addclouds highlighting the area within say 10% or something closer like 7% from the high/low. Then I will be creating a crossover of the IV into the cloud area to change the line color. which then makes to easy to set for the scan when I can add an other parameter ---> volatility band crossovers, to refine my results.
 
Last edited by a moderator:
I think I was able to figure it out!! I'll post the code after I eat. I need to eat dinner and then I'll work on the second part of this which is line crossing within my cloud area and the line changes colors. And then I can convert it to a scan.
 
Alright here is what I made. BenTen posted a similar script and I changed it and subtracted some things and the other was by Markus who posted a script from Mobius which I also deleted a bunch of things from.

Code:
declare lower;
def high = ImpVolatility();
def low = ImpVolatility();
#----------------------------------------------------------------------------------
#input 52 week =  Highest(high(period = AggregationPeriod.WEEK), 52)
def number_of_days = 252;
def a1 = Highest(high, number_of_days);
def barNumber1 = BarNumber();
def bar1 = if IsNaN(a1)
then Double.NaN
else BarNumber();
def ThisBar1 = HighestAll(bar1);
def Line1 = if bar1 == ThisBar1
then a1
else Double.NaN;
plot P1 = if ThisBar1
then HighestAll(Line1)
else Double.NaN;
P1.SetStyle(Curve.SHORT_DASH);
P1.SetLineWeight(1);
P1.SetDefaultColor(CreateColor(75, 250, 150));
#----------------------------------------------------------------------------------
#def number_of_days = 252; is the same at the top, copy this line when creating an individual scan so the info is connected to the below data/chart.
def a2 = Lowest(low, number_of_days);
def barNumber = BarNumber();
def bar2 = if IsNaN(a2)
then Double.NaN
else BarNumber();
def ThisBar2 = HighestAll(bar2);
def Line2 = if bar2 == ThisBar2
then a2
else Double.NaN;
plot P2 = if ThisBar2
then HighestAll(Line2)
else Double.NaN;
P2.SetStyle(Curve.SHORT_DASH);
P2.SetLineWeight(1);
P2.SetDefaultColor(CreateColor(75, 250, 150));
#----------------------------------------------------------------------------------
def ImpVol = imp_volatility();
#----------------------------------------------------------------------------------

def number_of_days_low = p2;
def number_of_days_high = p1;

### type percent as a decimal.
def percent = 0.1;

def P = ImpVol;
def p11 = percent * p1;
def p21 = percent * p2;
def p22 = p21 + p2;
def p12 = p1 - p11;
def price = if IsNaN(P) then price[1] else P;
plot data = if IsNaN(close) then Double.NaN else price;
data.EnableApproximation();
data.SetDefaultColor(Color.MAGENTA);

AddCloud(p2, p22, CreateColor(250, 0, 0), CreateColor(250, 0, 0));
AddCloud(p12, p1, CreateColor(0, 250, 0), CreateColor(0, 250, 0));
 
here is what I got so far. NHL started back up today so I watched my teams first game in 11 months as they were one of 6 teams that didn't make it to the playoffs last year due to the covid situation.
Code:
#--------------------------------------------------------------
#Hint: plots the 52 week high and 52 week low Implied Volatility.
#----------------------------------------------------------------------------------
#CODE BELOW
#----------------------------------------------------------------------------------
declare lower;
def high = ImpVolatility();
def low = ImpVolatility();
#----------------------------------------------------------------------------------
####Can below Changed to show longer by multiplying # of days (252) by x # of years for longer time frames. Current implied Volatility is also plotted on this chart.
#input 52 week =  Highest(high(period = AggregationPeriod.WEEK), 52)
def number_of_days = 252;
def a1 = Highest(high, number_of_days);
def barNumber1 = BarNumber();
def bar1 = if IsNaN(a1)
then Double.NaN
else BarNumber();
def ThisBar1 = HighestAll(bar1);
def Line1 = if bar1 == ThisBar1
then a1
else Double.NaN;
plot P1 = if ThisBar1
then HighestAll(Line1)
else Double.NaN;
P1.SetStyle(Curve.SHORT_DASH);
P1.SetLineWeight(1);
P1.SetDefaultColor(CreateColor(255, 204, 204));
#----------------------------------------------------------------------------------
#def number_of_days = 252; is the same at the top, copy this line when creating an individual scan so the info is connected to the below data/chart.
def a2 = Lowest(low, number_of_days);
def barNumber = BarNumber();
def bar2 = if IsNaN(a2)
then Double.NaN
else BarNumber();
def ThisBar2 = HighestAll(bar2);
def Line2 = if bar2 == ThisBar2
then a2
else Double.NaN;
plot P2 = if ThisBar2
then HighestAll(Line2)
else Double.NaN;
P2.SetStyle(Curve.SHORT_DASH);
P2.SetLineWeight(1);
P2.SetDefaultColor(CreateColor(204, 255, 204));
#----------------------------------------------------------------------------------
def ImpVol = imp_volatility();
#----------------------------------------------------------------------------------
###Hint:type percent as a decimal. 10% = 0.1, 13% = 0.13, 6% = 0.06, etc...
input percent = 0.06;
##ImpVoly = p = ImpVol; system wont let me put P there so its staying as ImpVoly and the Implied volatility graph will be called ImpVoly.
##highs:
           #P1=    highest bigh of the current time frame.
           #P11=   number that is the % from the highest high as specified above.
           #P12=   actual number that is xyz % below the highest high.
##Low's:
           #P2=    lowest low of the current time frame.
           #P21=   number that is the % from the lowest low as specified above.
           #P22=   actual number that is xyz % above the lowest low.
#----------------------------------------------------------------------------------
def P = ImpVol;
def p11 = percent * p1;
def p21 = percent * p2;
plot p22 = p21 + p2;
plot p12 = p1 - p11;
def price = if IsNaN(P) then price[1] else P;
plot ImpVoly = if IsNaN(close) then Double.NaN else price;
ImpVoly.EnableApproximation();
AddCloud(p2, p22, CreateColor(204, 255, 204), CreateColor(204, 255, 204));
##AddCloud(OS, neutral, CreateColor(50, 0, 0), CreateColor(50, 0, 0));
AddCloud(p12, p1, CreateColor(255, 204, 204), CreateColor(255, 204, 204));

ImpVoly.DefineColor("The_High", GetColor());
ImpVoly.DefineColor("Normal", GetColor());
ImpVoly.DefineColor("The_Low", GetColor());
ImpVoly.assignValueColor(if ImpVoly > P12 then ImpVoly.color("The_High") else if ImpVoly < P22 then ImpVoly.color("The_Low") else ImpVoly.color("Normal"));
 

Similar threads

Top