Candlestick Indicator

PastorDave

New member
Hi guys. I've been working on this little piece of code for sometime now. Price action and candlestick analysis has always been a struggle for me, so I started studying candles and wanted to put all the information into an indicator. This is NOWHERE NEAR done right now, and still has a few problems (TOO MANY LINES) but I think it's a good start. Here's a quick rundown of the colors:

Spinning Top - Blue
Bullish - Green
Bullish - Red
Bullish or Bearish Engulfing - Dark Orange
*Bullish from Below / Bearish from Above
Doji - Dark Red
Piercing Line or Dark Cloud Cover - Cyan
Highwave - Plum
Inside Bar - White
Shooting Star - Orange

I've also written some basic descriptions of most of the candles in the code. I would love to write some more code for a few things but haven't gotten to it yet:
1. Have an on/off tab in setting to turn all the extended lines on and off.
2. Code once the close closes above the line, the line turns grey.
If you guys have any other ideas, please feel free to tweak it. To all you candlestick pros, some of the settings could probably be dialed in for better results.

Code:
#Candlestick Indicator

#----------------------------------------
#Bullish Candle - Typical Bullish Candles
#----------------------------------------

input length3 = 10;
def GreenCandle =
    IsLongWhite(length3);
plot Bullish = GreenCandle;

#----------------------------------------------------------
#Spinning Top - Immediately preceding trend losing momemtum
#----------------------------------------------------------

def body_top = Max(open, close);
def body_bottom = Min(open, close);
def upper_shadow = high - body_top;
def lower_shadow = body_bottom - low;
def candle_height = high - low;

def body_height_is_less_than_one_third_candle_height = candle_height > 3 * BodyHeight();
def upper_shadow_height_is_less_than_one_half_ish_candle_height = upper_shadow < (.001 + candle_height) / 2;
def lower_shadow_height_is_less_than_one_half_ish_candle_height = lower_shadow < (.001 + candle_height) / 2;

def smallbody = body_height_is_less_than_one_third_candle_height
    and upper_shadow_height_is_less_than_one_half_ish_candle_height
    and lower_shadow_height_is_less_than_one_half_ish_candle_height;

plot Spinning_Top = smallbody;

#-----------------------------------------------------------------------------------------
#Doji - Market is at a point of indecision. Highest high of doji and prior candle used for resistance. No support.
#-----------------------------------------------------------------------------------------

input length = 20;
input bodyFactor = 0.05;
Assert(bodyFactor >= 0, "'body factor' must not be negative: " + bodyFactor);
def Doji = IsDoji(length, bodyFactor);

plot DojiR = if Doji and high < high[1] then high[1] else if Doji and high >= high[1] then high else Double.NaN;
DojiR.SetPaintingStrategy(PaintingStrategy.DASHES);
DojiR.SetDefaultColor(Color.RED);
DojiR.SetLineWeight(2);

def DojiRext = if IsNaN(DojiR) then DojiRext[1] else DojiR;
plot DojiRextline = DojiRext;
DojiRextline.SetPaintingStrategy(PaintingStrategy.DASHES);
DojiRextline.SetDefaultColor(Color.RED);

#-------------------------------------------------------------------
#Inside Bar - Consolidation. There is a fight going on for direction
#-------------------------------------------------------------------

def IsUp = close > open;
def IsDown = close < open;
def IsDoji = IsDoji();
def avgRange = 0.05 * Average(high - low, 20);
def InsideBar =
    ((Sum(IsUp, 1)[1] >= 0)) and
    ((Sum(IsUp, 1)[0] >= 0)) and
    Highest(high[1], 1) >= Highest(high[0], 1) and
    Lowest(low[1], 1) <= Lowest(low[0], 1);

#-------------------------------------------------------------------------------------------
# High Wave Candlestick Pattern - Market is "confused." You do not want to stay in the trade
# Assembled by BenTen at useThinkScript.com
#-------------------------------------------------------------------------------------------

def Day = (high - low);
def Day_1 = (high[1] - low[1]);
def Day_2 = (high[2] - low[2]);
def Day_3 = (high[3] - low[3]);
def Day_4 = (high[4] - low[4]);
def Day_5 = (high[5] - low[5]);
def Avg = ((Day_1 + Day_2 + Day_3 + Day_4 + Day_5) / 5);
input Highwave_Wick_Multiplier = 3.0;
input Range_Multiplier = 1.0;

def Highwave = (((high - low) - (AbsValue(open - close))) > ((AbsValue(open - close)) * Highwave_Wick_Multiplier)) and ((Day > (Avg * Range_Multiplier)));

