#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © LonesomeTheBlue
#indicator(title="Ichimoku Oscillator", shorttitle="IO", explicit_plot_zorder = true)
# Converted by Sam4Cok@Samer800 - 02/2024
declare lower;
input conversionPeriods = 8; # "Conversion Line Length"
input basePeriods = 13; # "Base Line Length"
input LeadingSpanBLength = 26; # "Leading Span B Length"
input LaggingSpan = 13; # "Lagging Span"
input useAtr = yes; # "Use ATR", tooltip = "Protection from Whipsaw"
input atrLen = 9; # "?Length"
input atrMul = 2.0; # "?Mult",
input EnableBounceOff = no; # "Bounce Off Support/Resistance"
input useEma = yes; # "Show EMA"
input emaLen = 9; # "?Length"
input TakePartialProfitByEma = yes;
input ColoredBackground = yes; # "Colored Background"
def na = Double.NaN;
def last = isNaN(close);
#-- Colors
DefineGlobalColor("upcol1", CreateColor(0 , 250, 0)); # "Uptrend Colors"
DefineGlobalColor("upcol2", CreateColor(0 , 190, 0));
DefineGlobalColor("upcol3", CreateColor(0 , 130, 0));
DefineGlobalColor("upcol4", CreateColor(0 , 70, 0));
DefineGlobalColor("dbcol1", CreateColor(250, 0, 0)); # "Downtrend Colors"
DefineGlobalColor("dbcol2", CreateColor(190, 0, 0));
DefineGlobalColor("dbcol3", CreateColor(130, 0, 0));
DefineGlobalColor("dbcol4", CreateColor(70, 0, 0));
DefineGlobalColor("inSRcol", CreateColor(209,212,220));
DefineGlobalColor("emacol", CreateColor(209,196,233));
#// ichimoku calculation
script donchian {
input len = 9;
def hh = Highest(high, len);
def ll = Lowest(low, len);
def donchian = (ll + hh) / 2;
plot out = donchian;
}
def conversionLine = donchian(conversionPeriods);
def baseLine = donchian(basePeriods);
def leadLine1 = (conversionLine + baseLine) / 2;
def leadLine2 = donchian(LeadingSpanBLength);
#// Oscillator calculation
def cloudup = leadLine1 >= leadLine2;
def clouddn = leadLine1 <= leadLine2;
def CloudMin = Min(leadLine1[LaggingSpan - 1], leadLine2[LaggingSpan - 1]);
def CloudMax = Max(leadLine1[LaggingSpan - 1], leadLine2[LaggingSpan - 1]);
def inthecloud = close >= CloudMin and close <= CloudMax;
def mtrd = if close > CloudMax then 1 else if close < CloudMin then -1 else mtrd[1];
def mTrend = if isNaN(mtrd) then 0 else mtrd;
#// first layer
def Oscline = if mtrend == 1 then (close - CloudMin) else (close - CloudMax);
#//second layer
def Lagging = Oscline + (if mtrend == 1 then Max(close - CloudMax[LaggingSpan - 1], 0) else
Min(close - CloudMin[LaggingSpan - 1], 0));
#//third layer
def ConvBase = Lagging + (if mtrend == 1 then Max((conversionLine - baseLine), 0) else Min((conversionLine - baseLine), 0));
#// fourth layer
def cloud = ConvBase + (if mtrend == 1 then Max(leadLine1 - leadLine2, 0) else Min(leadLine1 - leadLine2, 0));
def anyofthemrising = (conversionLine - conversionLine[1]) > 0 or (baseLine - baseLine[1]) > 0;
def anyofthemfalling = (conversionLine - conversionLine[1]) < 0 or (baseLine - baseLine[1]) < 0;
def convoverbase = conversionLine >= baseLine;
def baseoverconv = conversionLine <= baseLine;
#// little Protection from whipsaw
def tole = if useatr then ATR(Length = atrlen) * atrmul else 0;
#// calculation of the trend and getting entry level if trend appears
def entrylevel;
def preTrend;
def trend;
if mtrend == 1 {
preTrend = if mtrend[1] == -1 then 0 else trend[1];
if preTrend < 4 and close > CloudMax {
trend = (if Lagging > Oscline then 1 else 0) +
(if convoverbase and anyofthemrising then 1 else 0) +
(if cloudup then 1 else 0) + 1;
entrylevel = if trend == 4 then close else entrylevel[1];
} else {
trend = (if conversionLine < baseLine - tole then 0 else preTrend);
entrylevel = entrylevel[1];
}
} else if mtrend == -1 {
preTrend = if mtrend[1] == 1 then 0 else trend[1];
if preTrend > - 4 and close < CloudMin {
trend = (if Lagging < Oscline then -1 else 0) -
(if baseoverconv and anyofthemfalling then 1 else 0) -
(if clouddn then 1 else 0) - 1;
entrylevel = if trend == -4 then close else entrylevel[1];
} else {
trend = (if conversionLine > baseLine + tole then 0 else preTrend);
entrylevel = entrylevel[1];
}
} else {
preTrend = preTrend[1];
trend = if last then na else preTrend;
entrylevel = entrylevel[1];
}
#// Background coloring if Enabled
def dir = if !isNaN(trend) then trend else 0;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
def uptmult = 159 + power(trend, 3);
def dntmult = 159 - power(trend, 3);
def trendExtUp = (dir > 2) or (dir[1] > 2);
def trendExtDn = (dir <-2) or (dir[1] <-2);
def trendUp = (dir > 0) or (dir[1] > 0);
def trendDn = (dir < 0) or (dir[1] < 0);
AddCloud(if ColoredBackground then if trendExtUp then pos else na else na, neg, Color.DARK_GREEN);
AddCloud(if ColoredBackground then if trendExtDn then pos else na else na, neg, Color.DARK_RED);
AddCloud(if ColoredBackground then if trendUp then pos else na else na, neg, GlobalColor("upcol4"));
AddCloud(if ColoredBackground then if trendDn then pos else na else na, neg, GlobalColor("dbcol4"));
#/ ploting layers
def area = cloud;
def baseArea = ConvBase;
def lagLine = Lagging;
def oscArea = Oscline;
def ConvBaseCol = if conversionLine >= baseLine then 1 else - 1;#? upcol3 : dbcol3
def lagCol = if close > CloudMax then 1 else if close < CloudMin then -1 else 0;
def Osccolor = if inthecloud then 0 else if close > CloudMax then 1 else -1;#? upcol1 : dbcol1
AddCloud(if Osccolor==0 or Osccolor[1] == 0 then oscArea else na, 0, GlobalColor("inSRcol"), GlobalColor("inSRcol"), yes);
AddCloud(if Osccolor > 0 or Osccolor[1] > 0 then oscArea else na, 0, GlobalColor("upcol1"), GlobalColor("upcol1"), yes);
AddCloud(if Osccolor < 0 or Osccolor[1] < 0 then oscArea else na, 0, GlobalColor("dbcol1"), GlobalColor("dbcol1"), yes);
AddCloud(if lagCol > 0 or lagCol[1] > 0 then lagLine else na, 0, GlobalColor("upcol2"), GlobalColor("upcol2"), yes);
AddCloud(if lagCol < 0 or lagCol[1] < 0 then lagLine else na, 0, GlobalColor("dbcol2"), GlobalColor("dbcol2"), yes);
AddCloud(if ConvBaseCol > 0 or ConvBaseCol[1] > 0 then baseArea else na, 0, GlobalColor("upcol3"), GlobalColor("upcol3"), yes);
AddCloud(if ConvBaseCol < 0 or ConvBaseCol[1] < 0 then baseArea else na, 0, GlobalColor("dbcol3"), GlobalColor("dbcol3"), yes);
AddCloud(area, 0, GlobalColor("upcol4"), GlobalColor("dbcol4"));
#// closing main position
def closetrade = (trend-trend[1]) != 0 and AbsValue(trend[1]) == 4;
plot closeShape = if closetrade then cloud else na;
closeShape.SetPaintingStrategy(PaintingStrategy.SQUARES);
closeShape.AssignValueColor(if trend[1] > 0 then Color.WHITE else Color.YELLOW);
#// take long position
def uptrend = trend == 4 and (trend-trend[1]) != 0;
plot LongShape = if uptrend then 0 else na;
LongShape.SetLineWeight(2);
LongShape.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
LongShape.SetDefaultColor(Color.CYAN);
def bouncbackup = EnableBounceOff and trend == 4 and close > CloudMax and inthecloud[1];
plot bounceUp = if bouncbackup then 0 else na;
bounceUp.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
bounceUp.SetDefaultColor(Color.VIOLET);
#// take short position
def downtrend = trend == -4 and (trend-trend[1]) != 0;
plot shortShape = if downtrend then 0 else na;
shortShape.SetLineWeight(2);
shortShape.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
shortShape.SetDefaultColor(Color.MAGENTA);
def bouncbackdn = EnableBounceOff and trend == -4 and close < CloudMin and inthecloud[1];
plot bounceDn = if bouncbackdn then 0 else na;
bounceDn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
bounceDn.SetDefaultColor(Color.PLUM);
#/ plot ema line if enabled
def emaline = ExpAverage(cloud, emalen);
plot ema = if useema then emaline else na;
ema.SetDefaultColor(GlobalColor("emacol"));
#// possible/partially take profit if profitable
def convcrossbase = Crosses(conversionLine, baseLine, CrossingDirection.ABOVE);
def basecrosscons = Crosses(baseLine, conversionLine, CrossingDirection.ABOVE);
def emacrosscloud = Crosses(emaline, cloud, CrossingDirection.ABOVE);
def cloudcrossema = Crosses(cloud, emaline, CrossingDirection.ABOVE);
def profitable = trend == 4 and close > entrylevel or trend == -4 and close < entrylevel;
def posexitcol = if profitable then
if trend == 4 and basecrosscons then 1 else
if trend == 4 and emacrosscloud and TakePartialProfitByEma then 0 else
if trend ==-4 and convcrossbase then -1 else
if trend ==-4 and cloudcrossema and TakePartialProfitByEma then 0 else na else na;
plot profit = posexitcol;
profit.SetPaintingStrategy(PaintingStrategy.POINTS);
profit.AssignValueColor(if posexitcol > 0 then Color.WHITE else
if posexitcol < 0 then Color.YELLOW else GlobalColor("emacol"));
#-- END of CODE