#//AK MACD BB
#//created by Algokid
#study("AK MACD BB v 1.00")
# Converted and mod by Sam4Cok@Samer800 - 10/2022
declare lower;
def na = Double.NaN;
input ColorBar = no;
input colorBackground = yes;
input bbLength = 10; # "BB Periods"
input BBDeviation = 1.0; # "Deviations"
input macdSmoothing = 14;
input ShowCloud = no;
input DynamicBand = no;
input DynamicPerios = 100;
input DynamicPerCent = 100;
input source = close;
#//MACD
input maSource = {Default EMA, SMA}; # "MACD MA"
input fastLength = 12;
input slowLength = 26;
def fastMA = if maSource == maSource.EMA then ExpAverage(source, fastLength)
else SimpleMovingAvg(source, fastLength);
def slowMA = if maSource == maSource.EMA then ExpAverage(source, slowLength)
else SimpleMovingAvg(source, slowLength);;
def macd = fastMA - slowMA;
#//BollingerBands
def Std = StDev(macd, bbLength);
def Upper = (Std * BBDeviation + (SimpleMovingAvg(macd, bbLength)));
def Lower = ((SimpleMovingAvg(macd, bbLength)) - (Std * BBDeviation));
plot Band1 = Upper; # "Upper Band"
Band1.SetHiding(DynamicBand);
Band1.SetDefaultColor(Color.DARK_GRAY);
plot Band2 = Lower; # "lower Band"
Band2.SetHiding(DynamicBand);
Band2.SetDefaultColor(Color.DARK_GRAY);
def mc = if macd >= Upper then 1 else
if macd <= Lower then -1 else 0;
#// Indicator
plot macdLine = macd;
macdLine.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
macdLine.AssignValueColor(if mc > 0 then Color.GREEN else
if mc < 0 then Color.RED else Color.DARK_RED);
plot zeroline = if IsNaN(close) then na else 0;
zeroline.SetDefaultColor(Color.GRAY);
zeroline.SetHiding(DynamicBand);
#zeroline.SetStyle(Curve.LONG_DASH);
# Dynamic OB / OS
def macdHi = Highest(macd, DynamicPerios);
def macdLo = Lowest (macd, DynamicPerios);
def RSIDiff = (macdHi - macdLo) * (DynamicPerCent / 100);
def UpperLine = SimpleMovingAvg(macdLo + RSIDiff, DynamicPerios);
def LowerLine = SimpleMovingAvg(macdHi - RSIDiff, DynamicPerios);
plot OBband = UpperLine;
OBband.SetDefaultColor(Color.DARK_GRAY);
OBband.SetHiding(!DynamicBand);
plot OSband = LowerLine;
OSband.SetDefaultColor(Color.DARK_GRAY);
OSband.SetHiding(!DynamicBand);
def MidDynamicBand = (UpperLine + LowerLine) / 2;
### DynamicBand 2
def macdSmooth = ExpAverage(macd, macdSmoothing);
def offs = (1.6185 * StDev(macd, DynamicPerios));
def upBand = macdSmooth + offs;
def dnBand = macdSmooth - offs;
plot Oband = upBand;
Oband.SetDefaultColor(Color.DARK_GRAY);
Oband.SetHiding(!DynamicBand);
plot Sband = dnBand;
Sband.SetDefaultColor(Color.DARK_GRAY);
Sband.SetHiding(!DynamicBand);
#---- Signal Line
def SigUp = mc > 0 and Sband < OSband;
def SigDn = mc < 0 and Oband > OBband;
plot Signal = if SigUp then if(DynamicBand,MidDynamicBand,1) else
if SigDn then if(DynamicBand,MidDynamicBand,1) else na;
Signal.SetStyle(Curve.POINTS);
Signal.AssignValueColor(if SigUp then Color.CYAN else
if SigDn then Color.MAGENTA else Color.DARK_GRAY);
plot Midband = if !SigUp and !SigDn then MidDynamicBand else na;
Midband.SetStyle(Curve.MEDIUM_DASH);
Midband.AssignValueColor(Color.GRAY);
Midband.SetHiding(!DynamicBand);
#--- Background
AddCloud(if colorBackground and sigUp then Double.POSITIVE_INFINITY else na,
Double.NEGATIVE_INFINITY,CreateColor(4, 103, 117));
AddCloud(if colorBackground and sigDn then Double.POSITIVE_INFINITY else na,
Double.NEGATIVE_INFINITY,CreateColor(100, 2, 117));
#------ cloud -----
AddCloud(if macd > OBband then Oband else na,if ShowCloud then OBband else na, Color.DARK_RED);
AddCloud(if macd < OSband then OSband else na,if ShowCloud then Sband else na , Color.DARK_GREEN);
AddCloud(if DynamicBand then OBband else na, OSband, Color.DARK_GRAY);
AddCloud(if OBband < Oband then Oband else na, if ShowCloud then OBband else na, Color.DARK_RED);
AddCloud(if OSband > Sband then OSband else na, if ShowCloud then Sband else na, Color.DARK_GREEN);
AddCloud(Band1, Band2, Color.BLUE);
# BarColor
AssignPriceColor(if ColorBar then if mc > 0 then Color.CYAN else
if mc < 0 then Color.MAGENTA else Color.GRAY else Color.CURRENT);
#--- Div
input LookBack = 5;
input DivBull = no; # "Plot Bullish"
input DivHiddenBull = no; # "Plot Hidden Bullish"
input DivBear = no; # "Plot Bearish"
input DivHiddenBear = no; # "Plot Hidden Bearish"
def divSrc = macd;
def H = high; # high price
def L = low; # low price
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
##############
_BN = BarNumber();
_nan = Double.NaN;
_VStop = if !isNaN(dat) 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;
}
script valuewhen {
input cond = 0;
input pr = 0;
input n2 = 0;
def n = n2 + 1;
def offset2 = fold j = 0 to 200
with p
while p < n + 1
do p + ( if p == n then j - n else if GetValue(cond, j) then 1 else 0 );
plot price = GetValue(pr, offset2-1);
}
#_inRange(cond) =>
script _inRange {
input cond = yes;
input rangeUpper = 60;
input rangeLower = 5;
def bars = if cond then 0 else bars[1] + 1;
def inrange = (rangeLower <= bars) and (bars <= rangeUpper);
plot retrun = inRange;
}
def ph = findpivots(divSrc, 1, LookBack, LookBack);
def pl = findpivots(divSrc, -1, LookBack, LookBack);
def phFound = if isNaN(ph) then 0 else 1;
def plFound = if isNaN(pl) then 0 else 1;
def vhFound = valuewhen(phFound, divSrc, 1);
def vlFound = valuewhen(plFound, divSrc, 1);
def phPrice = valuewhen(phFound, h, 1);
def plPrice = valuewhen(plFound, l, 1);
#// Regular Bullish
def oscHL = divSrc > vlFound and _inRange(plFound[1],60,5);
def priceLL = l < plPrice;
def bullCond = DivBull and plFound and oscHL and priceLL;
#// Hidden Bullish
def oscLL = divSrc < vlFound and _inRange(plFound[1],60,5);
def priceHL = l > plPrice;
def hiddenBullCond = DivHiddenBull and plFound and oscLL and priceHL;
#// Regular Bearish
def oscLH = divSrc < vhFound and _inRange(phFound[1],60,5);
def priceHH = h > phPrice;
def bearCond = DivBear and phFound and oscLH and priceHH;
#// Hidden Bearish
def oscHH = divSrc > vhFound and _inRange(phFound[1],60,5);
def priceLH = h < phPrice;
def hiddenBearCond = DivHiddenBear and phFound and oscHH and priceLH;
#----Bubbles
addchartbubble(bullCond, divSrc, "R", CreateColor(4, 188, 217), no);
addchartbubble(bearCond, divSrc, "R", CreateColor(216, 0, 255), yes);
addchartbubble(hiddenBullCond, divSrc, "H", color.DARK_green, no);
addchartbubble(hiddenBearCond, divSrc, "H", color.DARK_red, yes);
#---- END