Sloped Lines


Does anyone know how to slope lines in TOS based on a factor? Been trying to figure this out for days, what I'm doing here is aggregating these lines on a quarterly basis, and at the moment they're horizontal but am trying to aggregate them on an angle with a certain factor like .5 is anyone able to help with this, thank you.


<
input aggregation = AggregationPeriod.quaRTER;

def high = high(period = aggregation)[1];
def close = close(period = aggregation)[1];

plot line = (close* .5) + high;
plot line1 = (close * .618) + high;

def paintingStrategy = PaintingStrategy.HORIZONTAL ;
line.SetPaintingStrategy(paintingStrategy);
line1.SetPaintingStrategy(paintingStrategy);
 
Does anyone know how to slope lines in TOS based on a factor? Been trying to figure this out for days, what I'm doing here is aggregating these lines on a quarterly basis, and at the moment they're horizontal but am trying to aggregate them on an angle with a certain factor like .5 is anyone able to help with this, thank you.

Take a look at the Inertia() function or InertiaAll(). You can see an example of the latter's use in TOS's StandardDevChannel study.
 
I would but from what I've read is that it uses the chart's entire bar count, rather what I'm looking for is the bar count in a certain aggregated period like the quarterly.
If you RTFM at the link I sent you'll see Inertia() takes a length. InertiaAll() uses the entire chart. Hence the all.
 
Yeah just saw it 5mins ago spoke too soon, how would I define length by the length of bars in the aggregation period then

On average there's 21 trading days per month. Assuming you're using this on a daily chart, if you want a quarter use a length of 63 days. For an intraday chart you can use

Ruby:
def whatever = Inertia(close(period = AggregationPeriod.DAY), 63);
 
Is there a way to measure the number of bars between the high and the low of the aggregation period, I think that's what it need when inputting length.

I haven't ever used it but you could try GetValue() and for the first parameter use BarNumber(). In theory, you should be able to get the bar numbers of the high and low bars and subtract to find the range. That would be your length. Assuming the current bar is after those, you would also need to put an index on the data being passed to Inertia() such as Inertia(close[Max(highBar, lowBar)], length) so you're calculating for a range of x bars that ended y bars ago.
 
please add to your first post and explain what do you want to see on the chart.
from which point(s) on a chart, do you want to see sloped line(s) drawn from ( and to).
you mention specifying a slope factor, so i assume you want to draw a diagonal line from some point , for every quarter?
you mention aggregate, which means to combine. what do you want to combine?
do you want to draw 2 diagonal lines and combine them into a 3rd line?


...if you want a sloped line, you need a slope.
...to calculate a slope, you need a difference in price (Y) and a difference in time (X)
slope = (y2-y)/(x2-x)
https://www.intmath.com/plane-analytic-geometry/1b-gradient-slope-line.php
...to plot a diagonal line, add the slope to the previous point on the line
diag = previous diag + slope
Code:
def slope1= 0.2;
plot diag1 = diag1[1] + slope1;
this will start drawing a diagonal line from the first bar. need to identify the starting point on the graph and add that to the calculations.

you mentioned using a factor to alter the slope. you could use trigonometry and tangent and multiply a factor by an angle, to come up with a change in height (price).
or just enter a price amount, to add to the previous value, to create a diagonal line.
 
I haven't ever used it but you could try GetValue() and for the first parameter use BarNumber(). In theory, you should be able to get the bar numbers of the high and low bars and subtract to find the range. That would be your length. Assuming the current bar is after those, you would also need to put an index on the data being passed to Inertia() such as Inertia(close[Max(highBar, lowBar)], length) so you're calculating for a range of x bars that ended y bars ago.
please add to your first post and explain what do you want to see on the chart.
from which point(s) on a chart, do you want to see sloped line(s) drawn from ( and to).
you mention specifying a slope factor, so i assume you want to draw a diagonal line from some point , for every quarter?
you mention aggregate, which means to combine. what do you want to combine?
do you want to draw 2 diagonal lines and combine them into a 3rd line?


