# https://www.tradingview.com/v/y3NTW7he/
#// This source code is subject to the terms of the Mozilla Public License 2.0
#// © ChartPrime
#indicator("Ichimoku Oscillator [ChartPrime]",
# Converted by Sam4Cok@Samer800 - 10 / 2023
declare lower;
input SignalSource = close; # "Signal Source"
input conversionLineLength = 9; # "Conversion Line Length"
input BaseLineLength = 26; # "Base Line Length"
input LeadingSpanBLength = 52; # "Leading Span B Length"
input LaggingSpan = 26; # "Lagging Span"
input movAvgLength = 12; # "Moving Average Length"
input signalSmoothing = 3; # "Smoothing"
input extra_smoothing = yes; # "signal smoothing"
input normalize = {"All", default "Window", "Disabled"}; # "Normalization"
input window_size = 20; # "When enabled it will scale from 100 to -100.
input ClampToRange = yes; # "Clamp to Range"
input MaxBandwidth = 2.0; # "Max Bandwidth"
input MidBandwidth = 1.5; # "Mid Bandwidth"
input DivergenceLookRight = 10; # "Divergence Look Right"
input DivergenceLookLeft = 15; # "Divergence Look Left"
input MaximumLookback = 100; # "Maximum Lookback"
input MinimumLookback = 5; # "Minimum Lookback"
input RegularBullish = yes; # "Regular Bullish"
input HiddenBullish = no; # "Hidden Bullish"
input RegularBearish = yes; # "Regular Bearish"
input HiddenBearish = no; #, "Hidden Bearish", group = "Divergence", inline = "Brd")
input showSignalLine = yes; # "Show Signal"
input show_chikou = no; # "Show Chikou"
input show_conversion_base = no; # "Show Conversion and Base"
input showMovingAvg = no; # "Show Moving Average"
input show_min_max = yes; # "Show Min/Max"
input show_kumo = {default "Full", "Current", "Disabled"}; # "Show Kumo"
input show_kumo_lines = no; # "Show Kumbo Lines"
input color_on_conversion = yes; # "Color Signal by Conversion Crosses"
input signalLineWidth = 2;
def na = Double.NaN;
def last = isNaN(close);
def bar_index = AbsValue(BarNumber());
def Disabled = normalize == normalize."Disabled";
def clamp = ClampToRange;
def right = DivergenceLookRight;
def left = DivergenceLookLeft;
#-- Colors
DefineGlobalColor("signal_color", CreateColor(243,107,22));
DefineGlobalColor("MovAvg", CreateColor(83, 152, 255));
DefineGlobalColor("n_color", CreateColor(242,219,46));
DefineGlobalColor("conv_color", CreateColor(121,216,224));
DefineGlobalColor("conv_bull", Color.GREEN);#CreateColor(28,122,36));
DefineGlobalColor("conv_bear", Color.RED);#CreateColor(223,140,140));
DefineGlobalColor("base_color", CreateColor(228,98,178));
DefineGlobalColor("base_bull", Color.DARK_GREEN);#CreateColor(100,165,104));
DefineGlobalColor("base_bear", Color.DARK_RED);#CreateColor(255,68,68));
DefineGlobalColor("kumoBull", CreateColor(34,234,112));
DefineGlobalColor("kumoBear", CreateColor(255,47,28));
DefineGlobalColor("max_color", CreateColor(92,153,112));
DefineGlobalColor("high_color", CreateColor(50,83,61));
DefineGlobalColor("min_color", CreateColor(221,96,85));
DefineGlobalColor("low_color", CreateColor(207,55,41));
#in_range(cond, lower_range, upper_range) =>
script in_range {
input cond = yes;
input lower_range = 5;
input upper_range = 100;
def bars = if (cond == yes) then 0 else bars[1] + 1;
def range = (lower_range <= bars) and (bars <= upper_range);
plot out = range;
}
script FindPivots {
input dat = close; # default data or study being evaluated
input HL = 0; # default high or low pivot designation, -1 low, +1 high
input lbL = 5; # default Pivot Lookback Left
input lbR = 1; # default Pivot Lookback Right
##############
def _nan; # used for non-number returns
def _BN; # the current barnumber
def _VStop; # confirms that the lookforward period continues the pivot trend
def _V; # the Value at the actual pivot point
def _pivotRange;
##############
_BN = BarNumber();
_nan = Double.NaN;
_pivotRange = lbL + lbL;
_VStop = if !IsNaN(dat[_pivotRange]) and lbR > 0 and lbL > 0 then
fold a = 1 to lbR + 1 with b=1 while b do
if HL > 0 then dat > GetValue(dat, -a) else dat < GetValue(dat, -a) else _nan;
if (HL > 0) {
_V = if _BN > lbL and dat == Highest(dat, lbL + 1) and _VStop
then dat else _nan;
} else {
_V = if _BN > lbL and dat == Lowest(dat, lbL + 1) and _VStop
then dat else _nan;
}
plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
#min_max(source, min, max, enable, clamp)=>
script min_max {
input source = close;
input min = 0;
input max = 100;
input enable = "Disabled";
input clamp = yes;
def min_max;
if enable != "Disabled" {
if clamp {
min_max = (Max(Min(1, (source - min) / (max - min)), 0) - 0.5) * 200;
} else {
min_max = ((source - min) / (max - min) - 0.5) * 200;
}
} else {
min_max = source;
}
plot out = min_max;
}
script donchian {
input len = 14;
def hh = Highest(high, len);
def ll = Lowest(low, len);
def don = (hh + ll) / 2;
plot out = don;
}
#// Custom cosh function
script cosh {
input x = 0;
def cosh = (Exp(x) + Exp(-x)) / 2;
plot out = cosh;
}
#// Custom acosh function
script acosh {
input x = 0;
def acosh = if x < 1 then Double.NaN else
Log(x + Sqrt(x * x - 1));
plot out = acosh;
}
#// Custom sinh function
script sinh {
input x = 0;
def sinh = (Exp(x) - Exp(-x)) / 2;
plot out = sinh;
}
#// Custom asinh function
script asinh {
input x = 0;
def asinh = Log(x + Sqrt(x * x + 1));
plot out = asinh;
}
#// Chebyshev Type I Moving Average
script chebyshevI {
input src = close;
input len = 8;
input ripple = 0.05;
def a = cosh(1 / len * acosh(1 / (1 - ripple)));
def b = sinh(1 / len * asinh(1 / ripple));
def c = (a - b) / (a + b);
def chebyshev = (1 - c) * src + c * chebyshev[1];
plot out = chebyshev;
}
script gaussian_kernel {
input source = close;
input size = 64;
input h = 4;
input r = 0.5;
def pi = Double.Pi;
def weight = fold i = 0 to size with p do
p + (Exp(-Power(Power(i, 2) / (Power(h, 2) * r) / r, 2) / 2) / Sqrt(2 * pi)) * source[i];
def weight_sum = fold j = 0 to size with q do
q + (Exp(-Power(Power(j, 2) / (Power(h, 2) * r) / r, 2) / 2) / Sqrt(2 * pi));
def gaussian_kernel = weight / weight_sum;
plot out = gaussian_kernel;
}
script smoothing {
input source = close;
input length = 14;
input extra = yes;
def cheby = chebyshevI(source, length, 0.5);
def gaussian = gaussian_kernel(cheby, 4, 2, 1);
def smoothing = if extra then gaussian else cheby;
plot out = smoothing;
}
def start = bar_index >= LeadingSpanBLength + LaggingSpan - 1;
def conversion = donchian(conversionLineLength);
def base = donchian(BaseLineLength);
def avgConBase = (conversion + base) / 2;
def kumo_a = smoothing(avgConBase, signalSmoothing, extra_smoothing);
def kumo_b = smoothing(donchian(LeadingSpanBLength), signalSmoothing, extra_smoothing);
def kumo_a_offset = kumo_a[LaggingSpan - 1];
def kumo_b_offset = kumo_b[LaggingSpan - 1];
def kumo_condition = kumo_a > kumo_b;
def kumo_offset_condition = kumo_a_offset > kumo_b_offset;
def kumo_center = (kumo_a + kumo_b) / 2;
def kumo_center_offset = kumo_center[LaggingSpan - 1];
def kumo_a_centered = kumo_a - kumo_center;
def kumo_b_centered = kumo_b - kumo_center;
def kumo_a_offset_centered = kumo_a_offset - kumo_center_offset;
def kumo_b_offset_centered = kumo_b_offset - kumo_center_offset;
def future_kumo_a = kumo_a - kumo_center_offset;
def future_kumo_b = kumo_b - kumo_center_offset;
def chikou = SignalSource[LaggingSpan + 1];
def chikou_centered = smoothing(SignalSource - chikou, signalSmoothing, extra_smoothing);
def conversion_base_condition = conversion > base;
def conversion_centered = smoothing(conversion - kumo_center_offset, signalSmoothing, extra_smoothing);
def base_centered = smoothing(base - kumo_center_offset, signalSmoothing, extra_smoothing);
def signal = smoothing(SignalSource - kumo_center_offset, signalSmoothing, extra_smoothing);
def ma = WMA(signal, movAvgLength);
def kumo_color = if kumo_condition then 1 else -1;
def kumo_offset_color = if kumo_offset_condition then 1 else -1;
def dev;# = 0
if normalize == normalize."All" and !IsNaN(kumo_a_offset) {
dev = Sqrt(TotalSum(Power(signal, 2)) / TotalSum(1));
} else
if normalize == normalize."Window" {
dev = Sqrt(Sum(Power(signal, 2), window_size) / (window_size - 1));
} else {
dev = 0;
}
def max_level = dev * MaxBandwidth;
def min_level = -dev * MaxBandwidth;
def high_level = dev * MidBandwidth;
def low_level = -dev * MidBandwidth;
#// Define conditions
def show_kumo_full = show_kumo == show_kumo."Current";
def show_kumo_current = show_kumo == show_kumo."Full";
#// Define the sources and offsets for the plots based on the condition
def plot_source_a = if show_kumo_full then kumo_a_offset_centered else
if show_kumo_current then kumo_a_centered else na;
def plot_source_b = if show_kumo_full then kumo_b_offset_centered else
if show_kumo_current then kumo_b_centered else na;
def offset_val = if show_kumo_full then 0 else LaggingSpan - 1;
def color_val = if show_kumo_full then kumo_offset_color else
if show_kumo_current then kumo_color else 0;
def disp = if show_kumo_full then !last else yes;#LaggingSpan - 1 else 0;
def normal_source_a = min_max(plot_source_a, min_level, max_level, normalize, clamp);
def normal_plot_source_b = min_max(plot_source_b, min_level, max_level, normalize, clamp);
def normal_plot_source_a = if disp then normal_source_a else na;
def normal_max_level = min_max(max_level, min_level, max_level, normalize, clamp);
def normal_high_level = min_max(high_level, min_level, max_level, normalize, clamp);
def normal_min_level = min_max(min_level, min_level, max_level, normalize, clamp);
def normal_low_level = min_max(low_level, min_level, max_level, normalize, clamp);
def normal_conversion_centered = min_max(conversion_centered, min_level, max_level, normalize, clamp);
def normal_base_centered = min_max(base_centered, min_level, max_level, normalize, clamp);
def normal_chikou_centered = min_max(chikou_centered, min_level, max_level, normalize, clamp);
def normal_ma = min_max(ma, min_level, max_level, normalize, clamp);
def normal_signal = min_max(signal, min_level, max_level, normalize, clamp);
def normal_kumo_a_offset_centered =
min_max(kumo_a_offset_centered, min_level[LaggingSpan - 1], max_level[LaggingSpan - 1], normalize, clamp);
def normal_kumo_b_offset_centered =
min_max(kumo_b_offset_centered, min_level[LaggingSpan - 1], max_level[LaggingSpan - 1], normalize, clamp);
def kumo_max = Max(normal_kumo_a_offset_centered, normal_kumo_b_offset_centered);
def kumo_min = Min(normal_kumo_a_offset_centered, normal_kumo_b_offset_centered);
def conversion_base_max = Max(normal_conversion_centered, normal_base_centered);
def conversion_base_min = Min(normal_conversion_centered, normal_base_centered);
def conversion_base_color = if normal_signal >= kumo_max then
if conversion_base_condition then 2 else 1 else
if normal_signal <= kumo_min then
if conversion_base_condition then -2 else -1 else 0;
def main_color = if color_on_conversion then conversion_base_color else na;
def bounce_flag;# = 0
def bounce_up;# = false
def bounce_down;# = false
if Crosses(normal_signal, kumo_max, CrossingDirection.BELOW) and bounce_flag == 0 {
bounce_flag = 1;
bounce_up = no;
bounce_down = no;
} else
if Crosses(normal_signal, kumo_min, CrossingDirection.BELOW) and bounce_flag == 1 {
bounce_flag = 0;
bounce_up = no;
bounce_down = no;
} else
if Crosses(normal_signal, kumo_min, CrossingDirection.ABOVE) and bounce_flag == 0 {
bounce_flag = -1;
bounce_up = no;
bounce_down = no;
} else
if Crosses(normal_signal, kumo_min, CrossingDirection.ABOVE) and bounce_flag == -1 {
bounce_flag = 0;
bounce_up = no;
bounce_down = no;
} else
if Crosses(normal_signal, kumo_max, CrossingDirection.ABOVE) and bounce_flag == 1 {
bounce_up = yes;
bounce_down = no;
bounce_flag = 0;
} else
if Crosses(normal_signal, kumo_min, CrossingDirection.BELOW) and bounce_flag == -1 {
bounce_up = no;
bounce_down = yes;
bounce_flag = 0;
} else {
bounce_up = no;
bounce_down = no;
bounce_flag = bounce_flag[1];
}
plot SigLine = if showSignalLine then normal_signal else na; # "Signal"
plot MovAvgLine = if showMovingAvg then normal_ma else na; # "Moving Average"
plot ChikouLine = if show_chikou and start then normal_chikou_centered else na;#, "Chikou"
plot base_plot = if show_conversion_base then normal_base_centered else na;#, "Base", base_color)
plot conversion_plot = if show_conversion_base then normal_conversion_centered else na;#, "Conversion",
SigLine.SetLineWeight(signalLineWidth);
SigLine.AssignValueColor(if isNaN(main_color) then GlobalColor("signal_color") else
if main_color==2 then GlobalColor("conv_bull") else
if main_color==1 then GlobalColor("base_bull") else
if main_color==-2 then GlobalColor("conv_bear") else
if main_color==-1 then GlobalColor("base_bear") else GlobalColor("n_color"));
ChikouLine.SetDefaultColor(Color.GRAY);
MovAvgLine.SetDefaultColor(GlobalColor("MovAvg"));
conversion_plot.SetDefaultColor(Color.CYAN);
base_plot.SetDefaultColor(GlobalColor("base_color"));
plot low_plot = if show_min_max and !Disabled then normal_low_level else na;#, "Low Range", low_color)
plot min_plot = if show_min_max and !Disabled then normal_min_level else na;#, "Bottom Range", min_color)
plot high_plot = if show_min_max and !Disabled then normal_high_level else na;#, "High Range", high_color)
plot max_plot = if show_min_max and !Disabled then normal_max_level else na;#, "Top Range", max_color)
plot zeroLine = if last[offset_val] then na else 0;
max_plot.SetDefaultColor(GlobalColor("max_color"));
high_plot.SetDefaultColor(GlobalColor("high_color"));
min_plot.SetDefaultColor(GlobalColor("min_color"));
low_plot.SetDefaultColor(GlobalColor("low_color"));
zeroLine.SetDefaultColor(Color.DARK_GRAY);
zeroLine.SetStyle(Curve.SHORT_DASH);
AddCloud(max_plot, high_plot, Color.DARK_GREEN); # "Top Fill"
AddCloud(low_plot, min_plot, Color.DARK_RED); # "Bottom Fill"
#// Plotting kumo
plot fill_top_plot = if show_kumo_lines then normal_plot_source_a[offset_val] else na;#, "Kumo Upper"
plot fill_bottom_plot = if show_kumo_lines then normal_plot_source_b[offset_val] else na;#, "Kumo Lower"
fill_top_plot.SetDefaultColor(GlobalColor("kumoBull"));
fill_bottom_plot.SetDefaultColor(GlobalColor("kumoBear"));
AddCloud( normal_plot_source_a[offset_val], normal_plot_source_b[offset_val], Color.DARK_GREEN, Color.DARK_RED);
#----Div-----------
def divSrc = normal_signal;
def h = high;
def l = low;
def pl_ = findpivots(divSrc,-1, Left, right);
def ph_ = findpivots(divSrc, 1, Left, right);
def pl = !isNaN(pl_);
def ph = !isNaN(ph_);
def pll = lowest(divSrc,Left);
def phh = highest(divSrc,Left);
def sll = lowest(l, Left);
def shh = highest(h, Left);
#-- Pvt Low
def plStart = if pl then yes else plStart[1];
def plFound = if (plStart and pl) then 1 else 0;
def vlFound1 = if plFound then divSrc else vlFound1[1];
def vlFound_ = if vlFound1!=vlFound1[1] then vlFound1[1] else vlFound_[1];
def vlFound = if !vlFound_ then pll else vlFound_;
def plPrice1 = if plFound then l else plPrice1[1];
def plPrice_ = if plPrice1!=plPrice1[1] then plPrice1[1] else plPrice_[1];
def plPrice = if !plPrice_ then sll else plPrice_;
#-- Pvt High
def phStart = if ph then yes else phStart[1];
def phFound = if (phStart and ph) then 1 else 0;
def vhFound1 = if phFound then divSrc else vhFound1[1];
def vhFound_ = if vhFound1!=vhFound1[1] then vhFound1[1] else vhFound_[1];
def vhFound = if !vhFound_ then phh else vhFound_;
def phPrice1 = if phFound then h else phPrice1[1];
def phPrice_ = if phPrice1!=phPrice1[1] then phPrice1[1] else phPrice_[1];
def phPrice = if !phPrice_ then sll else phPrice_;
#// Regular Bullish
def inRangePl = in_range(plFound[1],MinimumLookback, MaximumLookback);
def oscHL = divSrc > vlFound and inRangePl;
def priceLL = l < plPrice and divSrc <= 40;
def bullCond = plFound and oscHL and priceLL;
#// Hidden Bullish
def oscLL = divSrc < vlFound and inRangePl;
def priceHL = l > plPrice and divSrc <= 50;
def hiddenBullCond = plFound and oscLL and priceHL;
#// Regular Bearish
def inRangePh = in_range(phFound[1],MinimumLookback, MaximumLookback);
def oscLH = divSrc < vhFound and inRangePh;
def priceHH = h > phPrice and divSrc >= 60;;
def bearCond = phFound and oscLH and priceHH;
#// Hidden Bearish
def oscHH = divSrc > vhFound and inRangePh;
def priceLH = h < phPrice and divSrc >= 50;
def hiddenBearCond = phFound and oscHH and priceLH;
#------ Bubbles
def bullBub = RegularBullish and bullCond;
def HbullBub = HiddenBullish and hiddenBullCond;
def bearBub = RegularBearish and bearCond;
def HbearBub = HiddenBearish and hiddenBearCond;
addchartbubble(bullBub, divSrc , "R", color.GREEN, no);
addchartbubble(bearBub, divSrc , "R", Color.MAGENTA, yes);
addchartbubble(HbullBub, divSrc, "H", color.DARK_green, no);
addchartbubble(HbearBub, divSrc, "H", color.DARK_red, yes);
##### Lines
def bar = BarNumber();
#-- Bear Line
def lastPhBar = if ph then bar else lastPhBar[1];
def prePhBar = if lastPhBar!=lastPhBar[1] then lastPhBar[1] else prePhBar[1];
def priorPHBar = if bearCond then prePhBar else priorPHBar[1];
#-- Bull Line
def lastPlBar = if pl then bar else lastPlBar[1];
def prePlBar = if lastPlBar!=lastPlBar[1] then lastPlBar[1] else prePlBar[1];
def priorPLBar = if bullCond then prePlBar else priorPLBar[1];
def lastBullBar = if bullCond then bar else lastBullBar[1];
def lastBearBar = if bearCond then bar else lastBearBar[1];
def HighPivots = ph and bar >= HighestAll(priorPHBar) and bar <= HighestAll(lastBearBar);
def LowPivots = pl and bar >= HighestAll(priorPLBar) and bar <= HighestAll(lastBullBar);
def pivotHigh = if HighPivots then divSrc else na;
def pivotLow = if LowPivots then divSrc else na;
plot PlotHline = if RegularBearish then pivotHigh else na;
PlotHline.EnableApproximation();
PlotHline.SetDefaultColor(Color.MAGENTA);
plot PlotLline = if RegularBullish then pivotLow else na;
PlotLline.EnableApproximation();
PlotLline.SetDefaultColor(Color.GREEN);
#--- Hidden Lines
#-- Bear Line
def priorHPHBar = if hiddenBearCond then prePhBar else priorHPHBar[1];
#-- Bull Line
def priorHPLBar = if hiddenBullCond then prePlBar else priorHPLBar[1];
def lastHBullBar = if hiddenBullCond then bar else lastHBullBar[1];
def lastHBearBar = if hiddenBearCond then bar else lastHBearBar[1];
def HighHPivots = ph and bar >= HighestAll(priorHPHBar) and bar <= HighestAll(lastHBearBar);
def LowHPivots = pl and bar >= HighestAll(priorHPLBar) and bar <= HighestAll(lastHBullBar);
def pivotHidHigh = if HighHPivots then divSrc else na;
def pivotHidLow = if LowHPivots then divSrc else na;
plot PlotHBearline = if HiddenBearish then pivotHidHigh else na;
PlotHBearline.EnableApproximation();
PlotHBearline.SetDefaultColor(Color.PLUM);
plot PlotHBullline = if HiddenBullish then pivotHidLow else na;
PlotHBullline.EnableApproximation();
PlotHBullline.SetDefaultColor(Color.DARK_GREEN);
#-- END of CODE