Repaints ZigZag with Auto Fibonacci Levels For ThinkOrSwim

Repaints
ZigZag Study with Auto Fibonacci Levels For ThinkOrSwim
This is a fib extension study that I posted here using a zigzag study. I have added labels for the fib extensions that are already plotted on the script.
Also, added are labels for fib retracements. It seems to be working correctly, although I have not extensively tested it.

Zig Zag Study with Auto Fibonacci Levels For ThinkOrSwim
The FB chart below shows the 2 sets of labels, a manually drawn fib retracement for testing and a green fib retracement bubble as part of the code. The fib retracements do not plot, just labels.

Rember, the underlying zigzag study can 'repaint'
Capture.jpg
Ruby:
## AO_SupplyDemandCompositeVer2_2
## START CODE
## ZigZagSign TOMO modification, v0.2 written by Linus @Thinkscripter Lounge adapted from
## Thinkorswim ZigZagSign Script
##8.24.13 Mod by Lar to add Supply/Demand Levels (Red Zones are Supply, Green are Demand), ability to enter percentage, amount or atr for reversalAmount (using the greater of the three at any reversal)
##2.20.14 Mod by Linus to hide non-active Supply/Demand Levels.
##2.20.14 Mods by Linus to remove everything but Supply/Demand levels and arrows.
##3.04.14 Mods by Linus to change Supply/Demand levels to start at arrows. (Ver2.1)
##3.12.14 Mods by Linus to fix first Supply/Demand levels to not start at Zero. (Ver2.2)
##6.4.14 Mods by Lar using some of Linus changes to allow showing just today's fibs and to show only a user selectable number of fib extension changes within the chart, along with their applicable bubbles (and b = number of spaces to move bubbles in expansion)

def   price               = close;
def   priceH              = high;    # swing high
def   priceL              = low;     # swing low
input ATRreversalfactor   = 3.0;#Hint ATRreversalfactor: 3 is standard, adjust to whatever instrument/timeframe you are trading.
input ATRlength           = 5;#Hint ATRlength: 5 is standard, adjust to whatever instrument/timeframe you are trading
input zigzagpercent       = 3.0;#LAR original is 0.2, but modified in testing for 4h charting (may modify further later)
input zigzagamount        = .15;
def ATR                   = reference ATR(length = ATRlength);
def reversalAmount        = if (close * zigzagpercent / 100) > Max
(zigzagamount < ATRreversalfactor * ATR, zigzagamount) then
(close * zigzagpercent / 100) else if zigzagamount < ATRreversalfactor * ATR then
ATRreversalfactor * ATR else zigzagamount;
input showSupplyDemand    = {Off, default Arrow, Pivot};
input showArrows          = no; #orignal by LAR was no
input useAlerts           = no; #orignal by LAR was no
 
#Original TOS ZigZag code Modified by Linus
def barNumber = BarNumber();
def barCount = HighestAll(If(IsNaN(price), 0, barNumber));

rec state = {default init, undefined, uptrend, downtrend};
rec minMaxPrice;

if (GetValue(state, 1) == GetValue(state.init, 0)) {
    minMaxPrice = price;
    state = state.undefined;
} else if (GetValue(state, 1) == GetValue(state.undefined, 0)) {
    if (price <= GetValue(minMaxPrice, 1) - reversalAmount) {
        state = state.downtrend;
        minMaxPrice = priceL;
    } else if (price >= GetValue(minMaxPrice, 1) + reversalAmount) {
        state = state.uptrend;
        minMaxPrice = priceH;
    } else {
        state = state.undefined;
        minMaxPrice = GetValue(minMaxPrice, 1);
    }
} else if (GetValue(state, 1) == GetValue(state.uptrend, 0)) {
    if (price <= GetValue(minMaxPrice, 1) - reversalAmount) {
        state = state.downtrend;
        minMaxPrice = priceL;
    } else {
        state = state.uptrend;
        minMaxPrice = Max(priceH, GetValue(minMaxPrice, 1));
    }
} else {
    if (price >= GetValue(minMaxPrice, 1) + reversalAmount) {
        state = state.uptrend;
        minMaxPrice = priceH;
    } else {
        state = state.downtrend;
        minMaxPrice = Min(priceL, GetValue(minMaxPrice, 1));
    }
}

def isCalculated = GetValue(state, 0) != GetValue(state, 1) and barNumber >= 1;
def futureDepth =  barCount - barNumber;
def tmpLastPeriodBar;
if (isCalculated) {
    if (futureDepth >= 1 and GetValue(state, 0) == GetValue(state, -1)) {
        tmpLastPeriodBar = fold lastPeriodBarI = 2 to futureDepth + 1 with
lastPeriodBarAcc = 1
            while lastPeriodBarAcc > 0
            do if (GetValue(state, 0) != GetValue(state, -lastPeriodBarI))
                then -lastPeriodBarAcc
                else lastPeriodBarAcc + 1;
    } else {
        tmpLastPeriodBar = 0;
    }
} else {
    tmpLastPeriodBar = Double.NaN;
}

def lastPeriodBar = if (!IsNaN(tmpLastPeriodBar)) then -AbsValue
(tmpLastPeriodBar) else -futureDepth;

rec currentPriceLevel;
rec currentPoints;
if (state == state.uptrend and isCalculated) {
    currentPriceLevel =
        fold barWithMaxOnPeriodI = lastPeriodBar to 1 with barWithMaxOnPeriodAcc
= minMaxPrice
            do Max(barWithMaxOnPeriodAcc, GetValue(minMaxPrice,
barWithMaxOnPeriodI));
    currentPoints =
        fold maxPointOnPeriodI = lastPeriodBar to 1 with maxPointOnPeriodAcc =
Double.NaN
            while IsNaN(maxPointOnPeriodAcc)
            do if (GetValue(priceH, maxPointOnPeriodI) == currentPriceLevel)
                then maxPointOnPeriodI
                else maxPointOnPeriodAcc;
} else if (state == state.downtrend and isCalculated) {
    currentPriceLevel =
        fold barWithMinOnPeriodI = lastPeriodBar to 1 with barWithMinOnPeriodAcc
= minMaxPrice
            do Min(barWithMinOnPeriodAcc, GetValue(minMaxPrice,
barWithMinOnPeriodI));
    currentPoints =
        fold minPointOnPeriodI = lastPeriodBar to 1 with minPointOnPeriodAcc =
Double.NaN
            while IsNaN(minPointOnPeriodAcc)
            do if (GetValue(priceL, minPointOnPeriodI) == currentPriceLevel)
                then minPointOnPeriodI
                else minPointOnPeriodAcc;
} else if (!isCalculated and (state == state.uptrend or state ==
state.downtrend)) {
    currentPriceLevel = GetValue(currentPriceLevel, 1);
    currentPoints = GetValue(currentPoints, 1) + 1;
} else {
    currentPoints = 1;
    currentPriceLevel = GetValue(price, currentPoints);
}