...if you want a sloped line, you need a slope.
...to calculate a slope, you need a difference in price (Y) and a difference in time (X)
slope = (y2-y)/(x2-x)
https://www.intmath.com/plane-analytic-geometry/1b-gradient-slope-line.php
...to plot a diagonal line, add the slope to the previous point on the line
diag = previous diag + slope
Code:
def slope1= 0.2;
plot diag1 = diag1[1] + slope1;
this will start drawing a diagonal line from the first bar. need to identify the starting point on the graph and add that to the calculations.

you mentioned using a factor to alter the slope. you could use trigonometry and tangent and multiply a factor by an angle, to come up with a change in height (price).
or just enter a price amount, to add to the previous value, to create a diagonal line.
Alright, so I think I got what you guys are saying down and coded it here, thank you for the input this has been great, but now my problem seems to be with the line compounding on itself as seen here, the more bars I include the more this seems to go higher, where is the problem do you think that is causing the compounding?

<input aggregation = AggregationPeriod.DAY;

def high = ((high(period = aggregation)[1]));
def close = ((close(period = aggregation)[1]));
def low = (low(period = aggregation)[1]);

def barhigh = GetValue(BarNumber(), high);
def barlow = GetValue(BarNumber(), low);
def x = AbsValue(barlow - barhigh );
input fib = 0;
def line = high + ((high - close) * fib);

def slope = (line - low) / x;
plot line2 = low + (slope * (barlow - low));>

 
Last edited:
you didn't answer my questions, so i still don't know what you are trying to do, so it's hard to steer you in the right direction.

my guess is you want to draw a new line for each quarter period, so you need a way to reset the line to some new start price, bar, time,...

here is a study that may help
...identifies the first bar of a time period , yellow arrow
...finds the lowest low during some 2nd agg period ( default is day), red arrow
...draws yellow diagonal lines, each one starting from the lowest low of the period

problems,
...if there are 2+ bars with the same low, may not plot correctly
...by using a price for the slope increment, when looking at stocks priced near $1, the lines will appear vertical.
for more consistent lines, on many stocks, may want to use a percentage of the stock price, as the slope increment.

there is a bubble line commented out. i use them to display variable values, to help debug scripts.

Ruby:
# test2_period

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

input agg =  AggregationPeriod.DAY;
def cls = close(period = agg);

# without the check for bn == 1, it errors and is always 1.
#  because on bar 1, it tries to read a previous bar value, and there isn't one.
def periodstart = if bn == 1 then 0 else if ( cls[1] == cls ) then 0 else 1;

#  when the bar is the last bar in a period, it will be different than the next
def periodend = if bn == 1 then 0 else if ( cls[0] == cls[-1] ) then 0 else 1;

plot z = periodstart;
z.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_down);
z.SetDefaultColor(Color.yellow);

#addchartbubble(1,low, cls[1] + "\n" + cls + "\n" + periodstart , color.cyan, no);

# start a line on the lowest price durign an agg period
def lowday = (low(period = agg));

# look for the bar with the low
def lowz =
 if periodend then na
 else if low == lowday then low
 else lowz[1];

# look for the barnumber of the low
def lowzbn =
 if periodend then na
 else if low == lowday then bn
 else lowzbn[1];

input slope = 0.5;
plot diag = lowz + (( bn - lowzbn)* slope);
diag.SetDefaultColor(Color.yellow);
diag.SetStyle(Curve.MEDIUM_DASH);

plot w = ( lowzbn == bn );
w.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_up);
w.SetDefaultColor(Color.red);
#
#

fV2j8g2.jpg
 
Last edited:
you didn't answer my questions, so i still don't know what you are trying to do, so it's hard to steer you in the right direction.

my guess is you want to draw a new line for each quarter period, so you need a way to reset the line to some new start price, bar, time,...

here is a study that may help
...identifies the first bar of a time period , yellow arrow
...finds the lowest low during some 2nd agg period ( default is day), red arrow
...draws yellow diagonal lines, each one starting from the lowest low of the period

