True Bitcoin Value USD - Mario M
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//Author - Mario Moerman

//@version=4
study("True Bitcoin Value - Mario M" ,overlay=true)


// INPUTS

p_china = 0.04
p_og = 0.05
p_new = 0.0645
elec_pct = 1
plot_mp = true

// DATA

fees = security("QUANDL:BCHAIN/TRFUS","D",close)

elec_consumption() =>
    // Cambridge Bitcoin Electricity Consumption Index (CBECI) - Bitcoin's global electricity consumption in TwH.
    // NB: Uses MONTHLY averages of raw data from CBECI. TV script run-time is too slow with Daily/Weekly data here.
    //     This requires manual updating once a month for ongoing accuracy.
    
    d = dayofmonth
    m = month
    y = year
    
    e = float(na)
    e := d==1 and m==1 and y==2012? 0.06 :
     d==8 and m==1 and y==2012? 0.07 :
     d==15 and m==1 and y==2012? 0.07 :
     d==22 and m==1 and y==2012? 0.07 :
     d==29 and m==1 and y==2012? 0.07 :
     d==5 and m==2 and y==2012? 0.08 :
     d==12 and m==2 and y==2012? 0.07 :
     d==19 and m==2 and y==2012? 0.07 :
     d==26 and m==2 and y==2012? 0.08 :
     d==4 and m==3 and y==2012? 0.08 :
     d==11 and m==3 and y==2012? 0.08 :
     d==18 and m==3 and y==2012? 0.08 :
     d==25 and m==3 and y==2012? 0.09 :
     d==1 and m==4 and y==2012? 0.09 :
     d==8 and m==4 and y==2012? 0.08 :
     d==15 and m==4 and y==2012? 0.08 :
     d==22 and m==4 and y==2012? 0.08 :
     d==29 and m==4 and y==2012? 0.08 :
     d==6 and m==5 and y==2012? 0.09 :
     d==13 and m==5 and y==2012? 0.09 :
     d==20 and m==5 and y==2012? 0.09 :
     d==27 and m==5 and y==2012? 0.08 :
     d==3 and m==6 and y==2012? 0.08 :
     d==10 and m==6 and y==2012? 0.09 :
     d==17 and m==6 and y==2012? 0.09 :
     d==24 and m==6 and y==2012? 0.09 :
     d==1 and m==7 and y==2012? 0.09 :
     d==8 and m==7 and y==2012? 0.1 :
     d==15 and m==7 and y==2012? 0.1 :
     d==22 and m==7 and y==2012? 0.1 :
     d==29 and m==7 and y==2012? 0.11 :
     d==5 and m==8 and y==2012? 0.11 :
     d==12 and m==8 and y==2012? 0.12 :
     d==19 and m==8 and y==2012? 0.13 :
     d==26 and m==8 and y==2012? 0.13 :
     d==2 and m==9 and y==2012? 0.14 :
     d==9 and m==9 and y==2012? 0.15 :
     d==16 and m==9 and y==2012? 0.15 :
     d==23 and m==9 and y==2012? 0.16 :
     d==30 and m==9 and y==2012? 0.16 :
     d==7 and m==10 and y==2012? 0.15 :
     d==14 and m==10 and y==2012? 0.17 :
     d==21 and m==10 and y==2012? 0.17 :
     d==28 and m==10 and y==2012? 0.18 :
     d==4 and m==11 and y==2012? 0.18 :
     d==11 and m==11 and y==2012? 0.18 :
     d==18 and m==11 and y==2012? 0.17 :
     d==25 and m==11 and y==2012? 0.19 :
     d==2 and m==12 and y==2012? 0.2 :
     d==9 and m==12 and y==2012? 0.07 :
     d==16 and m==12 and y==2012? 0.05 :
     d==23 and m==12 and y==2012? 0.05 :
     d==30 and m==12 and y==2012? 0.05 :
     d==6 and m==1 and y==2013? 0.05 :
     d==13 and m==1 and y==2013? 0.05 :
     d==20 and m==1 and y==2013? 0.05 :
     d==27 and m==1 and y==2013? 0.05 :
     d==3 and m==2 and y==2013? 0.13 :
     d==10 and m==2 and y==2013? 0.18 :
     d==17 and m==2 and y==2013? 0.18 :
     d==24 and m==2 and y==2013? 0.18 :
     d==3 and m==3 and y==2013? 0.19 :
     d==10 and m==3 and y==2013? 0.21 :
     d==17 and m==3 and y==2013? 0.23 :
     d==24 and m==3 and y==2013? 0.3 :
     d==31 and m==3 and y==2013? 0.32 :
     d==7 and m==4 and y==2013? 0.35 :
     d==14 and m==4 and y==2013? 1.02 :
     d==21 and m==4 and y==2013? 0.78 :
     d==28 and m==4 and y==2013? 0.44 :
     d==5 and m==5 and y==2013? 0.45 :
     d==12 and m==5 and y==2013? 0.51 :
     d==19 and m==5 and y==2013? 0.51 :
     d==26 and m==5 and y==2013? 0.53 :
     d==2 and m==6 and y==2013? 0.65 :
     d==9 and m==6 and y==2013? 0.77 :
     d==16 and m==6 and y==2013? 0.85 :
     d==23 and m==6 and y==2013? 0.89 :
     d==30 and m==6 and y==2013? 0.35 :
     d==7 and m==7 and y==2013? 0.3 :
     d==14 and m==7 and y==2013? 0.34 :
     d==21 and m==7 and y==2013? 0.38 :
     d==28 and m==7 and y==2013? 0.4 :
     d==4 and m==8 and y==2013? 0.49 :
     d==11 and m==8 and y==2013? 0.61 :
     d==18 and m==8 and y==2013? 0.71 :
     d==25 and m==8 and y==2013? 0.82 :
     d==1 and m==9 and y==2013? 1.04 :
     d==8 and m==9 and y==2013? 1.15 :
     d==15 and m==9 and y==2013? 1.49 :
     d==22 and m==9 and y==2013? 0.91 :
     d==29 and m==9 and y==2013? 0.51 :
     d==6 and m==10 and y==2013? 0.58 :
     d==13 and m==10 and y==2013? 0.72 :
     d==20 and m==10 and y==2013? 1.03 :
     d==27 and m==10 and y==2013? 1.29 :
     d==3 and m==11 and y==2013? 1.43 :
     d==10 and m==11 and y==2013? 1.19 :
     d==17 and m==11 and y==2013? 1.47 :
     d==24 and m==11 and y==2013? 1.55 :
     d==1 and m==12 and y==2013? 3.64 :
     d==8 and m==12 and y==2013? 8.61 :
     d==15 and m==12 and y==2013? 5.42 :
     d==22 and m==12 and y==2013? 2.86 :
     d==29 and m==12 and y==2013? 3.11 :
     d==5 and m==1 and y==2014? 3.46 :
     d==12 and m==1 and y==2014? 4.31 :
     d==19 and m==1 and y==2014? 4.74 :
     d==26 and m==1 and y==2014? 5.27 :
     d==2 and m==2 and y==2014? 5.93 :
     d==9 and m==2 and y==2014? 6.48 :
     d==16 and m==2 and y==2014? 7.29 :
     d==23 and m==2 and y==2014? 8.32 :
     d==2 and m==3 and y==2014? 8.84 :
     d==9 and m==3 and y==2014? 6 :
     d==16 and m==3 and y==2014? 5.74 :
     d==23 and m==3 and y==2014? 6.47 :
     d==30 and m==3 and y==2014? 7.15 :
     d==6 and m==4 and y==2014? 6.71 :
     d==13 and m==4 and y==2014? 2.79 :
     d==20 and m==4 and y==2014? 3.19 :
     d==27 and m==4 and y==2014? 3.31 :
     d==4 and m==5 and y==2014? 3.43 :
     d==11 and m==5 and y==2014? 3.74 :
     d==18 and m==5 and y==2014? 4.21 :
     d==25 and m==5 and y==2014? 4.39 :
     d==1 and m==6 and y==2014? 4.66 :
     d==8 and m==6 and y==2014? 5.08 :
     d==15 and m==6 and y==2014? 5.62 :
     d==22 and m==6 and y==2014? 6.59 :
     d==29 and m==6 and y==2014? 7.04 :
     d==6 and m==7 and y==2014? 5.4 :
     d==13 and m==7 and y==2014? 5.32 :
     d==20 and m==7 and y==2014? 5.59 :
     d==27 and m==7 and y==2014? 5.32 :
     d==3 and m==8 and y==2014? 5.96 :
     d==10 and m==8 and y==2014? 6.13 :
     d==17 and m==8 and y==2014? 6.71 :
     d==24 and m==8 and y==2014? 7.75 :
     d==31 and m==8 and y==2014? 3.33 :
     d==7 and m==9 and y==2014? 2.83 :
     d==14 and m==9 and y==2014? 2.93 :
     d==21 and m==9 and y==2014? 3.4 :
     d==28 and m==9 and y==2014? 3.14 :
     d==5 and m==10 and y==2014? 3.45 :
     d==12 and m==10 and y==2014? 3.41 :
     d==19 and m==10 and y==2014? 3.4 :
     d==26 and m==10 and y==2014? 3.49 :
     d==2 and m==11 and y==2014? 3.83 :
     d==9 and m==11 and y==2014? 3.92 :
     d==16 and m==11 and y==2014? 3.8 :
     d==23 and m==11 and y==2014? 3.8 :
     d==30 and m==11 and y==2014? 3.9 :
     d==7 and m==12 and y==2014? 2.99 :
     d==14 and m==12 and y==2014? 3.06 :
     d==21 and m==12 and y==2014? 2.8 :
     d==28 and m==12 and y==2014? 3.24 :
     d==4 and m==1 and y==2015? 3.43 :
     d==11 and m==1 and y==2015? 3.17 :
     d==18 and m==1 and y==2015? 3.24 :
     d==25 and m==1 and y==2015? 3.03 :
     d==1 and m==2 and y==2015? 3.32 :
     d==8 and m==2 and y==2015? 3.31 :
     d==15 and m==2 and y==2015? 3.39 :
     d==22 and m==2 and y==2015? 3.62 :
     d==1 and m==3 and y==2015? 3.54 :
     d==8 and m==3 and y==2015? 3.61 :
     d==15 and m==3 and y==2015? 3.55 :
     d==22 and m==3 and y==2015? 3.5 :
     d==29 and m==3 and y==2015? 3.84 :
     d==5 and m==4 and y==2015? 3.65 :
     d==12 and m==4 and y==2015? 3.54 :
     d==19 and m==4 and y==2015? 3.4 :
     d==26 and m==4 and y==2015? 2.12 :
     d==3 and m==5 and y==2015? 2.9 :
     d==10 and m==5 and y==2015? 3.53 :
     d==17 and m==5 and y==2015? 3.83 :
     d==24 and m==5 and y==2015? 3.54 :
     d==31 and m==5 and y==2015? 3.62 :
     d==7 and m==6 and y==2015? 3.67 :
     d==14 and m==6 and y==2015? 3.79 :
     d==21 and m==6 and y==2015? 3.65 :
     d==28 and m==6 and y==2015? 3.81 :
     d==5 and m==7 and y==2015? 3.8 :
     d==12 and m==7 and y==2015? 3.88 :
     d==19 and m==7 and y==2015? 3.92 :
     d==26 and m==7 and y==2015? 4.01 :
     d==2 and m==8 and y==2015? 4.02 :
     d==9 and m==8 and y==2015? 3.95 :
     d==16 and m==8 and y==2015? 4.02 :
     d==23 and m==8 and y==2015? 3.95 :
     d==30 and m==8 and y==2015? 2.5 :
     d==6 and m==9 and y==2015? 2.22 :
     d==13 and m==9 and y==2015? 2.12 :
     d==20 and m==9 and y==2015? 2.05 :
     d==27 and m==9 and y==2015? 2.2 :
     d==4 and m==10 and y==2015? 2.23 :
     d==11 and m==10 and y==2015? 2.15 :
     d==18 and m==10 and y==2015? 2.2 :
     d==25 and m==10 and y==2015? 2.23 :
     d==1 and m==11 and y==2015? 2.58 :
     d==8 and m==11 and y==2015? 3.95 :
     d==15 and m==11 and y==2015? 4.27 :
     d==22 and m==11 and y==2015? 4.5 :
     d==29 and m==11 and y==2015? 4.7 :
     d==6 and m==12 and y==2015? 4.95 :
     d==13 and m==12 and y==2015? 5.62 :
     d==20 and m==12 and y==2015? 6.01 :
     d==27 and m==12 and y==2015? 6.43 :
     d==3 and m==1 and y==2016? 4.76 :
     d==10 and m==1 and y==2016? 4.13 :
     d==17 and m==1 and y==2016? 4.03 :
     d==24 and m==1 and y==2016? 4.5 :
     d==31 and m==1 and y==2016? 4.87 :
     d==7 and m==2 and y==2016? 5.45 :
     d==14 and m==2 and y==2016? 5.98 :
     d==21 and m==2 and y==2016? 5.53 :
     d==28 and m==2 and y==2016? 5.54 :
     d==6 and m==3 and y==2016? 5.84 :
     d==13 and m==3 and y==2016? 5.87 :
     d==20 and m==3 and y==2016? 5.83 :
     d==27 and m==3 and y==2016? 5.99 :
     d==3 and m==4 and y==2016? 6.06 :
     d==10 and m==4 and y==2016? 6.47 :
     d==17 and m==4 and y==2016? 6.51 :
     d==24 and m==4 and y==2016? 6.44 :
     d==1 and m==5 and y==2016? 6.33 :
     d==8 and m==5 and y==2016? 7.02 :
     d==15 and m==5 and y==2016? 6.92 :
     d==22 and m==5 and y==2016? 7.07 :
     d==29 and m==5 and y==2016? 7.13 :
     d==5 and m==6 and y==2016? 5.88 :
     d==12 and m==6 and y==2016? 5.48 :
     d==19 and m==6 and y==2016? 5.52 :
     d==26 and m==6 and y==2016? 5.5 :
     d==3 and m==7 and y==2016? 5.74 :
     d==10 and m==7 and y==2016? 5.68 :
     d==17 and m==7 and y==2016? 5.55 :
     d==24 and m==7 and y==2016? 4.53 :
     d==31 and m==7 and y==2016? 3.93 :
     d==7 and m==8 and y==2016? 4.43 :
     d==14 and m==8 and y==2016? 4.13 :
     d==21 and m==8 and y==2016? 4.33 :
     d==28 and m==8 and y==2016? 4.31 :
     d==4 and m==9 and y==2016? 4.5 :
     d==11 and m==9 and y==2016? 4.45 :
     d==18 and m==9 and y==2016? 4.78 :
     d==25 and m==9 and y==2016? 4.79 :
     d==2 and m==10 and y==2016? 4.95 :
     d==9 and m==10 and y==2016? 5.31 :
     d==16 and m==10 and y==2016? 4.76 :
     d==23 and m==10 and y==2016? 5.01 :
     d==30 and m==10 and y==2016? 5.2 :
     d==6 and m==11 and y==2016? 4.93 :
     d==13 and m==11 and y==2016? 5.48 :
     d==20 and m==11 and y==2016? 5.55 :
     d==27 and m==11 and y==2016? 5.67 :
     d==4 and m==12 and y==2016? 5.9 :
     d==11 and m==12 and y==2016? 6.09 :
     d==18 and m==12 and y==2016? 5.95 :
     d==25 and m==12 and y==2016? 6.42 :
     d==1 and m==1 and y==2017? 6.59 :
     d==8 and m==1 and y==2017? 6.71 :
     d==15 and m==1 and y==2017? 7.2 :
     d==22 and m==1 and y==2017? 7.87 :
     d==29 and m==1 and y==2017? 8.28 :
     d==5 and m==2 and y==2017? 7.41 :
     d==12 and m==2 and y==2017? 6.54 :
     d==19 and m==2 and y==2017? 6.87 :
     d==26 and m==2 and y==2017? 7.27 :
     d==5 and m==3 and y==2017? 7 :
     d==12 and m==3 and y==2017? 7.34 :
     d==19 and m==3 and y==2017? 7.62 :
     d==26 and m==3 and y==2017? 7.69 :
     d==2 and m==4 and y==2017? 6.34 :
     d==9 and m==4 and y==2017? 6.48 :
     d==16 and m==4 and y==2017? 7.21 :
     d==23 and m==4 and y==2017? 7.43 :
     d==30 and m==4 and y==2017? 7.46 :
     d==7 and m==5 and y==2017? 7.75 :
     d==14 and m==5 and y==2017? 8.31 :
     d==21 and m==5 and y==2017? 9.83 :
     d==28 and m==5 and y==2017? 11.95 :
     d==4 and m==6 and y==2017? 12.68 :
     d==11 and m==6 and y==2017? 13.34 :
     d==18 and m==6 and y==2017? 12.64 :
     d==25 and m==6 and y==2017? 12.86 :
     d==2 and m==7 and y==2017? 13.48 :
     d==9 and m==7 and y==2017? 12.84 :
     d==16 and m==7 and y==2017? 14.27 :
     d==23 and m==7 and y==2017? 11.68 :
     d==30 and m==7 and y==2017? 11.77 :
     d==6 and m==8 and y==2017? 11.85 :
     d==13 and m==8 and y==2017? 13.86 :
     d==20 and m==8 and y==2017? 15.32 :
     d==27 and m==8 and y==2017? 12.12 :
     d==3 and m==9 and y==2017? 15.67 :
     d==10 and m==9 and y==2017? 16.76 :
     d==17 and m==9 and y==2017? 17.84 :
     d==24 and m==9 and y==2017? 17.41 :
     d==1 and m==10 and y==2017? 17.47 :
     d==8 and m==10 and y==2017? 18.95 :
     d==15 and m==10 and y==2017? 19.03 :
     d==22 and m==10 and y==2017? 21.85 :
     d==29 and m==10 and y==2017? 20.83 :
     d==5 and m==11 and y==2017? 21.5 :
     d==12 and m==11 and y==2017? 18.33 :
     d==19 and m==11 and y==2017? 20.88 :
     d==26 and m==11 and y==2017? 22.33 :
     d==3 and m==12 and y==2017? 23.62 :
     d==10 and m==12 and y==2017? 30.97 :
     d==17 and m==12 and y==2017? 45.74 :
     d==24 and m==12 and y==2017? 44.99 :
     d==31 and m==12 and y==2017? 46.55 :
     d==7 and m==1 and y==2018? 47.98 :
     d==14 and m==1 and y==2018? 51.15 :
     d==21 and m==1 and y==2018? 42.57 :
     d==28 and m==1 and y==2018? 39.36 :
     d==4 and m==2 and y==2018? 39.93 :
     d==11 and m==2 and y==2018? 38.28 :
     d==18 and m==2 and y==2018? 35.6 :
     d==25 and m==2 and y==2018? 36.18 :
     d==4 and m==3 and y==2018? 37.7 :
     d==11 and m==3 and y==2018? 40.35 :
     d==18 and m==3 and y==2018? 40.6 :
     d==25 and m==3 and y==2018? 40.06 :
     d==1 and m==4 and y==2018? 39.42 :
     d==8 and m==4 and y==2018? 38.2 :
     d==15 and m==4 and y==2018? 38.79 :
     d==22 and m==4 and y==2018? 41.62 :
     d==29 and m==4 and y==2018? 38.76 :
     d==6 and m==5 and y==2018? 39.9 :
     d==13 and m==5 and y==2018? 39.9 :
     d==20 and m==5 and y==2018? 40.28 :
     d==27 and m==5 and y==2018? 44.27 :
     d==3 and m==6 and y==2018? 45.3 :
     d==10 and m==6 and y==2018? 47.4 :
     d==17 and m==6 and y==2018? 49.05 :
     d==24 and m==6 and y==2018? 53.42 :
     d==1 and m==7 and y==2018? 47.16 :
     d==8 and m==7 and y==2018? 47.61 :
     d==15 and m==7 and y==2018? 45.23 :
     d==22 and m==7 and y==2018? 50.59 :
     d==29 and m==7 and y==2018? 56.59 :
     d==5 and m==8 and y==2018? 59.65 :
     d==12 and m==8 and y==2018? 56.41 :
     d==19 and m==8 and y==2018? 52.43 :
     d==26 and m==8 and y==2018? 53.33 :
     d==2 and m==9 and y==2018? 55.81 :
     d==9 and m==9 and y==2018? 51.16 :
     d==16 and m==9 and y==2018? 51.66 :
     d==23 and m==9 and y==2018? 53.82 :
     d==30 and m==9 and y==2018? 56.06 :
     d==7 and m==10 and y==2018? 52.93 :
     d==14 and m==10 and y==2018? 52.24 :
     d==21 and m==10 and y==2018? 53.6 :
     d==28 and m==10 and y==2018? 51.55 :
     d==4 and m==11 and y==2018? 54.74 :
     d==11 and m==11 and y==2018? 48.23 :
     d==18 and m==11 and y==2018? 46.6 :
     d==25 and m==11 and y==2018? 41.38 :
     d==2 and m==12 and y==2018? 35.5 :
     d==9 and m==12 and y==2018? 32.97 :
     d==16 and m==12 and y==2018? 33.47 :
     d==23 and m==12 and y==2018? 36.59 :
     d==30 and m==12 and y==2018? 38.17 :
     d==6 and m==1 and y==2019? 39.55 :
     d==13 and m==1 and y==2019? 39.38 :
     d==20 and m==1 and y==2019? 37.81 :
     d==27 and m==1 and y==2019? 38.83 :
     d==3 and m==2 and y==2019? 38.73 :
     d==10 and m==2 and y==2019? 39.82 :
     d==17 and m==2 and y==2019? 39.16 :
     d==24 and m==2 and y==2019? 39.57 :
     d==3 and m==3 and y==2019? 40.94 :
     d==10 and m==3 and y==2019? 38.61 :
     d==17 and m==3 and y==2019? 41 :
     d==24 and m==3 and y==2019? 44.54 :
     d==31 and m==3 and y==2019? 42.55 :
     d==7 and m==4 and y==2019? 39.52 :
     d==14 and m==4 and y==2019? 40.48 :
     d==21 and m==4 and y==2019? 43.06 :
     d==28 and m==4 and y==2019? 42.21 :
     d==5 and m==5 and y==2019? 44.98 :
     d==12 and m==5 and y==2019? 43.23 :
     d==19 and m==5 and y==2019? 47.07 :
     d==26 and m==5 and y==2019? 50.84 :
     d==2 and m==6 and y==2019? 56.13 :
     d==9 and m==6 and y==2019? 54.99 :
     d==16 and m==6 and y==2019? 48.99 :
     d==23 and m==6 and y==2019? 53.38 :
     d==30 and m==6 and y==2019? 64.61 :
     d==7 and m==7 and y==2019? 68.53 :
     d==14 and m==7 and y==2019? 68.12 :
     d==21 and m==7 and y==2019? 66.78 :
     d==28 and m==7 and y==2019? 63.38 :
     d==4 and m==8 and y==2019? 64.43 :
     d==11 and m==8 and y==2019? 65.27 :
     d==18 and m==8 and y==2019? 62.25 :
     d==25 and m==8 and y==2019? 67.32 :
     d==1 and m==9 and y==2019? 67.53 :
     d==8 and m==9 and y==2019? 72.54 :
     d==15 and m==9 and y==2019? 76.34 :
     d==22 and m==9 and y==2019? 81.31 :
     d==29 and m==9 and y==2019? 76.85 :
     d==6 and m==10 and y==2019? 74.32 :
     d==13 and m==10 and y==2019? 76.37 :
     d==20 and m==10 and y==2019? 74.58 :
     d==27 and m==10 and y==2019? 75.7 :
     d==3 and m==11 and y==2019? 71.07 :
     d==10 and m==11 and y==2019? 71.83 :
     d==17 and m==11 and y==2019? 73.81 :
     d==24 and m==11 and y==2019? 70.91 :
     d==1 and m==12 and y==2019? 65.97 :
     d==8 and m==12 and y==2019? 69.74 :
     d==15 and m==12 and y==2019? 64.55 :
     d==22 and m==12 and y==2019? 71.42 :
     d==29 and m==12 and y==2019? 66.49 :
     d==5 and m==1 and y==2020? 76.84 :
     d==12 and m==1 and y==2020? 75.13 :
     d==19 and m==1 and y==2020? 76.51 :
     d==26 and m==1 and y==2020? 80.9 :
     d==2 and m==2 and y==2020? 79.81 :
     d==9 and m==2 and y==2020? 81.15 :
     d==16 and m==2 and y==2020? 79.68 :
     d==23 and m==2 and y==2020? 85.03 :
     d==1 and m==3 and y==2020? 85.22 :
     d==8 and m==3 and y==2020? 87.07 :
     d==15 and m==3 and y==2020? 77.14 :
     d==22 and m==3 and y==2020? 53.97 :
     d==29 and m==3 and y==2020? 52.07 :
     d==5 and m==4 and y==2020? 60.1 :
     d==12 and m==4 and y==2020? 75.36 :
     d==19 and m==4 and y==2020? 78.98 :
     d==26 and m==4 and y==2020? 76.09 :
     d==3 and m==5 and y==2020? 80.15 :
     d==10 and m==5 and y==2020? 81.8 :
     d==17 and m==5 and y==2020? 68.6 :
     d==24 and m==5 and y==2020? 48.84 :
     d==31 and m==5 and y==2020? 48.76 :
     d==7 and m==6 and y==2020? 52.29 :
     d==14 and m==6 and y==2020? 54.34 :
     d==21 and m==6 and y==2020? 50.34 :
     d==28 and m==6 and y==2020? 54.59 :
     d==5 and m==7 and y==2020? 55.46 :
     d==12 and m==7 and y==2020? 54.6 :
     d==19 and m==7 and y==2020? 50.91 :
     d==26 and m==7 and y==2020? 54.67 :
     d==2 and m==8 and y==2020? 56.71 :
     d==9 and m==8 and y==2020? 60.28 :
     d==16 and m==8 and y==2020? 63.45 :
     d==23 and m==8 and y==2020? 59.15 :
     d==30 and m==8 and y==2020? 59.78 :
     d==6 and m==9 and y==2020? 60.26 :
     d==13 and m==9 and y==2020? 64.41 :
     d==20 and m==9 and y==2020? 66.06 :
     d==27 and m==9 and y==2020? 66.29 :
     d==4 and m==10 and y==2020? 60.36 :
     d==11 and m==10 and y==2020? 60.05 :
     d==18 and m==10 and y==2020? 66.09 :
     d==25 and m==10 and y==2020? 61.11 :
     d==1 and m==11 and y==2020? 51.14 :
     d==8 and m==11 and y==2020? 65.24 :
     d==15 and m==11 and y==2020? 80.76 :
     d==22 and m==11 and y==2020? 89.54 :
     d==29 and m==11 and y==2020? 83.36 :
     d==6 and m==12 and y==2020? 83.85 :
     d==13 and m==12 and y==2020? 84.67 :
     d==20 and m==12 and y==2020? 85.38 :
     d==27 and m==12 and y==2020? 86.5 :
     d==3 and m==1 and y==2021? 99.96 :
     d==10 and m==1 and y==2021? 109.01 :
     d==17 and m==1 and y==2021? 109.34 :
     d==24 and m==1 and y==2021? 105.8 :
     d==31 and m==1 and y==2021? 108.78 :
     d==7 and m==2 and y==2021? 113.68 :
     d==14 and m==2 and y==2021? 111.34 :
     d==21 and m==2 and y==2021? 114.37 :
     d==28 and m==2 and y==2021? 120.55 :
     d==7 and m==3 and y==2021? 119.87 :
     d==14 and m==3 and y==2021? 121.87 :
     d==21 and m==3 and y==2021? 124.68 :
     d==28 and m==3 and y==2021? 129.81 :
     d==4 and m==4 and y==2021? 125.94 :
     d==11 and m==4 and y==2021? 133.9 :
     d==18 and m==4 and y==2021? 117.6 :
     d==25 and m==4 and y==2021? 116.23 :
     d==2 and m==5 and y==2021? 125.59 :
     d==9 and m==5 and y==2021? 138.55 :
     d==16 and m==5 and y==2021? 134.59 :
     d==23 and m==5 and y==2021? 106.06 :
     d==30 and m==5 and y==2021? 107.26 :
     d==6 and m==6 and y==2021? 105.36 :
     d==13 and m==6 and y==2021? 92.83 :
     d==20 and m==6 and y==2021? 83.74 :
     d==27 and m==6 and y==2021? 64.85 :
     d==4 and m==7 and y==2021? 61.9 :
     d==11 and m==7 and y==2021? 66.83 :
     d==18 and m==7 and y==2021? 69.41 :
     d==25 and m==7 and y==2021? 68.89 :
     d==1 and m==8 and y==2021? 77.92 :
     d==8 and m==8 and y==2021? 79.39 :
     d==15 and m==8 and y==2021? 82.65 :
     d==22 and m==8 and y==2021? 87.07 :
     d==29 and m==8 and y==2021? 92.65 :
     d==5 and m==9 and y==2021? 91.17 :
     d==12 and m==9 and y==2021? 94.3 :
     d==19 and m==9 and y==2021? 98.54 :
     d==26 and m==9 and y==2021? 96.21 :
     d==3 and m==10 and y==2021? 98.84 :
     d==10 and m==10 and y==2021? 99.21 :
     d==17 and m==10 and y==2021? 101.76 :
     d==24 and m==10 and y==2021? 106.24 :
     d==31 and m==10 and y==2021? 111.68 :
     d==7 and m==11 and y==2021? 116.45 :
     d==14 and m==11 and y==2021? 114.26 :
     d==21 and m==11 and y==2021? 114.62 :
     d==28 and m==11 and y==2021? 110.6 :
     d==5 and m==12 and y==2021? 118.51 :
     d==12 and m==12 and y==2021? 121.88 :
     d==19 and m==12 and y==2021? 112.55 :
     d==26 and m==12 and y==2021? 120.29 :
     d==2 and m==1 and y==2022? 119.19 :
     d==9 and m==1 and y==2022? 117.33 :
     d==16 and m==1 and y==2022? 130.54 :
     d==23 and m==1 and y==2022? 126.67 :
     d==30 and m==1 and y==2022? 125.4 :
     d==6 and m==2 and y==2022? 116.02 :
     d==13 and m==2 and y==2022? 126.43 :
     d==20 and m==2 and y==2022? 137.13 :
     d==27 and m==2 and y==2022? 117.54 :
     d==6 and m==3 and y==2022? 119.82 :
     d==13 and m==3 and y==2022? 122.52 :
     d==20 and m==3 and y==2022? 126.38 :
     d==27 and m==3 and y==2022? 125.28 :
     d==3 and m==4 and y==2022? 128.57 :
     d==10 and m==4 and y==2022? 131.8 :
     d==17 and m==4 and y==2022? 125.56 :
     d==24 and m==4 and y==2022? 126.89 :
     d==1 and m==5 and y==2022? 132.89 :
     d==8 and m==5 and y==2022? 132.1 :
     d==15 and m==5 and y==2022? 125.38 :
     d==22 and m==5 and y==2022? 119.22 :
     d==29 and m==5 and y==2022? 120.04 :
     d==5 and m==6 and y==2022? 117.89 :
     d==12 and m==6 and y==2022? 128.79 :
     d==19 and m==6 and y==2022? 105.19 :
     d==26 and m==6 and y==2022? 92.26 :
     d==3 and m==7 and y==2022? 91.91 :
     d==10 and m==7 and y==2022? 84.87 :
     d==17 and m==7 and y==2022? 80.07 :
     d==24 and m==7 and y==2022? 83.87 :
     d==31 and m==7 and y==2022? 88.81 :
     d==7 and m==8 and y==2022? 91.01 :
     d==14 and m==8 and y==2022? 88.5 :
     d==21 and m==8 and y==2022? 92.17 :
     d==28 and m==8 and y==2022? 97.3 :
     d==4 and m==9 and y==2022? 94.8 :
     d==11 and m==9 and y==2022? 96.54 :
     d==18 and m==9 and y==2022? 95.34 :
     d==25 and m==9 and y==2022? 92.67 :
     d==2 and m==10 and y==2022? 96.61 :
     d==9 and m==10 and y==2022? 105.96 :
     d==16 and m==10 and y==2022? 106.5 :
     d==23 and m==10 and y==2022? 100.36 :
     d==30 and m==10 and y==2022? 97.93 :
     d==6 and m==11 and y==2022? 101.41 :
     d==13 and m==11 and y==2022? 102.5 :
     d==20 and m==11 and y==2022? 94.18 :
     d==27 and m==11 and y==2022? 80.79 :
     d==4 and m==12 and y==2022? 86.58 :
     d==11 and m==12 and y==2022? 92.49 :
     d==18 and m==12 and y==2022? 88.68 :
     d==25 and m==12 and y==2022? 83.68 :
     d==1 and m==1 and y==2023? 91.37 :
     d==8 and m==1 and y==2023? 94.75 :
     d==15 and m==1 and y==2023? 98.16 :
     d==22 and m==1 and y==2023? 102.84 :
     d==29 and m==1 and y==2023? 115.62 :
         
     na
    e