#----------------------------------------------------------------------
#Hammer - "A chance to buy at the bottom." Use Low of hammer as Support
#----------------------------------------------------------------------

def MovingAvg = 52;
def Avg2 = SimpleMovingAvg(close, MovingAvg);
def CandleBody = AbsValue(open - close);
def UpperWick = high - Max(open, close);
def LowerWick = Min(open, close) – low;
def Hammer = LowerWick > CandleBody * 2 and UpperWick <= LowerWick * 0.2 and low < low[1] and low < low[2] and low < low[3] and low < low[4] and low < low[5] and low < low[6] and low < low[7] and low < low[8] and close > Avg;

plot Hammer2 = if Hammer then low else Double.NaN;
Hammer2.SetPaintingStrategy(PaintingStrategy.DASHES);
Hammer2.SetDefaultColor(Color.GREEN);
Hammer2.SetLineWeight(2);
def hammer2ext = if IsNaN(Hammer2) then hammer2ext[1] else Hammer2;
plot Hammer2extline = hammer2ext;
Hammer2extline.SetPaintingStrategy(PaintingStrategy.DASHES);
Hammer2extline.SetDefaultColor(Color.GREEN);

#----------------------------------------------------------
#Bullish Engulfing - A Bottom Reversal. The bullish candle wraps around the entire body of the previous candle in a downtrend. Lowest low of the two sessions is Support.
#----------------------------------------------------------

def BullEngulf =
    IsDescending(close, 3)[2] and
    IsDown[1] and
    IsUp[0] and
    open[1] < close[0] and
    close[1] >= open[0];

plot BullSupport = if BullEngulf and low > low [1] then low[1] else if BullEngulf and low <= low[1] then low else Double.NaN;
BullSupport.SetPaintingStrategy(PaintingStrategy.DASHES);
BullSupport.SetDefaultColor(Color.DARK_ORANGE);
BullSupport.SetLineWeight(2);
def Bullext = if IsNaN(BullSupport) then Bullext[1] else BullSupport;
plot Bullextline = Bullext;
Bullextline.SetPaintingStrategy(PaintingStrategy.DASHES);
Bullextline.SetDefaultColor(Color.DARK_ORANGE);

#----------------------------------------------------------------------------------------------
#Bullish Harami - Market in a downtrend. First Candle is unusually Long Downward Candle. Second candle has a small body within the prior red candle body. Prior trend is "losing breathe."
#----------------------------------------------------------------------------------------------

input length1 = 20;
input trendSetup1 = 3;
input bodyFactor1 = 0.3;
Assert(bodyFactor1 >= 0, "'body factor' must not be negative: " + bodyFactor1);

def BodyHeight = BodyHeight();
def IsShort = BodyHeight < bodyFactor1 * Average(BodyHeight, length);
def BodyMax = Max(open, close);
def BodyMin = Min(open, close);
def IsEngulfing = BodyMax < BodyMax[1] and
    BodyMin > BodyMin[1];

plot BullishHarami = IsDescending(close, trendSetup1)[1] and
    IsLongBlack(length)[1] and
    IsEngulfing and
    IsShort;

BullishHarami.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
BullishHarami.SetDefaultColor(color.green);

#----------------------------------------
#Bearish Candles - Typical Bearish Candles
#----------------------------------------

input length4 = 10;
def RedCandles =
    IsLongBlack(length4);
plot Bearish = RedCandles;

#-------------------------
#Bearish Engulfing Candle - A Top Reversal. The bearish candle wraps around the entire body of the previous candle in a uptrend. Highest High of the two sessions is Resistance.
#-------------------------

def BearEngulf =
    IsAscending(close, 3)[2] and
    IsUp[1] and
    IsDown[0] and
    close[1] < open[0] and
    open[1] > close[0];
plot BResistance = if BearEngulf and high < high[1] then high[1] else if BearEngulf and high >= high[1] then high else Double.NaN;
BResistance.SetPaintingStrategy(PaintingStrategy.DASHES);
BResistance.SetDefaultColor(Color.DARK_ORANGE);
BResistance.SetLineWeight(2);

def BRext = if IsNaN(BResistance) then BRext[1] else BResistance;
plot BRextline = BRext;
BRextline.SetPaintingStrategy(PaintingStrategy.DASHES);
BRextline.SetDefaultColor(Color.DARK_ORANGE);

#-----------------------------------------------------------------------------------------------
#Piercing Line - Market in Downtrend. Candle opens below prior low and closes more than 50% into prior down candle. Lowest low of the two candles is Support.
#-----------------------------------------------------------------------------------------------

input length2 = 20;
input trendSetup = 3;

