plotting a sloped line

cfire23

New member

halcyonguy,

I've studied your formula 3
https://usethinkscript.com/threads/plotting-in-the-expansion-area.9765/#post-91019

for plotting the sloped line and tried unsuccessfully for the past 3 days to modify it so that the slope line begins at the last swinglow within the last 12 bars on a 5minute chart and extends thru current 5minute bar close. I don't know either what's so hard for me to comprehend but any help you would provide is greatly appreciated.

here is image of what i'm trying to achieve:
SwvjmR6.png


here is my modified code:

input formula_bars = 6;
def na = Double.NaN;
def bn = BarNumber();
def lastbar = !IsNaN(close[0]) and IsNaN(close[-1]);
def lastbarbn = if lastbar then bn else lastbarbn[1];
def h30mh1 = highest(high,6)[1];

# formula3 plots line based on the slope between the highest high within the last 6 bars to the current bar close
def slope = if bn < lastbarbn then na
# else if lastbar then (close - close[1] )
else if lastbar then (close - h30mh1)
else slope[1];

def formula3 = if (bn < lastbarbn or bn > (lastbarbn + formula_bars)) then na
else if bn == (lastbarbn) then h30mh1
else (formula3[1] + slope);

plot f3 = formula3[-3];
# plot f3 = formula3[-6] works but i want the line plot to always start from the last highest high from the previous 6 bars; formula[-6] offsets the plot by 6 bars it doesn't start the plot from h30mh1 which is the highest high from the previous 6 bars. how can this be done without using [-6]? h30mh1 is a variable where i want the line plot to begin but i need to reference it as a constant. i have no idea how to reference the h30mh1 variable as a constant without getting an error?
 
Last edited by a moderator:
Solution
halcyonguy,

I've studied your formula 3
https://usethinkscript.com/threads/plotting-in-the-expansion-area.9765/#post-91019

for plotting the sloped line and tried unsuccessfully for the past 3 days to modify it so that the slope line begins at the last swinglow within the last 12 bars on a 5minute chart and extends thru current 5minute bar close. I don't know either what's so hard for me to comprehend but any help you would provide is greatly appreciated.

here is image of what i'm trying to achieve:


here is my modified code:

input formula_bars = 6;
def na = Double.NaN;
def bn = BarNumber();
def lastbar = !IsNaN(close[0]) and IsNaN(close[-1]);
def lastbarbn = if lastbar then bn else lastbarbn[1];
def h30mh1 =...
samer800,
would you modify the sextant code so that the line plot begins at the high price from 6 bars back to the current bar close price on a 5 minute chart?

here is image of request:
cgI5aEt.png


Greetings,

Looking for assistance to understand why i am unable to achieve the following with thinkorswim: plot a dynamic trendline from the highest high from the previous 6 bars to the current bar close price. so the trendline needs to update/redraw every 6 bars as time progresses.

I tried to modify Halcyonguy's plot in expansion area script to plot a dynamic trendline
https://usethinkscript.com/threads/plotting-in-the-expansion-area.9765/#post-91019
with limited success.

what i can't understand is how to code where the trendline plot begins which would be the highest high from the previous 6 bars.

It works by using [ -6 ] but is there a way to use h30mh1 which is the defined variable for highest high from the previous 6 bars?

DAizHxz.png


here is modified code:

input formula_bars = 6;
def na = Double.NaN;
def bn = BarNumber();
def lastbar = !IsNaN(close[0]) and IsNaN(close[-1]);
def lastbarbn = if lastbar then bn else lastbarbn[1];
def h30mh1 = highest(high,6)[1];

# formula3 plots line based on the slope between the highest high within the last 6 bars to the current bar close
def slope = if bn < lastbarbn then na
else if lastbar then (close - h30mh1)
else slope[1];

def formula3 = if (bn < lastbarbn or bn > (lastbarbn + formula_bars)) then na
else if bn == (lastbarbn) then h30mh1
else (formula3[1] + slope);

