Candlestick Trading (Malaysia Stock Market)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © taufan_ganas

//@version=5
indicator("Candlestick Trading", overlay = true)

grp_1 = 'SHOWS BEARISH CANDLES IF ABOVE:'

//Appear if above MA:

MACS1 = input.int(title='MA 1', defval=20, minval=1, group = grp_1)
MACS1type = input.string(title='MA 1 Type', defval='SMA', options=['SMA', 'EMA'])
macs1(close, type, length) =>
    type == 'SMA' ? ta.sma(close, MACS1) : type == 'EMA' ? ta.ema(close, MACS1) : na

// MA1 Name
MACS_1 = macs1(close, MACS1type, MACS1)

MACS2 = input.int(title='MA 2', defval=50, minval=1, group = grp_1)
MACS2type = input.string(title='MA 2 Type', defval='SMA', options=['SMA', 'EMA'])
macs2(close, type, length) =>
    type == 'SMA' ? ta.sma(close, MACS2) : type == 'EMA' ? ta.ema(close, MACS2) : na

// MA2 Name
MACS_2 = macs2(close, MACS2type, MACS2)

MACS3 = input.int(title='MA 3', defval=200, minval=1, group = grp_1)
MACS3type = input.string(title='MA 3 Type', defval='SMA', options=['SMA', 'EMA'])
macs3(close, type, length) =>
    type == 'SMA' ? ta.sma(close, MACS3) : type == 'EMA' ? ta.ema(close, MACS3) : na

// MA2 Name
MACS_3 = macs3(close, MACS3type, MACS3)

grp_2 = 'BEARISH CANDLE TYPES AND LAST CANDLES DONE'

//Bearish Candlestick Criteria

//Bids

xyz = close < 1 ? 0.005 : close >= 1 and close < 10 ? 2 * 0.005 : close >= 10 and close < 100 ? 4 * 0.005 : 0.10
change = close - open
bids = change / xyz

minbids = bids >= 3

//Dark Cloud Cover

inputdccover = input(title='Dark Cloud Cover', defval=true, group = grp_2)
darkcloudcover = inputdccover and open > close[1] and close >= open[1] and close[1] > close and open > close and close[1] > open[1] and (open - close)/xyz >= 4 and close >= MACS_1 and close >= MACS_2 and close >= MACS_3

