
Author Message:
Overview
The Fibonacci Oscillator is a multi-faceted oscillator designed to provide traders with a comprehensive understanding of market trends and retracement points. Built on the Fibonacci ratios, it combines the functionalities of popular oscillators like RSI and MACD with unique insights into the market structure. This oscillator not only helps identify trend direction but also pinpoints overbought and oversold levels, making it an essential tool for various trading strategies.
More Details : https://www.tradingview.com/v/KkAOEljd/
CODE:
CSS:
#// https://www.tradingview.com/v/KkAOEljd/
#// © Zeiierman {
#//@version=5
#indicator("Fibonacci Oscillator (Expo)",overlay=false, preci
# converted by Sam4Cok@Samer800 - 09/2023
Declare Lower;
#// ~~ Inputs {
input OscillatorType = {default "RSI", "MACD", "Histogram"}; # "Oscillator Type"
input fibonacciLevel = {Default "0.236","0.382","0.50","0.618","0.786", "Custom Level"}; # "Fibonacci Level?"
input customLevel = 0.1; # "Custom Value"
input rsiLength = 14; # "RSI Length????"
input rsiSignalLength = 14; # "Signal Length"
input macdAvgType = AverageType.EXPONENTIAL;
input macdFastLength = 12; # "Fast??"
input macdSlowLength = 26; # "Slow"
input macdSignalLength = 9; # "Signal"
input HistogramSource = close; #,"Source"
def na = Double.NaN;
def last = isNaN(close);
def hist = OscillatorType == OscillatorType."Histogram";
def cusVal = min(customLevel, 0.999);
def custVal = cusVal;
DefineGlobalColor("rsi_col", Color.MAGENTA);
DefineGlobalColor("rsi_sigcol", CreateColor(255,235,59));
DefineGlobalColor("macd_col", CreateColor(41, 98, 255));
DefineGlobalColor("macd_sigcol", CreateColor(255, 109, 0));
DefineGlobalColor("grow_above", CreateColor(38, 166, 154));
DefineGlobalColor("fall_above", CreateColor(178, 223, 219));
DefineGlobalColor("grow_below", CreateColor(255, 205, 210));
DefineGlobalColor("fall_below", CreateColor(255, 82, 82));
DefineGlobalColor("hist_colup", CreateColor(38, 166, 154));
DefineGlobalColor("hist_coldn", CreateColor(255, 82, 82));
#/ ~~ Variables {
#def b = AbsValue(BarNumber());
def pos;# = 0
def hi;# = high
def lo;# = low
def retrace;# = 0.0
def fib;
# "0.236","0.382","0.50","0.618","0.786", "Custom Level"}; # "Fibonacci Level?"
Switch (fibonacciLevel) {
Case "0.382" : fib = 0.382;
Case "0.50" : fib = 0.50;
Case "0.618" : fib = 0.618;
Case "0.786" : fib = 0.786;
Case "Custom Level" : fib = custVal;
Default : fib = 0.236;
}
#// ~~ Switch Function {
Script OscType {
input r = close;
input h = close;
input m = close;
input oscType = "RSI";
def output = if oscType == "RSI" then r else
if oscType == "Histogram" then h else
if oscType == "MACD" then m else Double.NaN;
plot out = output;
}
#// ~~ Track high/low and calculate fibonacci level {
if pos[1] >= 0 {
if high < retrace[1] {
pos = -1;
hi = hi[1];
lo = low;
retrace = lo + (hi - lo ) * fib;
} else if high > hi[1] {
pos = pos[1];
hi = high;
lo = if lo[1] then lo[1] else low;
retrace = hi - (hi - lo) * fib;
} else {
pos = pos[1];
hi = hi[1];
lo = lo[1];
retrace = retrace[1];
}
} else
if pos[1] <= 0 {
if low > retrace[1] {
pos = 1;
hi = high;
lo = if lo[1] then lo[1] else low;
retrace = hi - (hi - lo) * fib;
} else if low < lo[1] {
pos = pos[1];
hi = if hi[1] then hi[1] else high;
lo = low;
retrace = lo + (hi - lo) * fib;
} else {
pos = pos[1];
hi = hi[1];
lo = lo[1];
retrace = retrace[1];
}
} else {
pos = if isNaN(pos[1]) then 0 else isNaN(pos[1]);
hi = if isNaN(hi[1]) then high else hi[1];
lo = if isNaN(lo[1]) then low else lo[1];
retrace = if isNaN(retrace[1]) then 0 else retrace[1];
}
def nRSI = RSI(Price = retrace, Length = rsiLength);
def mcdFast = MovingAverage(macdAvgType, retrace, macdFastLength);
def mcdSlow = MovingAverage(macdAvgType, retrace, macdSlowLength);
def Value = mcdFast - mcdSlow;
def Avg = MovingAverage(macdAvgType, Value, macdSignalLength);
def Diff = Value - Avg;
def src;
def signal;
def osc;
switch (OscillatorType) {
case "RSI" :
src = na;
signal = Average(nRSI, rsiSignalLength);
osc = nRSI;
case "Histogram" :
src = na;
signal = na;
osc = HistogramSource - retrace;
case "MACD" :
src = Value;
signal = Avg;
osc = Diff;
}
def rsiCol = 0;
def hisCol = if osc>=0 then 3 else -3;
def mcdCol = if osc>=0 then
(if osc[1]<osc then 2 else 1) else
(if osc[1]<osc then -1 else -2);
def osCol = OscType(rsiCol,hisCol,mcdCol, OscillatorType);
def m3Col = OscType(rsiCol, na,mcdCol, OscillatorType);
def m2 = OscType(na, na,src, OscillatorType);
def m3 = OscType(signal,na,signal, OscillatorType);
plot signalLine = if last then na else m3;#,"Signal"
signalLine.AssignValueColor(if m3Col==0 then GlobalColor("rsi_sigcol") else GlobalColor("macd_sigcol"));
plot macdLine = if last then na else m2;#,"MACD Line"
macdLine.SetDefaultColor(GlobalColor("macd_col"));
plot oscLine = if last or osCol!=0 then na else osc;
oscLine.SetLineWeight(2);
oscLine.SetDefaultColor(GlobalColor("rsi_col"));
plot oscHist = if last or osCol==0 then na else osc;
oscHist.SetLineWeight(4);
oscHist.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
oscHist.AssignValueColor(if osCol==3 then GlobalColor("hist_colup") else
if osCol==-3 then GlobalColor("hist_coldn") else
if osCol==2 then GlobalColor("grow_above") else
if osCol==1 then GlobalColor("fall_above") else
if osCol==-1 then GlobalColor("grow_below") else
if osCol==-2 then GlobalColor("fall_below") else Color.GRAY);
#// ~~ Gradient Fill {
def hiOsc = highest(osc,1000);
def loOsc = lowest(osc,1000);
def hiSrc = highest(src,1000);
def loSrc = lowest(src,1000);
def stdOsc = stdev(osc,20);
def stdSrc = stdev(Src,20);
def emaOsc = ExpAverage(stdOsc,20);
def emaSrc = ExpAverage(stdSrc,20);
def upper = OscType(100 ,hiOsc ,hiSrc ,OscillatorType);
def lower = OscType(0 ,loOsc ,loSrc , OscillatorType);
def mid = OscType(50, 0, 0, OscillatorType);
def upper_ = OscType(90, upper - emaOsc, upper - emaSrc, OscillatorType);
def lower_ = OscType(10, lower + emaOsc, lower + emaSrc, OscillatorType);
def upper1 = OscType(80, upper - emaOsc, upper - emaSrc, OscillatorType);
def lower1 = OscType(20, lower + emaOsc, lower + emaSrc, OscillatorType);
def channel_upper = if last then na else upper;
def channel_lower = if last then na else lower;
plot channel_mid = if last then na else mid;
channel_mid.SetDefaultColor(Color.GRAY);
channel_mid.SetStyle(Curve.SHORT_DASH);
#/ ~~ Channel Gradient Fill {
def p1 = if !isNaN(oscLine) then oscLine else if hist then oscHist else macdLine;
def p1ConLo = if p1 < lower_ then p1 else na;
def p1ConUp = if p1 > upper_ then p1 else na;
AddCloud(channel_upper, upper1, Color.DARK_GREEN);
AddCloud(lower1, channel_lower, Color.DARK_RED);
#// ~~ Overbought/Oversold Gradient Fill {
AddCloud(p1ConUp, upper_ , Color.GREEN, Color.GREEN, hist);
AddCloud(lower_,p1ConLo, Color.RED, Color.RED, hist);
#-- END of CODE