Fibonacci Extension and Retracement In One Script?

mikeraya

New member
Is it possible to write a code that gives a Fibonacci Extension and Retracement on one draw? Thank you in advance.
 
Solution
Is it possible to write a code that gives a Fibonacci Extension and Retracement on one draw? Thank you in advance.

Here are 2 auto scripts (retracement and extension) that I posted in here that are modified to be in one script.

It uses the previous day for the retacement and the current day's developing candles for the extension. If the previous day's low was before the high of the day (only aggperiod tested) then the developing low of the current day would be used for the extension. If the previous day's high was first then the current day will use the high.

Screenshot-2023-04-30-154416.png
Code:
#Combined_AutoFib_Retracement_Extension

#Auto_FibExtensions_flips_fib_levels_including_intraday_timeframes_using_Open_as_ZeroBase

def limit_display =...

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

Is it possible to write a code that gives a Fibonacci Extension and Retracement on one draw? Thank you in advance.

Here are 2 auto scripts (retracement and extension) that I posted in here that are modified to be in one script.

It uses the previous day for the retacement and the current day's developing candles for the extension. If the previous day's low was before the high of the day (only aggperiod tested) then the developing low of the current day would be used for the extension. If the previous day's high was first then the current day will use the high.

Screenshot-2023-04-30-154416.png
Code:
#Combined_AutoFib_Retracement_Extension

#Auto_FibExtensions_flips_fib_levels_including_intraday_timeframes_using_Open_as_ZeroBase

def limit_display = yes;
def display_x     = 1;
input aggperiod     = {"1 MIN", "2 MIN", "3 MIN", "5 MIN", "10 MIN", "15 MIN", "30 MIN", "1 HOUR", default "DAY", "WEEK", "MONTH", "YEAR"};
input showbubbles   = yes;
input bubblemover   = -1;
input showverticals = yes;

def bm  = bubblemover;
def period;
def yyyymmdd = GetYYYYMMDD();
def seconds = SecondsFromTime(0);
def month = GetYear() * 12 + GetMonth();
def day_number = DaysFromDate(First(yyyymmdd)) + GetDayOfWeek(First(yyyymmdd));
def dom = GetDayOfMonth(yyyymmdd);
def dow = GetDayOfWeek(yyyymmdd - dom + 1);

switch (aggperiod) {
case "1 MIN":
    period = Floor(seconds / 60 + day_number * 24 * 60);
case "2 MIN":
    period = Floor(seconds / 120 + day_number * 24);
case "3 MIN":
    period = Floor(seconds / 180 + day_number * 24);
case "5 MIN":
    period = Floor(seconds / 300 + day_number * 24);
case "10 MIN":
    period = Floor(seconds / 600 + day_number * 24);
case "15 MIN":
    period = Floor(seconds / 900 + day_number * 24);
case "30 MIN":
    period = Floor(seconds / 1800 + day_number * 24);
case "1 HOUR":
    period = Floor(seconds / 3600 + day_number * 24);
case "DAY":
    period = CountTradingDays(Min(First(yyyymmdd), yyyymmdd), yyyymmdd) - 1;
case "WEEK":
    period = Floor(day_number / 7);
case "MONTH":
    period = Floor(month - First(month));
case "YEAR":
    period = GetYear();
}

def na = Double.NaN;
def bn = BarNumber();
def H  = high(period = aggperiod);
def L  = low(period = aggperiod);
def O  = open(period = aggperiod);

def hbar = if high == H then bn else hbar[1];
def lbar = if low == L  then bn else lbar[1];

def basis;
switch (aggperiod) {
case "1 MIN":
    basis = period != period[-1];
case "2 MIN":
    basis = period != period[-1];
case "3 MIN":
    basis = period != period[-1];
case "5 MIN":
    basis = period != period[-1];
case "10 MIN":
    basis = period != period[-1];
case "15 MIN":
    basis = period != period[-1];
case "30 MIN":
    basis = period != period[-1];
case "1 HOUR":
    basis = period != period[-1];
case "DAY":
    basis = GetDay() != GetDay()[-1];
case "WEEK":
    basis = GetWeek() != GetWeek()[-1];
case "MONTH":
    basis = GetMonth() != GetMonth()[-1];
case "YEAR":
    basis = GetYear() != GetYear()[-1];
}
input F1 = -1.00;
input F2 = -.618;
input F3 = -.27;
input F4 = 0.236;
input F5 = 0.382;
input F6 = 0.500;
input F7 = 0.618;
input F8 = 0.764;
input F9 = 1.00;
input F10 = 1.27;
input F11 = 1.618;
input F12 = 2.00;

