Trend change indicator. I need help adjusting the plot.

Haamu

New member
I have built an indicator when a candle changes trend. The plots work fairly well.
I am looking to have the lines extend all the way to the end of the chart and then have the lines stop or go dashed when a candle closes through (not on wicks) preferably a toggle if possible. Thanks in advance!
Screenshot 2025-01-05 223956.png




# Aggregation period for weekly levels
input aggPeriod = AggregationPeriod.WEEK;

# Define high, low, and close for the weekly timeframe
def weekOpen = open(period = aggPeriod);
def weekHigh = high(period = aggPeriod);
def weekLow = low(period = aggPeriod);
def weekClose = close(period = aggPeriod);

# Trend change conditions on the weekly timeframe
def weekDownToUp = weekClose > weekOpen and weekClose[1] < weekOpen[1];
def weekUpToDown = weekClose < weekOpen and weekClose[1] > weekOpen[1];

# Persistent Down-to-Up levels (extend until invalidated on the weekly timeframe)
rec weekDownToUpLevel1 =
if weekDownToUp then weekOpen
else if weekClose[1] <= weekDownToUpLevel1[1] then weekDownToUpLevel1[1]
else weekDownToUpLevel1[1];

rec weekDownToUpLevel2 =
if weekDownToUp and weekClose[1] <= weekDownToUpLevel1 then weekOpen
else if weekClose[1] <= weekDownToUpLevel2[1] then weekDownToUpLevel2[1]
else weekDownToUpLevel2[1];

rec weekDownToUpLevel3 =
if weekDownToUp and weekClose[1] <= weekDownToUpLevel2 then weekOpen
else if weekClose[1] <= weekDownToUpLevel3[1] then weekDownToUpLevel3[1]
else weekDownToUpLevel3[1];

# Persistent Up-to-Down levels (extend until invalidated on the weekly timeframe)
rec weekUpToDownLevel1 =
if weekUpToDown then weekOpen
else if weekClose[1] >= weekUpToDownLevel1[1] then weekUpToDownLevel1[1]
else weekUpToDownLevel1[1];

rec weekUpToDownLevel2 =
if weekUpToDown and weekClose[1] >= weekUpToDownLevel1 then weekOpen
else if weekClose[1] >= weekUpToDownLevel2[1] then weekUpToDownLevel2[1]
else weekUpToDownLevel2[1];

rec weekUpToDownLevel3 =
if weekUpToDown and weekClose[1] >= weekUpToDownLevel2 then weekOpen
else if weekClose[1] >= weekUpToDownLevel3[1] then weekUpToDownLevel3[1]
else weekUpToDownLevel3[1];

# Plot Down-to-Up levels
plot weekDownToUpPlot1 = weekDownToUpLevel1;
weekDownToUpPlot1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
weekDownToUpPlot1.SetDefaultColor(Color.GREEN);

plot weekDownToUpPlot2 = weekDownToUpLevel2;
weekDownToUpPlot2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
weekDownToUpPlot2.SetDefaultColor(Color.LIGHT_GREEN);

plot weekDownToUpPlot3 = weekDownToUpLevel3;
weekDownToUpPlot3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
weekDownToUpPlot3.SetDefaultColor(Color.DARK_GREEN);

# Plot Up-to-Down levels
plot weekUpToDownPlot1 = weekUpToDownLevel1;
weekUpToDownPlot1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
weekUpToDownPlot1.SetDefaultColor(Color.RED);

plot weekUpToDownPlot2 = weekUpToDownLevel2;
weekUpToDownPlot2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
weekUpToDownPlot2.SetDefaultColor(Color.PINK);

plot weekUpToDownPlot3 = weekUpToDownLevel3;
weekUpToDownPlot3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
weekUpToDownPlot3.SetDefaultColor(Color.DARK_RED);
 
Solution
I have built an indicator when a candle changes trend. The plots work fairly well.
I am looking to have the lines extend all the way to the end of the chart and then have the lines stop or go dashed when a candle closes through (not on wicks) preferably a toggle if possible. Thanks in advance!
View attachment 23702



# Aggregation period for weekly levels
input aggPeriod = AggregationPeriod.WEEK;

# Define high, low, and close for the weekly timeframe
def weekOpen = open(period = aggPeriod);
def weekHigh = high(period = aggPeriod);
def weekLow = low(period = aggPeriod);
def weekClose = close(period = aggPeriod);

# Trend change conditions on the weekly timeframe
def weekDownToUp = weekClose > weekOpen and weekClose[1] < weekOpen[1];
def...
I have built an indicator when a candle changes trend. The plots work fairly well.
I am looking to have the lines extend all the way to the end of the chart and then have the lines stop or go dashed when a candle closes through (not on wicks) preferably a toggle if possible. Thanks in advance!
View attachment 23702



# Aggregation period for weekly levels
input aggPeriod = AggregationPeriod.WEEK;

# Define high, low, and close for the weekly timeframe
def weekOpen = open(period = aggPeriod);
def weekHigh = high(period = aggPeriod);
def weekLow = low(period = aggPeriod);
def weekClose = close(period = aggPeriod);

