Candlestick & Moving Avg Library for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
below code can detect 40 candlestick pattern and option to plot 15 different moving average where you can add to your trading arsenal.

CSS:
#/ ================================== //
#// --------> Candlestick Library<---------- //
#// ================================== //

input trendRule = {default "Trend Rule 1", "Trend Rule 2", "No detection"};    #"Detect Trend Based On"
input maType  = {default SMA, EMA, SMMA, WMA, VWMA, RMS, DEMA, TEMA, ZLSMA, ZLDEMA, ZLTEMA, McGinley, HMA, ALMA, SWMA, Gaussian};
input src = close;
input ShowMovingAvg = yes;
input priceAvgLength = 50;
input MovAvgLength1 = 50;
input MovAvgLength2 = 200;
input EngulfingMustEngulfWick = no;  # "Engulfing Must Engulf Wick"
input TweezerCloseHalf        = no;  # "Tweezer Close Over Half"
input rejectWickMax = 0.0; # "[EC] Max Reject Wick Size" 
input hammerFib     = 33;  # "[HS] H&S Ratio (%)"
input hsShadowPerc  = 5.0; # "[HS] H&S Max Shadow (%)"
input dojiSize      = 5.0; # "[DJ] Doji Size (%)"
input dojiWickSize  = 2.0; # "[DJ] Max Doji Wick Size"
input LongShadowRatio = 75;  # "[LS] Long Shadow (%)"
input spinWickSize    = 34;  # "[ST] Spinning Top Wick Size"
input SoldiersCrowsWick = 5;   # "[SC] Soldiers and Crows Wick"
input doji            = no;
input bullEngulf      = no;
input bearEngulf      = no;
input hammer          = no;
input star            = no;
input dragonflyDoji   = no;
input tweezerBottom   = no;
input tweezerTop      = no;
input spinningTopBull = no;
input spinningTopBear = no;
input morningStar     = no;
input eveningStar     = no;
input haramiBull      = no;
input haramiBear      = no;
input haramiBullCross = no;
input haramiBearCross = no;
input marubullzu      = no;
input marubearzu      = no;
input abandonedBull   = no;
input abandonedBear   = no;
input piercing        = no;
input darkCloudCover  = no;
input tasukiBull      = no;
input tasukiBear      = no;
input risingThree     = no;
input fallingThree    = no;
input risingWindow    = no;
input fallingWindow   = no;
input kickingBull     = no;
input kickingBear     = no;
input lls             = no;
input lus             = no;
input bullNeck        = no;
input bearNeck        = no;
input soldiers        = no;
input crows           = no;
input triStarBull     = no;
input triStarBear     = no;
input insidebar       = no;
input doubleInsideBar = no;