def rz         = if period != period[1]
                 then if hbar[1] < lbar[1]
                      then high(period=aggperiod)
                      else low(period=aggperiod)
                 else rz[1];
def rplusminus = if period != period[1]
                 then if hbar[1] < lbar[1]
                      then 0
                      else 1
                 else rplusminus[1];

def Count  = CompoundValue(1, if (H + L != H[1] + L[1]) then Count[1] + 1 else Count[1], 0);

plot rzero = if limit_display and HighestAll(Count) - Count <= (display_x - 1)
             then rz
             else if !limit_display
             then rz else na;


def range  = AbsValue(H[1] - L[1]);

plot FF1   = if rplusminus == 1 then rzero + (range * F1) else rzero - (range * F1);
plot FF2   = if rplusminus == 1 then rzero + (range * F2) else rzero - (range * F2);
plot FF3   = if rplusminus == 1 then rzero + (range * F3) else rzero - (range * F3);
plot FF4   = if rplusminus == 1 then rzero + (range * F4) else rzero - (range * F4);
plot FF5   = if rplusminus == 1 then rzero + (range * F5) else rzero - (range * F5);
plot FF6   = if rplusminus == 1 then rzero + (range * F6) else rzero - (range * F6);
plot FF7   = if rplusminus == 1 then rzero + (range * F7) else rzero - (range * F7);
plot FF8   = if rplusminus == 1 then rzero + (range * F8) else rzero - (range * F8);
plot FF9   = if rplusminus == 1 then rzero + (range * F9) else rzero - (range * F9);
plot FF10  = if rplusminus == 1 then rzero + (range * F10) else rzero - (range * F10);
plot FF11  = if rplusminus == 1 then rzero + (range * F11) else rzero - (range * F11);
plot FF12  = if rplusminus == 1 then rzero + (range * F12) else rzero - (range * F12);

rzero.SetDefaultColor(Color.WHITE);
rzero.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
rzero.SetLineWeight(1);

FF1.SetDefaultColor(Color.WHITE);
FF1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF1.SetLineWeight(1);

FF2.SetDefaultColor(Color.YELLOW);
FF2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF2.SetLineWeight(1);

FF3.SetDefaultColor(Color.CYAN);
FF3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF3.SetLineWeight(1);

FF4.SetDefaultColor(Color.CYAN);
FF4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF4.SetLineWeight(1);

FF4.SetDefaultColor(Color.CYAN);
FF4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF4.SetLineWeight(1);

FF4.SetDefaultColor(Color.CYAN);
FF4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF4.SetLineWeight(1);

FF5.SetDefaultColor(Color.YELLOW);
FF5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF5.SetLineWeight(1);

FF6.SetDefaultColor(Color.WHITE);
FF6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF6.SetLineWeight(1);

FF7.SetDefaultColor(Color.YELLOW);
FF7.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF7.SetLineWeight(1);

FF8.SetDefaultColor(Color.CYAN);
FF8.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF8.SetLineWeight(1);

FF9.SetDefaultColor(Color.WHITE);
FF9.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF9.SetLineWeight(1);

FF10.SetDefaultColor(Color.CYAN);
FF10.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF10.SetLineWeight(1);

FF11.SetDefaultColor(Color.YELLOW);
FF11.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF11.SetLineWeight(1);

FF12.SetDefaultColor(Color.WHITE);
FF12.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF12.SetLineWeight(1);