# Trend change conditions on the weekly timeframe
def weekDownToUp = weekClose > weekOpen and weekClose[1] < weekOpen[1];
def weekUpToDown = weekClose < weekOpen and weekClose[1] > weekOpen[1];

# Persistent Down-to-Up levels (extend until invalidated on the weekly timeframe)
rec weekDownToUpLevel1 =
if weekDownToUp then weekOpen
else if weekClose[1] <= weekDownToUpLevel1[1] then weekDownToUpLevel1[1]
else weekDownToUpLevel1[1];

rec weekDownToUpLevel2 =
if weekDownToUp and weekClose[1] <= weekDownToUpLevel1 then weekOpen
else if weekClose[1] <= weekDownToUpLevel2[1] then weekDownToUpLevel2[1]
else weekDownToUpLevel2[1];

rec weekDownToUpLevel3 =
if weekDownToUp and weekClose[1] <= weekDownToUpLevel2 then weekOpen
else if weekClose[1] <= weekDownToUpLevel3[1] then weekDownToUpLevel3[1]
else weekDownToUpLevel3[1];

# Persistent Up-to-Down levels (extend until invalidated on the weekly timeframe)
rec weekUpToDownLevel1 =
if weekUpToDown then weekOpen
else if weekClose[1] >= weekUpToDownLevel1[1] then weekUpToDownLevel1[1]
else weekUpToDownLevel1[1];

rec weekUpToDownLevel2 =
if weekUpToDown and weekClose[1] >= weekUpToDownLevel1 then weekOpen
else if weekClose[1] >= weekUpToDownLevel2[1] then weekUpToDownLevel2[1]
else weekUpToDownLevel2[1];

rec weekUpToDownLevel3 =
if weekUpToDown and weekClose[1] >= weekUpToDownLevel2 then weekOpen
else if weekClose[1] >= weekUpToDownLevel3[1] then weekUpToDownLevel3[1]
else weekUpToDownLevel3[1];

# Plot Down-to-Up levels
plot weekDownToUpPlot1 = weekDownToUpLevel1;
weekDownToUpPlot1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
weekDownToUpPlot1.SetDefaultColor(Color.GREEN);

plot weekDownToUpPlot2 = weekDownToUpLevel2;
weekDownToUpPlot2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
weekDownToUpPlot2.SetDefaultColor(Color.LIGHT_GREEN);

plot weekDownToUpPlot3 = weekDownToUpLevel3;
weekDownToUpPlot3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
weekDownToUpPlot3.SetDefaultColor(Color.DARK_GREEN);

# Plot Up-to-Down levels
plot weekUpToDownPlot1 = weekUpToDownLevel1;
weekUpToDownPlot1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
weekUpToDownPlot1.SetDefaultColor(Color.RED);

plot weekUpToDownPlot2 = weekUpToDownLevel2;
weekUpToDownPlot2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
weekUpToDownPlot2.SetDefaultColor(Color.PINK);

plot weekUpToDownPlot3 = weekUpToDownLevel3;
weekUpToDownPlot3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
weekUpToDownPlot3.SetDefaultColor(Color.DARK_RED);

here is one way to draw a line that changes to a dash, when it crosses a bar body.
it will also change to a dash if it passes through a gap in bars.
it starts drawing a line at some barnumber. change it so it is near a lowest low.
if it crosses the next bar, then you won't see any solid line, it will change to dash right away.

i didn't do anything with your study. if you want multiple lines to exist on a bar, you will need a plot formula for each line and all the supporting formulas. so if you want 10 lines, you will need 10 copies of this study.

Code:
# bar_cross_line_dash

#https://usethinkscript.com/threads/trend-change-indicator-i-need-help-adjusting-the-plot.20287/

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

# define start of a horz line
#input line_start_bn = 104;
input line_start_bn = 116;
input price = low;

def bodytop = max(open,close);
def bodybot = min(open,close);

def line1 = if bn == 1 then na
 # start defining a line
 else if bn == line_start_bn then price
 else if isnan(close) then na
 # chk for bar crossing line, chg to neg #
 else if bodytop > line1[1] and bodybot < line1[1] and line1[1] > 0 then -line1[1]
# test if line goes thru a gap in bars
 else if (bodybot[1] > line1[1] and bodytop < line1[1]) or (bodytop[1] < line1[1] and bodybot > line1[1]) then -line1[1]
 else line1[1];

# plot solid line from a bar
plot z1 = if line1 > 0 then line1 else na;
z1.SetDefaultColor(Color.yellow);
#z1.setlineweight(1);
z1.hidebubble();

# plot a dash line after crossing a bar 
plot z2 = if line1 < 0 then -line1 else na;
z2.SetStyle(Curve.MEDIUM_DASH);
z2.SetDefaultColor(Color.cyan);
z2.hidebubble();

addchartbubble(0, low,
 (bodybot[1] > line1[1] and bodytop < line1[1]) + " BT\n" +
 (bodytop[1] < line1[1] and bodybot > line1[1]) + " TB"
, color.yellow, no);
#
 

Attachments

  • img1.JPG
    img1.JPG
    28.5 KB · Views: 95
  • img2.JPG
    img2.JPG
    42.9 KB · Views: 92
Solution

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