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'
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'
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: