How to draw automatic retracements?

derpderp

New member
Hi folks,
I'm struggling figuring out how to programmatically draw fib retracements on a chart.
This is kind of working, though my desired outcome would be to include an input for which retracements to draw and ONLY draw the retracements off of the highest high and have the line going horizontally through the charts. Also, I haven't figured out how to look at the whole chart instead of using a manual lookback period.
Any advice?
In essence, I'd like it to look like the blue lines on the attached image.

Thank you!!

Screenshot 2024-02-14 at 2.39.00 PM.png

Code:
# Retracement Levels Study
# Draws lines at 5%, 10%, 15%, and 20% retracements from the highest bar

# Define the lookback period for detecting the highest high
input lookbackPeriod = 20;

# Calculate the highest high over the lookback period
def highestHigh = Highest(high, lookbackPeriod);

# Calculate retracement levels
def retracement5 = highestHigh * 0.95;
def retracement10 = highestHigh * 0.90;
def retracement15 = highestHigh * 0.85;
def retracement20 = highestHigh * 0.80;

# Plot lines at retracement levels
plot Line5 = retracement5;
plot Line10 = retracement10;
plot Line15 = retracement15;
plot Line20 = retracement20;

Line5.SetDefaultColor(Color.CYAN);
Line10.SetDefaultColor(Color.ORANGE);
Line15.SetDefaultColor(Color.MAGENTA);
Line20.SetDefaultColor(Color.RED);
 
Solution
Hi folks,
I'm struggling figuring out how to programmatically draw fib retracements on a chart.
This is kind of working, though my desired outcome would be to include an input for which retracements to draw and ONLY draw the retracements off of the highest high and have the line going horizontally through the charts. Also, I haven't figured out how to look at the whole chart instead of using a manual lookback period.
Any advice?
In essence, I'd like it to look like the blue lines on the attached image.

Thank you!!

View attachment 21068
Code:
# Retracement Levels Study
# Draws lines at 5%, 10%, 15%, and 20% retracements from the highest bar

# Define the lookback period for detecting the highest high
input lookbackPeriod = 20;

# Calculate the...
Hi folks,
I'm struggling figuring out how to programmatically draw fib retracements on a chart.
This is kind of working, though my desired outcome would be to include an input for which retracements to draw and ONLY draw the retracements off of the highest high and have the line going horizontally through the charts. Also, I haven't figured out how to look at the whole chart instead of using a manual lookback period.
Any advice?
In essence, I'd like it to look like the blue lines on the attached image.

Thank you!!

View attachment 21068
Code:
# Retracement Levels Study
# Draws lines at 5%, 10%, 15%, and 20% retracements from the highest bar

# Define the lookback period for detecting the highest high
input lookbackPeriod = 20;

# Calculate the highest high over the lookback period
def highestHigh = Highest(high, lookbackPeriod);

# Calculate retracement levels
def retracement5 = highestHigh * 0.95;
def retracement10 = highestHigh * 0.90;
def retracement15 = highestHigh * 0.85;
def retracement20 = highestHigh * 0.80;

# Plot lines at retracement levels
plot Line5 = retracement5;
plot Line10 = retracement10;
plot Line15 = retracement15;
plot Line20 = retracement20;

Line5.SetDefaultColor(Color.CYAN);
Line10.SetDefaultColor(Color.ORANGE);
Line15.SetDefaultColor(Color.MAGENTA);
Line20.SetDefaultColor(Color.RED);

This should help

Screenshot 2024-02-28 144525.png
Code:
# Retracement Levels Study
# Draws lines at 5%, 10%, 15%, and 20% retracements from the highest bar

# Define the lookback period for detecting the highest high
input showonexpansion = no;
input lookbackPeriod  = 20;
input r1 = .95;
input r2 = .90;
input r3 = .85;
input r4 = .80;

# Calculate the highest high over the lookback period
def bn    = BarNumber();
def hHbar = if high == Highest(high, lookbackPeriod) then bn else Double.NaN;
def hHigh = if bn == HighestAll(hHbar) then high else Double.NaN;
;
plot highesthigh = if showonexpansion and !isnan(close) then double.nan else HighestAll(hHigh);

# Calculate retracement levels
plot retracement1 = highesthigh * r1;
plot retracement2 = highesthigh * r2;
plot retracement3 = highesthigh * r3;
plot retracement4 = highesthigh * r4;

# Plot lines at retracement levels
retracement1.SetDefaultColor(Color.CYAN);
retracement2.SetDefaultColor(Color.ORANGE);
retracement3.SetDefaultColor(Color.MAGENTA);
retracement4.SetDefaultColor(Color.RED);

input bubbles = yes;
input bubblemover = 4;
def   mover = bubbles and IsNaN(close[bubblemover]) and !IsNaN(close[bubblemover + 1]);

AddChartBubble(mover, highesthigh, "HH " + asdollars(highesthigh[bubblemover]), highesthigh.TakeValueColor());
AddChartBubble(mover, retracement1, aspercent(1-r1) + " :" + asdollars(retracement1[bubblemover]), retracement1.TakeValueColor());
AddChartBubble(mover, retracement2, aspercent(1-r2) + " :" + asdollars(retracement2[bubblemover]), retracement2.TakeValueColor());
AddChartBubble(mover, retracement3, aspercent(1-r3) + " :" + asdollars(retracement3[bubblemover]), retracement3.TakeValueColor());
AddChartBubble(mover, retracement4, aspercent(1-r4) + " :" + asdollars(retracement4[bubblemover]), retracement4.TakeValueColor());

#
 
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
381 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