
Author Message:
Overview
The Fibonacci Structure & Trend Channel (Expo) is designed to identify trend direction and potential reversal levels and offer insights into price structure based on Fibonacci ratios. The algorithm plots a Fibonacci channel, making it easier for traders to identify potential retracement points. Additionally, the Fibonacci market structure is plotted to enhance traders' understanding of the underlying order flow.
CODE:
CSS:
#// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International
#https://www.tradingview.com/v/EhJK1FZL/
#// © Zeiierman {
#indicator("Fibonacci Structure Trend Channel (Expo)",overlay=true, max_labels_count=500)
# converted by Sam4Cok@Samer800 - 09/2023 - Not Typical Conv.
#// ~~ Inputs {
input fibonacciLevel = {"Manual Level", "0.236", default "0.382", "0.50", "0.618", "0.786"};
input ManualFibonacciLevel = 0.5;
input FibonacciStructure = yes;
input StructureLookback = 5;
input channelAtrLength = 500;
input FiboChannelSmoothLength = 10;
input FibonacciChannel = no; # "Fibonacci Channel"
input FibChannelMulti = 3.5; # "Fib Channel width"
input BarColor = no;
input FibonacciLevels = yes; # "Fibonacci Levels"
input highlightFiboLevels = no;
input highlightTrend = yes;
input levelLabel = yes; # "Labels"
input infoLabel = yes; # "Fib Trend Information"
def na = Double.NaN;
def last = isNaN(close);
def ohlc = if highlightTrend then ohlc4 else na;
def structure = FibonacciStructure;
def sen = StructureLookback;
def hiSrc = high;
def loSrc = low;
#-- Colors
DefineGlobalColor("bull", CreateColor(0,230,118));
DefineGlobalColor("bear", CreateColor(255,82,82));
DefineGlobalColor("limitUp", CreateColor(95,75,188));
DefineGlobalColor("LimitDn", CreateColor(203,68,140));
DefineGlobalColor("786", CreateColor(244,67,54));
DefineGlobalColor("618", CreateColor(129,199,132));
DefineGlobalColor("500", CreateColor(76,175,80));
DefineGlobalColor("382", CreateColor(0,150,136));
DefineGlobalColor("236", CreateColor(100,181,246));
DefineGlobalColor("000", CreateColor(95,95,95));
#-- Calc
def fibonacci;
Switch (fibonacciLevel) {
Case "Manual Level" : fibonacci = ManualFibonacciLevel;
Case "0.236": fibonacci = 0.236;
Case "0.382": fibonacci = 0.382;
Case "0.50": fibonacci = 0.50;
Case "0.618": fibonacci = 0.618;
Case "0.786": fibonacci = 0.786;
}
#// ~~ Variables {
def pos;# = 0
def hi;# = high
def lo;# = low
def hloc;# = b
def lloc;# = b
def retrace;# = 0.0
def tD;# = string(na)
def tM;# = string(na)
def tY;# = string(na)
def label;
def line;
def DD = GetDayOfMonth(GetYYYYMMDD());
def MM = GetMonth();
def YY = GetYear();
def fib = fibonacci;
def bar_index = CompoundValue(1, barNumber(), 0);
def b = bar_index;
def cnt;
if pos[1] >= 0 {
if hiSrc < retrace[1] {
hi = if hi[1] then hi[1] else hiSrc;
lo = loSrc;
pos = -1;
hloc = na;#if hloc[1] then hloc[1] else b;
lloc = b;
retrace = lo + (hi - lo) * fib;
tD = DD;
tM = MM;
tY = YY;
label = yes;
line = line[1];
cnt = if line==line[1] then cnt[1] + 1 else cnt[1];
} else if hiSrc > hi[1] {
hi = hiSrc;
lo = if lo[1] then lo[1] else loSrc;
pos = if structure and hi[1] == hi[sen] then pos[1] + 1 else pos[1];
hloc = b;
lloc = lloc[1];#if lloc[1] then lloc[1] else b;
retrace = hi - (hi - lo) * fib;
tD = tD[1];
tM = tM[1];
tY = tY[1];
label = na;
line = hi;
cnt = 0;
} else {
hi = if hi[1] then hi[1] else hiSrc;
lo = if lo[1] then lo[1] else loSrc;
pos = pos[1];
hloc = if hloc[1] then hloc[1] else b;
lloc = if lloc[1] then lloc[1] else b;
retrace = retrace[1];
tD = tD[1];
tM = tM[1];
tY = tY[1];
label = na;
line = line[1];
cnt = if line==line[1] then cnt[1] + 1 else cnt[1];
}
} else
if pos[1] <= 0 {
if loSrc > retrace[1] {
hi = hiSrc;
lo = if lo[1] then lo[1] else loSrc;
pos = 1;
hloc = b;
lloc = na;
retrace = hi - (hi - lo) * fib;
tD = DD;
tM = MM;
tY = YY;
label = yes;
line = na;#line[1];
cnt = if line==line[1] then cnt[1] + 1 else cnt[1];
} else if loSrc < lo[1] {
hi = if hi[1] then hi[1] else hiSrc;
lo = loSrc;
pos = if structure and lo[1] == lo[sen] then pos[1] - 1 else pos[1];
hloc = hloc[1];#na;#if hloc[1] then hloc[1] else b;
lloc = b;
retrace = lo + (hi - lo) * fib;
tD = tD[1];
tM = tM[1];
tY = tY[1];
label = na;
line = lo;
cnt = 0;
} else {
hi = if hi[1] then hi[1] else hiSrc;
lo = if lo[1] then lo[1] else loSrc;
pos = pos[1];
hloc = hloc[1];#na;#if hloc[1] then hloc[1] else b;
lloc = lloc[1];#if lloc[1] then lloc[1] else b;
retrace = retrace[1];
tD = tD[1];
tM = tM[1];
tY = tY[1];
label = na;#label[1];
line = line[1];
cnt = if line==line[1] then cnt[1] + 1 else cnt[1];
}
} else {
hi = if hi[1] then hi[1] else hiSrc;
lo = if lo[1] then lo[1] else loSrc;
pos = pos[1];
hloc = if hloc[1] then hloc[1] else b;
lloc = if lloc[1] then lloc[1] else b;
retrace = retrace[1];
tD = tD[1];
tM = tD[1];
tY = tD[1];
label = na;
line = line[1];
cnt = cnt[1];
}
#//~~~~~~~~~~~~~~~~~~~~~~
#// ~~ Fibonacci Channel {
def bull = pos > 0;
def bear = pos < 0;
def nATR_ = atr(Length = channelAtrLength);
def width = nATR_ * FibChannelMulti;
def ma = wma(retrace, FiboChannelSmoothLength);
def nATR = Average(width, FiboChannelSmoothLength * 2);
def max = (ma + nATR);
def min = (ma - nATR);
#//~~~~~~~~~~~~~~~~~~~~~~}
#// ~~ Plot & Barcolor {
plot ret = if last then na else
if bull and bull[1] then retrace[1] else
if bear and bear[1] then retrace[1] else na; # "Fibonacci Trend"
ret.AssignValueColor(if pos>0 then GlobalColor("bull") else GlobalColor("bear"));
AddCloud(ohlc, ret, Color.DARK_GREEN, Color.DARK_RED);
plot UpFibChannel = if FibonacciChannel then max else na; # "Upper Fibonacci Channel"
plot LoFibChannel = if FibonacciChannel then min else na; # "Lower Fibonacci Channel"
UpFibChannel.SetDefaultColor(GlobalColor("limitUp"));
LoFibChannel.SetDefaultColor(GlobalColor("limitDn"));
def retTime = if ret!=ret[1] then b else retTime[1];
def hiRetTime = highestAll(retTime);
def condPlot = FibonacciLevels and b >= hiRetTime and !last[2];
def lvl000 = if bull then lo else hi;
def lvl236 = if bull then lo + (hi-lo) * 0.236 else hi - (hi-lo) * 0.236;
def lvl382 = if bull then lo + (hi-lo) * 0.382 else hi - (hi-lo) * 0.382;
def lvl500 = if bull then lo + (hi-lo) * 0.500 else hi - (hi-lo) * 0.500;
def lvl618 = if bull then lo + (hi-lo) * 0.618 else hi - (hi-lo) * 0.618;
def lvl786 = if bull then lo + (hi-lo) * 0.786 else hi - (hi-lo) * 0.786;
def lvl100 = if bull then lo + (hi-lo) * 1.000 else hi - (hi-lo) * 1.000;
plot lvl10 = if condPlot then lvl100 else na;
plot lvl78 = if condPlot then lvl786 else na;
plot lvl61 = if condPlot then lvl618 else na;
plot lvl50 = if condPlot then lvl500 else na;
plot lvl38 = if condPlot then lvl382 else na;
plot lvl23 = if condPlot then lvl236 else na;
plot lvl00 = if condPlot then lvl000 else na;
lvl10.SetPaintingStrategy(PaintingStrategy.DASHES);
lvl78.SetPaintingStrategy(PaintingStrategy.DASHES);
lvl61.SetPaintingStrategy(PaintingStrategy.DASHES);
lvl50.SetPaintingStrategy(PaintingStrategy.DASHES);
lvl38.SetPaintingStrategy(PaintingStrategy.DASHES);
lvl23.SetPaintingStrategy(PaintingStrategy.DASHES);
lvl00.SetPaintingStrategy(PaintingStrategy.DASHES);
lvl10.SetDefaultColor(GlobalColor("000"));
lvl78.SetDefaultColor(GlobalColor("786"));
lvl61.SetDefaultColor(GlobalColor("618"));
lvl50.SetDefaultColor(GlobalColor("500"));
lvl38.SetDefaultColor(GlobalColor("382"));
lvl23.SetDefaultColor(GlobalColor("236"));
lvl00.SetDefaultColor(GlobalColor("000"));
AddCloud(if !highlightFiboLevels then na else lvl10, lvl78, GlobalColor("000"), GlobalColor("000"));
AddCloud(if !highlightFiboLevels then na else lvl78, lvl61, GlobalColor("236"), GlobalColor("236"));
AddCloud(if !highlightFiboLevels then na else lvl61, lvl50, GlobalColor("382"), GlobalColor("382"));
AddCloud(if !highlightFiboLevels then na else lvl50, lvl38, GlobalColor("500"), GlobalColor("500"));
AddCloud(if !highlightFiboLevels then na else lvl38, lvl23, GlobalColor("618"), GlobalColor("618"));
AddCloud(if !highlightFiboLevels then na else lvl23, lvl00, GlobalColor("786"), GlobalColor("786"));
AddChartBubble(levelLabel and isNaN(lvl10) and !isNaN(lvl10[1]), lvl10[1], "0.00%", GlobalColor("000"));
AddChartBubble(levelLabel and isNaN(lvl78) and !isNaN(lvl78[1]), lvl78[1], "23.6%", GlobalColor("786"));
AddChartBubble(levelLabel and isNaN(lvl61) and !isNaN(lvl61[1]), lvl61[1], "38.2%", GlobalColor("618"));
AddChartBubble(levelLabel and isNaN(lvl50) and !isNaN(lvl50[1]), lvl50[1], "50.0%", GlobalColor("500"));
AddChartBubble(levelLabel and isNaN(lvl38) and !isNaN(lvl38[1]), lvl38[1], "61.8%", GlobalColor("382"));
AddChartBubble(levelLabel and isNaN(lvl23) and !isNaN(lvl23[1]), lvl23[1], "78.6%", GlobalColor("236"));
AddChartBubble(levelLabel and isNaN(lvl00) and !isNaN(lvl00[1]), lvl00[1], "100%", GlobalColor("000"));
#--
def roundLvl = Round(retrace, 2);
def lastBarUp = if hloc!=hloc[1] then b else lastBarUp[1];
def lastBarDn = if lloc!=lloc[1] then b else lastBarDn[1];
def struCondUp = FibonacciStructure and bull and retTime < b and b > (lastBarUp);
def struCondDn = FibonacciStructure and bear and retTime < b and b > (lastBarDn);
plot limitUp = if struCondUp and cnt>= sen then line else na;
plot limitDn = if struCondDn and cnt>= sen then line else na;
limitUp.SetDefaultColor(GlobalColor("limitUp"));
limitDn.SetDefaultColor(GlobalColor("limitDn"));
def hiLastBarUp = highestAll(lastBarUp);
def hiLastBarDn = highestAll(lastBarDn);
def zig = if hiLastBarUp == b then hiSrc else
if hiLastBarDn ==b then loSrc else na;
plot zigzag = zig;
zigzag.SetStyle(Curve.SHORT_DASH);
zigzag.SetDefaultColor(GlobalColor("000"));
zigzag.EnableApproximation();
def hilvl100 = highestAll(lvl00);
plot strUp = if last then na else
if hiLastBarUp <=b then hilvl100 else na;
plot strDn = if last then na else
if hiLastBarDn <=b then hilvl100 else na;
strUp.SetDefaultColor(GlobalColor("000"));
strDn.SetDefaultColor(GlobalColor("000"));
strUp.SetPaintingStrategy(PaintingStrategy.DASHES);
strDn.SetPaintingStrategy(PaintingStrategy.DASHES);
plot TrendStart = if label==yes then retrace else na;#retrace[1] else na;
TrendStart.SetPaintingStrategy(PaintingStrategy.POINTS);
TrendStart.SetLineWeight(2);
TrendStart.AssignValueColor(if bull then GlobalColor("bull") else GlobalColor("bear"));
#-- Label
AddLabel(infoLabel,"Trend : " + if bull then "Bullish" else "Bearish",
if bull then GlobalColor("bull") else GlobalColor("bear"));
AddLabel(infoLabel,"Trend Level: " + "$" + roundLvl,
if bull then GlobalColor("bull") else GlobalColor("bear"));
AddLabel(infoLabel,"Trend Started: " + tD + "/" + tM + "/" + tY,
if bull then GlobalColor("bull") else GlobalColor("bear"));
#-- Bar Color
AssignPriceColor(if !BarColor then Color.CURRENT else
if pos>0 and close > max then Color.GREEN else
if pos>0 then GlobalColor("bull") else
if close < min then Color.RED else GlobalColor("bear"));
#-- END of CODE