#// ] -------------- FUNCTIONS : Moving Avg ------------------ [
script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if !isNaN(data) then data else repl;
    plot return = ret_val;
}
#rms(source, length)=>
script rms {
input source = close;
input length = 14;
    def rms = sqrt(sum(power(source, 2), length)/length);
plot return = rms;
}
#Gaussianma(values, length) =>
script Gaussian {
    input values = close;
    input length = 20;
    def stddev = length / 4;
    def indices = length - 1;
    def weights = Exp(-0.5 * (Power((indices - length), 2) / Power(stddev, 2)));
    def sum = Sum(values * weights, length);
    def gMA = sum / Sum(weights, length);
    plot return = gMA;
}
#pine_swma(source) =>
script swma {
input source = close;
    def swma = source[3] * 1 / 6 + source[2] * 2 / 6 +  source[1] * 2 / 6 + source[0] * 1 / 6;
    plot retun = swma;
}
#export zlSma(float src, simple int len) =>
script zlSma {
    input src = close;
    input len = 14;
    def lsma = Inertia(src, len);
    def lsma2 = Inertia(lsma, len);
    def eq = lsma - lsma2;
    def zlsma = lsma + eq;
    plot return = zlsma;
}
#export zlDema(float src, simple int len) =>
script zlDema {
    input src = close;
    input len = 14;
    def zdema1 = ExpAverage(src, len);
    def zdema2 = ExpAverage(zdema1, len);
    def dema1 = 2 * zdema1 - zdema2;
    def zdema12 = ExpAverage(dema1, len);
    def zdema22 = ExpAverage(zdema12, len);
    def zldema = 2 * zdema12 - zdema22;
    plot return = zldema;
}
#export zlTema(float src, simple int len) =>
script zlTema {
    input src = close;
    input len = 14;
    def ema1 = ExpAverage(src, len);
    def ema2 = ExpAverage(ema1, len);
    def ema3 = ExpAverage(ema2, len);
    def tema1 = 3 * (ema1 - ema2) + ema3;
    def ema1a = ExpAverage(tema1, len);
    def ema2a = ExpAverage(ema1a, len);
    def ema3a = ExpAverage(ema2a, len);
    def zltema = 3 * (ema1a - ema2a) + ema3a;
    plot return = zltema;
}
#pine_alma(series, windowsize, offset, sigma) =>
script ALMA {
  input series = close;
  input windowsize = 9;
  input Offset = 0.85;
  input Sigma = 6;

    def m = Offset * (windowsize - 1);
    def s = windowsize / Sigma;

    def norm  = fold z = 0 to windowsize with CW do
         CW + Exp(-(Sqr(z - m)) / (2 * Sqr(s)));

    def sum  = fold y = 0 to windowsize with WS do
         WS + Exp(-(Sqr(y - m)) / (2 * Sqr(s))) * GetValue(series, windowsize - 1 - y);

    plot ALMA = sum  / norm ;
}
#export mcginley(float src, simple int len)=>
script mcginley {
    input src = close;
    input len = 14;
    def mg;
    def t = ExpAverage(src, len);
    mg = if IsNaN(mg[1]) then t else
        CompoundValue(1 ,mg[1] + (src - mg[1]) / (len * Power(src / mg[1], 4)),src);
    plot return = mg;
}
#vwma(source, length)
script VWMA {
    input src = close;
    input len = 15;
    def v = volume;
    def VWMA = SimpleMovingAvg(src * nz(v,1), len) / SimpleMovingAvg(nz(v,1), len);
    plot result = VWMA;
}
#export multiMa(float source, simple int length, string type) =>
script multiMa {
    input source = close;
    input length = 14;
    input type = "SMA";
    def multiMa =
        if type == "SMA"    then SimpleMovingAvg(source, length) else
        if type == "EMA"    then ExpAverage(source, length) else
        if type == "SMMA"   then WildersAverage(source, length) else
        if type == "WMA"    then WMA(source, length) else
        if type == "VWMA"   then vwma(source, length) else
        if type == "DEMA"   then DEMA(source, length) else
        if type == "TEMA"   then TEMA(source, length) else
        if type == "RMS"   then RMS(source, length) else
        if type == "ZLSMA"  then zlSma(source, length) else
        if type == "ZLDEMA" then zlDema(source, length) else
        if type == "ZLTEMA" then zlTema(source, length) else
        if type == "McGinley" then mcginley(source, length) else
        if type == "ALMA" then ALMA(source, length) else
        if type == "SWMA" then SWMA(source) else
        if type == "Gaussian"   then Gaussian(source, length) else
        if type == "HMA"    then  HullMovingAvg(source, length ) else Double.NaN;
    plot return = multiMa;
}
#--- Patterns
#doji(float dojiSize = 5.0, float dojiWickSize = 2.0) =>
script doji {
    input dojiSize = 5.0;
    input dojiWickSize = 2.0;
    def topWickSize = AbsValue(Max(close, open) - high);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def bodySize       = AbsValue( close - open);
    def candleSize     = AbsValue( high  - low);
    def bodyPcnt       = bodySize / candleSize * 100;
    def calcDoji = topWickSize <= bottomWickSize * dojiWickSize and bottomWickSize <= topWickSize * dojiWickSize;
    plot result  = bodyPcnt <= dojiSize and calcDoji;
}
#bullEngulf(float maxRejectWick = 0.0, bool mustEngulfWick = false) =>
script bullEngulf {
    input maxRejectWick = 0.0;
    input mustEngulfWick = no;
    def topWickSize = AbsValue(Max(close, open) - high);
    def bodySize       = AbsValue( close - open);
    def rejectionRule = maxRejectWick == 0.0 or topWickSize / bodySize < (maxRejectWick / 100);
    plot result = close[1] <= open[1] and close >= open[1] and open <= close[1] and rejectionRule and (!mustEngulfWick or close >= high[1]) and bodySize > 0;
}
#bearEngulf(float maxRejectWick = 0.0, bool mustEngulfWick = false) =>
script bearEngulf {
    input maxRejectWick = 0.0;
    input mustEngulfWick = no;
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def bodySize       = AbsValue( close - open);
    def rejectionRule = maxRejectWick == 0.0 or bottomWickSize / bodySize < (maxRejectWick / 100);
    plot result = close[1] >= open[1] and close <= open[1] and open >= close[1] and rejectionRule and (!mustEngulfWick or close <= low[1]) and bodySize > 0;
}
#hammer(float ratio = 33, float shadowPercent = 5.0) =>
script hammer {
    input ratio = 33;
    input shadowPercent = 5.0;
    def topWickSize = AbsValue(Max(close, open) - high);
    def bodySize    = AbsValue( close - open);
    def bodyLow     = Min( close, open);
    def bullRatio = (low - high) * (ratio / 100) + high;
    def hasShadow = topWickSize > shadowPercent / 100 * bodySize;
    plot result   = bodySize > 0 and bodyLow >= bullRatio and !hasShadow;
}
#star(float ratio = 33, float shadowPercent = 5.0) =>
script star {
    input ratio = 33;
    input shadowPercent = 5.0;
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def bodySize       = AbsValue( close - open);
    def bodyHigh       = Max( close, open);
    def bearRatio = (high - low) * (ratio / 100) + low;
    def hasShadow = bottomWickSize > shadowPercent / 100 * bodySize;
    plot result   = bodySize > 0 and bodyHigh <= bearRatio and !hasShadow;
}
#dragonflyDoji() =>
script dragonflyDoji {
    def topWickSize = AbsValue(Max(close, open) - high);
    def bodySize    = AbsValue( close - open);
    def candleSize  = AbsValue( high  - low);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    plot result = bodyIsDoji  and topWickSize <= bodySize;
}
#tweezerBottom(bool closeUpperHalf = false) =>
script tweezerBottom {
    input closeUpperHalf = no;
    def upperHalf = close > hl2[1];
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def topWickSize = AbsValue(Max(close, open) - high);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def candleSize  = AbsValue( high  - low);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    def topShadow   = topWickSize > 5 / 100 * bodySize;
    def bottomShadow = bottomWickSize > 5 / 100 * bodySize;
    def tallBody     = bodySize > bodyAvg;
    plot result   = (!bodyIsDoji or (topShadow and bottomShadow)) and AbsValue(low - low[1]) <= bodyAvg * 0.05 and dwnCandle[1] and upCandle and tallBody[1] and (!closeUpperHalf or (closeUpperHalf and upperHalf));
}
#tweezerTop(bool closeLowerHalf = false) =>
script tweezerTop {
    input closeLowerHalf = no;
    def lowerHalf = close < hl2[1];
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def topWickSize = AbsValue(Max(close, open) - high);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def candleSize  = AbsValue( high  - low);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    def topShadow   = topWickSize > 5 / 100 * bodySize;
    def bottomShadow = bottomWickSize > 5 / 100 * bodySize;
    def tallBody     = bodySize > bodyAvg;
    plot result   = (!bodyIsDoji  or (topShadow and bottomShadow)) and AbsValue(high - high[1]) <= bodyAvg * 0.05 and upCandle[1] and dwnCandle and tallBody[1] and (!closeLowerHalf or (closeLowerHalf and lowerHalf));
}
#spinningTopBull(float wickSize = 34) =>
script spinningTopBull {
    input wickSize = 34;
    def upCandle  = close > open;
    def candleSize  = AbsValue( high  - low);
    def topWickSize = AbsValue(Max(close, open) - high);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def bodySize    = AbsValue( close - open);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    plot result = bottomWickSize >= candleSize / 100 * wickSize and topWickSize >= candleSize / 100 * wickSize and upCandle and !bodyIsDoji;
}
#spinningTopBear(float wickSize = 34) =>
script spinningTopBear {
    input wickSize = 34;
    def dwnCandle = close < open;
    def candleSize  = AbsValue( high  - low);
    def topWickSize = AbsValue(Max(close, open) - high);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def bodySize    = AbsValue( close - open);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    plot result = bottomWickSize >= candleSize / 100 * wickSize and topWickSize >= candleSize / 100 * wickSize and dwnCandle and !bodyIsDoji;
}
#spinningTop(float wickSize = 34) =>
script spinningTop {
    input wickSize = 34;
    def topWickSize = AbsValue(Max(close, open) - high);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def candleSize  = AbsValue( high  - low);
    def bodySize    = AbsValue( close - open);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    plot result = bottomWickSize >= candleSize / 100 * wickSize and topWickSize >= candleSize / 100 * wickSize and !bodyIsDoji;
}
#morningStar() =>
script morningStar {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodyHigh  = Max( close, open);
    def bodyLow   = Min( close, open);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    def bodyUpGap   = bodyHigh[1] < bodyLow;
    def bodyDwnGap  = bodyHigh    < bodyLow[1];
    def middleBody  = bodySize / 2 + bodyLow;
    plot result = tallBody[2] and shortBody[1] and tallBody and dwnCandle[2] and bodyDwnGap[1] and upCandle and bodyHigh >= middleBody[2] and bodyHigh < bodyHigh[2] and bodyUpGap;
}
#eveningStar() =>
script eveningStar {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodyHigh  = Max( close, open);
    def bodyLow   = Min( close, open);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    def bodyUpGap   = bodyHigh[1] < bodyLow;
    def bodyDwnGap  = bodyHigh    < bodyLow[1];
    def middleBody  = bodySize / 2 + bodyLow;
    plot result = tallBody[2] and shortBody[1] and tallBody and upCandle[2] and bodyUpGap[1] and dwnCandle and bodyLow <= middleBody[2] and bodyLow > bodyLow[2] and bodyDwnGap;
}
#haramiBull() =>
script haramiBull {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodyHigh  = Max( close, open);
    def bodyLow   = Min( close, open);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    plot result = tallBody[1] and dwnCandle[1] and upCandle and shortBody and high <= bodyHigh[1] and low >= bodyLow[1];
}
#haramiBear() =>
script haramiBear {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodyHigh  = Max( close, open);
    def bodyLow   = Min( close, open);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    plot result = tallBody[1] and upCandle[1] and dwnCandle and shortBody and high <= bodyHigh[1] and low >= bodyLow[1];
}
#haramiBullCross() =>
script haramiBullCross {
    def dwnCandle = close < open;
    def bodyHigh  = Max( close, open);
    def bodyLow   = Min( close, open);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def candleSize  = AbsValue( high  - low);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    plot result = tallBody[1] and dwnCandle[1] and bodyIsDoji  and high <= bodyHigh[1] and low >= bodyLow[1];
}
#haramiBearCross() =>
script haramiBearCross {
    def upCandle  = close > open;
    def bodyHigh  = Max( close, open);
    def bodyLow   = Min( close, open);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def candleSize  = AbsValue( high  - low);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    plot result = tallBody[1] and upCandle[1] and bodyIsDoji  and high <= bodyHigh[1] and low >= bodyLow[1];
}
#marubullzu() =>
script marubullzu {
    def upCandle  = close > open;
    def topWickSize = AbsValue(Max(close, open) - high);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    plot result = upCandle and tallBody and  5 > topWickSize / bodySize * 100 and 5 > bottomWickSize / bodySize * 100;
}
#marubearzu() =>
script marubearzu {
    def dwnCandle = close < open;
    def topWickSize = AbsValue(Max(close, open) - high);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    plot result = dwnCandle and tallBody and  5 > topWickSize / bodySize * 100 and 5 > bottomWickSize / bodySize * 100;
}
#abandonedBull() =>
script abandonedBull {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def upGap     = low    > high[1];
    def dwnGap    = low[1] > high;
    def bodySize    = AbsValue( close - open);
    def candleSize  = AbsValue( high  - low);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    plot result = dwnCandle[2] and bodyIsDoji [1] and dwnGap[1] and upCandle and upGap;
}
#abandonedBear() =>
script abandonedBear {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def upGap     = low    > high[1];
    def dwnGap    = low[1] > high;
    def bodySize    = AbsValue( close - open);
    def candleSize  = AbsValue( high  - low);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    plot result = upCandle[2] and bodyIsDoji [1] and upGap[1] and dwnCandle and dwnGap;
}
#piercing() =>
script piercing {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def bodyLow   = Min( close, open);
    def middleBody  = bodySize / 2 + bodyLow;
    plot result = dwnCandle[1] and tallBody[1] and upCandle and open <= low[1] and close > middleBody[1] and close < open[1];
}
#darkCloudCover() =>
script darkCloudCover {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def bodyLow   = Min( close, open);
    def middleBody  = bodySize / 2 + bodyLow;
    plot result = upCandle[1] and tallBody[1] and dwnCandle and open >= high[1] and close < middleBody[1] and close > open[1];
}
#tasukiBull() =>
script tasukiBull {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodyHigh  = Max( close, open);
    def bodyLow   = Min( close, open);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    def bodyUpGap   = bodyHigh[1] < bodyLow;
    plot result =  tallBody[2] and shortBody[1] and upCandle[2] and bodyUpGap[1] and upCandle[1] and dwnCandle and bodyLow >= bodyHigh[2] and bodyLow <= bodyLow[1];
}
#tasukiBear() =>
script tasukiBear {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodyHigh  = Max( close, open);
    def bodyLow   = Min( close, open);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    def bodyDwnGap  = bodyHigh < bodyLow[1];
    plot result = tallBody[2] and shortBody[1] and dwnCandle[2] and bodyDwnGap[1] and dwnCandle[1] and upCandle and bodyHigh <= bodyLow[2] and bodyHigh >= bodyHigh[1];
}
#risingThree() =>
script risingThree {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    plot result = tallBody[4] and upCandle[4] and shortBody[3] and dwnCandle[3] and open[3] < high[4] and close[3] > low[4] and shortBody[2] and dwnCandle[2] and open[2] < high[4] and close[2] > low[4] and shortBody[1] and dwnCandle[1] and  open[1] < high[4]   and close[1] > low[4] and tallBody and upCandle and close > close[4];
}
#fallingThree() =>
script fallingThree {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    plot result = tallBody[4] and dwnCandle[4] and shortBody[3] and upCandle[3] and open[3] > low[4] and close[3] < high[4] and shortBody[2] and upCandle[2] and open[2] > low[4] and close[2] < high[4]  and shortBody[1]  and upCandle[1] and open[1] > low[4] and close[1] < high[4] and tallBody and dwnCandle and close < close[4];
}
#risingWindow() =>
script risingWindow {
    def candleSize     = AbsValue( high  - low);
    plot result = candleSize != 0 and candleSize[1] != 0 and low > high[1];
}
#fallingWindow() =>
script fallingWindow {
    def candleSize     = AbsValue( high  - low);
    plot result = candleSize != 0 and candleSize[1] != 0 and high < low[1];
}
#kickingBull() =>
script kickingBull {
    def upGap     = low > high[1];
    plot result = marubearzu()[1] and marubullzu() and upGap;
}
#kickingBear() =>
script kickingBear {
    def dwnGap    = low[1] > high;
    plot result  = marubullzu()[1] and marubearzu() and dwnGap;
}
#lls(float ratio = 75) =>"Long Lower Shadow" candle patterns
script lls {
    input ratio = 75;
    def candleSize = AbsValue( high  - low);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    plot result = bottomWickSize > candleSize / 100 * ratio;
}
#lus(float ratio = 75) =>"Long Upper Shadow" candle patterns
script lus {
    input ratio = 75;
    def candleSize = AbsValue( high  - low);
    def topWickSize = AbsValue(Max(close, open) - high);
    plot result = topWickSize > candleSize / 100 * ratio;
}
#bullNeck() =>
script bullNeck {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def candleSize  = AbsValue( high  - low);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    plot result = upCandle[1] and tallBody[1] and dwnCandle and open > close[1] and shortBody and candleSize != 0 and AbsValue(close - high[1]) <= bodyAvg * 0.05;
}
#bearNeck() =>
script bearNeck {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def candleSize  = AbsValue( high  - low);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    plot result = dwnCandle[1] and tallBody[1] and upCandle and open < close[1] and shortBody and candleSize != 0 and AbsValue(close - low[1]) <= bodyAvg * 0.05;
}
#soldiers(float wickSize = 5) =>
script soldiers {
    input wickSize = 5;
    def upCandle  = close > open;
    def candleSize  = AbsValue( high  - low);
    def topWickSize = AbsValue(Max(close, open) - high);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def wicks = candleSize * wickSize / 100 > topWickSize;
    plot result = tallBody and tallBody[1] and tallBody[2] and upCandle and upCandle[1] and upCandle[2] and
                      close   > close[1] and close[1] > close[2] and open < close[1] and open > open[1] and
                      open[1] < close[2] and open[1]  > open[2]  and wicks and wicks[1] and wicks[2];
}
#crows(float wickSize = 5) =>
script crows {
    input wickSize = 5;
    def dwnCandle = close < open;
    def candleSize  = AbsValue( high  - low);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def wicks = candleSize * wickSize / 100 > bottomWickSize;
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    plot result = tallBody and tallBody[1] and tallBody[2]   and dwnCandle and dwnCandle[1] and dwnCandle[2]   and
                  close   < close[1] and close[1] < close[2] and open > close[1]            and open < open[1] and
                  open[1] > close[2] and open[1]  < open[2]  and wicks and wicks[1]         and wicks[2];
}
#triStarBull() =>
script triStarBull {
    input dojiSize = 5.0;
    input dojiWickSize = 2.0;
    def bodyHigh  = Max( close, open);
    def bodyLow   = Min( close, open);
    def bodyUpGap   = bodyHigh[1] < bodyLow;
    def bodyDwnGap  = bodyHigh < bodyLow[1];
    plot result = Doji(dojiSize,dojiWickSize) and Doji(dojiSize,dojiWickSize)[2] and Doji(dojiSize,dojiWickSize)[3] and bodyDwnGap[1] and bodyUpGap;
}
#triStarBear() =>
script triStarBear {
    input dojiSize = 5.0;
    input dojiWickSize = 2.0;
    def bodyHigh  = Max(close, open);
    def bodyLow   = Min(close, open);
    def bodyUpGap   = bodyHigh[1] < bodyLow;
    def bodyDwnGap  = bodyHigh < bodyLow[1];
    plot result = Doji(dojiSize,dojiWickSize) and Doji(dojiSize,dojiWickSize)[2] and Doji(dojiSize,dojiWickSize)[3] and bodyDwnGap and bodyUpGap[1];
}
#insideBar() =>
script insideBar {
    plot result  = high < high[1] and low > low[1];
}
#doubleInside() =>
script doubleInside {
    plot result  = insideBar() and insideBar()[1];
}

