BTC Indicator By Megalodon Trading
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MegalodonTrading

//@version=4
study("BTC Indicator By Megalodon Trading", max_bars_back=366, overlay=false)

buy_below  = input(defval = -61.8 , title = "Buy Below Initial Trigger", minval = -100, maxval = 100)
sell_above  = input(defval = -38.2 , title = "Sell Above Initial Trigger", minval = -100, maxval = 100)

calculate_fib()=>
    fib_price_close = nz(close)
    fib_gradual_increase_sum = 0.0
    fib_average_count = 0
    fib_price_high = nz(high)
    fib_price_low = nz(low)
    fib_counter_for_reduction_of_total_value1=0.0
    return_fib = 0.0
    fib_value_color_increase_sum = 0.0
    fib_gradual_increase_average_total = 0.0
    for i=365 to 7
    //Going in term of every 7 days starting at 30 days look back to 
        fib_lookback_days = (i-1) 
        if(fib_lookback_days<bar_index)
            fib_average_count := fib_average_count + 1
            fib_maximum =  highest(fib_price_high, fib_lookback_days)
            fib_minumum = lowest(fib_price_low,fib_lookback_days)
            max_min_difference = fib_maximum - fib_minumum
            fib_price_of_sell_618 = (fib_maximum - (max_min_difference * 0.382))
            fib_price_of_buy_618 = (fib_maximum - (max_min_difference * 0.618))
            fib_value = fib_price_close >= ( fib_maximum - (0.382) * max_min_difference) ? 1 : fib_price_close <= ( fib_maximum - (0.618/100) * max_min_difference) ? -1 : 0 //+ if in the sell zone -if in the buy zone
            fib_gradual_increase_average = fib_value == 1 ?(((((fib_price_close - fib_price_of_sell_618 ) / max_min_difference * 100 ) * 2617.801) + 100000) / 2)  : fib_value == -1 ? (((((fib_price_of_buy_618 - fib_price_close) / max_min_difference * 100) * -2617.801) + 100000) / 2) : 50000
            fib_gradual_increase_sum := fib_gradual_increase_sum + fib_gradual_increase_average
    fib_average_total = fib_gradual_increase_sum / (fib_average_count* 1000)
    fib_new = (100 - fib_average_total) * -1

total_daily = security(syminfo.tickerid,"", calculate_fib())


color_new = total_daily <= buy_below ? color.new(color.green,78) : total_daily >= sell_above ? color.new(color.red,78) :  color.new(color.gray,100)

plot(total_daily)
bgcolor(color_new)


