#//@version=3
#study("Reversal","Fibbonaci Accuracy",overlay=false)
# Converted by sam4Cok@Samer800 - 01/2024
declare lower;
input trend_candles = 5;#,"#Trend Candles")
input lookbackPeriod = 1000;
def na = Double.NaN;
def last = isNaN(close);
def fib0 = lowest(low, lookbackPeriod); # "#FibMin"
def fib100 = highest(high, lookbackPeriod);# "FibMax"
def fibRange = (fib100 - fib0);
script closest_fib {
input x = close;
input fib0 = low;
input fib100 = high;
def fibRange = (fib100 - fib0);
def fib23 = fib0 + fibRange * 0.236;
def fib38 = fib0 + fibRange * 0.382;
def fib50 = fib0 + fibRange * 0.50;
def fib61 = fib0 + fibRange * 0.618;
def fib78 = fib0 + fibRange * 0.786;
def closest_fib = if AbsValue(fib0 - x) < AbsValue(fib23 - x) then fib0 else
if AbsValue(fib23 - x) < AbsValue(fib38 - x) then fib23 else
if AbsValue(fib38 - x) < AbsValue(fib50 - x) then fib38 else
if AbsValue(fib50 - x) < AbsValue(fib61 - x) then fib50 else
if AbsValue(fib61 - x) < AbsValue(fib78 - x) then fib61 else
if AbsValue(fib78 - x) < AbsValue(fib100 - x) then fib78 else fib100;
plot out = closest_fib;
}
Script Pivot {
input series = close;
input leftBars = 10;
input rightBars = 10;
input isHigh = yes;
def na = Double.NaN;
def HH = series == Highest(series, leftBars + 1);
def LL = series == Lowest(series, leftBars + 1);
def pivotRange = (leftBars + rightBars + 1);
def leftEdgeValue = if series[pivotRange] == 0 then na else series[pivotRange];
def pvtCond = !IsNaN(series) and leftBars > 0 and rightBars > 0 and !IsNaN(leftEdgeValue);
def barIndexH = if pvtCond then
fold i = 1 to rightBars + 1 with p=1 while p do
series > GetValue(series, - i) else na;
def barIndexL = if pvtCond then
fold j = 1 to rightBars + 1 with q=1 while q do
series < GetValue(series, - j) else na;
def PivotPoint;
if isHigh {
PivotPoint = if HH and barIndexH then series else na;
} else {
PivotPoint = if LL and barIndexL then series else na;
}
plot pvt = if IsNaN(PivotPoint) then 0 else PivotPoint;
}
#// get Pivot High/Low
def last_high = pivot(high, trend_candles, trend_candles, yes);
def last_low = pivot(low, trend_candles, trend_candles, no);
def rel_close = if last_high then high else if last_low then low else na;
def points_step = if (last_high or last_low) then rel_close else na;
def fobLevel = closest_fib(points_step, fib0, fib100);
def closest_steps = if (last_high or last_low) then fobLevel else na;
def pointsp = (points_step - fib0) * 100/fibRange;
def stepsp = (closest_steps - fib0) * 100/fibRange;
def diffp = pointsp - stepsp;
plot diffPnt = diffp;
diffPnt.SetLineWeight(3);
diffPnt.SetPaintingStrategy(PaintingStrategy.SQUARES);
diffPnt.AssignValueColor(if last_low then Color.red else Color.GREEN);
plot zero = if last then na else 0;
zero.SetDefaultColor(Color.GRAY);
#-- END of CODE