#--- Calc
def na = Double.NaN;
def priceAvg = SimpleMovingAvg(close  ,   priceAvgLength);
def sma200   = multiMa(close  ,   MovAvgLength2, maType);
def sma50    = multiMa(close  ,   MovAvgLength1, maType);

def utr1     =   close > priceAvg;
def utr2     =   close > sma50 and sma50 > sma200;

def dtr1     =   close < priceAvg;
def dtr2     =   close < sma50 and sma50 < sma200;

def upTrend  = if trendRule == trendRule."Trend Rule 1" then utr1 else
               if trendRule == trendRule."Trend Rule 2" then utr2 else yes;
def downTrend = if trendRule == trendRule."Trend Rule 1" then dtr1 else
                if trendRule == trendRule."Trend Rule 2" then dtr2 else yes;
#-- MovAvg
plot Mov1 = sma50;
plot Mov2 = sma200;
Mov1.SetDefaultColor(Color.CYAN);
Mov2.SetDefaultColor(Color.DARK_ORANGE);
Mov1.SetHiding(!ShowMovingAvg);
Mov2.SetHiding(!ShowMovingAvg);

#--- Pattern calc
def d = doji and Doji(dojiSize, dojiWickSize);
def bew = bullEngulf and bullEngulf(rejectWickMax , EngulfingMustEngulfWick) and upTrend;
def beb = bearEngulf and bearEngulf(rejectWickMax ,EngulfingMustEngulfWick) and downTrend;
def h   = hammer and Hammer(hammerFib , hsShadowPerc) and downTrend;
def ss  = star and star(hammerFib, hsShadowPerc) and upTrend;
def dd      = dragonflyDoji and dragonflyDoji();
#def gd      = gravestoneDoji and gravestoneDoji();
def tb      = tweezerBottom and tweezerBottom(TweezerCloseHalf) and downTrend[1];
def tt      = tweezerTop and tweezerTop(TweezerCloseHalf) and upTrend[1];
def stw     = spinningTopBull and spinningTopBull(spinWickSize);
def stb     = spinningTopBear and spinningTopBear(spinWickSize);
def ms      = morningStar and morningStar() and downTrend;
def es      = eveningStar and eveningStar() and upTrend;
def bhw     = haramiBull and haramiBull()   and downTrend[1];
def bhb     = haramiBear and haramiBear()   and upTrend[1];
def hcw     = haramiBullCross and haramiBullCross() and downTrend[1];
def hcb     = haramiBearCross and haramiBearCross() and upTrend[1];
def mw      = marubullzu and marubullzu();
def mb      = marubearzu and marubearzu();
def abw     = abandonedBull and abandonedBull() and downTrend[1];
def abb     = abandonedBear and abandonedBear() and upTrend[1];
def p       = piercing      and piercing() and downTrend[1];
def dcc     = darkCloudCover and darkCloudCover() and upTrend[1];
def utg     = tasukiBull and tasukiBull()        and upTrend;
def dtg     = tasukiBear and tasukiBear()        and downTrend;
def rtm     = risingThree and risingThree()      and upTrend[4];
def ftm     = fallingThree and fallingThree()    and downTrend[4];
def rw      = risingWindow and risingWindow()    and upTrend[1];
def fw      = fallingWindow and fallingWindow()  and downTrend[1];
def kw      = kickingBull and kickingBull();
def kb      = kickingBear and kickingBear();
def ll      = lls and lls(LongShadowRatio);
def lu      = lus and lus(LongShadowRatio);
def nw      = bullNeck and bullNeck() and upTrend;
def nb      = bearNeck and bearNeck() and downTrend;
def ws      = soldiers and soldiers(SoldiersCrowsWick);
def bc      = crows and crows(SoldiersCrowsWick);
def tsw     = triStarBull and triStarBull(dojiSize, dojiWickSize) and downTrend[2];
def tsb     = triStarBear and triStarBear(dojiSize, dojiWickSize) and upTrend[2];
def ib      = insidebar and insideBar();
def dib     = doubleInsideBar and doubleInside();

AddChartBubble(d, if close>open then low else high, "doji", Color.GRAY, if close>open then no else yes);
AddChartBubble(bew, low, "bull Engulf", Color.GREEN, no);
AddChartBubble(beb, high, "bear Engulf", Color.RED, yes);
AddChartBubble(h, low, "hammer", Color.GREEN, no);
AddChartBubble(ss, high, "star", Color.RED, yes);
AddChartBubble(dd, low, "dragonfly Doji", Color.GREEN, no);
AddChartBubble(tb, low, "tweezer Bottom", Color.GREEN, no);
AddChartBubble(tt, high, "tweezer Top", Color.RED, yes);
AddChartBubble(stw,low , "spinning Top Bull", Color.GREEN, no);
AddChartBubble(stb,high, "spinning Top Bear", Color.RED, yes);
AddChartBubble(ms, low, "morning Star", Color.GREEN, no);
AddChartBubble(es, high, "evening Star", Color.RED, yes);
AddChartBubble(bhw, low, "harami Bull", Color.GREEN, no);
AddChartBubble(bhb, high, "harami Bear", Color.RED, yes);
AddChartBubble(hcw, low, "harami Bull Cross", Color.GREEN, no);
AddChartBubble(hcb, high, "harami Bear Cross", Color.RED, yes);
AddChartBubble(mw, low, "marubullzu", Color.GREEN, no);
AddChartBubble(mb, high, "marubearzu", Color.RED, yes);
AddChartBubble(abw, low, "abandoned Bull", Color.GREEN, no);
AddChartBubble(abb, high, "abandoned Bear", Color.RED, yes);
AddChartBubble(p, low, "piercing", Color.GREEN, no);
AddChartBubble(dcc, high, "dark Cloud Cover", Color.RED, yes);
AddChartBubble(utg, low, "tasuki Bull", Color.GREEN, no);
AddChartBubble(dtg, high, "tasuki Bear", Color.RED, yes);
AddChartBubble(rtm, low, "rising Three", Color.GREEN, no);
AddChartBubble(ftm, high, "falling Three", Color.RED, yes);
AddChartBubble(rw, low, "rising Window", Color.GREEN, no);
AddChartBubble(fw, high, "falling Window", Color.RED, yes);
AddChartBubble(kw, low, "kicking Bull", Color.GREEN, no);
AddChartBubble(kb, high, "kicking Bear", Color.RED, yes);
AddChartBubble(ll, low, "lls", Color.GREEN, no);
AddChartBubble(lu, high, "lus", Color.RED, yes);
AddChartBubble(nw, low, "bull Neck", Color.GREEN, no);
AddChartBubble(nb, high, "bear Neck", Color.RED, yes);
AddChartBubble(ws, low, "soldiers", Color.GREEN, no);
AddChartBubble(bc, high, "crows", Color.RED, yes);
AddChartBubble(tsw, low, "triStar Bull", Color.GREEN, no);
AddChartBubble(tsb, high, "triStar Bear", Color.RED, yes);
AddChartBubble(ib, if close>open then low else high, "inside bar", Color.GRAY, if close>open then no else yes);
AddChartBubble(dib, if close>open then low else high, "double inside bar", Color.GRAY, if close>open then no else yes);


#--- END CODE
 
below code can detect 40 candlestick pattern and option to plot 15 different moving average where you can add to your trading arsenal.

CSS:
#/ ================================== //
#// --------> Candlestick Library<---------- //
#// ================================== //

input trendRule = {default "Trend Rule 1", "Trend Rule 2", "No detection"};    #"Detect Trend Based On"
input maType  = {default SMA, EMA, SMMA, WMA, VWMA, RMS, DEMA, TEMA, ZLSMA, ZLDEMA, ZLTEMA, McGinley, HMA, ALMA, SWMA, Gaussian};
input src = close;
input ShowMovingAvg = yes;
input priceAvgLength = 50;
input MovAvgLength1 = 50;
input MovAvgLength2 = 200;
input EngulfingMustEngulfWick = no;  # "Engulfing Must Engulf Wick"
input TweezerCloseHalf        = no;  # "Tweezer Close Over Half"
input rejectWickMax = 0.0; # "[EC] Max Reject Wick Size"
input hammerFib     = 33;  # "[HS] H&S Ratio (%)"
input hsShadowPerc  = 5.0; # "[HS] H&S Max Shadow (%)"
input dojiSize      = 5.0; # "[DJ] Doji Size (%)"
input dojiWickSize  = 2.0; # "[DJ] Max Doji Wick Size"
input LongShadowRatio = 75;  # "[LS] Long Shadow (%)"
input spinWickSize    = 34;  # "[ST] Spinning Top Wick Size"
input SoldiersCrowsWick = 5;   # "[SC] Soldiers and Crows Wick"
input doji            = no;
input bullEngulf      = no;
input bearEngulf      = no;
input hammer          = no;
input star            = no;
input dragonflyDoji   = no;
input tweezerBottom   = no;
input tweezerTop      = no;
input spinningTopBull = no;
input spinningTopBear = no;
input morningStar     = no;
input eveningStar     = no;
input haramiBull      = no;
input haramiBear      = no;
input haramiBullCross = no;
input haramiBearCross = no;
input marubullzu      = no;
input marubearzu      = no;
input abandonedBull   = no;
input abandonedBear   = no;
input piercing        = no;
input darkCloudCover  = no;
input tasukiBull      = no;
input tasukiBear      = no;
input risingThree     = no;
input fallingThree    = no;
input risingWindow    = no;
input fallingWindow   = no;
input kickingBull     = no;
input kickingBear     = no;
input lls             = no;
input lus             = no;
input bullNeck        = no;
input bearNeck        = no;
input soldiers        = no;
input crows           = no;
input triStarBull     = no;
input triStarBear     = no;
input insidebar       = no;
input doubleInsideBar = no;