plot "ZZ$" = if (barNumber == barCount or barNumber == 1) then if state ==
state.uptrend then priceH else priceL else if (currentPoints == 0) then
currentPriceLevel else Double.NaN;

rec zzSave =  if !IsNaN("ZZ$") then if (barNumber == barCount or barNumber == 1)
then if IsNaN(barNumber[-1]) and  state == state.uptrend then priceH else priceL
else currentPriceLevel else GetValue(zzSave, 1);

def chg = (if barNumber == barCount and currentPoints < 0 then priceH else if
barNumber == barCount and currentPoints > 0 then priceL else currentPriceLevel)
- GetValue(zzSave, 1);

def isUp = chg >= 0;
rec isConf = AbsValue(chg) >= reversalAmount or (IsNaN(GetValue("ZZ$", 1)) and
GetValue(isConf, 1));

"ZZ$".EnableApproximation();
"ZZ$".DefineColor("Up Trend", Color.GREEN);
"ZZ$".DefineColor("Down Trend", Color.RED);
"ZZ$".DefineColor("Undefined", Color.DARK_ORANGE);
"ZZ$".AssignValueColor(if !isConf then "ZZ$".Color("Undefined") else if isUp
then "ZZ$".Color("Up Trend") else "ZZ$".Color("Down Trend"));
"ZZ$".SetLineWeight(2);
DefineGlobalColor("Unconfirmed", Color.DARK_ORANGE);
DefineGlobalColor("Up", Color.GREEN);
DefineGlobalColor("Down", Color.RED);

#Showlabel for Confirmed/Unconfirmed Status of Current Zigzag
input show_unconfirmed_label = no;
AddLabel(show_unconfirmed_label and barNumber != 1, (if isConf then "Confirmed " else "Unconfirmed ") + "ZigZag: " + chg, if !isConf then GlobalColor("Unconfirmed") else if isUp then GlobalColor("Up") else GlobalColor("Down"));
 
#Arrows

def zzL = if !IsNaN("ZZ$") and state == state.downtrend then priceL else
GetValue(zzL, 1);

def zzH = if !IsNaN("ZZ$") and state == state.uptrend then priceH else GetValue
(zzH, 1);

def dir = CompoundValue(1, if zzL != zzL[1] then 1 else if zzH != zzH[1] then -1
else dir[1], 0);

def signal = CompoundValue(1,
    if dir > 0 and low > zzL then
        if signal[1] <= 0 then 1 else signal[1]
    else if dir < 0 and high < zzH then
        if signal[1] >= 0 then -1 else signal[1]
    else signal[1]
, 0);

plot U1 = showArrows and signal > 0 and signal[1] <= 0;
U1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
U1.SetDefaultColor(Color.GREEN);
U1.SetLineWeight(4);

plot D1 = showArrows and signal < 0 and signal[1] >= 0;
D1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
D1.SetDefaultColor(Color.RED);
D1.SetLineWeight(4);

Alert(useAlerts and U1, "ZIG-UP", Alert.BAR, Sound.Bell);
Alert(useAlerts and D1, "ZAG-DOWN", Alert.BAR, Sound.Chimes);

#Supply Demand Areas
def idx = if showSupplyDemand == showSupplyDemand.Pivot then 1 else 0;
def rLow;
def rHigh;

if BarNumber() == 1 {
    rLow = Double.NaN;
    rHigh = Double.NaN;
} else if signal crosses 0 {
    rLow = low[idx];
    rHigh = high[idx];
} else {
    rLow = rLow[1];
    rHigh = rHigh[1];
}

plot HighLine = if showSupplyDemand and !IsNaN(close) then rHigh else
Double.NaN;
HighLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
HighLine.AssignValueColor(if signal > 0 then Color.GREEN else Color.RED);
HighLine.HideBubble();

plot LowLine = if showSupplyDemand and !IsNaN(close) then rLow else Double.NaN;
LowLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LowLine.AssignValueColor(if signal > 0 then Color.GREEN else Color.RED);
LowLine.HideBubble();

def hlUp = if signal > 0 then HighLine else Double.NaN;
def hlDn = if signal < 0 then HighLine else Double.NaN;

AddCloud(hlUp, LowLine, Color.GREEN, Color.GREEN);
AddCloud(hlDn, LowLine, Color.RED, Color.RED);

#Store Previous Data
def zzsave1 = if !IsNaN(zzSave) then zzSave else zzsave1[1];
def zzsave2 = zzsave1;
rec priorzz1 = if zzsave2  != zzsave2[1]  then zzsave2[1]  else priorzz1[1];
rec priorzz2 = if priorzz1 != priorzz1[1] then priorzz1[1] else priorzz2[1];
rec priorzz3 = if priorzz2 != priorzz2[1] then priorzz2[1] else priorzz3[1];
rec priorzz4 = if priorzz3 != priorzz3[1] then priorzz3[1] else priorzz4[1];
rec priorzz5 = if priorzz4 != priorzz4[1] then priorzz4[1] else priorzz5[1];
rec priorzz6 = if priorzz5 != priorzz5[1] then priorzz5[1] else priorzz6[1];