AddChartBubble(if IsNaN(rzero[-1]) then Double.NaN else showbubbles and basis[bm] , rzero[bm], 0, if rplusminus[bm] == 1 then Color.GREEN else Color.RED);
AddChartBubble(if IsNaN(FF1[-1]) then Double.NaN else showbubbles and basis[bm], FF1[bm], F1, FF1.TakeValueColor());
AddChartBubble(if IsNaN(FF2[-1]) then Double.NaN else showbubbles and basis[bm], FF2[bm], F2, FF2.TakeValueColor());
AddChartBubble(if IsNaN(FF3[-1]) then Double.NaN else showbubbles and basis[bm], FF3[bm], F3, FF3.TakeValueColor());
AddChartBubble(if IsNaN(FF4[-1]) then Double.NaN else showbubbles and basis[bm], FF4[bm], F4, FF4.TakeValueColor());
AddChartBubble(if IsNaN(FF5[-1]) then Double.NaN else showbubbles and basis[bm], FF5[bm],  F5, FF5.TakeValueColor());
AddChartBubble(if IsNaN(FF6[-1]) then Double.NaN else showbubbles and basis[bm], FF6[bm], F6, FF6.TakeValueColor());
AddChartBubble(if IsNaN(FF7[-1]) then Double.NaN else showbubbles and basis[bm], FF7[bm], F7 , FF7.TakeValueColor());
AddChartBubble(if IsNaN(FF8[-1]) then Double.NaN else showbubbles and basis[bm], FF8[bm],  F8, FF8.TakeValueColor());
AddChartBubble(if IsNaN(FF9[-1]) then Double.NaN else showbubbles and basis[bm], FF9[bm],  F9, if rplusminus == 1 then Color.GREEN else Color.RED);
AddChartBubble(if IsNaN(FF10[-1]) then Double.NaN else showbubbles and basis[bm], FF10[bm],  F10, FF10.TakeValueColor());
AddChartBubble(if IsNaN(FF11[-1]) then Double.NaN else showbubbles and basis[bm], FF11[bm],  F11, FF11.TakeValueColor());
AddChartBubble(if IsNaN(FF12[-1]) then Double.NaN else showbubbles and basis[bm], FF12[bm], F12, FF12.TakeValueColor());

AddVerticalLine(if limit_display and HighestAll(Count) - Count <= (display_x - 1)  and period!=period[1] then 1 else if !limit_display and showverticals and period != period[1] then 1 else na, " ", Color.WHITE );
;

#=========================================================================

#Auto_FibPrevDayRetracements_plots_previous_day_and_fib_levels_based_thereon
input ShowTodayOnly = { default "No", "Yes"};

input displace      = 1;

def ORHa = if isnan(close) or ShowTodayOnly and !IsNaN(close(period = aggperiod)[-1]) then Double.NaN else high(period = aggperiod)[displace];
def ORLa = if isnan(close) or ShowTodayOnly and !IsNaN(close(period = aggperiod)[-1]) then Double.NaN else  low(period = aggperiod)[displace];


def hbara = if high == high(period = aggperiod) then bn else hbara[1];
def lbara = if low == low(period = aggperiod) then bn else lbara[1];


input F1a = -1.00;
input F2a = -0.62;
input F3a = -0.27;
input F4a = 0.236;
input F5a = 0.382;
input F6a = 0.500;
input F7a = 0.618;
input F8a = 0.786;
input F10a = 1.27;
input F11a = 1.618;
input F12a = 2.00;

def Counta  = CompoundValue(1, if (ORHa + ORLa != ORHa[1] + ORLa[1]) then Count[1] + 1 else Count[1], 0);

plot rzeroa = if limit_display and HighestAll(Count) - Count <= (display_x - 1)
             then rz
             else if !limit_display
             then rz else na;


plot rHia =  if limit_display and HighestAll(Count) - Count <= (display_x - 1)
            then ORHa
            else if !limit_display
            then ORHa  
            else na;
plot rLoa =   if limit_display and HighestAll(Count) - Count <= (display_x - 1)
            then ORLa
            else if !limit_display
            then ORLa  
            else na;

rHia.SetDefaultColor(Color.WHITE);
rHia.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
rHia.SetLineWeight(2);

rLoa.SetDefaultColor(Color.WHITE);
rLoa.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
rLoa.SetLineWeight(2);


def rangea  = rHia - rLoa;
plot FF1a   = rLoa + (rangea * F1a);
plot FF2a   = rLoa + (rangea * F2a);
plot FF3a   = rLoa + (rangea * F3a);
plot FF4a   = rLoa + (rangea * F4a);
plot FF5a   = rLoa + (rangea * F5a);
plot FF6a   = rLoa + (rangea * F6a);
plot FF7a   = rLoa + (rangea * F7a);
plot FF8a   = rLoa + (rangea * F8a);
plot FF10a  = rLoa + (rangea * F10a);
plot FF11a  = rLoa + (rangea * F11a);
plot FF12a  = rLoa + (rangea * F12a);

def ha = high(period = AggregationPeriod.DAY);
def la = low(period = AggregationPeriod.DAY);

FF1a.SetDefaultColor(Color.GREEN);
FF1a.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF1a.SetLineWeight(1);

FF2a.SetDefaultColor(Color.GREEN);
FF2a.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF2a.SetLineWeight(1);

FF3a.SetDefaultColor(Color.GREEN);
FF3a.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF3a.SetLineWeight(1);

FF4a.SetDefaultColor(Color.CYAN);
FF4a.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF4a.SetLineWeight(1);