#// ] -------------- FUNCTIONS : Moving Avg ------------------ [
script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if !isNaN(data) then data else repl;
    plot return = ret_val;
}
#rms(source, length)=>
script rms {
input source = close;
input length = 14;
    def rms = sqrt(sum(power(source, 2), length)/length);
plot return = rms;
}
#Gaussianma(values, length) =>
script Gaussian {
    input values = close;
    input length = 20;
    def stddev = length / 4;
    def indices = length - 1;
    def weights = Exp(-0.5 * (Power((indices - length), 2) / Power(stddev, 2)));
    def sum = Sum(values * weights, length);
    def gMA = sum / Sum(weights, length);
    plot return = gMA;
}
#pine_swma(source) =>
script swma {
input source = close;
    def swma = source[3] * 1 / 6 + source[2] * 2 / 6 +  source[1] * 2 / 6 + source[0] * 1 / 6;
    plot retun = swma;
}
#export zlSma(float src, simple int len) =>
script zlSma {
    input src = close;
    input len = 14;
    def lsma = Inertia(src, len);
    def lsma2 = Inertia(lsma, len);
    def eq = lsma - lsma2;
    def zlsma = lsma + eq;
    plot return = zlsma;
}
#export zlDema(float src, simple int len) =>
script zlDema {
    input src = close;
    input len = 14;
    def zdema1 = ExpAverage(src, len);
    def zdema2 = ExpAverage(zdema1, len);
    def dema1 = 2 * zdema1 - zdema2;
    def zdema12 = ExpAverage(dema1, len);
    def zdema22 = ExpAverage(zdema12, len);
    def zldema = 2 * zdema12 - zdema22;
    plot return = zldema;
}
#export zlTema(float src, simple int len) =>
script zlTema {
    input src = close;
    input len = 14;
    def ema1 = ExpAverage(src, len);
    def ema2 = ExpAverage(ema1, len);
    def ema3 = ExpAverage(ema2, len);
    def tema1 = 3 * (ema1 - ema2) + ema3;
    def ema1a = ExpAverage(tema1, len);
    def ema2a = ExpAverage(ema1a, len);
    def ema3a = ExpAverage(ema2a, len);
    def zltema = 3 * (ema1a - ema2a) + ema3a;
    plot return = zltema;
}
#pine_alma(series, windowsize, offset, sigma) =>
script ALMA {
  input series = close;
  input windowsize = 9;
  input Offset = 0.85;
  input Sigma = 6;

    def m = Offset * (windowsize - 1);
    def s = windowsize / Sigma;

    def norm  = fold z = 0 to windowsize with CW do
         CW + Exp(-(Sqr(z - m)) / (2 * Sqr(s)));

    def sum  = fold y = 0 to windowsize with WS do
         WS + Exp(-(Sqr(y - m)) / (2 * Sqr(s))) * GetValue(series, windowsize - 1 - y);

    plot ALMA = sum  / norm ;
}
#export mcginley(float src, simple int len)=>
script mcginley {
    input src = close;
    input len = 14;
    def mg;
    def t = ExpAverage(src, len);
    mg = if IsNaN(mg[1]) then t else
        CompoundValue(1 ,mg[1] + (src - mg[1]) / (len * Power(src / mg[1], 4)),src);
    plot return = mg;
}
#vwma(source, length)
script VWMA {
    input src = close;
    input len = 15;
    def v = volume;
    def VWMA = SimpleMovingAvg(src * nz(v,1), len) / SimpleMovingAvg(nz(v,1), len);
    plot result = VWMA;
}
#export multiMa(float source, simple int length, string type) =>
script multiMa {
    input source = close;
    input length = 14;
    input type = "SMA";
    def multiMa =
        if type == "SMA"    then SimpleMovingAvg(source, length) else
        if type == "EMA"    then ExpAverage(source, length) else
        if type == "SMMA"   then WildersAverage(source, length) else
        if type == "WMA"    then WMA(source, length) else
        if type == "VWMA"   then vwma(source, length) else
        if type == "DEMA"   then DEMA(source, length) else
        if type == "TEMA"   then TEMA(source, length) else
        if type == "RMS"   then RMS(source, length) else
        if type == "ZLSMA"  then zlSma(source, length) else
        if type == "ZLDEMA" then zlDema(source, length) else
        if type == "ZLTEMA" then zlTema(source, length) else
        if type == "McGinley" then mcginley(source, length) else
        if type == "ALMA" then ALMA(source, length) else
        if type == "SWMA" then SWMA(source) else
        if type == "Gaussian"   then Gaussian(source, length) else
        if type == "HMA"    then  HullMovingAvg(source, length ) else Double.NaN;
    plot return = multiMa;
}
#--- Patterns
#doji(float dojiSize = 5.0, float dojiWickSize = 2.0) =>
script doji {
    input dojiSize = 5.0;
    input dojiWickSize = 2.0;
    def topWickSize = AbsValue(Max(close, open) - high);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def bodySize       = AbsValue( close - open);
    def candleSize     = AbsValue( high  - low);
    def bodyPcnt       = bodySize / candleSize * 100;
    def calcDoji = topWickSize <= bottomWickSize * dojiWickSize and bottomWickSize <= topWickSize * dojiWickSize;
    plot result  = bodyPcnt <= dojiSize and calcDoji;
}
#bullEngulf(float maxRejectWick = 0.0, bool mustEngulfWick = false) =>
script bullEngulf {
    input maxRejectWick = 0.0;
    input mustEngulfWick = no;
    def topWickSize = AbsValue(Max(close, open) - high);
    def bodySize       = AbsValue( close - open);
    def rejectionRule = maxRejectWick == 0.0 or topWickSize / bodySize < (maxRejectWick / 100);
    plot result = close[1] <= open[1] and close >= open[1] and open <= close[1] and rejectionRule and (!mustEngulfWick or close >= high[1]) and bodySize > 0;
}
#bearEngulf(float maxRejectWick = 0.0, bool mustEngulfWick = false) =>
script bearEngulf {
    input maxRejectWick = 0.0;
    input mustEngulfWick = no;
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def bodySize       = AbsValue( close - open);
    def rejectionRule = maxRejectWick == 0.0 or bottomWickSize / bodySize < (maxRejectWick / 100);
    plot result = close[1] >= open[1] and close <= open[1] and open >= close[1] and rejectionRule and (!mustEngulfWick or close <= low[1]) and bodySize > 0;
}
#hammer(float ratio = 33, float shadowPercent = 5.0) =>
script hammer {
    input ratio = 33;
    input shadowPercent = 5.0;
    def topWickSize = AbsValue(Max(close, open) - high);
    def bodySize    = AbsValue( close - open);
    def bodyLow     = Min( close, open);
    def bullRatio = (low - high) * (ratio / 100) + high;
    def hasShadow = topWickSize > shadowPercent / 100 * bodySize;
    plot result   = bodySize > 0 and bodyLow >= bullRatio and !hasShadow;
}
#star(float ratio = 33, float shadowPercent = 5.0) =>
script star {
    input ratio = 33;
    input shadowPercent = 5.0;
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def bodySize       = AbsValue( close - open);
    def bodyHigh       = Max( close, open);
    def bearRatio = (high - low) * (ratio / 100) + low;
    def hasShadow = bottomWickSize > shadowPercent / 100 * bodySize;
    plot result   = bodySize > 0 and bodyHigh <= bearRatio and !hasShadow;
}
#dragonflyDoji() =>
script dragonflyDoji {
    def topWickSize = AbsValue(Max(close, open) - high);
    def bodySize    = AbsValue( close - open);
    def candleSize  = AbsValue( high  - low);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    plot result = bodyIsDoji  and topWickSize <= bodySize;
}
#tweezerBottom(bool closeUpperHalf = false) =>
script tweezerBottom {
    input closeUpperHalf = no;
    def upperHalf = close > hl2[1];
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def topWickSize = AbsValue(Max(close, open) - high);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def candleSize  = AbsValue( high  - low);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    def topShadow   = topWickSize > 5 / 100 * bodySize;
    def bottomShadow = bottomWickSize > 5 / 100 * bodySize;
    def tallBody     = bodySize > bodyAvg;
    plot result   = (!bodyIsDoji or (topShadow and bottomShadow)) and AbsValue(low - low[1]) <= bodyAvg * 0.05 and dwnCandle[1] and upCandle and tallBody[1] and (!closeUpperHalf or (closeUpperHalf and upperHalf));
}
#tweezerTop(bool closeLowerHalf = false) =>
script tweezerTop {
    input closeLowerHalf = no;
    def lowerHalf = close < hl2[1];
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def topWickSize = AbsValue(Max(close, open) - high);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def candleSize  = AbsValue( high  - low);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    def topShadow   = topWickSize > 5 / 100 * bodySize;
    def bottomShadow = bottomWickSize > 5 / 100 * bodySize;
    def tallBody     = bodySize > bodyAvg;
    plot result   = (!bodyIsDoji  or (topShadow and bottomShadow)) and AbsValue(high - high[1]) <= bodyAvg * 0.05 and upCandle[1] and dwnCandle and tallBody[1] and (!closeLowerHalf or (closeLowerHalf and lowerHalf));
}
#spinningTopBull(float wickSize = 34) =>
script spinningTopBull {
    input wickSize = 34;
    def upCandle  = close > open;
    def candleSize  = AbsValue( high  - low);
    def topWickSize = AbsValue(Max(close, open) - high);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def bodySize    = AbsValue( close - open);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    plot result = bottomWickSize >= candleSize / 100 * wickSize and topWickSize >= candleSize / 100 * wickSize and upCandle and !bodyIsDoji;
}
#spinningTopBear(float wickSize = 34) =>
script spinningTopBear {
    input wickSize = 34;
    def dwnCandle = close < open;
    def candleSize  = AbsValue( high  - low);
    def topWickSize = AbsValue(Max(close, open) - high);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def bodySize    = AbsValue( close - open);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    plot result = bottomWickSize >= candleSize / 100 * wickSize and topWickSize >= candleSize / 100 * wickSize and dwnCandle and !bodyIsDoji;
}
#spinningTop(float wickSize = 34) =>
script spinningTop {
    input wickSize = 34;
    def topWickSize = AbsValue(Max(close, open) - high);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def candleSize  = AbsValue( high  - low);
    def bodySize    = AbsValue( close - open);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    plot result = bottomWickSize >= candleSize / 100 * wickSize and topWickSize >= candleSize / 100 * wickSize and !bodyIsDoji;
}
#morningStar() =>
script morningStar {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodyHigh  = Max( close, open);
    def bodyLow   = Min( close, open);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    def bodyUpGap   = bodyHigh[1] < bodyLow;
    def bodyDwnGap  = bodyHigh    < bodyLow[1];
    def middleBody  = bodySize / 2 + bodyLow;
    plot result = tallBody[2] and shortBody[1] and tallBody and dwnCandle[2] and bodyDwnGap[1] and upCandle and bodyHigh >= middleBody[2] and bodyHigh < bodyHigh[2] and bodyUpGap;
}
#eveningStar() =>
script eveningStar {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodyHigh  = Max( close, open);
    def bodyLow   = Min( close, open);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    def bodyUpGap   = bodyHigh[1] < bodyLow;
    def bodyDwnGap  = bodyHigh    < bodyLow[1];
    def middleBody  = bodySize / 2 + bodyLow;
    plot result = tallBody[2] and shortBody[1] and tallBody and upCandle[2] and bodyUpGap[1] and dwnCandle and bodyLow <= middleBody[2] and bodyLow > bodyLow[2] and bodyDwnGap;
}
#haramiBull() =>
script haramiBull {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodyHigh  = Max( close, open);
    def bodyLow   = Min( close, open);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    plot result = tallBody[1] and dwnCandle[1] and upCandle and shortBody and high <= bodyHigh[1] and low >= bodyLow[1];
}
#haramiBear() =>
script haramiBear {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodyHigh  = Max( close, open);
    def bodyLow   = Min( close, open);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    plot result = tallBody[1] and upCandle[1] and dwnCandle and shortBody and high <= bodyHigh[1] and low >= bodyLow[1];
}
#haramiBullCross() =>
script haramiBullCross {
    def dwnCandle = close < open;
    def bodyHigh  = Max( close, open);
    def bodyLow   = Min( close, open);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def candleSize  = AbsValue( high  - low);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    plot result = tallBody[1] and dwnCandle[1] and bodyIsDoji  and high <= bodyHigh[1] and low >= bodyLow[1];
}
#haramiBearCross() =>
script haramiBearCross {
    def upCandle  = close > open;
    def bodyHigh  = Max( close, open);
    def bodyLow   = Min( close, open);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def candleSize  = AbsValue( high  - low);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    plot result = tallBody[1] and upCandle[1] and bodyIsDoji  and high <= bodyHigh[1] and low >= bodyLow[1];
}
#marubullzu() =>
script marubullzu {
    def upCandle  = close > open;
    def topWickSize = AbsValue(Max(close, open) - high);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    plot result = upCandle and tallBody and  5 > topWickSize / bodySize * 100 and 5 > bottomWickSize / bodySize * 100;
}
#marubearzu() =>
script marubearzu {
    def dwnCandle = close < open;
    def topWickSize = AbsValue(Max(close, open) - high);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    plot result = dwnCandle and tallBody and  5 > topWickSize / bodySize * 100 and 5 > bottomWickSize / bodySize * 100;
}
#abandonedBull() =>
script abandonedBull {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def upGap     = low    > high[1];
    def dwnGap    = low[1] > high;
    def bodySize    = AbsValue( close - open);
    def candleSize  = AbsValue( high  - low);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    plot result = dwnCandle[2] and bodyIsDoji [1] and dwnGap[1] and upCandle and upGap;
}
#abandonedBear() =>
script abandonedBear {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def upGap     = low    > high[1];
    def dwnGap    = low[1] > high;
    def bodySize    = AbsValue( close - open);
    def candleSize  = AbsValue( high  - low);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    plot result = upCandle[2] and bodyIsDoji [1] and upGap[1] and dwnCandle and dwnGap;
}
#piercing() =>
script piercing {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def bodyLow   = Min( close, open);
    def middleBody  = bodySize / 2 + bodyLow;
    plot result = dwnCandle[1] and tallBody[1] and upCandle and open <= low[1] and close > middleBody[1] and close < open[1];
}
#darkCloudCover() =>
script darkCloudCover {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def bodyLow   = Min( close, open);
    def middleBody  = bodySize / 2 + bodyLow;
    plot result = upCandle[1] and tallBody[1] and dwnCandle and open >= high[1] and close < middleBody[1] and close > open[1];
}
#tasukiBull() =>
script tasukiBull {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodyHigh  = Max( close, open);
    def bodyLow   = Min( close, open);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    def bodyUpGap   = bodyHigh[1] < bodyLow;
    plot result =  tallBody[2] and shortBody[1] and upCandle[2] and bodyUpGap[1] and upCandle[1] and dwnCandle and bodyLow >= bodyHigh[2] and bodyLow <= bodyLow[1];
}
#tasukiBear() =>
script tasukiBear {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodyHigh  = Max( close, open);
    def bodyLow   = Min( close, open);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    def bodyDwnGap  = bodyHigh < bodyLow[1];
    plot result = tallBody[2] and shortBody[1] and dwnCandle[2] and bodyDwnGap[1] and dwnCandle[1] and upCandle and bodyHigh <= bodyLow[2] and bodyHigh >= bodyHigh[1];
}
#risingThree() =>
script risingThree {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    plot result = tallBody[4] and upCandle[4] and shortBody[3] and dwnCandle[3] and open[3] < high[4] and close[3] > low[4] and shortBody[2] and dwnCandle[2] and open[2] < high[4] and close[2] > low[4] and shortBody[1] and dwnCandle[1] and  open[1] < high[4]   and close[1] > low[4] and tallBody and upCandle and close > close[4];
}
#fallingThree() =>
script fallingThree {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    plot result = tallBody[4] and dwnCandle[4] and shortBody[3] and upCandle[3] and open[3] > low[4] and close[3] < high[4] and shortBody[2] and upCandle[2] and open[2] > low[4] and close[2] < high[4]  and shortBody[1]  and upCandle[1] and open[1] > low[4] and close[1] < high[4] and tallBody and dwnCandle and close < close[4];
}
#risingWindow() =>
script risingWindow {
    def candleSize     = AbsValue( high  - low);
    plot result = candleSize != 0 and candleSize[1] != 0 and low > high[1];
}
#fallingWindow() =>
script fallingWindow {
    def candleSize     = AbsValue( high  - low);
    plot result = candleSize != 0 and candleSize[1] != 0 and high < low[1];
}
#kickingBull() =>
script kickingBull {
    def upGap     = low > high[1];
    plot result = marubearzu()[1] and marubullzu() and upGap;
}
#kickingBear() =>
script kickingBear {
    def dwnGap    = low[1] > high;
    plot result  = marubullzu()[1] and marubearzu() and dwnGap;
}
#lls(float ratio = 75) =>"Long Lower Shadow" candle patterns
script lls {
    input ratio = 75;
    def candleSize = AbsValue( high  - low);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    plot result = bottomWickSize > candleSize / 100 * ratio;
}
#lus(float ratio = 75) =>"Long Upper Shadow" candle patterns
script lus {
    input ratio = 75;
    def candleSize = AbsValue( high  - low);
    def topWickSize = AbsValue(Max(close, open) - high);
    plot result = topWickSize > candleSize / 100 * ratio;
}
#bullNeck() =>
script bullNeck {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def candleSize  = AbsValue( high  - low);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    plot result = upCandle[1] and tallBody[1] and dwnCandle and open > close[1] and shortBody and candleSize != 0 and AbsValue(close - high[1]) <= bodyAvg * 0.05;
}
#bearNeck() =>
script bearNeck {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def candleSize  = AbsValue( high  - low);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    plot result = dwnCandle[1] and tallBody[1] and upCandle and open < close[1] and shortBody and candleSize != 0 and AbsValue(close - low[1]) <= bodyAvg * 0.05;
}
#soldiers(float wickSize = 5) =>
script soldiers {
    input wickSize = 5;
    def upCandle  = close > open;
    def candleSize  = AbsValue( high  - low);
    def topWickSize = AbsValue(Max(close, open) - high);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def wicks = candleSize * wickSize / 100 > topWickSize;
    plot result = tallBody and tallBody[1] and tallBody[2] and upCandle and upCandle[1] and upCandle[2] and
                      close   > close[1] and close[1] > close[2] and open < close[1] and open > open[1] and
                      open[1] < close[2] and open[1]  > open[2]  and wicks and wicks[1] and wicks[2];
}
#crows(float wickSize = 5) =>
script crows {
    input wickSize = 5;
    def dwnCandle = close < open;
    def candleSize  = AbsValue( high  - low);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def wicks = candleSize * wickSize / 100 > bottomWickSize;
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    plot result = tallBody and tallBody[1] and tallBody[2]   and dwnCandle and dwnCandle[1] and dwnCandle[2]   and
                  close   < close[1] and close[1] < close[2] and open > close[1]            and open < open[1] and
                  open[1] > close[2] and open[1]  < open[2]  and wicks and wicks[1]         and wicks[2];
}
#triStarBull() =>
script triStarBull {
    input dojiSize = 5.0;
    input dojiWickSize = 2.0;
    def bodyHigh  = Max( close, open);
    def bodyLow   = Min( close, open);
    def bodyUpGap   = bodyHigh[1] < bodyLow;
    def bodyDwnGap  = bodyHigh < bodyLow[1];
    plot result = Doji(dojiSize,dojiWickSize) and Doji(dojiSize,dojiWickSize)[2] and Doji(dojiSize,dojiWickSize)[3] and bodyDwnGap[1] and bodyUpGap;
}
#triStarBear() =>
script triStarBear {
    input dojiSize = 5.0;
    input dojiWickSize = 2.0;
    def bodyHigh  = Max(close, open);
    def bodyLow   = Min(close, open);
    def bodyUpGap   = bodyHigh[1] < bodyLow;
    def bodyDwnGap  = bodyHigh < bodyLow[1];
    plot result = Doji(dojiSize,dojiWickSize) and Doji(dojiSize,dojiWickSize)[2] and Doji(dojiSize,dojiWickSize)[3] and bodyDwnGap and bodyUpGap[1];
}
#insideBar() =>
script insideBar {
    plot result  = high < high[1] and low > low[1];
}
#doubleInside() =>
script doubleInside {
    plot result  = insideBar() and insideBar()[1];
}

