All-in-One-BTFinancials
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5
indicator(title='All-in-One-BTFinancials', shorttitle='All-in-One-BTFinancials', overlay=true, max_boxes_count=20, format=format.price, precision=0)
_v = input.string("1.0.1", title="Version", options=["1.0.1"], group="Balusen", tooltip="MOhanOBChop")
sens = input.int(28, minval=1, title='MohanOBChop', group='Balusen', tooltip='MohanOBChop')
sens /= 100

//OB Colors
col_bullish = input.color(#5db49e, title="Bullish OB Border", inline="a", group="Order Block")
col_bullish_ob = input.color(color.new(#64C4AC, 90), title="Background", inline="a", group="Order Block")

col_bearish = input.color(#4760bb, title="Bearish OB Border", inline="b", group="Order Block")
col_bearish_ob = input.color(color.new(#506CD3, 90), title="Background", inline="b", group="Order Block")


// Alerts
buy_alert = input.bool(title='Buy Signal', defval=true, group='Alerts', tooltip='An alert will be sent when price goes below the top of a bullish order block.')
sell_alert = input.bool(title='Sell Signal', defval=true, group='Alerts', tooltip='An alert will be sent when price goes above the bottom of a bearish order block.')


// Delacring Variables
bool ob_created = false
bool ob_created_bull = false
var int cross_index = na

// Declaring Box Arrays
var box drawlongBox = na
var longBoxes = array.new_box()
var box drawShortBox = na
var shortBoxes = array.new_box()


// Custom Rate of Change (ROC) calculation. This is to calculate high momentum moves in the market.
pc = (open - open[4]) / open[4] * 100

// If the ROC crossover our Sensitivty input - Then create an Order Block
// Sensitivty is negative as this is a Bearish OB
if ta.crossunder(pc, -sens)
    ob_created := true
    cross_index := bar_index
    cross_index

// If the ROC crossover our Sensitivty input - Then create an Order Block
if ta.crossover(pc, sens)
    ob_created_bull := true
    cross_index := bar_index
    cross_index


// -------------------------------
// Bearish OB Creation
// -------------------------------
// Check if we should create a OB, Also check if we haven't created an OB in the last 5 candles.
if ob_created and cross_index - cross_index[1] > 5
    float last_green = 0
    float highest = 0
    // Loop through the most recent candles and find the first GREEN (Bullish) candle. We will place our OB here.
    for i = 4 to 15 by 1
        if close[i] > open[i]
            last_green := i
            break
    // Draw our OB on that candle - then push the box into our box arrays.
    drawShortBox := box.new(left=bar_index[last_green], top=high[last_green], bottom=low[last_green], right=bar_index[last_green], bgcolor=col_bearish_ob, border_color=col_bearish, border_width=0, extend=extend.right)
    array.push(shortBoxes, drawShortBox)
    alert("Resistance OB Created - SELL NOW")

// -------------------------------
// Bullish OB Creation
// -------------------------------
// Check if we should create a OB, Also check if we haven't created an OB in the last 5 candles.
if ob_created_bull and cross_index - cross_index[1] > 5
    float last_red = 0
    float highest = 0
    // Loop through the most recent candles and find the first RED (Bearish) candle. We will place our OB here.
    for i = 4 to 15 by 1
        if close[i] < open[i]
            last_red := i
            break
    // Draw our OB on that candle - then push the box into our box arrays.
    drawlongBox := box.new(left=bar_index[last_red], top=high[last_red], bottom=low[last_red], right=bar_index[last_red], bgcolor=col_bullish_ob, border_color=col_bullish, border_width=0, extend=extend.right)
    array.push(longBoxes, drawlongBox)
    alert("Support OB Created - BUY NOW")

// ----------------- Bearish Order Block -------------------
// Clean up OB boxes and place alerts
if array.size(shortBoxes) > 0
    for i = array.size(shortBoxes) - 1 to 0 by 1
        sbox = array.get(shortBoxes, i)
        top = box.get_top(sbox)
        bot = box.get_bottom(sbox)
        // If the two last closes are above the high of the bearish OB - Remove the OB
        if close[1] > top and close[2] > top
            array.remove(shortBoxes, i)
            box.delete(sbox)
        // Alerts
        if high > bot and sell_alert
            alert('Price inside Resistance OB BOX', alert.freq_once_per_bar)

// ----------------- Bullish Clean Up -------------------
// Clean up OB boxes and place alerts
if array.size(longBoxes) > 0
    for i = array.size(longBoxes) - 1 to 0 by 1
        sbox = array.get(longBoxes, i)
        bot = box.get_bottom(sbox)
        top = box.get_top(sbox)
        // If the two last closes are below the low of the bullish OB - Remove the OB
        if close[1] < bot and close[2] < bot
            array.remove(longBoxes, i)
            box.delete(sbox)
        // Alerts
        if low < top and buy_alert
            alert('Price inside SUPPORT OB BOX', alert.freq_once_per_bar)
            


// CPR START

pivottimeframe = input.timeframe(title="Pivot Resolution", defval="D", options=["D", "W", "M"])
dp = input(true, title="Display Floor Pivots")
cp = input(true, title="Display Camarilla Pivots")
hl = input(true, title="Display M, W, D Highs/Lows")
tp = input(false, title="Display Tomorrow Pivots")
 
//dp in the prefix implies daily pivot calculation
dpopen = request.security(syminfo.tickerid, pivottimeframe, open[1], barmerge.gaps_off, barmerge.lookahead_on)
dphigh = request.security(syminfo.tickerid, pivottimeframe, high[1], barmerge.gaps_off, barmerge.lookahead_on)
dplow = request.security(syminfo.tickerid, pivottimeframe, low[1], barmerge.gaps_off, barmerge.lookahead_on)
dpclose = request.security(syminfo.tickerid, pivottimeframe, close[1], barmerge.gaps_off, barmerge.lookahead_on)
dprange = dphigh - dplow
 
//Expanded Floor Pivots Formula
pivot = (dphigh + dplow + dpclose) / 3.0
bc = (dphigh + dplow) / 2.0
tc = pivot - bc + pivot
r1 = pivot * 2 - dplow
r2 = pivot + dphigh - dplow
r3 = r1 + dphigh - dplow
r4 = r3 + r2 - r1
s1 = pivot * 2 - dphigh
s2 = pivot - (dphigh - dplow)
s3 = s1 - (dphigh - dplow)
s4 = s3 - (s1 - s2)
 
//Expanded Camarilla Pivots Formula
h1 = dpclose + dprange * (1.1 / 12)
h2 = dpclose + dprange * (1.1 / 6)
h3 = dpclose + dprange * (1.1 / 4)
h4 = dpclose + dprange * (1.1 / 2)
h5 = dphigh / dplow * dpclose
l1 = dpclose - dprange * (1.1 / 12)
l2 = dpclose - dprange * (1.1 / 6)
l3 = dpclose - dprange * (1.1 / 4)
l4 = dpclose - dprange * (1.1 / 2)
l5 = dpclose - (h5 - dpclose)
 


// AUTO FIBONACCI start									  

devTooltip = "Deviation is a multiplier that affects how much the price should deviate from the previous pivot in order for the bar to become a new pivot."
depthTooltip = "The minimum number of bars that will be taken into account when calculating the indicator."
// pivots threshold
threshold_multiplier = input.float(title="Deviation", defval=2.5, minval=0, tooltip=devTooltip)
dev_threshold = ta.atr(10) / close * 100 * threshold_multiplier
depth = input.int(title="Depth", defval=100, minval=1, tooltip=depthTooltip)
deleteLastLine          = input.bool(title="Delete Last Line", defval=false)
bgcolorChange           = input.bool(title="Change BgColor", defval=false)

reverse = input(false, "Reverse")
var extendLeft = input(false, "Extend Left    |    Extend Right", inline = "Extend Lines")
var extendRight = input(true, "", inline = "Extend Lines")
var extending = extend.none
if extendLeft and extendRight
    extending := extend.both
if extendLeft and not extendRight
    extending := extend.left
if not extendLeft and extendRight
    extending := extend.right
prices = input(true, "Show Prices")
levels = input(true, "Show Levels", inline = "Levels")
levelsFormat = input.string("Values", "", options = ["Values", "Percent"], inline = "Levels")
labelsPosition = input.string("Right", "Labels Position", options = ["Left", "Right"])
var int bg = input.int(100, "bg", minval = 0, maxval = 100)

						  
var line lineLast = na
var int iLast = 0
var int iPrev = 0
var float pLast = 0
var isHighLast = false // otherwise the last pivot is a low pivot

																			   
pivots(src, length, isHigh) =>
    l2 = length * 2
    c = nz(src[length])
    ok = true
    for i = 0 to l2
        if isHigh and src[i] > c
            ok := false

        if not isHigh and src[i] < c
            ok := false
    if ok
        [bar_index[length], c]
    else
        [int(na), float(na)]

													
[iH, pH] = pivots(high, depth / 2, true)
[iL, pL] = pivots(low, depth / 2, false)

																			 
calc_dev(base_price, price) =>
    100 * (price - base_price) / price

																		
pivotFound(dev, isHigh, index, price) =>
    if isHighLast == isHigh and not na(lineLast)
        // same direction
        if isHighLast ? price > pLast : price < pLast
            line.set_xy2(lineLast, index, price)
            [lineLast, isHighLast]
        else
            [line(na), bool(na)]
    else // reverse the direction (or create the very first line)
        if math.abs(dev) > dev_threshold
            // price move is significant
            id = line.new(iLast, pLast, index, price, color=color.gray, width=1, style=line.style_dashed)
            [id, isHigh]
        else
            [line(na), bool(na)]

																		   
if not na(iH)
    dev = calc_dev(pLast, pH)
    [id, isHigh] = pivotFound(dev, true, iH, pH)
    if not na(id)
        if id != lineLast
            line.delete(lineLast)
        lineLast := id
        isHighLast := isHigh
        iPrev := iLast
        iLast := iH
        pLast := pH
else
    if not na(iL)
        dev = calc_dev(pLast, pL)
        [id, isHigh] = pivotFound(dev, false, iL, pL)
        if not na(id)
            if id != lineLast and deleteLastLine
                line.delete(lineLast)
            lineLast := id
            isHighLast := isHigh
            iPrev := iLast
            iLast := iL
            pLast := pL

_draw_line(price, col) =>
    var id = line.new(iLast, price, bar_index, price, color=col, width=1, extend=extend.right)
    if not na(lineLast)
        line.set_xy1(id, line.get_x1(lineLast), price)
        line.set_xy2(id, line.get_x2(lineLast), price)  
	id  


_draw_label(price, txt, txtColor) =>
    x = labelsPosition == "Left" ? line.get_x1(lineLast) : not extendRight ? line.get_x2(lineLast) : bar_index
    labelStyle = labelsPosition == "Left" ? label.style_label_right : label.style_label_left
    align = labelsPosition == "Left" ? text.align_right : text.align_left
    labelsAlignStrLeft = txt + '\n ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏ \n'
    labelsAlignStrRight = '       ' + txt + '\n ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏ \n'
    labelsAlignStr = labelsPosition == "Left" ? labelsAlignStrLeft : labelsAlignStrRight
    var id = label.new(x=x, y=price, text=labelsAlignStr, textcolor=txtColor, style=labelStyle, textalign=align, color=#00000000)
    label.set_xy(id, x, price)
    label.set_text(id, labelsAlignStr)
    label.set_textcolor(id, txtColor)

_wrap(txt) =>
    "(" + str.tostring(txt, format.mintick) + ")"

_label_txt(level, price) =>
    l = levelsFormat == "Values" ? str.tostring(level) : str.tostring(level * 100) + "%"
    (levels ? l : "") + (prices ? _wrap(price) : "")

_crossing_level(sr, r) =>
    (r > sr and r < sr[1]) or (r < sr and r > sr[1])

startPrice = reverse ? line.get_y1(lineLast) : pLast
endPrice = reverse ? pLast : line.get_y1(lineLast)

iHL = startPrice > endPrice
diff = (iHL ? -1 : 1) * math.abs(startPrice - endPrice)

processLevel(show, value, colorL, lineIdOther) =>
    float m = value
	r = startPrice + diff * m
    if show
		lineId = _draw_line(r, colorL)
        _draw_label(r, _label_txt(m, r), colorL)
        if _crossing_level(close, r)
            alert("Autofib: " + syminfo.ticker + " crossing level " + str.tostring(value),alert.freq_once_per_bar_close)
        if not na(lineIdOther)
            //linefill.new(lineId, lineIdOther, color = color.new(colorL, bg))
            linefill.new(lineId, lineIdOther, color = color.new(colorL, 100))
		lineId
    else
		lineIdOther

show_0 = input(true, "", inline = "Level0")
value_0 = input(0, "", inline = "Level0")
color_0 = input(#787b86, "", inline = "Level0")

show_0_236 = input(true, "", inline = "Level0")
value_0_236 = input(0.236, "", inline = "Level0")
color_0_236 = input(#f44336, "", inline = "Level0")

show_0_382 = input(true, "", inline = "Level1")
value_0_382 = input(0.382, "", inline = "Level1")
color_0_382 = input(#81c784, "", inline = "Level1")

show_0_5 = input(true, "", inline = "Level1")
value_0_5 = input(0.5, "", inline = "Level1")
color_0_5 = input(#4caf50, "", inline = "Level1")

show_0_618 = input(true, "", inline = "Level2")
value_0_618 = input(0.618, "", inline = "Level2")
color_0_618 = input(#009688, "", inline = "Level2")

show_0_65 = input(false, "", inline = "Level2")
value_0_65 = input(0.65, "", inline = "Level2")
color_0_65 = input(#009688, "", inline = "Level2")

show_0_786 = input(true, "", inline = "Level3")
value_0_786 = input(0.786, "", inline = "Level3")
color_0_786 = input(#64b5f6, "", inline = "Level3")

show_1 = input(true, "", inline = "Level3")
value_1 = input(1, "", inline = "Level3")
color_1 = input(#787b86, "", inline = "Level3")

show_1_272 = input(false, "", inline = "Level4")
value_1_272 = input(1.272, "", inline = "Level4")
color_1_272 = input(#81c784, "", inline = "Level4")

show_1_414 = input(false, "", inline = "Level4")
value_1_414 = input(1.414, "", inline = "Level4")
color_1_414 = input(#f44336, "", inline = "Level4")

show_1_618 = input(true, "", inline = "Level5")
value_1_618 = input(1.618, "", inline = "Level5")
color_1_618 = input(#2962ff, "", inline = "Level5")

show_1_65 = input(false, "", inline = "Level5")
value_1_65 = input(1.65, "", inline = "Level5")
color_1_65 = input(#2962ff, "", inline = "Level5")

show_2_618 = input(true, "", inline = "Level6")
value_2_618 = input(2.618, "", inline = "Level6")
color_2_618 = input(#f44336, "", inline = "Level6")

show_2_65 = input(false, "", inline = "Level6")
value_2_65 = input(2.65, "", inline = "Level6")
color_2_65 = input(#f44336, "", inline = "Level6")

show_3_618 = input(true, "", inline = "Level7")
value_3_618 = input(3.618, "", inline = "Level7")
color_3_618 = input(#9c27b0, "", inline = "Level7")

show_3_65 = input(false, "", inline = "Level7")
value_3_65 = input(3.65, "", inline = "Level7")
color_3_65 = input(#9c27b0, "", inline = "Level7")

show_4_236 = input(true, "", inline = "Level8")
value_4_236 = input(4.236, "", inline = "Level8")
color_4_236 = input(#e91e63, "", inline = "Level8")

show_4_618 = input(false, "", inline = "Level8")
value_4_618 = input(4.618, "", inline = "Level8")
color_4_618 = input(#81c784, "", inline = "Level8")

show_neg_0_236 = input(false, "", inline = "Level9")
value_neg_0_236 = input(-0.236, "", inline = "Level9")
color_neg_0_236 = input(#f44336, "", inline = "Level9")

show_neg_0_382 = input(false, "", inline = "Level9")
value_neg_0_382 = input(-0.382, "", inline = "Level9")
color_neg_0_382 = input(#81c784, "", inline = "Level9")

show_neg_0_618 = input(false, "", inline = "Level10")
value_neg_0_618 = input(-0.618, "", inline = "Level10")
color_neg_0_618 = input(#009688, "", inline = "Level10")

show_neg_0_65 = input(false, "", inline = "Level10")
value_neg_0_65 = input(-0.65, "", inline = "Level10")
color_neg_0_65 = input(#009688, "", inline = "Level10")


lineId0 = processLevel(show_neg_0_65, value_neg_0_65, color_neg_0_65, line(na))
lineId1 = processLevel(show_neg_0_618, value_neg_0_618, color_neg_0_618, lineId0)
lineId2 = processLevel(show_neg_0_382, value_neg_0_382, color_neg_0_382, lineId1)
lineId3 = processLevel(show_neg_0_236, value_neg_0_236, color_neg_0_236, lineId2)
lineId4 = processLevel(show_0, value_0, color_0, lineId3)
lineId5 = processLevel(show_0_236, value_0_236, color_0_236, lineId4)
lineId6 = processLevel(show_0_382, value_0_382, color_0_382, lineId5)
lineId7 = processLevel(show_0_5, value_0_5, color_0_5, lineId6)
lineId8 = processLevel(show_0_618, value_0_618, color_0_618, lineId7)
lineId9 = processLevel(show_0_65, value_0_65, color_0_65, lineId8)
lineId10 = processLevel(show_0_786, value_0_786, color_0_786, lineId9)
lineId11 = processLevel(show_1, value_1, color_1, lineId10)
lineId12 = processLevel(show_1_272, value_1_272, color_1_272, lineId11)
lineId13 = processLevel(show_1_414, value_1_414, color_1_414, lineId12)
lineId14 = processLevel(show_1_618, value_1_618, color_1_618, lineId13)
lineId15 = processLevel(show_1_65, value_1_65, color_1_65, lineId14)
lineId16 = processLevel(show_2_618, value_2_618, color_2_618, lineId15)
lineId17 = processLevel(show_2_65, value_2_65, color_2_65, lineId16)
lineId18 = processLevel(show_3_618, value_3_618, color_3_618, lineId17)
lineId19 = processLevel(show_3_65, value_3_65, color_3_65, lineId18)
lineId20 = processLevel(show_4_236, value_4_236, color_4_236, lineId19)
lineId21 = processLevel(show_4_618, value_4_618, color_4_618, lineId20)

//AUTO IMPULSIVE MOVE - SUPPORT and RESISTANCE

// Get starting and ending high/low price of the current pivot line
//bgcolorChange           = input.bool(title="Change BgColor", defval=false)
startIndex = line.get_x1(lineLast)
startPrice := line.get_y1(lineLast)
endIndex   = line.get_x2(lineLast)
endPrice   := line.get_y2(lineLast)

// Draw top & bottom of impulsive move

plot(startPrice, color=color.green)
plot(endPrice, color=color.red)
shortest = ta.ema(close, 8)
short = ta.ema(close, 16)
longer = ta.ema(close, 100)
longest = ta.ema(close, 200)
longCond = ta.crossover(shortest, short)
shortCond = ta.crossunder(shortest, short)
//longCond = ta.crossover(shortest, short) and (short > longer) and (longer > longest)
//shortCond = ta.crossunder(shortest, short) and (short < longer) and (longer < longest)

plotshape(series=longCond, title="Long", style=shape.triangleup, location=location.belowbar, color=#43A047, text="", size=size.small)
plotshape(series=shortCond, title="Short", style=shape.triangledown, location=location.abovebar, color=#e91e1e, text="", size=size.small)

// Do what you like with these pivot values :)
// Keep in mind there will be an X bar delay between pivot price values updating based on Depth setting
//dist = math.abs(startPrice - endPrice)
//plot(dist, color=color.new(color.purple,100))
//bullish = endPrice > startPrice
//offsetBG = -(impulsiveDepth / 2)
//bgcolor(bgcolorChange ? bullish ? color.new(color.green,90) : color.new(color.red,90) : na, offset=offsetBG)
//AUTO PIVOT - SUPPORT and RESISTANCE END

//AUTO FIBONACCI end