plot f3 = formula3 [ -6 ];
# plot f3 = formula3[-6] works but i want the line plot to always start from the last highest high from the previous 6 bars; formula[-6] offsets the plot by 6 bars it doesn't start the plot from h30mh1 which is the highest high from the previous 6 bars. how can this be done without using [-6]? h30mh1 is the variable specifying where to begin line plot but i need to reference it as a constant. i have no idea how to reference the h30mh1 variable as a constant without getting an error?
 
Last edited by a moderator:

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

halcyonguy,

I've studied your formula 3
https://usethinkscript.com/threads/plotting-in-the-expansion-area.9765/#post-91019

for plotting the sloped line and tried unsuccessfully for the past 3 days to modify it so that the slope line begins at the last swinglow within the last 12 bars on a 5minute chart and extends thru current 5minute bar close. I don't know either what's so hard for me to comprehend but any help you would provide is greatly appreciated.

here is image of what i'm trying to achieve:


here is my modified code:

input formula_bars = 6;
def na = Double.NaN;
def bn = BarNumber();
def lastbar = !IsNaN(close[0]) and IsNaN(close[-1]);
def lastbarbn = if lastbar then bn else lastbarbn[1];
def h30mh1 = highest(high,6)[1];

# formula3 plots line based on the slope between the highest high within the last 6 bars to the current bar close
def slope = if bn < lastbarbn then na
# else if lastbar then (close - close[1] )
else if lastbar then (close - h30mh1)
else slope[1];

def formula3 = if (bn < lastbarbn or bn > (lastbarbn + formula_bars)) then na
else if bn == (lastbarbn) then h30mh1
else (formula3[1] + slope);

plot f3 = formula3[-3];
# plot f3 = formula3[-6] works but i want the line plot to always start from the last highest high from the previous 6 bars; formula[-6] offsets the plot by 6 bars it doesn't start the plot from h30mh1 which is the highest high from the previous 6 bars. how can this be done without using [-6]? h30mh1 is a variable where i want the line plot to begin but i need to reference it as a constant. i have no idea how to reference the h30mh1 variable as a constant without getting an error?

reply to #1

sorry, i guess i missed this one.
looking at it.

its good to have detailed pics. but also put the notes as text in a post.
then they can be copied and used as the design rules

you don't seem to have code to define a valley?
 
Last edited:
halcyonguy,

I've studied your formula 3
https://usethinkscript.com/threads/plotting-in-the-expansion-area.9765/#post-91019

for plotting the sloped line and tried unsuccessfully for the past 3 days to modify it so that the slope line begins at the last swinglow within the last 12 bars on a 5minute chart and extends thru current 5minute bar close. I don't know either what's so hard for me to comprehend but any help you would provide is greatly appreciated.

here is image of what i'm trying to achieve:


here is my modified code:

input formula_bars = 6;
def na = Double.NaN;
def bn = BarNumber();
def lastbar = !IsNaN(close[0]) and IsNaN(close[-1]);
def lastbarbn = if lastbar then bn else lastbarbn[1];
def h30mh1 = highest(high,6)[1];

# formula3 plots line based on the slope between the highest high within the last 6 bars to the current bar close
def slope = if bn < lastbarbn then na
# else if lastbar then (close - close[1] )
else if lastbar then (close - h30mh1)
else slope[1];

def formula3 = if (bn < lastbarbn or bn > (lastbarbn + formula_bars)) then na
else if bn == (lastbarbn) then h30mh1
else (formula3[1] + slope);

plot f3 = formula3[-3];
# plot f3 = formula3[-6] works but i want the line plot to always start from the last highest high from the previous 6 bars; formula[-6] offsets the plot by 6 bars it doesn't start the plot from h30mh1 which is the highest high from the previous 6 bars. how can this be done without using [-6]? h30mh1 is a variable where i want the line plot to begin but i need to reference it as a constant. i have no idea how to reference the h30mh1 variable as a constant without getting an error?

reply to #1

this draws 2 diagonal lines. (can turn them off)
.. from last peak to current bar
.. from last valley to current bar

can show arrows on peaks and valleys
peaks and valleys , calc by highest or lowest within 7 bars