#--- Calc
def na = Double.NaN;
def priceAvg = SimpleMovingAvg(close  ,   priceAvgLength);
def sma200   = multiMa(close  ,   MovAvgLength2, maType);
def sma50    = multiMa(close  ,   MovAvgLength1, maType);

def utr1     =   close > priceAvg;
def utr2     =   close > sma50 and sma50 > sma200;

def dtr1     =   close < priceAvg;
def dtr2     =   close < sma50 and sma50 < sma200;

def upTrend  = if trendRule == trendRule."Trend Rule 1" then utr1 else
               if trendRule == trendRule."Trend Rule 2" then utr2 else yes;
def downTrend = if trendRule == trendRule."Trend Rule 1" then dtr1 else
                if trendRule == trendRule."Trend Rule 2" then dtr2 else yes;
#-- MovAvg
plot Mov1 = sma50;
plot Mov2 = sma200;
Mov1.SetDefaultColor(Color.CYAN);
Mov2.SetDefaultColor(Color.DARK_ORANGE);
Mov1.SetHiding(!ShowMovingAvg);
Mov2.SetHiding(!ShowMovingAvg);

#--- Pattern calc
def d = doji and Doji(dojiSize, dojiWickSize);
def bew = bullEngulf and bullEngulf(rejectWickMax , EngulfingMustEngulfWick) and upTrend;
def beb = bearEngulf and bearEngulf(rejectWickMax ,EngulfingMustEngulfWick) and downTrend;
def h   = hammer and Hammer(hammerFib , hsShadowPerc) and downTrend;
def ss  = star and star(hammerFib, hsShadowPerc) and upTrend;
def dd      = dragonflyDoji and dragonflyDoji();
#def gd      = gravestoneDoji and gravestoneDoji();
def tb      = tweezerBottom and tweezerBottom(TweezerCloseHalf) and downTrend[1];
def tt      = tweezerTop and tweezerTop(TweezerCloseHalf) and upTrend[1];
def stw     = spinningTopBull and spinningTopBull(spinWickSize);
def stb     = spinningTopBear and spinningTopBear(spinWickSize);
def ms      = morningStar and morningStar() and downTrend;
def es      = eveningStar and eveningStar() and upTrend;
def bhw     = haramiBull and haramiBull()   and downTrend[1];
def bhb     = haramiBear and haramiBear()   and upTrend[1];
def hcw     = haramiBullCross and haramiBullCross() and downTrend[1];
def hcb     = haramiBearCross and haramiBearCross() and upTrend[1];
def mw      = marubullzu and marubullzu();
def mb      = marubearzu and marubearzu();
def abw     = abandonedBull and abandonedBull() and downTrend[1];
def abb     = abandonedBear and abandonedBear() and upTrend[1];
def p       = piercing      and piercing() and downTrend[1];
def dcc     = darkCloudCover and darkCloudCover() and upTrend[1];
def utg     = tasukiBull and tasukiBull()        and upTrend;
def dtg     = tasukiBear and tasukiBear()        and downTrend;
def rtm     = risingThree and risingThree()      and upTrend[4];
def ftm     = fallingThree and fallingThree()    and downTrend[4];
def rw      = risingWindow and risingWindow()    and upTrend[1];
def fw      = fallingWindow and fallingWindow()  and downTrend[1];
def kw      = kickingBull and kickingBull();
def kb      = kickingBear and kickingBear();
def ll      = lls and lls(LongShadowRatio);
def lu      = lus and lus(LongShadowRatio);
def nw      = bullNeck and bullNeck() and upTrend;
def nb      = bearNeck and bearNeck() and downTrend;
def ws      = soldiers and soldiers(SoldiersCrowsWick);
def bc      = crows and crows(SoldiersCrowsWick);
def tsw     = triStarBull and triStarBull(dojiSize, dojiWickSize) and downTrend[2];
def tsb     = triStarBear and triStarBear(dojiSize, dojiWickSize) and upTrend[2];
def ib      = insidebar and insideBar();
def dib     = doubleInsideBar and doubleInside();