rec data = CompoundValue(1, if (zzSave == priceH or zzSave == priceL) then data[1] + 1 else data[1], 0);
def datacount = (HighestAll(data) - data[1]);
input numberextfibstoshow = 2;
input showFibExtLines = yes;
input showtodayonly = no;
def today = if showtodayonly == yes then GetDay() == GetLastDay() else GetDay();
def extfib1 = if zzSave == priceH then high - AbsValue(priorzz2 - priorzz1) * 1
else extfib1[1];
plot extfib100 = if datacount <= numberextfibstoshow and today and showFibExtLines and currentPoints != 0 and !IsNaN(extfib1) and dir < 0 then extfib1[1] else Double.NaN;
extfib100.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib100.SetDefaultColor(Color.RED);
extfib100.SetLineWeight(1);
extfib100.HideBubble();
def extfib2 = if zzSave == priceH then high - AbsValue(priorzz2 - priorzz1) *
0.618 else extfib2[1];
plot extfib618 = if datacount <= numberextfibstoshow and today and showFibExtLines and currentPoints != 0 and !IsNaN(extfib2) and dir < 0 then extfib2[1] else Double.NaN;
extfib618.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib618.SetDefaultColor(Color.RED);
extfib618.SetLineWeight(1);
extfib618.HideBubble();
def extfib3 = if zzSave == priceH then high - AbsValue(priorzz2 - priorzz1) *
1.618 else extfib3[1];
plot extfib1618 = if datacount <= numberextfibstoshow and today and showFibExtLines and currentPoints != 0 and !IsNaN(extfib3) and dir < 0 then extfib3[1] else Double.NaN;
extfib1618.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib1618.SetDefaultColor(Color.RED);
extfib1618.SetLineWeight(1);
extfib1618.HideBubble();
def extfib4 = if zzSave == priceH then high - AbsValue(priorzz2 - priorzz1) *
2.618 else extfib4[1];
plot extfib2618 = if  datacount <= numberextfibstoshow and today and showFibExtLines and currentPoints != 0 and !IsNaN(extfib4) and dir < 0 then extfib4[1] else Double.NaN;
extfib2618.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib2618.SetDefaultColor(Color.RED);
extfib2618.SetLineWeight(1);
extfib2618.HideBubble();
def extfib1_ = if zzSave == priceL then low + AbsValue(priorzz2 - priorzz1) * 1
else extfib1_[1];
plot extfib100_ = if datacount <= numberextfibstoshow and today and showFibExtLines and currentPoints != 0 and !IsNaN(extfib1_) and dir > 0 then extfib1_[1] else Double.NaN;
extfib100_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib100_.SetDefaultColor(Color.GREEN);
extfib100_.SetLineWeight(1);
extfib100_.HideBubble();
def extfib2_ = if zzSave == priceL then low + AbsValue(priorzz2 - priorzz1) *
0.618 else extfib2_[1];
plot extfib618_ = if datacount <= numberextfibstoshow and today and showFibExtLines and currentPoints != 0 and !IsNaN(extfib2_) and dir > 0 then extfib2_[1] else Double.NaN;
extfib618_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib618_.SetDefaultColor(Color.GREEN);
extfib618_.SetLineWeight(1);
extfib618_.HideBubble();
def extfib3_ = if zzSave == priceL then low + AbsValue(priorzz2 - priorzz1) *
1.618 else extfib3_[1];
plot extfib1618_ = if datacount <= numberextfibstoshow and today and showFibExtLines and currentPoints != 0 and !IsNaN(extfib3_) and dir > 0 then extfib3_[1] else Double.NaN;
extfib1618_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib1618_.SetDefaultColor(Color.GREEN);
extfib1618_.SetLineWeight(1);
extfib1618_.HideBubble();
def extfib4_ = if zzSave == priceL then low + AbsValue(priorzz2 - priorzz1) *
2.618 else extfib4_[1];
plot extfib2618_ = if datacount <= numberextfibstoshow and today and showFibExtLines and currentPoints != 0 and !IsNaN(extfib4_) and dir > 0 then extfib4_[1]  else Double.NaN;
extfib2618_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib2618_.SetDefaultColor(Color.GREEN);
extfib2618_.SetLineWeight(1);
extfib2618_.HideBubble();


input b = 2;
def direction = if !isUp then 1 else 0;

AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib1[b + 2], "100%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib2[b + 2], "61.8%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib3[b + 2], "161.8%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib4[b + 2], "261.8%", Color.RED, no);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib1_[b + 2], "100%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib2_[b + 2], "61.8%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib3_[b + 2], "161.8%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib4_[b + 2], "261.8%", Color.GREEN, yes);


input showlabel_fibext = yes;
AddLabel(showlabel_fibext, "Fib Ext ", Color.YELLOW);
AddLabel( direction == 1 and showlabel_fibext , "0% " + AsText(priorzz1),  Color.RED);
AddLabel( direction == 1 and showlabel_fibext , "61.8% " + AsText(extfib2),  Color.RED);
AddLabel( direction == 1 and showlabel_fibext , "100% " + AsText(extfib1), Color.RED);
AddLabel( direction == 1 and showlabel_fibext , "161.8% " + AsText(extfib3) ,  Color.RED);
AddLabel( direction == 1 and showlabel_fibext , "261.8% " + AsText(extfib4),  Color.RED);
AddLabel( direction == 0 and showlabel_fibext , "0% " + AsText(priorzz1),  Color.GREEN);
AddLabel( direction == 0 and showlabel_fibext , "61.8% " + AsText(extfib2_[1]),  Color.GREEN);
AddLabel( direction == 0 and showlabel_fibext , "100% " + AsText(extfib1_[1]), Color.GREEN);
AddLabel( direction == 0 and showlabel_fibext , "161.8% " + AsText(extfib3_[1]) ,  Color.GREEN);
AddLabel( direction == 0 and showlabel_fibext , "261.8% " + AsText(extfib4_[1]),  Color.GREEN);

def fib_range = AbsValue(priorzz2 - priorzz1) ;
def fib1_0 = priorzz1 + fib_range * .236;
def fib2_0 = priorzz1 + fib_range * .382;
def fib3_0 = priorzz1 + fib_range * .500;
def fib4_0 = priorzz1 + fib_range * .618;
def fib5_0 = priorzz1 + fib_range * .764;
def fib6_0 = priorzz1;
def fib1_1 = priorzz1 - fib_range * .236;
def fib2_1 = priorzz1 - fib_range * .382;
def fib3_1 = priorzz1 - fib_range * .500;
def fib4_1 = priorzz1 - fib_range * .618;
def fib5_1 = priorzz1 - fib_range * .764;
def fib6_1 = priorzz1;

input showlabel_fibretrace = yes;
AddLabel(showlabel_fibretrace, "Fib Retrace ", Color.YELLOW);
AddLabel( direction == 0 and showlabel_fibretrace , "0% " + AsText(fib6_0),  Color.GREEN);
AddLabel( direction == 0 and showlabel_fibretrace , "23.6% " + AsText(fib1_0),  Color.GREEN);
AddLabel( direction == 0 and showlabel_fibretrace , "38.2% " + AsText(fib2_0),  Color.GREEN);
AddLabel( direction == 0 and showlabel_fibretrace , "50% " + AsText(fib3_0),  Color.GREEN);
AddLabel( direction == 0 and showlabel_fibretrace , "61.8% " + AsText(fib4_0),  Color.GREEN);
AddLabel( direction == 0 and showlabel_fibretrace , "76.4 " + AsText(fib5_0),  Color.GREEN);
AddLabel( direction == 1 and showlabel_fibretrace , "0% " + AsText(fib6_1),  Color.RED);
AddLabel( direction == 1 and showlabel_fibretrace , "23.6% " + AsText(fib1_1),  Color.RED);
AddLabel( direction == 1 and showlabel_fibretrace , "38.2% " + AsText(fib2_1),  Color.RED);
AddLabel( direction == 1 and showlabel_fibretrace , "50% " + AsText(fib3_1),  Color.RED);
AddLabel( direction == 1 and showlabel_fibretrace , "61.8% " + AsText(fib4_1),  Color.RED);
AddLabel( direction == 1 and showlabel_fibretrace , "76.4 " + AsText(fib5_1),  Color.RED);
 