btc_mined_per_day() =>
    blocks_per_day = 144
    block_reward = time < timestamp(2012,11,28,0,0) ? 50 :
         time < timestamp(2016,7,9,0,0) ? 25 :
         time < timestamp(2020,5,11,19,0) ? 12.5 :
         time < timestamp(2024,5,12,0,0) ? 6.25 : 3.125 // 2024 estimate
    btc_per_day = blocks_per_day*block_reward
    btc_per_day    

// CALCULATIONS


miner_price = close + fees/btc_mined_per_day()

elec = float(na)
elec := na(elec_consumption()) ? elec[1] : elec_consumption()

p =  time < timestamp(2019,6,30,0,0) ? p_og : 
     time < timestamp(2021,4,20,0,0) ? p_china : p_new 
    
btc_elec_cost = float(na)
btc_elec_cost := (elec/365.25)*pow(10,9) / btc_mined_per_day() * p //convert: --> days --> KwH --> Per BTC --> * Price/KwH

total_cost = btc_elec_cost/elec_pct   

osc_e = (miner_price/btc_elec_cost - 1.0)*100.0
osc_t = (miner_price/total_cost - 1.0)*100.0

// PLOT


c = close
clr = total_cost > c ? color.green : color.red
tc = plot(total_cost,color=clr,linewidth=2,title="TBV")
cp = plot(c,color=color.rgb(0, 0, 0, 100),linewidth=1,title="Close")

fill(tc,cp,color=clr,transp=70,title="TBV Fill")