AddChartBubble(d, if close>open then low else high, "doji", Color.GRAY, if close>open then no else yes);
AddChartBubble(bew, low, "bull Engulf", Color.GREEN, no);
AddChartBubble(beb, high, "bear Engulf", Color.RED, yes);
AddChartBubble(h, low, "hammer", Color.GREEN, no);
AddChartBubble(ss, high, "star", Color.RED, yes);
AddChartBubble(dd, low, "dragonfly Doji", Color.GREEN, no);
AddChartBubble(tb, low, "tweezer Bottom", Color.GREEN, no);
AddChartBubble(tt, high, "tweezer Top", Color.RED, yes);
AddChartBubble(stw,low , "spinning Top Bull", Color.GREEN, no);
AddChartBubble(stb,high, "spinning Top Bear", Color.RED, yes);
AddChartBubble(ms, low, "morning Star", Color.GREEN, no);
AddChartBubble(es, high, "evening Star", Color.RED, yes);
AddChartBubble(bhw, low, "harami Bull", Color.GREEN, no);
AddChartBubble(bhb, high, "harami Bear", Color.RED, yes);
AddChartBubble(hcw, low, "harami Bull Cross", Color.GREEN, no);
AddChartBubble(hcb, high, "harami Bear Cross", Color.RED, yes);
AddChartBubble(mw, low, "marubullzu", Color.GREEN, no);
AddChartBubble(mb, high, "marubearzu", Color.RED, yes);
AddChartBubble(abw, low, "abandoned Bull", Color.GREEN, no);
AddChartBubble(abb, high, "abandoned Bear", Color.RED, yes);
AddChartBubble(p, low, "piercing", Color.GREEN, no);
AddChartBubble(dcc, high, "dark Cloud Cover", Color.RED, yes);
AddChartBubble(utg, low, "tasuki Bull", Color.GREEN, no);
AddChartBubble(dtg, high, "tasuki Bear", Color.RED, yes);
AddChartBubble(rtm, low, "rising Three", Color.GREEN, no);
AddChartBubble(ftm, high, "falling Three", Color.RED, yes);
AddChartBubble(rw, low, "rising Window", Color.GREEN, no);
AddChartBubble(fw, high, "falling Window", Color.RED, yes);
AddChartBubble(kw, low, "kicking Bull", Color.GREEN, no);
AddChartBubble(kb, high, "kicking Bear", Color.RED, yes);
AddChartBubble(ll, low, "lls", Color.GREEN, no);
AddChartBubble(lu, high, "lus", Color.RED, yes);
AddChartBubble(nw, low, "bull Neck", Color.GREEN, no);
AddChartBubble(nb, high, "bear Neck", Color.RED, yes);
AddChartBubble(ws, low, "soldiers", Color.GREEN, no);
AddChartBubble(bc, high, "crows", Color.RED, yes);
AddChartBubble(tsw, low, "triStar Bull", Color.GREEN, no);
AddChartBubble(tsb, high, "triStar Bear", Color.RED, yes);
AddChartBubble(ib, if close>open then low else high, "inside bar", Color.GRAY, if close>open then no else yes);
AddChartBubble(dib, if close>open then low else high, "double inside bar", Color.GRAY, if close>open then no else yes);


#--- END CODE
Hi can you please add Inverted Hammer also? thanks
 
below code can detect 40 candlestick pattern and option to plot 15 different moving average where you can add to your trading arsenal.

CSS:
#/ ================================== //
#// --------> Candlestick Library<---------- //
#// ================================== //

input trendRule = {default "Trend Rule 1", "Trend Rule 2", "No detection"};    #"Detect Trend Based On"
input maType  = {default SMA, EMA, SMMA, WMA, VWMA, RMS, DEMA, TEMA, ZLSMA, ZLDEMA, ZLTEMA, McGinley, HMA, ALMA, SWMA, Gaussian};
input src = close;
input ShowMovingAvg = yes;
input priceAvgLength = 50;
input MovAvgLength1 = 50;
input MovAvgLength2 = 200;
input EngulfingMustEngulfWick = no;  # "Engulfing Must Engulf Wick"
input TweezerCloseHalf        = no;  # "Tweezer Close Over Half"
input rejectWickMax = 0.0; # "[EC] Max Reject Wick Size"
input hammerFib     = 33;  # "[HS] H&S Ratio (%)"
input hsShadowPerc  = 5.0; # "[HS] H&S Max Shadow (%)"
input dojiSize      = 5.0; # "[DJ] Doji Size (%)"
input dojiWickSize  = 2.0; # "[DJ] Max Doji Wick Size"
input LongShadowRatio = 75;  # "[LS] Long Shadow (%)"
input spinWickSize    = 34;  # "[ST] Spinning Top Wick Size"
input SoldiersCrowsWick = 5;   # "[SC] Soldiers and Crows Wick"
input doji            = no;
input bullEngulf      = no;
input bearEngulf      = no;
input hammer          = no;
input star            = no;
input dragonflyDoji   = no;
input tweezerBottom   = no;
input tweezerTop      = no;
input spinningTopBull = no;
input spinningTopBear = no;
input morningStar     = no;
input eveningStar     = no;
input haramiBull      = no;
input haramiBear      = no;
input haramiBullCross = no;
input haramiBearCross = no;
input marubullzu      = no;
input marubearzu      = no;
input abandonedBull   = no;
input abandonedBear   = no;
input piercing        = no;
input darkCloudCover  = no;
input tasukiBull      = no;
input tasukiBear      = no;
input risingThree     = no;
input fallingThree    = no;
input risingWindow    = no;
input fallingWindow   = no;
input kickingBull     = no;
input kickingBear     = no;
input lls             = no;
input lus             = no;
input bullNeck        = no;
input bearNeck        = no;
input soldiers        = no;
input crows           = no;
input triStarBull     = no;
input triStarBear     = no;
input insidebar       = no;
input doubleInsideBar = no;