Last edited by a moderator:

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

evanevans

Active member
I'm wondering if someone would be interested in creating/coding a ZigZag/Fib Retracement % indicator?

The premise is this:

A market, stock, for a particular period is "choppy" (ranging/non-trending) if rally retracements exceed a % such as 61.8% (or 50%). The same is considered trending if retracements are short of the same or another % (ie: 49%).

If we can plot those %'s we should be able to get a clear image of trending vs non-trending moments.

It's a starting place at least. I'm sure we get more fancy with it, or if you have any ideas let's try them.

Looking forward to seeing how this comes out!

@Welkin interested? :)
 
I can't seem to find this indicator which I think exists here somewhere. Basically it draws the fib retracement levels for a current trend, the detection and size of which you can define by the zig zag indicator.
 
I can't seem to find this indicator which I think exists here somewhere. Basically it draws the fib retracement levels for a current trend, the detection and size of which you can define by the zig zag indicator.
Did you manage to figure this one out? This would be a good indicator for any trader to have at their disposal .
 
@Lysergic
This uses TOS's zigzaghighlow indicator, which has inputs to adjust the pivot criteria. There are fib inputs to limit their plot or plot on the whole chart, whether to show bubbles and whether to show fib price in bubbles.

Please remember the zigzaghighlow indicator repaints and thus a pivot can disappear before a pivot in the opposite direction occurs.

Capture.jpg
Ruby:
#
# TD Ameritrade IP Company, Inc. (c) 2013-2021
#

input priceH = high;
input priceL = low;
input percentageReversal = 3.0;
input absoluteReversal = 0.0;
input atrLength = 5;
input atrReversal = 1.5;

Assert(percentageReversal >= 0, "'percentage reversal' must not be negative: " + percentageReversal);
Assert(absoluteReversal >= 0, "'absolute reversal' must not be negative: " + absoluteReversal);
Assert(atrReversal >= 0, "'atr reversal' must not be negative: " + atrReversal);
Assert(percentageReversal != 0 or absoluteReversal != 0 or atrReversal != 0, "Either 'percentage reversal' or 'absolute reversal' or 'atr reversal' must not be zero");

def hlPivot;
if (atrReversal != 0) {
    hlPivot = percentageReversal / 100 + WildersAverage(TrueRange(high, close, low), atrLength) / close * atrReversal;
} else {
    hlPivot = percentageReversal / 100;
}
def state = {default init, undefined, uptrend, downtrend};
def maxPriceH;
def minPriceL;
def newMax;
def newMin;
def prevMaxH = GetValue(maxPriceH, 1);
def prevMinL = GetValue(minPriceL, 1);

if GetValue(state, 1) == GetValue(state.init, 0) {
    maxPriceH = priceH;
    minPriceL = priceL;
    newMax = yes;
    newMin = yes;
    state = state.undefined;
} else if GetValue(state, 1) == GetValue(state.undefined, 0) {
    if priceH >= prevMaxH {
        state = state.uptrend;
        maxPriceH = priceH;
        minPriceL = prevMinL;
        newMax = yes;
        newMin = no;
    } else if priceL <= prevMinL {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        minPriceL = priceL;
        newMax = no;
        newMin = yes;
    } else {
        state = state.undefined;
        maxPriceH = prevMaxH;
        minPriceL = prevMinL;
        newMax = no;
        newMin = no;
    }
} else if GetValue(state, 1) == GetValue(state.uptrend, 0) {
    if priceL <= prevMaxH - prevMaxH * hlPivot - absoluteReversal {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        minPriceL = priceL;
        newMax = no;
        newMin = yes;
    } else {
        state = state.uptrend;
        if (priceH >= prevMaxH) {
            maxPriceH = priceH;
            newMax = yes;
        } else {
            maxPriceH = prevMaxH;
            newMax = no;
        }
        minPriceL = prevMinL;
        newMin = no;
    }
} else {
    if priceH >= prevMinL + prevMinL * hlPivot + absoluteReversal {
        state = state.uptrend;
        maxPriceH = priceH;
        minPriceL = prevMinL;
        newMax = yes;
        newMin = no;
    } else {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        newMax = no;
        if (priceL <= prevMinL) {
            minPriceL = priceL;
            newMin = yes;
        } else {
            minPriceL = prevMinL;
            newMin = no;
        }
    }
}

def barNumber = BarNumber();
def barCount = HighestAll(If(IsNaN(priceH), 0, barNumber));
def newState = GetValue(state, 0) != GetValue(state, 1);
def offset = barCount - barNumber + 1;
def highPoint = state == state.uptrend and priceH == maxPriceH;
def lowPoint = state == state.downtrend and priceL == minPriceL;

def lastH;
if highPoint and offset > 1 {
    lastH = fold iH = 1 to offset with tH = priceH while !IsNaN(tH) and !GetValue(newState, -iH) do if GetValue(newMax, -iH) or iH == offset - 1 and GetValue(priceH, -iH) == tH then Double.NaN else tH;
} else {
    lastH = Double.NaN;
}

def lastL;
if lowPoint and offset > 1 {
    lastL = fold iL = 1 to offset with tL = priceL while !IsNaN(tL) and !GetValue(newState, -iL) do if GetValue(newMin, -iL) or iL == offset - 1 and GetValue(priceL, -iL) == tL then Double.NaN else tL;
} else {
    lastL = Double.NaN;
}

plot ZZ;
if barNumber == 1 {
    ZZ = fold iF = 1 to offset with tP = Double.NaN while IsNaN(tP) do if GetValue(state, -iF) == GetValue(state.uptrend, 0) then priceL else if GetValue(state, -iF) == GetValue(state.downtrend, 0) then priceH else Double.NaN;
} else if barNumber == barCount {
    ZZ = if highPoint or state == state.downtrend and priceL > minPriceL then priceH else if lowPoint or state == state.uptrend and priceH < maxPriceH then priceL else Double.NaN;
} else {
    ZZ = if !IsNaN(lastH) then lastH else if !IsNaN(lastL) then lastL else Double.NaN;
}
ZZ.SetDefaultColor(GetColor(1));
ZZ.EnableApproximation();


#-----------------------
input limit_plot =  yes;
def fibh   = HighestAll(if IsNaN(close[-1]) then maxPriceH else Double.NaN);
def fibl   = HighestAll(if IsNaN(close[-1]) then minPriceL else Double.NaN);

def fibhbn  = CompoundValue(1, HighestAll(if high == fibh then BarNumber() else Double.NaN), fibh);
def fiblbn  = CompoundValue(1, HighestAll(if low  == fibl then BarNumber() else Double.NaN), fibl);