i didn't add anything to look back only within certain bars. it just finds the last valley and last peak.


Code:
#line_slope_from_valley

#https://usethinkscript.com/threads/plotting-a-sloped-line.20771/
#plotting a sloped line
#cfire23  Mar 8, 2025

#halconguy,
#I've studied your formula 3
#https://usethinkscript.com/threads/plotting-in-the-expansion-area.9765/#post-91019

#for plotting the sloped line and tried unsuccessfully for the past 3 days to modify it so that the slope line begins at the last swinglow within the last 12 bars on a 5minute chart and extends thru current 5minute bar close. I don't know either what's so hard for me to comprehend but any help you would provide is greatly appreciated.

#here is image of what i'm trying to achieve:

#here is my modified code:


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

#https://usethinkscript.com/threads/zigzag-high-low-with-supply-demand-zones-for-thinkorswim.172/#post-7048
#   Robert Payne
def na = double.nan;
def bn = BarNumber();

#def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def lastbn = HighestAll(if IsNaN(close) then 0 else bn);
def lastbar = bn == lastbn;
#def lastbar = (!isnan(close) and isnan(close[-1]));

def highx = high;
def lowx = low;
input length = 7;

def offset = Min(length - 1, lastbn - bn);
def peak = highx > Highest(highx[1], length - 1) and highx == GetValue(Highest(highx, length), -offset);
def valley = lowx < Lowest(lowx[1], length - 1) and lowx == GetValue(Lowest(lowx, length), -offset);

input show_pk_val = no;
plot zhi = if show_pk_val and peak then high*1.0004 else na;
plot zlo = if show_pk_val and valley then low*0.9996 else na;
zlo.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zlo.SetDefaultColor(Color.red);
zlo.setlineweight(1);
zlo.hidebubble();

zhi.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
zhi.SetDefaultColor(Color.green);
zhi.setlineweight(1);
zhi.hidebubble();

#--------------------
#input bars_back = 12;
#def en = (!isnan(close) and isnan(close[-(bars_back)]));
#def rngfirst = (!isnan(close[-(bars_back-1)]) and isnan(close[-(bars_back)]));
#addverticalline(rngfirst, "-");
#-----------------------
# keep bn of peaks and valleys
def pkbn2 = if peak then bn else pkbn2[1];
def valbn2 = if valley then bn else valbn2[1];
# find last bn of peaks and valleys
def pkbnhi = highestall(pkbn2);
def valbnhi = highestall(valbn2);

def n = 200;

def pkhi;
def pkbn;
def pkbarz;
def pkslope;
def vallo;
def valbn;
def valbarz;
def valslope;
#def first_pr;
if bn == 1 then {
 pkhi = 0;
 pkbn = 0;
 pkbarz = 0;
 pkslope = 0;

 vallo = 0;
 valbn = 0;
 valbarz = 0;
 valslope = 0;
} else if bn == valbnhi then {
# on last valley , calc slope to last bar
 pkhi = pkhi[1];
 pkbn = pkbn[1];
 pkbarz = pkbarz[1];
 pkslope = pkslope[1];

 vallo = low;
 valbn = bn;
 valbarz = lastbn - bn;
 valslope = (getvalue(close,-valbarz) - vallo)/valbarz;
} else if bn == pkbnhi then {
# on last peak , calc slope to last bar
 pkhi = high;
 pkbn = bn;
 pkbarz = lastbn - bn;
 pkslope = (getvalue(close,-pkbarz) - pkhi)/pkbarz;

 vallo = vallo[1];
 valbn = valbn[1];
 valbarz = valbarz[1];
 valslope = valslope[1];
} else {
 pkhi = pkhi[1];
 pkbn = pkbn[1];
 pkbarz = pkbarz[1];
 pkslope = pkslope[1];
 vallo = vallo[1];
 valbn = valbn[1];
 valbarz = valbarz[1];
 valslope = valslope[1];
}

# calc diag line from peak
def pkline = if isnan(close) then 0
 else if bn < pkbnhi then 0
 else if bn == pkbnhi then high
 else (pkline[1] + pkslope);