#// ] -------------- FUNCTIONS : Moving Avg ------------------ [
script nz {
    input data  = close;
    input repl  = 0;
    def ret_val = if !isNaN(data) then data else repl;
    plot return = ret_val;
}
#rms(source, length)=>
script rms {
input source = close;
input length = 14;
    def rms = sqrt(sum(power(source, 2), length)/length);
plot return = rms;
}
#Gaussianma(values, length) =>
script Gaussian {
    input values = close;
    input length = 20;
    def stddev = length / 4;
    def indices = length - 1;
    def weights = Exp(-0.5 * (Power((indices - length), 2) / Power(stddev, 2)));
    def sum = Sum(values * weights, length);
    def gMA = sum / Sum(weights, length);
    plot return = gMA;
}
#pine_swma(source) =>
script swma {
input source = close;
    def swma = source[3] * 1 / 6 + source[2] * 2 / 6 +  source[1] * 2 / 6 + source[0] * 1 / 6;
    plot retun = swma;
}
#export zlSma(float src, simple int len) =>
script zlSma {
    input src = close;
    input len = 14;
    def lsma = Inertia(src, len);
    def lsma2 = Inertia(lsma, len);
    def eq = lsma - lsma2;
    def zlsma = lsma + eq;
    plot return = zlsma;
}
#export zlDema(float src, simple int len) =>
script zlDema {
    input src = close;
    input len = 14;
    def zdema1 = ExpAverage(src, len);
    def zdema2 = ExpAverage(zdema1, len);
    def dema1 = 2 * zdema1 - zdema2;
    def zdema12 = ExpAverage(dema1, len);
    def zdema22 = ExpAverage(zdema12, len);
    def zldema = 2 * zdema12 - zdema22;
    plot return = zldema;
}
#export zlTema(float src, simple int len) =>
script zlTema {
    input src = close;
    input len = 14;
    def ema1 = ExpAverage(src, len);
    def ema2 = ExpAverage(ema1, len);
    def ema3 = ExpAverage(ema2, len);
    def tema1 = 3 * (ema1 - ema2) + ema3;
    def ema1a = ExpAverage(tema1, len);
    def ema2a = ExpAverage(ema1a, len);
    def ema3a = ExpAverage(ema2a, len);
    def zltema = 3 * (ema1a - ema2a) + ema3a;
    plot return = zltema;
}
#pine_alma(series, windowsize, offset, sigma) =>
script ALMA {
  input series = close;
  input windowsize = 9;
  input Offset = 0.85;
  input Sigma = 6;

    def m = Offset * (windowsize - 1);
    def s = windowsize / Sigma;

    def norm  = fold z = 0 to windowsize with CW do
         CW + Exp(-(Sqr(z - m)) / (2 * Sqr(s)));

    def sum  = fold y = 0 to windowsize with WS do
         WS + Exp(-(Sqr(y - m)) / (2 * Sqr(s))) * GetValue(series, windowsize - 1 - y);

    plot ALMA = sum  / norm ;
}
#export mcginley(float src, simple int len)=>
script mcginley {
    input src = close;
    input len = 14;
    def mg;
    def t = ExpAverage(src, len);
    mg = if IsNaN(mg[1]) then t else
        CompoundValue(1 ,mg[1] + (src - mg[1]) / (len * Power(src / mg[1], 4)),src);
    plot return = mg;
}
#vwma(source, length)
script VWMA {
    input src = close;
    input len = 15;
    def v = volume;
    def VWMA = SimpleMovingAvg(src * nz(v,1), len) / SimpleMovingAvg(nz(v,1), len);
    plot result = VWMA;
}
#export multiMa(float source, simple int length, string type) =>
script multiMa {
    input source = close;
    input length = 14;
    input type = "SMA";
    def multiMa =
        if type == "SMA"    then SimpleMovingAvg(source, length) else
        if type == "EMA"    then ExpAverage(source, length) else
        if type == "SMMA"   then WildersAverage(source, length) else
        if type == "WMA"    then WMA(source, length) else
        if type == "VWMA"   then vwma(source, length) else
        if type == "DEMA"   then DEMA(source, length) else
        if type == "TEMA"   then TEMA(source, length) else
        if type == "RMS"   then RMS(source, length) else
        if type == "ZLSMA"  then zlSma(source, length) else
        if type == "ZLDEMA" then zlDema(source, length) else
        if type == "ZLTEMA" then zlTema(source, length) else
        if type == "McGinley" then mcginley(source, length) else
        if type == "ALMA" then ALMA(source, length) else
        if type == "SWMA" then SWMA(source) else
        if type == "Gaussian"   then Gaussian(source, length) else
        if type == "HMA"    then  HullMovingAvg(source, length ) else Double.NaN;
    plot return = multiMa;
}
#--- Patterns
#doji(float dojiSize = 5.0, float dojiWickSize = 2.0) =>
script doji {
    input dojiSize = 5.0;
    input dojiWickSize = 2.0;
    def topWickSize = AbsValue(Max(close, open) - high);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def bodySize       = AbsValue( close - open);
    def candleSize     = AbsValue( high  - low);
    def bodyPcnt       = bodySize / candleSize * 100;
    def calcDoji = topWickSize <= bottomWickSize * dojiWickSize and bottomWickSize <= topWickSize * dojiWickSize;
    plot result  = bodyPcnt <= dojiSize and calcDoji;
}
#bullEngulf(float maxRejectWick = 0.0, bool mustEngulfWick = false) =>
script bullEngulf {
    input maxRejectWick = 0.0;
    input mustEngulfWick = no;
    def topWickSize = AbsValue(Max(close, open) - high);
    def bodySize       = AbsValue( close - open);
    def rejectionRule = maxRejectWick == 0.0 or topWickSize / bodySize < (maxRejectWick / 100);
    plot result = close[1] <= open[1] and close >= open[1] and open <= close[1] and rejectionRule and (!mustEngulfWick or close >= high[1]) and bodySize > 0;
}
#bearEngulf(float maxRejectWick = 0.0, bool mustEngulfWick = false) =>
script bearEngulf {
    input maxRejectWick = 0.0;
    input mustEngulfWick = no;
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def bodySize       = AbsValue( close - open);
    def rejectionRule = maxRejectWick == 0.0 or bottomWickSize / bodySize < (maxRejectWick / 100);
    plot result = close[1] >= open[1] and close <= open[1] and open >= close[1] and rejectionRule and (!mustEngulfWick or close <= low[1]) and bodySize > 0;
}
#hammer(float ratio = 33, float shadowPercent = 5.0) =>
script hammer {
    input ratio = 33;
    input shadowPercent = 5.0;
    def topWickSize = AbsValue(Max(close, open) - high);
    def bodySize    = AbsValue( close - open);
    def bodyLow     = Min( close, open);
    def bullRatio = (low - high) * (ratio / 100) + high;
    def hasShadow = topWickSize > shadowPercent / 100 * bodySize;
    plot result   = bodySize > 0 and bodyLow >= bullRatio and !hasShadow;
}
#star(float ratio = 33, float shadowPercent = 5.0) =>
script star {
    input ratio = 33;
    input shadowPercent = 5.0;
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def bodySize       = AbsValue( close - open);
    def bodyHigh       = Max( close, open);
    def bearRatio = (high - low) * (ratio / 100) + low;
    def hasShadow = bottomWickSize > shadowPercent / 100 * bodySize;
    plot result   = bodySize > 0 and bodyHigh <= bearRatio and !hasShadow;
}
#dragonflyDoji() =>
script dragonflyDoji {
    def topWickSize = AbsValue(Max(close, open) - high);
    def bodySize    = AbsValue( close - open);
    def candleSize  = AbsValue( high  - low);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    plot result = bodyIsDoji  and topWickSize <= bodySize;
}
#tweezerBottom(bool closeUpperHalf = false) =>
script tweezerBottom {
    input closeUpperHalf = no;
    def upperHalf = close > hl2[1];
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def topWickSize = AbsValue(Max(close, open) - high);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def candleSize  = AbsValue( high  - low);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    def topShadow   = topWickSize > 5 / 100 * bodySize;
    def bottomShadow = bottomWickSize > 5 / 100 * bodySize;
    def tallBody     = bodySize > bodyAvg;
    plot result   = (!bodyIsDoji or (topShadow and bottomShadow)) and AbsValue(low - low[1]) <= bodyAvg * 0.05 and dwnCandle[1] and upCandle and tallBody[1] and (!closeUpperHalf or (closeUpperHalf and upperHalf));
}
#tweezerTop(bool closeLowerHalf = false) =>
script tweezerTop {
    input closeLowerHalf = no;
    def lowerHalf = close < hl2[1];
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def topWickSize = AbsValue(Max(close, open) - high);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def candleSize  = AbsValue( high  - low);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    def topShadow   = topWickSize > 5 / 100 * bodySize;
    def bottomShadow = bottomWickSize > 5 / 100 * bodySize;
    def tallBody     = bodySize > bodyAvg;
    plot result   = (!bodyIsDoji  or (topShadow and bottomShadow)) and AbsValue(high - high[1]) <= bodyAvg * 0.05 and upCandle[1] and dwnCandle and tallBody[1] and (!closeLowerHalf or (closeLowerHalf and lowerHalf));
}
#spinningTopBull(float wickSize = 34) =>
script spinningTopBull {
    input wickSize = 34;
    def upCandle  = close > open;
    def candleSize  = AbsValue( high  - low);
    def topWickSize = AbsValue(Max(close, open) - high);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def bodySize    = AbsValue( close - open);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    plot result = bottomWickSize >= candleSize / 100 * wickSize and topWickSize >= candleSize / 100 * wickSize and upCandle and !bodyIsDoji;
}
#spinningTopBear(float wickSize = 34) =>
script spinningTopBear {
    input wickSize = 34;
    def dwnCandle = close < open;
    def candleSize  = AbsValue( high  - low);
    def topWickSize = AbsValue(Max(close, open) - high);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def bodySize    = AbsValue( close - open);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    plot result = bottomWickSize >= candleSize / 100 * wickSize and topWickSize >= candleSize / 100 * wickSize and dwnCandle and !bodyIsDoji;
}
#spinningTop(float wickSize = 34) =>
script spinningTop {
    input wickSize = 34;
    def topWickSize = AbsValue(Max(close, open) - high);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def candleSize  = AbsValue( high  - low);
    def bodySize    = AbsValue( close - open);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    plot result = bottomWickSize >= candleSize / 100 * wickSize and topWickSize >= candleSize / 100 * wickSize and !bodyIsDoji;
}
#morningStar() =>
script morningStar {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodyHigh  = Max( close, open);
    def bodyLow   = Min( close, open);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    def bodyUpGap   = bodyHigh[1] < bodyLow;
    def bodyDwnGap  = bodyHigh    < bodyLow[1];
    def middleBody  = bodySize / 2 + bodyLow;
    plot result = tallBody[2] and shortBody[1] and tallBody and dwnCandle[2] and bodyDwnGap[1] and upCandle and bodyHigh >= middleBody[2] and bodyHigh < bodyHigh[2] and bodyUpGap;
}
#eveningStar() =>
script eveningStar {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodyHigh  = Max( close, open);
    def bodyLow   = Min( close, open);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    def bodyUpGap   = bodyHigh[1] < bodyLow;
    def bodyDwnGap  = bodyHigh    < bodyLow[1];
    def middleBody  = bodySize / 2 + bodyLow;
    plot result = tallBody[2] and shortBody[1] and tallBody and upCandle[2] and bodyUpGap[1] and dwnCandle and bodyLow <= middleBody[2] and bodyLow > bodyLow[2] and bodyDwnGap;
}
#haramiBull() =>
script haramiBull {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodyHigh  = Max( close, open);
    def bodyLow   = Min( close, open);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    plot result = tallBody[1] and dwnCandle[1] and upCandle and shortBody and high <= bodyHigh[1] and low >= bodyLow[1];
}
#haramiBear() =>
script haramiBear {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodyHigh  = Max( close, open);
    def bodyLow   = Min( close, open);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    plot result = tallBody[1] and upCandle[1] and dwnCandle and shortBody and high <= bodyHigh[1] and low >= bodyLow[1];
}
#haramiBullCross() =>
script haramiBullCross {
    def dwnCandle = close < open;
    def bodyHigh  = Max( close, open);
    def bodyLow   = Min( close, open);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def candleSize  = AbsValue( high  - low);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    plot result = tallBody[1] and dwnCandle[1] and bodyIsDoji  and high <= bodyHigh[1] and low >= bodyLow[1];
}
#haramiBearCross() =>
script haramiBearCross {
    def upCandle  = close > open;
    def bodyHigh  = Max( close, open);
    def bodyLow   = Min( close, open);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def candleSize  = AbsValue( high  - low);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    plot result = tallBody[1] and upCandle[1] and bodyIsDoji  and high <= bodyHigh[1] and low >= bodyLow[1];
}
#marubullzu() =>
script marubullzu {
    def upCandle  = close > open;
    def topWickSize = AbsValue(Max(close, open) - high);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    plot result = upCandle and tallBody and  5 > topWickSize / bodySize * 100 and 5 > bottomWickSize / bodySize * 100;
}
#marubearzu() =>
script marubearzu {
    def dwnCandle = close < open;
    def topWickSize = AbsValue(Max(close, open) - high);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    plot result = dwnCandle and tallBody and  5 > topWickSize / bodySize * 100 and 5 > bottomWickSize / bodySize * 100;
}
#abandonedBull() =>
script abandonedBull {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def upGap     = low    > high[1];
    def dwnGap    = low[1] > high;
    def bodySize    = AbsValue( close - open);
    def candleSize  = AbsValue( high  - low);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    plot result = dwnCandle[2] and bodyIsDoji [1] and dwnGap[1] and upCandle and upGap;
}
#abandonedBear() =>
script abandonedBear {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def upGap     = low    > high[1];
    def dwnGap    = low[1] > high;
    def bodySize    = AbsValue( close - open);
    def candleSize  = AbsValue( high  - low);
    def bodyPcnt    = bodySize / candleSize * 100;
    def bodyIsDoji  = bodyPcnt <= 5;
    plot result = upCandle[2] and bodyIsDoji [1] and upGap[1] and dwnCandle and dwnGap;
}
#piercing() =>
script piercing {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def bodyLow   = Min( close, open);
    def middleBody  = bodySize / 2 + bodyLow;
    plot result = dwnCandle[1] and tallBody[1] and upCandle and open <= low[1] and close > middleBody[1] and close < open[1];
}
#darkCloudCover() =>
script darkCloudCover {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def bodyLow   = Min( close, open);
    def middleBody  = bodySize / 2 + bodyLow;
    plot result = upCandle[1] and tallBody[1] and dwnCandle and open >= high[1] and close < middleBody[1] and close > open[1];
}
#tasukiBull() =>
script tasukiBull {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodyHigh  = Max( close, open);
    def bodyLow   = Min( close, open);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    def bodyUpGap   = bodyHigh[1] < bodyLow;
    plot result =  tallBody[2] and shortBody[1] and upCandle[2] and bodyUpGap[1] and upCandle[1] and dwnCandle and bodyLow >= bodyHigh[2] and bodyLow <= bodyLow[1];
}
#tasukiBear() =>
script tasukiBear {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodyHigh  = Max( close, open);
    def bodyLow   = Min( close, open);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    def bodyDwnGap  = bodyHigh < bodyLow[1];
    plot result = tallBody[2] and shortBody[1] and dwnCandle[2] and bodyDwnGap[1] and dwnCandle[1] and upCandle and bodyHigh <= bodyLow[2] and bodyHigh >= bodyHigh[1];
}
#risingThree() =>
script risingThree {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    plot result = tallBody[4] and upCandle[4] and shortBody[3] and dwnCandle[3] and open[3] < high[4] and close[3] > low[4] and shortBody[2] and dwnCandle[2] and open[2] < high[4] and close[2] > low[4] and shortBody[1] and dwnCandle[1] and  open[1] < high[4]   and close[1] > low[4] and tallBody and upCandle and close > close[4];
}
#fallingThree() =>
script fallingThree {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    plot result = tallBody[4] and dwnCandle[4] and shortBody[3] and upCandle[3] and open[3] > low[4] and close[3] < high[4] and shortBody[2] and upCandle[2] and open[2] > low[4] and close[2] < high[4]  and shortBody[1]  and upCandle[1] and open[1] > low[4] and close[1] < high[4] and tallBody and dwnCandle and close < close[4];
}
#risingWindow() =>
script risingWindow {
    def candleSize     = AbsValue( high  - low);
    plot result = candleSize != 0 and candleSize[1] != 0 and low > high[1];
}
#fallingWindow() =>
script fallingWindow {
    def candleSize     = AbsValue( high  - low);
    plot result = candleSize != 0 and candleSize[1] != 0 and high < low[1];
}
#kickingBull() =>
script kickingBull {
    def upGap     = low > high[1];
    plot result = marubearzu()[1] and marubullzu() and upGap;
}
#kickingBear() =>
script kickingBear {
    def dwnGap    = low[1] > high;
    plot result  = marubullzu()[1] and marubearzu() and dwnGap;
}
#lls(float ratio = 75) =>"Long Lower Shadow" candle patterns
script lls {
    input ratio = 75;
    def candleSize = AbsValue( high  - low);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    plot result = bottomWickSize > candleSize / 100 * ratio;
}
#lus(float ratio = 75) =>"Long Upper Shadow" candle patterns
script lus {
    input ratio = 75;
    def candleSize = AbsValue( high  - low);
    def topWickSize = AbsValue(Max(close, open) - high);
    plot result = topWickSize > candleSize / 100 * ratio;
}
#bullNeck() =>
script bullNeck {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def candleSize  = AbsValue( high  - low);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    plot result = upCandle[1] and tallBody[1] and dwnCandle and open > close[1] and shortBody and candleSize != 0 and AbsValue(close - high[1]) <= bodyAvg * 0.05;
}
#bearNeck() =>
script bearNeck {
    def upCandle  = close > open;
    def dwnCandle = close < open;
    def candleSize  = AbsValue( high  - low);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def shortBody   = bodySize < bodyAvg;
    plot result = dwnCandle[1] and tallBody[1] and upCandle and open < close[1] and shortBody and candleSize != 0 and AbsValue(close - low[1]) <= bodyAvg * 0.05;
}
#soldiers(float wickSize = 5) =>
script soldiers {
    input wickSize = 5;
    def upCandle  = close > open;
    def candleSize  = AbsValue( high  - low);
    def topWickSize = AbsValue(Max(close, open) - high);
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    def wicks = candleSize * wickSize / 100 > topWickSize;
    plot result = tallBody and tallBody[1] and tallBody[2] and upCandle and upCandle[1] and upCandle[2] and
                      close   > close[1] and close[1] > close[2] and open < close[1] and open > open[1] and
                      open[1] < close[2] and open[1]  > open[2]  and wicks and wicks[1] and wicks[2];
}
#crows(float wickSize = 5) =>
script crows {
    input wickSize = 5;
    def dwnCandle = close < open;
    def candleSize  = AbsValue( high  - low);
    def bottomWickSize = AbsValue(Min( close, open) - low);
    def wicks = candleSize * wickSize / 100 > bottomWickSize;
    def bodySize    = AbsValue( close - open);
    def bodyAvg     = ExpAverage( bodySize, 14);
    def tallBody    = bodySize > bodyAvg;
    plot result = tallBody and tallBody[1] and tallBody[2]   and dwnCandle and dwnCandle[1] and dwnCandle[2]   and
                  close   < close[1] and close[1] < close[2] and open > close[1]            and open < open[1] and
                  open[1] > close[2] and open[1]  < open[2]  and wicks and wicks[1]         and wicks[2];
}
#triStarBull() =>
script triStarBull {
    input dojiSize = 5.0;
    input dojiWickSize = 2.0;
    def bodyHigh  = Max( close, open);
    def bodyLow   = Min( close, open);
    def bodyUpGap   = bodyHigh[1] < bodyLow;
    def bodyDwnGap  = bodyHigh < bodyLow[1];
    plot result = Doji(dojiSize,dojiWickSize) and Doji(dojiSize,dojiWickSize)[2] and Doji(dojiSize,dojiWickSize)[3] and bodyDwnGap[1] and bodyUpGap;
}
#triStarBear() =>
script triStarBear {
    input dojiSize = 5.0;
    input dojiWickSize = 2.0;
    def bodyHigh  = Max(close, open);
    def bodyLow   = Min(close, open);
    def bodyUpGap   = bodyHigh[1] < bodyLow;
    def bodyDwnGap  = bodyHigh < bodyLow[1];
    plot result = Doji(dojiSize,dojiWickSize) and Doji(dojiSize,dojiWickSize)[2] and Doji(dojiSize,dojiWickSize)[3] and bodyDwnGap and bodyUpGap[1];
}
#insideBar() =>
script insideBar {
    plot result  = high < high[1] and low > low[1];
}
#doubleInside() =>
script doubleInside {
    plot result  = insideBar() and insideBar()[1];
}