def range = fibh - fibl;
def f1 = range * .236;
def f2 = range * .382;
def f3 = range * .500;
def f4 = range * .618;
def f5 = range * .764;

plot fibhh = if limit_plot and BarNumber() < Min(fibhbn, fiblbn) then Double.NaN else fibh;
fibhh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fibhh.SetDefaultColor(Color.RED);

plot fibll = if limit_plot and BarNumber() < Min(fibhbn, fiblbn) then Double.NaN else fibl;
fibll.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fibll.SetDefaultColor(Color.GREEN);

plot fib1 = if limit_plot and BarNumber() < Min(fibhbn, fiblbn) then Double.NaN else HighestAll( if IsNaN(close[-1]) then minPriceL + f1 else Double.NaN);
fib1.SetPaintingStrategy(PaintingStrategy.DASHES);
fib1.SetLineWeight(2);
fib1.SetDefaultColor(Color.YELLOW);

plot fib2 = if limit_plot and BarNumber() < Min(fibhbn, fiblbn) then Double.NaN else HighestAll( if IsNaN(close[-1]) then minPriceL + f2 else Double.NaN);
fib2.SetPaintingStrategy(PaintingStrategy.DASHES);
fib2.SetLineWeight(2);
fib2.SetDefaultColor(Color.CYAN);

plot fib3 = if limit_plot and BarNumber() < Min(fibhbn, fiblbn) then Double.NaN else HighestAll(if IsNaN(close[-1]) then minPriceL + f3 else Double.NaN);
fib3.SetPaintingStrategy(PaintingStrategy.DASHES);
fib3.SetDefaultColor(Color.WHITE);
fib3.SetLineWeight(2);

plot fib4 = if limit_plot and BarNumber() < Min(fibhbn, fiblbn) then Double.NaN else HighestAll( if IsNaN(close[-1]) then minPriceL + f4 else Double.NaN);
fib4.SetPaintingStrategy(PaintingStrategy.DASHES);
fib4.SetLineWeight(2);
fib4.SetDefaultColor(Color.CYAN);

plot fib5 = if limit_plot and BarNumber() < Min(fibhbn, fiblbn) then Double.NaN else HighestAll( if IsNaN(close[-1]) then minPriceL + f5 else Double.NaN);
fib5.SetPaintingStrategy(PaintingStrategy.DASHES);
fib5.SetLineWeight(2);
fib5.SetDefaultColor(Color.YELLOW);

#Fib Bubbles--------------------------------------------
input bubblemover  = 8;
def b              = bubblemover;
def b1             = b + 1;
input showbubbles  = yes;
input bubble_price = yes;
AddChartBubble(showbubbles and IsNaN(close[b]) and !IsNaN(close[b1]), fibh[b],
               (if fibhbn[b1] > fiblbn[b1]
               then AsPercent(0)
               else AsPercent(1)) + "\n" + (if !bubble_price then "" else AsText(fibh[b1])),
               if fibhbn[b1] < fiblbn[b1] then Color.GREEN else Color.RED);
AddChartBubble(showbubbles and IsNaN(close[b]) and !IsNaN(close[b1]), fibl[b],
               (if fibhbn[b1] > fiblbn[b1]
               then AsPercent(1)
               else AsPercent(0)) + "\n" + (if !bubble_price then "" else AsText(fibl[b1])),
               if fibhbn[b1] < fiblbn[b1] then Color.GREEN else Color.RED);
AddChartBubble(showbubbles and IsNaN(close[b]) and !IsNaN(close[b1]), fib1[b],
               (if fibhbn[b1] > fiblbn[b1]
               then AsPercent(.764)
               else AsPercent(.236)) + "\n" + (if !bubble_price then "" else AsText(fib1[b1])),
               if fibhbn[b1] < fiblbn[b1] then Color.GREEN else Color.RED);
AddChartBubble(IsNaN(close[b]) and !IsNaN(close[b1]), fib2[b],
               (if fibhbn[b1] > fiblbn[b1]
               then AsPercent(.618)
               else AsPercent(.382)) + "\n" + (if !bubble_price then "" else AsText(fib2[b1])),
               if fibhbn[b1] < fiblbn[b1] then Color.GREEN else Color.RED);
AddChartBubble(IsNaN(close[b]) and !IsNaN(close[b1]), fib3[b],
               AsPercent(.500) + "\n" + (if !bubble_price then "" else AsText(fib3[b1])),
               if fibhbn[b1] < fiblbn[b1] then Color.GREEN else Color.RED);
AddChartBubble(IsNaN(close[b]) and !IsNaN(close[b1]), fib4[b],
               (if fibhbn[b1] > fiblbn[b1]
               then AsPercent(.382)
               else AsPercent(.618)) + "\n" + (if !bubble_price then "" else AsText(fib4[b1])),
               if fibhbn[b1] < fiblbn[b1] then Color.GREEN else Color.RED);
AddChartBubble(IsNaN(close[b]) and !IsNaN(close[b1]), fib5[b],
               (if fibhbn[b1] > fiblbn[b1]
               then AsPercent(.236)
               else AsPercent(.764)) + "\n" + (if !bubble_price then "" else AsText(fib5[b1])),
               if fibhbn[b1] < fiblbn[b1] then Color.GREEN else Color.RED);

fibhh.HideBubble();
fibll.HideBubble();
fib1.hidebubble();
fib2.hidebubble();
fib3.hidebubble();
fib4.hidebubble();
fib5.hidebubble();
 
I did a simple copy of the code into the scanner editor and got this message at the bottom: com.devexperts.tos.thinkscript.runtime.TooComplexException: The complexity of the expression suggests that it may not be reliable with real-time data.
Any thoughts or ideas why this didn't work?
I know enough to be dangerous around TOS.
 
What I'm trying to accomplish is to scan for a security that has retraced a variable percentage of the last zigzag swing.
 
What I'm trying to accomplish is to scan for a security that has retraced a variable percentage of the last zigzag swing.
did create a scanner out of a Zig-Zag study once, I will look for that code and try and post for you. Would I be willing to do again for this stud? probably not.

I can guide you, if you want to attempt.

To begin with most of the plots need to converted to def and get rid of the plot styles and all. Once you have that you have enough info in. the variables define condition they way you want and write/plot scanner code.

Can help if you are getting started.
 
ZigZag Study with Auto Fibonacci Levels For ThinkOrSwim
This is a fib extension study that I posted here using a zigzag study. I have added labels for the fib extensions that are already plotted on the script.
Also, added are labels for fib retracements. It seems to be working correctly, although I have not extensively tested it.

Zig Zag Study with Auto Fibonacci Levels For ThinkOrSwim
The FB chart below shows the 2 sets of labels, a manually drawn fib retracement for testing and a green fib retracement bubble as part of the code. The fib retracements do not plot, just labels.

