Bitcoin as Heikin Ashi Candles with Pivot Points
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © weak_hand

//@version=5
indicator("Bitcoin as Heikin Ashi Candles with Pivot Points", "BTC HA+PP", false, max_lines_count = 500)

string my_symbol = "INDEX:BTCUSD"

string my_timefr = timeframe.isdaily ? "W" : timeframe.isweekly ? "M" : timeframe.ismonthly ? "12M" : "D"
int x_start = time(my_timefr)
int x_stop  = time_close(my_timefr)

string is_type = input.string(defval = "Traditional", title = "Pivot Type", options=["Traditional", "Fibonacci", "Woodie", "Classic", "DM", "Camarilla"])
bool is_anchor = timeframe.change(my_timefr)
[open_ha, high_ha, low_ha, close_ha, pivots_ha] = request.security(ticker.heikinashi(my_symbol), timeframe.period, [open, high, low, close, ta.pivot_point_levels(is_type, is_anchor)])

color my_color = close_ha >= open_ha ? color.teal : color.red
plotcandle(open_ha, high_ha, low_ha, close_ha, "Average Pace", my_color, my_color, bordercolor = my_color, display = display.all)

type pivot_lines
    line p
    line r1
    line r2
    line r3
    line r4
    line r5
    line s1
    line s2
    line s3
    line s4
    line s5

var pivot_lines = array.new<pivot_lines>()
int pp_back = input.int(defval = 15, title = "Number of Pivots Back", minval = 1, maxval = 49)
if pivot_lines.size() > pp_back
    old_pivots = pivot_lines.pop()
    old_pivots.p.delete()
    old_pivots.r1.delete()
    old_pivots.r2.delete()
    old_pivots.r3.delete()
    old_pivots.r4.delete()
    old_pivots.r5.delete()
    old_pivots.s1.delete()
    old_pivots.s2.delete()
    old_pivots.s3.delete()
    old_pivots.s4.delete()
    old_pivots.s5.delete()

//import weak_hand/colors/17 as colors
//colors.theme color_auto = colors.find(syminfo.basecurrency)
color color_pp = input.color(#f7931a, "Color for Pivots")
//if input.bool(false, "Use Color Theme of the Token")
    //color_pp := color_auto.bg

if input.bool(false, "Show Central Pivots Back only") and pivot_lines.size() > 1
    for counter = pivot_lines.size() - 1 to 1
        pivot_lines old_pivots = pivot_lines.get(counter)
        old_pivots.r1.delete()
        old_pivots.r2.delete()
        old_pivots.r3.delete()
        old_pivots.r4.delete()
        old_pivots.r5.delete()
        old_pivots.s1.delete()
        old_pivots.s2.delete()
        old_pivots.s3.delete()
        old_pivots.s4.delete()
        old_pivots.s5.delete()

if is_anchor
    new_pivots     = pivot_lines.new()
    new_pivots.p  := line.new(x_start, pivots_ha.get(0), x_stop, pivots_ha.get(0), xloc.bar_time, extend.none, color_pp, line.style_solid, 2)
    new_pivots.r1 := line.new(x_start, pivots_ha.get(1), x_stop, pivots_ha.get(1), xloc.bar_time, extend.none, color_pp, line.style_dotted, 1)
    new_pivots.s1 := line.new(x_start, pivots_ha.get(2), x_stop, pivots_ha.get(2), xloc.bar_time, extend.none, color_pp, line.style_dotted, 1)
    new_pivots.r2 := line.new(x_start, pivots_ha.get(3), x_stop, pivots_ha.get(3), xloc.bar_time, extend.none, color_pp, line.style_dotted, 1)
    new_pivots.s2 := line.new(x_start, pivots_ha.get(4), x_stop, pivots_ha.get(4), xloc.bar_time, extend.none, color_pp, line.style_dotted, 1)
    new_pivots.r3 := line.new(x_start, pivots_ha.get(5), x_stop, pivots_ha.get(5), xloc.bar_time, extend.none, color_pp, line.style_dotted, 1)
    new_pivots.s3 := line.new(x_start, pivots_ha.get(6), x_stop, pivots_ha.get(6), xloc.bar_time, extend.none, color_pp, line.style_dotted, 1)
    new_pivots.r4 := line.new(x_start, pivots_ha.get(7), x_stop, pivots_ha.get(7), xloc.bar_time, extend.none, color_pp, line.style_dotted, 1)
    new_pivots.s4 := line.new(x_start, pivots_ha.get(8), x_stop, pivots_ha.get(8), xloc.bar_time, extend.none, color_pp, line.style_dotted, 1)
    new_pivots.r5 := line.new(x_start, pivots_ha.get(9), x_stop, pivots_ha.get(9), xloc.bar_time, extend.none, color_pp, line.style_dotted, 1)
    new_pivots.s5 := line.new(x_start, pivots_ha.get(10), x_stop, pivots_ha.get(10), xloc.bar_time, extend.none, color_pp, line.style_dotted, 1)
    pivot_lines.unshift(new_pivots.copy())