#--- Calc
def na = Double.NaN;
def priceAvg = SimpleMovingAvg(close  ,   priceAvgLength);
def sma200   = multiMa(close  ,   MovAvgLength2, maType);
def sma50    = multiMa(close  ,   MovAvgLength1, maType);

def utr1     =   close > priceAvg;
def utr2     =   close > sma50 and sma50 > sma200;

def dtr1     =   close < priceAvg;
def dtr2     =   close < sma50 and sma50 < sma200;

def upTrend  = if trendRule == trendRule."Trend Rule 1" then utr1 else
               if trendRule == trendRule."Trend Rule 2" then utr2 else yes;
def downTrend = if trendRule == trendRule."Trend Rule 1" then dtr1 else
                if trendRule == trendRule."Trend Rule 2" then dtr2 else yes;
#-- MovAvg
plot Mov1 = sma50;
plot Mov2 = sma200;
Mov1.SetDefaultColor(Color.CYAN);
Mov2.SetDefaultColor(Color.DARK_ORANGE);
Mov1.SetHiding(!ShowMovingAvg);
Mov2.SetHiding(!ShowMovingAvg);

#--- Pattern calc
def d = doji and Doji(dojiSize, dojiWickSize);
def bew = bullEngulf and bullEngulf(rejectWickMax , EngulfingMustEngulfWick) and upTrend;
def beb = bearEngulf and bearEngulf(rejectWickMax ,EngulfingMustEngulfWick) and downTrend;
def h   = hammer and Hammer(hammerFib , hsShadowPerc) and downTrend;
def ss  = star and star(hammerFib, hsShadowPerc) and upTrend;
def dd      = dragonflyDoji and dragonflyDoji();
#def gd      = gravestoneDoji and gravestoneDoji();
def tb      = tweezerBottom and tweezerBottom(TweezerCloseHalf) and downTrend[1];
def tt      = tweezerTop and tweezerTop(TweezerCloseHalf) and upTrend[1];
def stw     = spinningTopBull and spinningTopBull(spinWickSize);
def stb     = spinningTopBear and spinningTopBear(spinWickSize);
def ms      = morningStar and morningStar() and downTrend;
def es      = eveningStar and eveningStar() and upTrend;
def bhw     = haramiBull and haramiBull()   and downTrend[1];
def bhb     = haramiBear and haramiBear()   and upTrend[1];
def hcw     = haramiBullCross and haramiBullCross() and downTrend[1];
def hcb     = haramiBearCross and haramiBearCross() and upTrend[1];
def mw      = marubullzu and marubullzu();
def mb      = marubearzu and marubearzu();
def abw     = abandonedBull and abandonedBull() and downTrend[1];
def abb     = abandonedBear and abandonedBear() and upTrend[1];
def p       = piercing      and piercing() and downTrend[1];
def dcc     = darkCloudCover and darkCloudCover() and upTrend[1];
def utg     = tasukiBull and tasukiBull()        and upTrend;
def dtg     = tasukiBear and tasukiBear()        and downTrend;
def rtm     = risingThree and risingThree()      and upTrend[4];
def ftm     = fallingThree and fallingThree()    and downTrend[4];
def rw      = risingWindow and risingWindow()    and upTrend[1];
def fw      = fallingWindow and fallingWindow()  and downTrend[1];
def kw      = kickingBull and kickingBull();
def kb      = kickingBear and kickingBear();
def ll      = lls and lls(LongShadowRatio);
def lu      = lus and lus(LongShadowRatio);
def nw      = bullNeck and bullNeck() and upTrend;
def nb      = bearNeck and bearNeck() and downTrend;
def ws      = soldiers and soldiers(SoldiersCrowsWick);
def bc      = crows and crows(SoldiersCrowsWick);
def tsw     = triStarBull and triStarBull(dojiSize, dojiWickSize) and downTrend[2];
def tsb     = triStarBear and triStarBear(dojiSize, dojiWickSize) and upTrend[2];
def ib      = insidebar and insideBar();
def dib     = doubleInsideBar and doubleInside();

AddChartBubble(d, if close>open then low else high, "doji", Color.GRAY, if close>open then no else yes);
AddChartBubble(bew, low, "bull Engulf", Color.GREEN, no);
AddChartBubble(beb, high, "bear Engulf", Color.RED, yes);
AddChartBubble(h, low, "hammer", Color.GREEN, no);
AddChartBubble(ss, high, "star", Color.RED, yes);
AddChartBubble(dd, low, "dragonfly Doji", Color.GREEN, no);
AddChartBubble(tb, low, "tweezer Bottom", Color.GREEN, no);
AddChartBubble(tt, high, "tweezer Top", Color.RED, yes);
AddChartBubble(stw,low , "spinning Top Bull", Color.GREEN, no);
AddChartBubble(stb,high, "spinning Top Bear", Color.RED, yes);
AddChartBubble(ms, low, "morning Star", Color.GREEN, no);
AddChartBubble(es, high, "evening Star", Color.RED, yes);
AddChartBubble(bhw, low, "harami Bull", Color.GREEN, no);
AddChartBubble(bhb, high, "harami Bear", Color.RED, yes);
AddChartBubble(hcw, low, "harami Bull Cross", Color.GREEN, no);
AddChartBubble(hcb, high, "harami Bear Cross", Color.RED, yes);
AddChartBubble(mw, low, "marubullzu", Color.GREEN, no);
AddChartBubble(mb, high, "marubearzu", Color.RED, yes);
AddChartBubble(abw, low, "abandoned Bull", Color.GREEN, no);
AddChartBubble(abb, high, "abandoned Bear", Color.RED, yes);
AddChartBubble(p, low, "piercing", Color.GREEN, no);
AddChartBubble(dcc, high, "dark Cloud Cover", Color.RED, yes);
AddChartBubble(utg, low, "tasuki Bull", Color.GREEN, no);
AddChartBubble(dtg, high, "tasuki Bear", Color.RED, yes);
AddChartBubble(rtm, low, "rising Three", Color.GREEN, no);
AddChartBubble(ftm, high, "falling Three", Color.RED, yes);
AddChartBubble(rw, low, "rising Window", Color.GREEN, no);
AddChartBubble(fw, high, "falling Window", Color.RED, yes);
AddChartBubble(kw, low, "kicking Bull", Color.GREEN, no);
AddChartBubble(kb, high, "kicking Bear", Color.RED, yes);
AddChartBubble(ll, low, "lls", Color.GREEN, no);
AddChartBubble(lu, high, "lus", Color.RED, yes);
AddChartBubble(nw, low, "bull Neck", Color.GREEN, no);
AddChartBubble(nb, high, "bear Neck", Color.RED, yes);
AddChartBubble(ws, low, "soldiers", Color.GREEN, no);
AddChartBubble(bc, high, "crows", Color.RED, yes);
AddChartBubble(tsw, low, "triStar Bull", Color.GREEN, no);
AddChartBubble(tsb, high, "triStar Bear", Color.RED, yes);
AddChartBubble(ib, if close>open then low else high, "inside bar", Color.GRAY, if close>open then no else yes);
AddChartBubble(dib, if close>open then low else high, "double inside bar", Color.GRAY, if close>open then no else yes);


#--- END CODE

Can anyone advise how the definition of hammer could be slightly adjusted in this script to tolerate a small amount of upper wick as opposed to none? The definition is too strict currently so candles that I think would be considered hammers are not being labeled as hammers.
 
Can anyone advise how the definition of hammer could be slightly adjusted in this script to tolerate a small amount of upper wick as opposed to none? The definition is too strict currently so candles that I think would be considered hammers are not being labeled as hammers.
adjust "hammerFib" and/or "hsShadowPerc" from the settings
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
535 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top