Rember, the underlying zigzag study can 'repaint'
Capture.jpg
Ruby:
## AO_SupplyDemandCompositeVer2_2
## START CODE
## ZigZagSign TOMO modification, v0.2 written by Linus @Thinkscripter Lounge adapted from
## Thinkorswim ZigZagSign Script
##8.24.13 Mod by Lar to add Supply/Demand Levels (Red Zones are Supply, Green are Demand), ability to enter percentage, amount or atr for reversalAmount (using the greater of the three at any reversal)
##2.20.14 Mod by Linus to hide non-active Supply/Demand Levels.
##2.20.14 Mods by Linus to remove everything but Supply/Demand levels and arrows.
##3.04.14 Mods by Linus to change Supply/Demand levels to start at arrows. (Ver2.1)
##3.12.14 Mods by Linus to fix first Supply/Demand levels to not start at Zero. (Ver2.2)
##6.4.14 Mods by Lar using some of Linus changes to allow showing just today's fibs and to show only a user selectable number of fib extension changes within the chart, along with their applicable bubbles (and b = number of spaces to move bubbles in expansion)

def   price               = close;
def   priceH              = high;    # swing high
def   priceL              = low;     # swing low
input ATRreversalfactor   = 3.0;#Hint ATRreversalfactor: 3 is standard, adjust to whatever instrument/timeframe you are trading.
input ATRlength           = 5;#Hint ATRlength: 5 is standard, adjust to whatever instrument/timeframe you are trading
input zigzagpercent       = 3.0;#LAR original is 0.2, but modified in testing for 4h charting (may modify further later)
input zigzagamount        = .15;
def ATR                   = reference ATR(length = ATRlength);
def reversalAmount        = if (close * zigzagpercent / 100) > Max
(zigzagamount < ATRreversalfactor * ATR, zigzagamount) then
(close * zigzagpercent / 100) else if zigzagamount < ATRreversalfactor * ATR then
ATRreversalfactor * ATR else zigzagamount;
input showSupplyDemand    = {Off, default Arrow, Pivot};
input showArrows          = no; #orignal by LAR was no
input useAlerts           = no; #orignal by LAR was no
 
#Original TOS ZigZag code Modified by Linus
def barNumber = BarNumber();
def barCount = HighestAll(If(IsNaN(price), 0, barNumber));

rec state = {default init, undefined, uptrend, downtrend};
rec minMaxPrice;

if (GetValue(state, 1) == GetValue(state.init, 0)) {
    minMaxPrice = price;
    state = state.undefined;
} else if (GetValue(state, 1) == GetValue(state.undefined, 0)) {
    if (price <= GetValue(minMaxPrice, 1) - reversalAmount) {
        state = state.downtrend;
        minMaxPrice = priceL;
    } else if (price >= GetValue(minMaxPrice, 1) + reversalAmount) {
        state = state.uptrend;
        minMaxPrice = priceH;
    } else {
        state = state.undefined;
        minMaxPrice = GetValue(minMaxPrice, 1);
    }
} else if (GetValue(state, 1) == GetValue(state.uptrend, 0)) {
    if (price <= GetValue(minMaxPrice, 1) - reversalAmount) {
        state = state.downtrend;
        minMaxPrice = priceL;
    } else {
        state = state.uptrend;
        minMaxPrice = Max(priceH, GetValue(minMaxPrice, 1));
    }
} else {
    if (price >= GetValue(minMaxPrice, 1) + reversalAmount) {
        state = state.uptrend;
        minMaxPrice = priceH;
    } else {
        state = state.downtrend;
        minMaxPrice = Min(priceL, GetValue(minMaxPrice, 1));
    }
}

def isCalculated = GetValue(state, 0) != GetValue(state, 1) and barNumber >= 1;
def futureDepth =  barCount - barNumber;
def tmpLastPeriodBar;
if (isCalculated) {
    if (futureDepth >= 1 and GetValue(state, 0) == GetValue(state, -1)) {
        tmpLastPeriodBar = fold lastPeriodBarI = 2 to futureDepth + 1 with
lastPeriodBarAcc = 1
            while lastPeriodBarAcc > 0
            do if (GetValue(state, 0) != GetValue(state, -lastPeriodBarI))
                then -lastPeriodBarAcc
                else lastPeriodBarAcc + 1;
    } else {
        tmpLastPeriodBar = 0;
    }
} else {
    tmpLastPeriodBar = Double.NaN;
}

def lastPeriodBar = if (!IsNaN(tmpLastPeriodBar)) then -AbsValue
(tmpLastPeriodBar) else -futureDepth;

rec currentPriceLevel;
rec currentPoints;
if (state == state.uptrend and isCalculated) {
    currentPriceLevel =
        fold barWithMaxOnPeriodI = lastPeriodBar to 1 with barWithMaxOnPeriodAcc
= minMaxPrice
            do Max(barWithMaxOnPeriodAcc, GetValue(minMaxPrice,
barWithMaxOnPeriodI));
    currentPoints =
        fold maxPointOnPeriodI = lastPeriodBar to 1 with maxPointOnPeriodAcc =
Double.NaN
            while IsNaN(maxPointOnPeriodAcc)
            do if (GetValue(priceH, maxPointOnPeriodI) == currentPriceLevel)
                then maxPointOnPeriodI
                else maxPointOnPeriodAcc;
} else if (state == state.downtrend and isCalculated) {
    currentPriceLevel =
        fold barWithMinOnPeriodI = lastPeriodBar to 1 with barWithMinOnPeriodAcc
= minMaxPrice
            do Min(barWithMinOnPeriodAcc, GetValue(minMaxPrice,
barWithMinOnPeriodI));
    currentPoints =
        fold minPointOnPeriodI = lastPeriodBar to 1 with minPointOnPeriodAcc =
Double.NaN
            while IsNaN(minPointOnPeriodAcc)
            do if (GetValue(priceL, minPointOnPeriodI) == currentPriceLevel)
                then minPointOnPeriodI
                else minPointOnPeriodAcc;
} else if (!isCalculated and (state == state.uptrend or state ==
state.downtrend)) {
    currentPriceLevel = GetValue(currentPriceLevel, 1);
    currentPoints = GetValue(currentPoints, 1) + 1;
} else {
    currentPoints = 1;
    currentPriceLevel = GetValue(price, currentPoints);
}

plot "ZZ$" = if (barNumber == barCount or barNumber == 1) then if state ==
state.uptrend then priceH else priceL else if (currentPoints == 0) then
currentPriceLevel else Double.NaN;

rec zzSave =  if !IsNaN("ZZ$") then if (barNumber == barCount or barNumber == 1)
then if IsNaN(barNumber[-1]) and  state == state.uptrend then priceH else priceL
else currentPriceLevel else GetValue(zzSave, 1);

