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!!
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!!
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);