def PierceCandle = IsDescending(close, trendSetup)[1] and
    IsLongBlack(length2)[1] and
    open < low[1] and
    close > MidBodyVal()[1] and
    close < open[1];
def PiercingLine = PierceCandle;

plot PierceSupport = if PiercingLine and low > low [1] then low[1] else if PiercingLine and low <= low[1] then low else Double.NaN;
PierceSupport.SetPaintingStrategy(PaintingStrategy.DASHES);
PierceSupport.SetDefaultColor(Color.CYAN);
PierceSupport.SetLineWeight(2);
def Pext = if IsNaN(PierceSupport) then Pext[1] else PierceSupport;
plot Pextline = Pext;
Pextline.SetPaintingStrategy(PaintingStrategy.DASHES);
Pextline.SetDefaultColor(Color.CYAN);

#------------------
#Dark Cloud Covers - Market in Uptrend. Candle opens above prior high and closes more than 50% into prior high candle. Highest high of the two candles is Resistance.
#------------------

def CloudCandle = IsAscending(close, trendSetup)[1] and
    IsLongWhite(length)[1] and
    open > high[1] and
    close < MidBodyVal()[1] and
    close > open[1];
def DarkCloudCover = CloudCandle;

plot DResistance = if DarkCloudCover and high < high[1] then high[1] else if DarkCloudCover and high >= high[1] then high else Double.NaN;
DResistance.SetPaintingStrategy(PaintingStrategy.DASHES);
DResistance.SetDefaultColor(Color.CYAN);
DResistance.SetLineWeight(2);
def DRext = if IsNaN(DResistance) then DRext[1] else DResistance;
plot DRextline = DRext;
DRextline.SetPaintingStrategy(PaintingStrategy.DASHES);
DRextline.SetDefaultColor(Color.CYAN);

#--------------
#Bearish Harami - Market in a uptrend. First Candle is unusually Long Upward Candle. Second candle has a small body within the prior green candle body. Prior trend is "losing breathe."
#--------------
Assert(bodyFactor >= 0, "'body factor' must not be negative: " + bodyFactor);

def BodyHeight1 = BodyHeight();
def IsShort1 = BodyHeight < bodyFactor * Average(BodyHeight, length);
def BodyMax1 = Max(open, close);
def BodyMin1 = Min(open, close);
def IsEngulfing1 = BodyMax < BodyMax[1] and
    BodyMin > BodyMin[1];

plot BearishHarami = IsAscending(close, trendSetup)[1] and
    IsLongWhite(length)[1] and
    IsEngulfing and
    IsShort;

BearishHarami.SetPaintingStrategy(PaintingStrategy.BOOLEAN_wEDGE_DOWN);
BearishHarami.SetDefaultColor(color.Red);

#----------------------------------------------------------------------------------------------
#Shooting Star - Market in an uptrend. Small real body of candle at or near the bottom of range
#Upper shadow at least 2x height of body of candle
#----------------------------------------------------------------------------------------------

input Star_length = 30;
input Star_trendSetup = 3;
input Star_bodyFactor = 0.3;
input Star_shadowFactor = 2.0;

Assert(Star_bodyFactor >= 0, "'body factor' must not be negative: " + Star_bodyFactor);
Assert(Star_shadowFactor >= 0, "'shadow factor' must not be negative: " + Star_shadowFactor);

def Star_BodyHeight = BodyHeight();
def Star_AverageBodyHeight = Average(Star_BodyHeight, Star_length);
def Star_ErrMargin = 0.25 * Star_AverageBodyHeight;
def Star_IsShort = Star_BodyHeight <= Star_bodyFactor * Star_AverageBodyHeight;

def ShootingStar = IsAscending(close, Star_trendSetup)[1] and
 
    Star_IsShort and
    Min(open, close) - low <= Star_ErrMargin and
    high - Max(open, close) > Star_shadowFactor * Star_BodyHeight;
plot SSR = if ShootingStar then high else Double.NaN;
SSR.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
SSR.SetDefaultColor(Color.Red);
def SSRext = if IsNaN(SSR) then SSRext[1] else SSR;
plot SSRextline = SSRext;
SSRextline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
SSRextline.SetDefaultColor(Color.Red);

AssignPriceColor(if spinning_top then Color.BLUE else if Bullish then Color.GREEN else if Bearish then Color.RED else if BullEngulf or BearEngulf then Color.DARK_ORANGE else if Doji then Color.DARK_RED else if PiercingLine or DarkCloudCover then Color.CYAN else if Highwave then Color.PLUM else if InsideBar then Color.WHITE else if ShootingStar then Color.ORANGE else Color.CURRENT);
 
Last edited by a moderator:
Top