problems,
...if there are 2+ bars with the same low, may not plot correctly
...by using a price for the slope increment, when looking at stocks priced near $1, the lines will appear vertical.
for more consistent lines, on many stocks, may want to use a percentage of the stock price, as the slope increment.

there is a bubble line commented out. i use them to display variable values, to help debug scripts.

Ruby:
# test2_period

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

input agg =  AggregationPeriod.DAY;
def cls = close(period = agg);

# without the check for bn == 1, it errors and is always 1.
#  because on bar 1, it tries to read a previous bar value, and there isn't one.
def periodstart = if bn == 1 then 0 else if ( cls[1] == cls ) then 0 else 1;

#  when the bar is the last bar in a period, it will be different than the next
def periodend = if bn == 1 then 0 else if ( cls[0] == cls[-1] ) then 0 else 1;

plot z = periodstart;
z.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_down);
z.SetDefaultColor(Color.yellow);

#addchartbubble(1,low, cls[1] + "\n" + cls + "\n" + periodstart , color.cyan, no);

# start a line on the lowest price durign an agg period
def lowday = (low(period = agg));

# look for the bar with the low
def lowz =
 if periodend then na
 else if low == lowday then low
 else lowz[1];

# look for the barnumber of the low
def lowzbn =
 if periodend then na
 else if low == lowday then bn
 else lowzbn[1];

input slope = 0.5;
plot diag = lowz + (( bn - lowzbn)* slope);
diag.SetDefaultColor(Color.yellow);
diag.SetStyle(Curve.MEDIUM_DASH);

plot w = ( lowzbn == bn );
w.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_up);
w.SetDefaultColor(Color.red);
#
#

fV2j8g2.jpg
My apologies I can be clearer, this is extremely close to what is trying to be achieved, the slope is on point, now what I'm trying to do is have that line carry onto the aggregation after it like in the illustration below.
I'm trying to put in [1] in some parts of the code but the lines just disappear.


It is essentially trying to grab the sloped line from the prior aggreation and plot it over to the current
 
My apologies I can be clearer, this is extremely close to what is trying to be achieved, the slope is on point, now what I'm trying to do is have that line carry onto the aggregation after it like in the illustration below.
I'm trying to put in [1] in some parts of the code but the lines just disappear.


It is essentially trying to grab the sloped line from the prior aggreation and plot it over to the current
it's all good. when frustrated or trying to explain something new, the words don't always come easily. just trying to prod you to think about the problem and describe it in short concise sentences.

in my example, 1 variable is used for 1 line , during each period.
if you want the line to span 3 periods, then there will be 3 lines on 1 bar. then you need 3 sets of formulas/variables, to store data for each line. create a counter of the periods. start the lines on periods 1, 2 3. then check for and reset them on 3 period intervals.
 
it's all good. when frustrated or trying to explain something new, the words don't always come easily. just trying to prod you to think about the problem and describe it in short concise sentences.

in my example, 1 variable is used for 1 line , during each period.
if you want the line to span 3 periods, then there will be 3 lines on 1 bar. then you need 3 sets of formulas/variables, to store data for each line. create a counter of the periods. start the lines on periods 1, 2 3. then check for and reset them on 3 period intervals.
Thank you I appreciate your sincerity. Also, from what I'm understanding is that I will not be able to plot the lines ahead of an aggregation forward?
The idea is almost like the fib fans, but the idea behind this is that the fibs are reset based on aggregation rather than taking the current high and low. You take the high and low of the previous aggregation and plot it ahead into the next one which from what I've seen is respected very strongly.
 
Thank you I appreciate your sincerity. Also, from what I'm understanding is that I will not be able to plot the lines ahead of an aggregation forward?
The idea is almost like the fib fans, but the idea behind this is that the fibs are reset based on aggregation rather than taking the current high and low. You take the high and low of the previous aggregation and plot it ahead into the next one which from what I've seen is respected very strongly.
you can plot the lines on time periods after the one that the line starts on. you will have to add more variables and formulas , for each additional line.
 

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