def chg = (if barNumber == barCount and currentPoints < 0 then priceH else if
barNumber == barCount and currentPoints > 0 then priceL else currentPriceLevel)
- GetValue(zzSave, 1);

def isUp = chg >= 0;
rec isConf = AbsValue(chg) >= reversalAmount or (IsNaN(GetValue("ZZ$", 1)) and
GetValue(isConf, 1));

"ZZ$".EnableApproximation();
"ZZ$".DefineColor("Up Trend", Color.GREEN);
"ZZ$".DefineColor("Down Trend", Color.RED);
"ZZ$".DefineColor("Undefined", Color.DARK_ORANGE);
"ZZ$".AssignValueColor(if !isConf then "ZZ$".Color("Undefined") else if isUp
then "ZZ$".Color("Up Trend") else "ZZ$".Color("Down Trend"));
"ZZ$".SetLineWeight(2);
DefineGlobalColor("Unconfirmed", Color.DARK_ORANGE);
DefineGlobalColor("Up", Color.GREEN);
DefineGlobalColor("Down", Color.RED);

#Showlabel for Confirmed/Unconfirmed Status of Current Zigzag
input show_unconfirmed_label = no;
AddLabel(show_unconfirmed_label and barNumber != 1, (if isConf then "Confirmed " else "Unconfirmed ") + "ZigZag: " + chg, if !isConf then GlobalColor("Unconfirmed") else if isUp then GlobalColor("Up") else GlobalColor("Down"));
 
#Arrows

def zzL = if !IsNaN("ZZ$") and state == state.downtrend then priceL else
GetValue(zzL, 1);

def zzH = if !IsNaN("ZZ$") and state == state.uptrend then priceH else GetValue
(zzH, 1);

def dir = CompoundValue(1, if zzL != zzL[1] then 1 else if zzH != zzH[1] then -1
else dir[1], 0);

def signal = CompoundValue(1,
    if dir > 0 and low > zzL then
        if signal[1] <= 0 then 1 else signal[1]
    else if dir < 0 and high < zzH then
        if signal[1] >= 0 then -1 else signal[1]
    else signal[1]
, 0);

plot U1 = showArrows and signal > 0 and signal[1] <= 0;
U1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
U1.SetDefaultColor(Color.GREEN);
U1.SetLineWeight(4);

plot D1 = showArrows and signal < 0 and signal[1] >= 0;
D1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
D1.SetDefaultColor(Color.RED);
D1.SetLineWeight(4);

Alert(useAlerts and U1, "ZIG-UP", Alert.BAR, Sound.Bell);
Alert(useAlerts and D1, "ZAG-DOWN", Alert.BAR, Sound.Chimes);

#Supply Demand Areas
def idx = if showSupplyDemand == showSupplyDemand.Pivot then 1 else 0;
def rLow;
def rHigh;

if BarNumber() == 1 {
    rLow = Double.NaN;
    rHigh = Double.NaN;
} else if signal crosses 0 {
    rLow = low[idx];
    rHigh = high[idx];
} else {
    rLow = rLow[1];
    rHigh = rHigh[1];
}

plot HighLine = if showSupplyDemand and !IsNaN(close) then rHigh else
Double.NaN;
HighLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
HighLine.AssignValueColor(if signal > 0 then Color.GREEN else Color.RED);
HighLine.HideBubble();

plot LowLine = if showSupplyDemand and !IsNaN(close) then rLow else Double.NaN;
LowLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LowLine.AssignValueColor(if signal > 0 then Color.GREEN else Color.RED);
LowLine.HideBubble();

def hlUp = if signal > 0 then HighLine else Double.NaN;
def hlDn = if signal < 0 then HighLine else Double.NaN;

AddCloud(hlUp, LowLine, Color.GREEN, Color.GREEN);
AddCloud(hlDn, LowLine, Color.RED, Color.RED);

#Store Previous Data
def zzsave1 = if !IsNaN(zzSave) then zzSave else zzsave1[1];
def zzsave2 = zzsave1;
rec priorzz1 = if zzsave2  != zzsave2[1]  then zzsave2[1]  else priorzz1[1];
rec priorzz2 = if priorzz1 != priorzz1[1] then priorzz1[1] else priorzz2[1];
rec priorzz3 = if priorzz2 != priorzz2[1] then priorzz2[1] else priorzz3[1];
rec priorzz4 = if priorzz3 != priorzz3[1] then priorzz3[1] else priorzz4[1];
rec priorzz5 = if priorzz4 != priorzz4[1] then priorzz4[1] else priorzz5[1];
rec priorzz6 = if priorzz5 != priorzz5[1] then priorzz5[1] else priorzz6[1];