FF5a.SetDefaultColor(Color.YELLOW);
FF5a.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF5a.SetLineWeight(1);

FF6a.SetDefaultColor(Color.WHITE);
FF6a.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF6a.SetLineWeight(1);

FF7a.SetDefaultColor(Color.YELLOW);
FF7a.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF7a.SetLineWeight(1);

FF8a.SetDefaultColor(Color.CYAN);
FF8a.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF8a.SetLineWeight(1);

FF10a.SetDefaultColor(Color.GREEN);
FF10a.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF10a.SetLineWeight(1);

FF11a.SetDefaultColor(Color.GREEN);
FF11a.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF11a.SetLineWeight(1);

FF12a.SetDefaultColor(Color.GREEN);
FF12a.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
FF12a.SetLineWeight(1);

def basisa;
switch (aggperiod) {
case "1 MIN":
    basisa = period != period[1];
case "2 MIN":
    basisa = period != period[1];
case "3 MIN":
    basisa = period != period[1];
case "5 MIN":
    basisa = period != period[1];
case "10 MIN":
    basisa = period != period[1];
case "15 MIN":
    basisa = period != period[1];
case "30 MIN":
    basisa = period != period[1];
case "1 HOUR":
    basisa = period != period[1];
case "DAY":
    basisa = GetDay() != GetDay()[1];
case "WEEK":
    basisa = GetWeek() != GetWeek()[1];
case "MONTH":
    basisa = GetMonth() != GetMonth()[1];
case "YEAR":
    basisa = GetYear() != GetYear()[1];
}

AddChartBubble(showbubbles and basisa, rHia, if lbara > hbara then 100 else 0, if lbara > hbara then Color.GREEN else Color.RED);
AddChartBubble(showbubbles and basisa, rLoa, if lbara > hbara then 0 else 100, if lbara > hbara then Color.RED else Color.GREEN, no);
AddChartBubble(showbubbles and basisa, FF1a, if lbara > hbara then F1a else F12a, FF1a.TakeValueColor());
AddChartBubble(showbubbles and basisa, FF2a, if lbara > hbara then F2a else F11a, FF2a.TakeValueColor());
AddChartBubble(showbubbles and basisa, FF3a, if lbara > hbara then F3a else F10a, FF3a.TakeValueColor());
AddChartBubble(showbubbles and basisa, FF4a, if lbara > hbara then F4a else F8a, FF4a.TakeValueColor());
AddChartBubble(showbubbles and basisa, FF5a, if lbara > hbara then F5a else F7a, FF5a.TakeValueColor());
AddChartBubble(showbubbles and basisa, FF6a, if lbara > hbara then F6a else F6a, FF6a.TakeValueColor());
AddChartBubble(showbubbles and basisa, FF7a, if lbara > hbara then F7a else F5a, FF7a.TakeValueColor());
AddChartBubble(showbubbles and basisa, FF8a, if lbara > hbara then F8a else F4a, FF8a.TakeValueColor());
AddChartBubble(showbubbles and basisa, FF10a, if lbara > hbara then F10a else F3a, FF10a.TakeValueColor());
AddChartBubble(showbubbles and basisa, FF11a, if lbara > hbara then F11a else F2a, FF11a.TakeValueColor());
AddChartBubble(showbubbles and basisa, FF12a, if lbara > hbara then F12a else F1a, FF12a.TakeValueColor());
;
;
 
Last edited by a moderator:
Solution
Here are 2 auto scripts (retracement and extension) that I posted in here that are modified to be in one script.

It uses the previous day for the retacement and the current day's developing candles for the extension. If the previous day's low was before the high of the day (only aggperiod tested) then the developing low of the current day would be used for the extension. If the previous day's high was first then the current day will use the high.
Hi SleepyZ, can you add the codes that shows the two red dashed lines? I copied and pasted, but it does not show the red dashed lines. Thanks,
 
Hi SleepyZ, can you add the codes that shows the two red dashed lines? I copied and pasted, but it does not show the red dashed lines. Thanks,

Those are not part of the code. The red dashed lines, shaded circles and blue dashed horizontal lines were added to the image to help confirm the coding.
 
Here are 2 auto scripts (retracement and extension) that I posted in here that are modified to be in one script.

It uses the previous day for the retacement and the current day's developing candles for the extension. If the previous day's low was before the high of the day (only aggperiod tested) then the developing low of the current day would be used for the extension. If the previous day's high was first then the current day will use the high.
Is it possible to set this to premarket hours as well?
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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