plotshape(darkcloudcover, color = #E040FB, size = size.tiny, style = shape.triangledown, location = location.abovebar, text = "Dark \nCloud \nCover\n|\n|", show_last=input(title='Last Candles Done', defval=10, group = grp_2) )

//Kickers

inputkickers = input(title='Bearish Kickers', defval=true, group = grp_2)
kickers = inputkickers and close[1] - open[1] > 0 and open > close and close[1] > open and open[1] > close and close >= MACS_1 and close >= MACS_2
plotshape(kickers, color = #E040FB, size = size.tiny, style = shape.triangledown, location = location.abovebar, text = "Bearish \nKickers\n|\n|", show_last=input(title='Last Candles Done', defval=10, group = grp_2 ) )

//Bearish Engulfing

inputbeareng = input(title='Bearish Engulfing', defval=true, group = grp_2)
bearishengulfing = inputbeareng and open > close[1] and close < open[1] and open > close and close[1] > open[1] and close >= MACS_1 and close >= MACS_2 and close >= MACS_3

plotshape(bearishengulfing, color = #E040FB, size = size.tiny, style = shape.triangledown, location = location.abovebar, text = "Bearish \nEngulfing\n|\n|", show_last=input(title='Last Candles Done', defval=10, group = grp_2) )

//Evening Star //

inputevestar = input(title='Evening Star', defval=true, group = grp_2)
day2 = close[2] > open[2]
day1A = open[1] > close[1] and close[1] > close[2] and day2
day1B = open[1] == close[1] and high - open > 0 and close - low > 0 and close[1] > close[2] and day2 // abandoned baby
day1C = open[1] == close[1] and high - open == 0 and  close - low > 0 and close[1] > close[2] and day2 // dragon fly
day1D = open[1] == close[1] and high - open > 0 and close - low > 0 and close[1] > open[2] and day2 //harami cross
daytotal =  day1A or day1B or day1C or day1D
eveningstar = inputevestar and open > close and close[1] > open and daytotal and close[2] >= open and open >= open[2] and close >= MACS_1 and close >= MACS_2

plotshape(eveningstar, color = #E040FB, size = size.tiny, style = shape.triangledown, location = location.abovebar, text = "Evening \nStar\n|\n|", show_last=input(title='Last Candles Done', defval=10, group = grp_2) )

//Three Black Crows

input3bcrow = input(title='Three Black Crows', defval=true, group = grp_2)
day3 = close[3] > open[3] 
day2a = open[2] > close[2] and day3
day = open[1] > close[1] and open[2] > open[1] and close[2] > close[1] and day2a
threeblackcrows = input3bcrow and open > close and open[1] > open and close[1] > close and day and close >= MACS_1 and close >= MACS_2 //

plotshape(threeblackcrows, color = #E040FB, size = size.tiny, style = shape.triangledown, location = location.abovebar, text = "Three \nBlack \nCrows\n|\n|", show_last=input(title='Last Candles Done', defval=10, group = grp_2) )

//Hanging Man

inputhangman = input(title='Hanging Man', defval=true, group = grp_2)
hangingman = inputhangman and open > close? close >= close[1] and close - low >= 2 * math.abs(open - close) and open!=close and high - open <= math.abs(open - close) and close >= MACS_1 and close >= MACS_2 : open >= close[1] and low - close >= 2 * math.abs(close - open) and open!=close and high - close <= math.abs(close - open) and close >= MACS_1 and close >= MACS_2

plotshape(hangingman, color = #E040FB, size = size.tiny, style = shape.triangledown, location = location.abovebar, text = "Hanging \nMan\n|\n|", show_last=input(title='Last Candles Done', defval=10, group = grp_2 ))

//Shooting Star

inputshostar = input(title='Shooting Star', defval=true, group = grp_2)
shootingstar = inputshostar and open > close? close >= close[1] and high - open >= 2 * math.abs(open - close) and open!=close and close - low <= math.abs(open - close) and close >= MACS_1 and close >= MACS_2 : inputshostar and open >= close[1] and high - close >= 2 * math.abs(close - open) and open!=close and open - low <= math.abs(close - open) and close >= MACS_1 and close >= MACS_2

plotshape(shootingstar, color = #E040FB, size = size.tiny, style = shape.triangledown, location = location.abovebar, text = "Shooting \nStar\n|\n|", show_last=input(title='Last Candles Done', defval=10, group = grp_2) )

//Tweezer Top

inputtwetop = input(title='Tweezer Top', defval=true, group = grp_2)
tweezertop = inputtwetop and open == close[1] and open > close and (open - close)/xyz >= 3 and close[1] > open[1] and close >= MACS_1 and close >= MACS_2

plotshape(tweezertop, color = #E040FB, size = size.tiny, style = shape.triangledown, location = location.abovebar, text = "Tweezer \nTop\n|\n|", show_last=input(title='Last Candles Done', defval=10, group = grp_2) )

//Bearish Harami

inputharami = input(title='Bearish Harami', defval=true, group = grp_2)
bearishharami = inputharami and close[1] - open[1] > 0 and open > close and open - close < close[1]-open[1] and close > open[1] and close[1] > open and close >= MACS_1 and close >= MACS_2

plotshape(bearishharami, color = #E040FB, size = size.tiny, style = shape.triangledown, location = location.abovebar, text = "Bearish \nHarami\n|\n|", show_last=input(title='Last Candles Done', defval=10, group = grp_2) )

//Doji

inputdoji = input(title='Doji', defval=true, group = grp_2)
doji = inputdoji and close == open and high - open > 0 and close - low > 0 and close >= MACS_1 and close >= MACS_2 
plotshape(doji, color = #E040FB, size = size.tiny, style = shape.triangledown, location = location.abovebar, text = "Doji \n|\n|", show_last=input(title='Last Candles Done', defval=10, group = grp_2) )

//Potential Top

grp_3 = 'POTENTIAL TOP: STOCHASTIC AND SIGNAL SETTINGS'

periodK = input.int(14, title='K', minval=1, group = grp_3)
periodD = input.int(3, title='D', minval=1, group = grp_3)
smoothK = input.int(3, title='Smooth', minval=1, group = grp_3)
k = ta.sma(ta.stoch(close, high, low, periodK), smoothK)
d = ta.sma(k, periodD)

labelpotentialtop = input(title='Potential Top', defval=true, group = grp_3)
stotopline = input.int(75, title = 'Stochastic Level', minval = 0, maxval = 100)

candlelist = darkcloudcover or bearishengulfing or eveningstar or threeblackcrows or hangingman or shootingstar or tweezertop or bearishharami or doji or kickers
top = labelpotentialtop and k >= stotopline and candlelist

if top
    potentialtop = label.new(bar_index, na, text = 'Potential\nTop\n▼ \n\n', color=#FFFFFF00, color = color.red, textcolor=color.red, style=label.style_label_up, size=size.normal, yloc = yloc.abovebar) 
    label.delete(potentialtop[1])

//============================================================

grp_4 ='SHOW BULLISH CANDLES IF IN BETWEEN:'

// MA1

MA1 = input.int(title='MA 4 Length', defval=30, minval=1, group = grp_4)
MA1type = input.string(title='MA 4 Type', defval='EMA', options=['SMA', 'EMA'])
ma1(close, type, length) =>
    type == 'SMA' ? ta.sma(close, MA1) : type == 'EMA' ? ta.ema(close, MA1) : na

// MA1 Name
MA_1 = ma1(close, MA1type, MA1)


//MA2

MA2 = input.int(title='MA 5 Length', defval=100, minval=1, group = grp_4)
MA2type = input.string(title='MA 5 Type', defval='EMA', options=['SMA', 'EMA'])
ma2(close, type, length) =>
    type == 'SMA' ? ta.sma(close, MA2) : type == 'EMA' ? ta.ema(close, MA2) : na

// MA2 Name
MA_2 = ma2(close, MA2type, MA2)


grp_5 = 'BULLISH CANDLE TYPES AND LAST CANDLES DONE'

//Piercing

inputpiercing = input(title='Piercing', defval=true, group = grp_5)
piercing = inputpiercing and open[1] > close and close[1] > open and open[1] > open and close > open and open[1] > close[1] and (close - open)/xyz >= 4 and low <= MA_1 and close >= MA_2 

plotshape(piercing, color = #E040FB, size = size.tiny, style = shape.triangleup, location = location.belowbar, text = "\n|\n|\nPiercing ", show_last=input(title='Last Candles Done', defval=10, group = grp_5) )

//Bullish Kickers

inputbullkickers = input(title='Bullish Kickers', defval=true, group = grp_5)
bullkickers1 = inputbullkickers and open[1] > close[1] and close > open and open > open[1] and low <= MA_1 and close >= MA_2 
bullkickers2 = inputbullkickers and open[1] > close[1] and close > open and open > close[1] and close > open[1] and low <= MA_1 and close >= MA_2 
bullkickers = bullkickers1 or bullkickers2
plotshape(bullkickers, color = #E040FB, size = size.tiny, style = shape.triangleup, location = location.belowbar, text = "\n|\n|\nBullish\nKickers ", show_last=input(title='Last Candles Done', defval=10, group = grp_5) )

//Bullish Engulfing

inputbulleng = input(title='Bullish Engulfing', defval=true, group = grp_5)
bullishengulfing = inputbulleng and close > open[1] and close[1] > open and close > open and open[1] > close[1] and low <= MA_1 and close >= MA_2 
plotshape(bullishengulfing, color = #E040FB, size = size.tiny, style = shape.triangleup, location = location.belowbar, text = "\n|\n|\nBullish\nEngulfing ", show_last=input(title='Last Candles Done', defval=10, group = grp_5) )

//Morning Star

inputmorstar = input(title='Morning Star', defval=true, group = grp_5)
day2x = open[2] > close[2] //bear candle
day1Ax = close[1] > open[1] and close[2] > close[1] and day2x //morning star
day1Bx = close[1] == open[1] and open - low > 0 and high - close > 0 and close[2] > close[1] and day2x // bullish abandoned baby
day1Cx = close[1] == open[1] and high - open == 0 and  close - low > 0 and close[2] > close[1] and day2x // dragon fly
day1Dx = close[1] == open[1] and high - open > 0 and close - low > 0 and close[2] > close[1] and day2x //harami cross
daytotalx =  day1Ax or day1Bx or day1Cx or day1Dx
morningstar = inputmorstar and close > open and open > close[1] and daytotalx and close[2] >= open and close >= close[2] and low <= MA_1 and close >= MA_2  
plotshape(morningstar, color = #E040FB, size = size.tiny, style = shape.triangleup, location = location.belowbar, text = "\n|\n|\nMorning\nStar ", show_last=input(title='Last Candles Done', defval=10, group = grp_5) ) 

//Three White Soldiers

input3soldiers = input(title='Three White Soldiers', defval=true, group = grp_5)
day3x = open[3] >= close[3] 
day2ax = close[2] > open[2] and day3x
dayx = close[1] > open[1] and close[1] > close[2] and open[1] > open[2] and day2ax
whitesoldiers = input3soldiers and close > open and close > close[1] and open > open[1] and dayx and low <= MA_1 and close >= MA_2 
plotshape(whitesoldiers, color = #E040FB, size = size.tiny, style = shape.triangleup, location = location.belowbar, text = "\n|\n|\nThree\nWhite\nSoldiers ", show_last=input(title='Last Candles Done', defval=10, group = grp_5) )

//Hammer

inputhammer = input(title='Hammer', defval=true, group = grp_5)
hammer = inputhammer and open > close? close[1] >= close and close - low >= 2 * math.abs(open - close) and open!=close and high - open <= math.abs(open - close) and low <= MA_1 and close >= MA_2 : inputhammer and close[1] >= open and open - low >= 2 * math.abs(close - open) and open!=close and high - close <= math.abs(close - open) and low <= MA_1 and close >= MA_2 
plotshape(hammer, color = #E040FB, size = size.tiny, style = shape.triangleup, location = location.belowbar, text = "\n|\n|\nHammer ", show_last=input(title='Last Candles Done', defval=10, group = grp_5) )

//Inverted Hammer

inputinvhammer = input(title='Inverted Hammer', defval=true, group = grp_5)
invertedhammer = inputinvhammer and open > close? open[1] >= close[1] and close[1] > open and high - open >= 2 * math.abs(open - close) and open!=close and close - low <= math.abs(open - close) and low <= MA_1 and close >= MA_2 : inputinvhammer and open[1] >= close[1] and close[1] > close and high - close >= 2 * math.abs(close - open) and open!=close and open - low <= math.abs(close - open) and low <= MA_1 and close >= MA_2 

plotshape(invertedhammer, color = #E040FB, size = size.tiny, style = shape.triangleup, location = location.belowbar, text = "\n|\n|\nInverted\nHammer ", show_last=input(title='Last Candles Done', defval=10, group = grp_5) ) 

//Tweezer Bottom

inputtwebot = input(title='Tweezer Bottom', defval=true, group = grp_5)
tweezerbot = inputtwebot and open == close[1] and close > open and (close - open)/xyz >= 3 and open[1] > close[1] and low <= MA_1 and close >= MA_2
plotshape(tweezerbot, color = #E040FB, size = size.tiny, style = shape.triangleup, location = location.belowbar, text = "\n|\n|\nTweezer\nBottom ", show_last=input(title='Last Candles Done', defval=10, group = grp_5) )  

//Bullish Harami

inputbullharami = input(title='Bearish Harami', defval=true, group = grp_5)
bullishharami = inputbullharami and open[1] - close[1] > 0 and close > open and close - open < open[1]-close[1] and open > close[1] and open[1] > close and low <= MA_1 and close >= MA_2
plotshape(bullishharami, color = #E040FB, size = size.tiny, style = shape.triangleup, location = location.belowbar, text = "\n|\n|\nBullish\nHarami ", show_last=input(title='Last Candles Done', defval=10, group = grp_5) ) 

//Doji

inputbulldoji = input(title='Bullish Doji', defval=true, group = grp_5)
bulldoji = inputbulldoji and close == open and high - open > 0 and close - low > 0 and low <= MA_1 and close >= MA_2
plotshape(bulldoji, color = #E040FB, size = size.tiny, style = shape.triangleup, location = location.belowbar, text = "\n|\n|\nDoji", show_last=input(title='Last Candles Done', defval=10, group = grp_5) )

//================================================

//Potential Top And Bottom

grp_6 = 'POTENTIAL BOTTOM: SIGNAL SETTINGS'

pK = input.int(14, title='K', minval=1, group = grp_6)
pD = input.int(3, title='D', minval=1, group = grp_6)
sK = input.int(3, title='Smooth', minval=1, group = grp_6)
K = ta.sma(ta.stoch(close, high, low, pK), sK)
D = ta.sma(K, pD)

//Potential Bottom

labelpotentialbottom = input(title='Potential Bottom', defval=true, group = grp_6)
stobottomline = input.int(25, title = 'Stochastic Level : Bottom Line', minval = 0, maxval = 100)

bullcandlelist = piercing or bullkickers or bullishengulfing or morningstar or whitesoldiers or hammer or invertedhammer or tweezerbot or bullishharami or bulldoji
bottom = labelpotentialbottom and K <= stobottomline and bullcandlelist

if bottom
    potentialbottom = label.new(bar_index, na, text='\n▲\nPotential \nBottom', color=#FFFFFF00, textcolor=color.green, style=label.style_label_up, size=size.normal, yloc=yloc.belowbar)
    label.delete(potentialbottom[1])
    

