Bollys Perfect Candle Entry
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// This indicator is a combination of multiple open sources indicators
// Squeeze Momentum Indicator [LazyBear] SQZMOM_LB
// [NM]Improved Linear Regression Bull and Bear Power v02 BBP_NM_v02
// Cheezus by matheaufx
// © essjayeire

//@version=5
indicator(title="Bollys Perfect Candle Entry", shorttitle="Bollys Perfect Candle Entry", overlay=true, max_bars_back = 2000, max_labels_count=500)

src_1 = close
src_2 = close[1]
src_3 = close[2]

// [NM]Improved Linear Regression Bull and Bear Power v02 BBP_NM_v02
window = input.int(title='BBP Lookback Window:', defval=10)
smap = input.int(title='BBP Smooth factor', defval=5, minval=2, maxval=10)
sigma = input.int(title='BBP Sigma', defval=6)

f_exp_lr(_height, _length)=>
    _ret = _height + (_height/_length)

h_value = ta.highest(src_1, window)
l_value = ta.lowest(src_1, window)

h_bar = bar_index - ta.highestbars(src_1, window)
l_bar = bar_index - ta.lowestbars(src_1, window)

bbp_bear = 0-(f_exp_lr(h_value-src_1, bar_index-h_bar) > 0 ? f_exp_lr(h_value-src_1, bar_index-h_bar) : 0)
bbp_bull = 0+(f_exp_lr(src_1-l_value, bar_index-l_bar) > 0 ? f_exp_lr(src_1-l_value, bar_index-l_bar) : 0)
direction = true ? ta.alma(bbp_bull + bbp_bear, smap, 0.9, sigma) : bbp_bull*3 + bbp_bear*3
bbp_signal = true ? direction[0] > direction[1] ? 0 : direction[0] < direction[1] ? 1 : -1 : direction > bbp_bull ? 0 : direction < bbp_bear ? 1 : -1

// Squeeze Momentum Indicator [LazyBear] SQZMOM_LB
lengthKC=input.int(20, title="SQZMOM KC Length")
val = ta.linreg(src_1  -  math.avg(math.avg(ta.highest(high, lengthKC), ta.lowest(low, lengthKC)),ta.sma(src_1,lengthKC)), lengthKC,0)
bull_candle_signal = val > 0 ?  val > nz(val[1]) ? 0 : -1 : -1
bear_candle_signal = val < 0 ? val < nz(val[1]) ? 1 : -1 : -1

// Cheezus by matheaufx
hmalength = input.int(9, minval=1, title="CHEEZUS HMA Length")
smoothK = input.int(5, minval=1, title="CHEEZUS K Smoothing")
smoothD = input.int(5, minval=1, title="CHEEZUS D Smoothing")
lengthRSI = input.int(15, minval=1, title="CHEEZUS RSI Length")
lengthStoch = input.int(15, minval=1, title="CHEEZUS Stoch Length")
shift = input.int(2, minval=0, title="CHEEZUS HMA Line Shift")

hullMA2 = ta.hma(src_3, hmalength)
hullMA3 = ta.hma(src_2, hmalength)

buyLabels = input.string(title="Buy Label", defval="On", options=["On", "Off"])
sellLabels = input.string(title="Sell Label", defval="On", options=["On", "Off"])
lossLabel = input.string(title="Loss Label" , defval="Off", options=["On", "Off"])
bull = input(#7357c2, title="Up Trend")
bear = input(#c3025d, title="Down Trend")
loss = input(#2079f4, "downTrend")
colorLogic = src_1 > hullMA2 ? bull : bear

plot(hullMA2, offset=shift, style=plot.style_line, color=colorLogic, linewidth=4, title="WM", editable=false)

rsi1 = ta.rsi(src_1, lengthRSI)

k = ta.hma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = ta.ema(k, smoothD)

// Vol
symbols = syminfo.mintick == 1 ? 1 : syminfo.mintick == 0.1 ? 10 : syminfo.mintick  == 0.001 ? 100 : syminfo.mintick == 0.0001 ? 10000 : syminfo.mintick == 0.01 ? 100 : na
pipsBuy = (high - open) * symbols
pipsBuyClose = (close - open) * symbols
pipsSell = (low - open) * symbols
pipsSellClose = (close - open)  * symbols
pipsSellLogic = pipsSell * -1
pipsSellCloseLogic = pipsSellClose * -1

// Find Lowest Pip Value
pipsLossH = pipsBuy < 10 ? loss : bull
pipsLossL = pipsSellLogic < 10 ? loss : bear
HH = lossLabel == "On" and pipsBuy < 10 ? loss : bull
LL = lossLabel == "On" and pipsSellLogic < 10 ? loss : bear
// logic and labels
buy2= close[1] > hullMA2 and k[1] >= 66 and k >= 66 and close[1] >= close[2] and close[1] > hullMA3

if(buy2 and buyLabels == "On" and bull_candle_signal == 0 and bbp_signal == 0)
    label.new(bar_index, low, text="buy", color=HH, textcolor=color.white, size = size.normal, style= label.style_label_up)

sell2 = close[1] < hullMA2 and k[1] <= 33 and k<=33 and close[1] <= close[2] and close[1] < hullMA3

if(sell2 and sellLabels == "On" and bear_candle_signal == 1 and bbp_signal == 1)
    label.new(bar_index, high, text="Sell", color=LL, textcolor=color.white, size=size.normal, style = label.style_label_down)
    //label.new(bar_index, low, text=str.tostring(pipsSellLogic) + " Pips", color=LL, textcolor=color.white, size=size.normal, style=label.style_label_up)