input show_peak_line = yes;
plot zpk = if show_peak_line and pkline > 0 then pkline else na;

# calc diag line from valley
def valline = if isnan(close) then 0
 else if bn < valbnhi then 0
 else if bn == valbnhi then low
 else (valline[1] + valslope);

input show_valley_line = yes;
plot zval = if show_valley_line and valline > 0 then valline else na;

#
 

Attachments

  • img2.JPG
    img2.JPG
    62.5 KB · Views: 13
Solution
samer800,
would you modify the sextant code so that the line plot begins at the high price from 6 bars back to the current bar close price on a 5 minute chart?

here is image of request:


Greetings,

Looking for assistance to understand why i am unable to achieve the following with thinkorswim: plot a dynamic trendline from the highest high from the previous 6 bars to the current bar close price. so the trendline needs to update/redraw every 6 bars as time progresses.

I tried to modify Halcyonguy's plot in expansion area script to plot a dynamic trendline
https://usethinkscript.com/threads/plotting-in-the-expansion-area.9765/#post-91019
with limited success.

what i can't understand is how to code where the trendline plot begins which would be the highest high from the previous 6 bars.

It works by using [ -6 ] but is there a way to use h30mh1 which is the defined variable for highest high from the previous 6 bars?



here is modified code:

input formula_bars = 6;
def na = Double.NaN;
def bn = BarNumber();
def lastbar = !IsNaN(close[0]) and IsNaN(close[-1]);
def lastbarbn = if lastbar then bn else lastbarbn[1];
def h30mh1 = highest(high,6)[1];

# formula3 plots line based on the slope between the highest high within the last 6 bars to the current bar close
def slope = if bn < lastbarbn then na
else if lastbar then (close - h30mh1)
else slope[1];

def formula3 = if (bn < lastbarbn or bn > (lastbarbn + formula_bars)) then na
else if bn == (lastbarbn) then h30mh1
else (formula3[1] + slope);

plot f3 = formula3 [ -6 ];
# plot f3 = formula3[-6] works but i want the line plot to always start from the last highest high from the previous 6 bars; formula[-6] offsets the plot by 6 bars it doesn't start the plot from h30mh1 which is the highest high from the previous 6 bars. how can this be done without using [-6]? h30mh1 is the variable specifying where to begin line plot but i need to reference it as a constant. i have no idea how to reference the h30mh1 variable as a constant without getting an error?

reply to #2

this draws a line from the high of some bar in the past to the current bar

Code:
#line_slope_from_barx

#https://usethinkscript.com/threads/plotting-a-sloped-line.20771/
#plotting a sloped line

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

#def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def lastbn = HighestAll(if IsNaN(close) then 0 else bn);
def lastbar = bn == lastbn;
#def lastbar = (!isnan(close) and isnan(close[-1]));

#--------------------
input bars_back = 6;
def en = (!isnan(close[-(bars_back-1)]) and isnan(close[-bars_back]));
def rngfirst = (!isnan(close[-(bars_back-1)]) and isnan(close[-(bars_back)]));
#addverticalline(rngfirst, "-");
#-----------------------

def pkhi;
def pkbn;
def pkbarz;
def pkslope;
if bn == 1 then {
 pkhi = 0;
 pkbn = 0;
 pkbarz = 0;
 pkslope = 0;
} else if en then {
 pkhi = high;
 pkbn = bn;
 pkbarz = lastbn - bn;
 pkslope = (getvalue(close,-pkbarz) - pkhi)/pkbarz;
} else {
 pkhi = pkhi[1];
 pkbn = pkbn[1];
 pkbarz = pkbarz[1];
 pkslope = pkslope[1];
}

# calc diag line
def pkline = if bn == 1 or isnan(close) then 0
 else if bn < pkbn then 0
 else if bn == pkbn then high
 else (pkline[1] + pkslope);

input show_peak_line = yes;
plot zpk = if show_peak_line and pkline > 0 then pkline else na;
#
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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