rec data = CompoundValue(1, if (zzSave == priceH or zzSave == priceL) then data[1] + 1 else data[1], 0);
def datacount = (HighestAll(data) - data[1]);
input numberextfibstoshow = 2;
input showFibExtLines = yes;
input showtodayonly = no;
def today = if showtodayonly == yes then GetDay() == GetLastDay() else GetDay();
def extfib1 = if zzSave == priceH then high - AbsValue(priorzz2 - priorzz1) * 1
else extfib1[1];
plot extfib100 = if datacount <= numberextfibstoshow and today and showFibExtLines and currentPoints != 0 and !IsNaN(extfib1) and dir < 0 then extfib1[1] else Double.NaN;
extfib100.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib100.SetDefaultColor(Color.RED);
extfib100.SetLineWeight(1);
extfib100.HideBubble();
def extfib2 = if zzSave == priceH then high - AbsValue(priorzz2 - priorzz1) *
0.618 else extfib2[1];
plot extfib618 = if datacount <= numberextfibstoshow and today and showFibExtLines and currentPoints != 0 and !IsNaN(extfib2) and dir < 0 then extfib2[1] else Double.NaN;
extfib618.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib618.SetDefaultColor(Color.RED);
extfib618.SetLineWeight(1);
extfib618.HideBubble();
def extfib3 = if zzSave == priceH then high - AbsValue(priorzz2 - priorzz1) *
1.618 else extfib3[1];
plot extfib1618 = if datacount <= numberextfibstoshow and today and showFibExtLines and currentPoints != 0 and !IsNaN(extfib3) and dir < 0 then extfib3[1] else Double.NaN;
extfib1618.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib1618.SetDefaultColor(Color.RED);
extfib1618.SetLineWeight(1);
extfib1618.HideBubble();
def extfib4 = if zzSave == priceH then high - AbsValue(priorzz2 - priorzz1) *
2.618 else extfib4[1];
plot extfib2618 = if  datacount <= numberextfibstoshow and today and showFibExtLines and currentPoints != 0 and !IsNaN(extfib4) and dir < 0 then extfib4[1] else Double.NaN;
extfib2618.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib2618.SetDefaultColor(Color.RED);
extfib2618.SetLineWeight(1);
extfib2618.HideBubble();
def extfib1_ = if zzSave == priceL then low + AbsValue(priorzz2 - priorzz1) * 1
else extfib1_[1];
plot extfib100_ = if datacount <= numberextfibstoshow and today and showFibExtLines and currentPoints != 0 and !IsNaN(extfib1_) and dir > 0 then extfib1_[1] else Double.NaN;
extfib100_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib100_.SetDefaultColor(Color.GREEN);
extfib100_.SetLineWeight(1);
extfib100_.HideBubble();
def extfib2_ = if zzSave == priceL then low + AbsValue(priorzz2 - priorzz1) *
0.618 else extfib2_[1];
plot extfib618_ = if datacount <= numberextfibstoshow and today and showFibExtLines and currentPoints != 0 and !IsNaN(extfib2_) and dir > 0 then extfib2_[1] else Double.NaN;
extfib618_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib618_.SetDefaultColor(Color.GREEN);
extfib618_.SetLineWeight(1);
extfib618_.HideBubble();
def extfib3_ = if zzSave == priceL then low + AbsValue(priorzz2 - priorzz1) *
1.618 else extfib3_[1];
plot extfib1618_ = if datacount <= numberextfibstoshow and today and showFibExtLines and currentPoints != 0 and !IsNaN(extfib3_) and dir > 0 then extfib3_[1] else Double.NaN;
extfib1618_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib1618_.SetDefaultColor(Color.GREEN);
extfib1618_.SetLineWeight(1);
extfib1618_.HideBubble();
def extfib4_ = if zzSave == priceL then low + AbsValue(priorzz2 - priorzz1) *
2.618 else extfib4_[1];
plot extfib2618_ = if datacount <= numberextfibstoshow and today and showFibExtLines and currentPoints != 0 and !IsNaN(extfib4_) and dir > 0 then extfib4_[1]  else Double.NaN;
extfib2618_.SetPaintingStrategy(PaintingStrategy.DASHES);
extfib2618_.SetDefaultColor(Color.GREEN);
extfib2618_.SetLineWeight(1);
extfib2618_.HideBubble();


input b = 2;
def direction = if !isUp then 1 else 0;

AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib1[b + 2], "100%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib2[b + 2], "61.8%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib3[b + 2], "161.8%", Color.RED, no);
AddChartBubble( direction[b + 1] == 1 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib4[b + 2], "261.8%", Color.RED, no);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib1_[b + 2], "100%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib2_[b + 2], "61.8%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib3_[b + 2], "161.8%", Color.GREEN, yes);
AddChartBubble( direction[b + 1] == 0 and showFibExtLines and !IsNaN(close[b + 1]) and IsNaN(close[b]), extfib4_[b + 2], "261.8%", Color.GREEN, yes);


input showlabel_fibext = yes;
AddLabel(showlabel_fibext, "Fib Ext ", Color.YELLOW);
AddLabel( direction == 1 and showlabel_fibext , "0% " + AsText(priorzz1),  Color.RED);
AddLabel( direction == 1 and showlabel_fibext , "61.8% " + AsText(extfib2),  Color.RED);
AddLabel( direction == 1 and showlabel_fibext , "100% " + AsText(extfib1), Color.RED);
AddLabel( direction == 1 and showlabel_fibext , "161.8% " + AsText(extfib3) ,  Color.RED);
AddLabel( direction == 1 and showlabel_fibext , "261.8% " + AsText(extfib4),  Color.RED);
AddLabel( direction == 0 and showlabel_fibext , "0% " + AsText(priorzz1),  Color.GREEN);
AddLabel( direction == 0 and showlabel_fibext , "61.8% " + AsText(extfib2_[1]),  Color.GREEN);
AddLabel( direction == 0 and showlabel_fibext , "100% " + AsText(extfib1_[1]), Color.GREEN);
AddLabel( direction == 0 and showlabel_fibext , "161.8% " + AsText(extfib3_[1]) ,  Color.GREEN);
AddLabel( direction == 0 and showlabel_fibext , "261.8% " + AsText(extfib4_[1]),  Color.GREEN);

def fib_range = AbsValue(priorzz2 - priorzz1) ;
def fib1_0 = priorzz1 + fib_range * .236;
def fib2_0 = priorzz1 + fib_range * .382;
def fib3_0 = priorzz1 + fib_range * .500;
def fib4_0 = priorzz1 + fib_range * .618;
def fib5_0 = priorzz1 + fib_range * .764;
def fib6_0 = priorzz1;
def fib1_1 = priorzz1 - fib_range * .236;
def fib2_1 = priorzz1 - fib_range * .382;
def fib3_1 = priorzz1 - fib_range * .500;
def fib4_1 = priorzz1 - fib_range * .618;
def fib5_1 = priorzz1 - fib_range * .764;
def fib6_1 = priorzz1;

input showlabel_fibretrace = yes;
AddLabel(showlabel_fibretrace, "Fib Retrace ", Color.YELLOW);
AddLabel( direction == 0 and showlabel_fibretrace , "0% " + AsText(fib6_0),  Color.GREEN);
AddLabel( direction == 0 and showlabel_fibretrace , "23.6% " + AsText(fib1_0),  Color.GREEN);
AddLabel( direction == 0 and showlabel_fibretrace , "38.2% " + AsText(fib2_0),  Color.GREEN);
AddLabel( direction == 0 and showlabel_fibretrace , "50% " + AsText(fib3_0),  Color.GREEN);
AddLabel( direction == 0 and showlabel_fibretrace , "61.8% " + AsText(fib4_0),  Color.GREEN);
AddLabel( direction == 0 and showlabel_fibretrace , "76.4 " + AsText(fib5_0),  Color.GREEN);
AddLabel( direction == 1 and showlabel_fibretrace , "0% " + AsText(fib6_1),  Color.RED);
AddLabel( direction == 1 and showlabel_fibretrace , "23.6% " + AsText(fib1_1),  Color.RED);
AddLabel( direction == 1 and showlabel_fibretrace , "38.2% " + AsText(fib2_1),  Color.RED);
AddLabel( direction == 1 and showlabel_fibretrace , "50% " + AsText(fib3_1),  Color.RED);
AddLabel( direction == 1 and showlabel_fibretrace , "61.8% " + AsText(fib4_1),  Color.RED);
AddLabel( direction == 1 and showlabel_fibretrace , "76.4 " + AsText(fib5_1),  Color.RED);
@SleepyZ is it possible for this to work with Forex charts?
 
Can someone please see if alerts are working. I can't seem to get alerts to work or to be able to make watch list column says too complicated
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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