BlackMEX - Production Cost
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © capriole_charles

//@version=4
study("BlackMEX",overlay=true)

// NOTES

// Bitcoin's Value as determined by Joules of energy input only
// Calculations per Medium article EV = (Energy-in) / (Supply Growth Rate) * (Fiat Factor)
// Historic Energy Efficiency data can only be entered monthly due to processing speed constraints of below data load and should be considred an estimate only.
// Energy Efficiency Data requires manual updating. Currently accurate as of 28 December 2019

// INPUTS

fiat_factor = input(2.0,"Fiat Factor - A Constant Representing the Fiat USD value of a Joule (mult by 10^-15).")
lost = input(defval=1000000, title="Lost Bitcoins", type=input.integer)
raw = input(true, "Plot Energy Value Raw")
sma = input(true, "Plot Energy Value SMA")
len = input(20,"Energy Value SMA length (days)")
mv=input(false,"Plot MA bands")

// DATA & FUNCTIONS

energy_efficiency() =>

    // Average Bitcoin Mining Hardware Energy Efficiency (J/GH)
    // Uses Bitcoin Mining Hardware Manufacturer Spec Energy Efficiency (J/GH) 
    // Data Source: Bitcoin Wiki and CBECI
    
    d = dayofmonth
    m = month
    y = year
    
    e = float(na)
    
    e := d==15 and m==1 and y==2011? 7946.83220938736 :
     d==15 and m==2 and y==2011? 7919.41839243041 :
     d==15 and m==3 and y==2011? 7632.98344069555 :
     d==15 and m==4 and y==2011? 7481.77369210883 :
     d==15 and m==5 and y==2011? 7235.83629484419 :
     d==15 and m==6 and y==2011? 7004.3958189483 :
     d==15 and m==7 and y==2011? 7424.80653864182 :
     d==15 and m==8 and y==2011? 7424.80653864182 :
     d==15 and m==9 and y==2011? 6376.1280731424 :
     d==15 and m==10 and y==2011? 5194.95515659859 :
     d==15 and m==11 and y==2011? 4638.22732855883 :
     d==15 and m==12 and y==2011? 4726.69553622754 :
     d==15 and m==1 and y==2012? 4804.10521793766 :
     d==15 and m==2 and y==2012? 4016.5564250286 :
     d==15 and m==3 and y==2012? 3158.62595648445 :
     d==15 and m==4 and y==2012? 2803.23990550415 :
     d==15 and m==5 and y==2012? 2730.11445159239 :
     d==15 and m==6 and y==2012? 2725.35546233546 :
     d==15 and m==7 and y==2012? 2725.35546233546 :
     d==15 and m==8 and y==2012? 2725.35546233546 :
     d==15 and m==9 and y==2012? 2725.35546233546 :
     d==15 and m==10 and y==2012? 2725.35546233546 :
     d==15 and m==11 and y==2012? 2657.33647784477 :
     d==15 and m==12 and y==2012? 2693.23055902333 :
     d==15 and m==1 and y==2013? 2567.31168809632 :
     d==15 and m==2 and y==2013? 2010.50635911607 :
     d==15 and m==3 and y==2013? 1457.30785659375 :
     d==15 and m==4 and y==2013? 1189.03510277857 :
     d==15 and m==5 and y==2013? 1119.03463573955 :
     d==15 and m==6 and y==2013? 930.746036826081 :
     d==15 and m==7 and y==2013? 48.3605281757209 :
     d==15 and m==8 and y==2013? 45.4898976813005 :
     d==15 and m==9 and y==2013? 43.1908956549566 :
     d==15 and m==10 and y==2013? 39.9239675902243 :
     d==15 and m==11 and y==2013? 35.9404616591799 :
     d==15 and m==12 and y==2013? 32.5475862305335 :
     d==15 and m==1 and y==2014? 29.1884018211459 :
     d==15 and m==2 and y==2014? 26.0481147720191 :
     d==15 and m==3 and y==2014? 16.3168605075398 :
     d==15 and m==4 and y==2014? 8.89876391490287 :
     d==15 and m==5 and y==2014? 8.78751371891833 :
     d==15 and m==6 and y==2014? 8.60464060736008 :
     d==15 and m==7 and y==2014? 7.81762586226057 :
     d==15 and m==8 and y==2014? 7.0815417915592 :
     d==15 and m==9 and y==2014? 6.7522551823933 :
     d==15 and m==10 and y==2014? 6.27172203804964 :
     d==15 and m==11 and y==2014? 5.15937443536947 :
     d==15 and m==12 and y==2014? 4.52574122013518 :
     d==15 and m==1 and y==2015? 2.85085483477115 :
     d==15 and m==2 and y==2015? 1.60069849097814 :
     d==15 and m==3 and y==2015? 1.53698379742013 :
     d==15 and m==4 and y==2015? 1.4800675401877 :
     d==15 and m==5 and y==2015? 1.47013611727757 :
     d==15 and m==6 and y==2015? 1.45622339117475 :
     d==15 and m==7 and y==2015? 1.32267865317551 :
     d==15 and m==8 and y==2015? 1.13655134648048 :
     d==15 and m==9 and y==2015? 0.997126082644854 :
     d==15 and m==10 and y==2015? 0.888779078658476 :
     d==15 and m==11 and y==2015? 0.887188142202356 :
     d==15 and m==12 and y==2015? 0.878722281644594 :
     d==15 and m==1 and y==2016? 0.855124269986229 :
     d==15 and m==2 and y==2016? 0.847352818483432 :
     d==15 and m==3 and y==2016? 0.833440627395937 :
     d==15 and m==4 and y==2016? 0.785069524089441 :
     d==15 and m==5 and y==2016? 0.747064212787211 :
     d==15 and m==6 and y==2016? 0.678588928027975 :
     d==15 and m==7 and y==2016? 0.445662312187738 :
     d==15 and m==8 and y==2016? 0.26875945705385 :
     d==15 and m==9 and y==2016? 0.26875945705385 :
     d==15 and m==10 and y==2016? 0.26875945705385 :
     d==15 and m==11 and y==2016? 0.26875945705385 :
     d==15 and m==12 and y==2016? 0.26875945705385 :
     d==15 and m==1 and y==2017? 0.26875945705385 :
     d==15 and m==2 and y==2017? 0.240957775799832 :
     d==15 and m==3 and y==2017? 0.155416876823419 :
     d==15 and m==4 and y==2017? 0.140777777777778 :
     d==15 and m==5 and y==2017? 0.141666666666667 :
     d==15 and m==6 and y==2017? 0.141666666666667 :
     d==15 and m==7 and y==2017? 0.140111105161705 :
     d==15 and m==8 and y==2017? 0.137102226664446 :
     d==15 and m==9 and y==2017? 0.13439641795264 :
     d==15 and m==10 and y==2017? 0.131811367043751 :
     d==15 and m==11 and y==2017? 0.130574258325383 :
     d==15 and m==12 and y==2017? 0.124543446458589 :
     d==15 and m==1 and y==2018? 0.123202512380174 :
     d==15 and m==2 and y==2018? 0.125217779998889 :
     d==15 and m==3 and y==2018? 0.127373807540674 :
     d==15 and m==4 and y==2018? 0.130214592703648 :
     d==15 and m==5 and y==2018? 0.12419969325682 :
     d==15 and m==6 and y==2018? 0.119652587499354 :
     d==15 and m==7 and y==2018? 0.118646953462924 :
     d==15 and m==8 and y==2018? 0.116439991605504 :
     d==15 and m==9 and y==2018? 0.113189853989414 :
     d==15 and m==10 and y==2018? 0.107168798944558 :
     d==15 and m==11 and y==2018? 0.102537771579842 :
     d==15 and m==12 and y==2018? 0.101373824945235 :
     d==15 and m==1 and y==2019? 0.0994042769348746 :
     d==15 and m==2 and y==2019? 0.0982868686868687 :
     d==15 and m==3 and y==2019? 0.0973011667058178 :
     d==15 and m==4 and y==2019? 0.0944657760184726 :
     d==15 and m==5 and y==2019? 0.0912386349930043 :
     d==15 and m==6 and y==2019? 0.0895277777777777 :
     
     
     d==22 and m==6 and y==2019? 0.0891875 :
     d==29 and m==6 and y==2019? 0.0888472222222222 :
     d==6 and m==7 and y==2019? 0.0876992398408638 :
     d==13 and m==7 and y==2019? 0.0859502543336175 :
     d==20 and m==7 and y==2019? 0.0842012688263711 :
     d==27 and m==7 and y==2019? 0.0824522833191248 :
     d==3 and m==8 and y==2019? 0.0811829347983412 :
     d==10 and m==8 and y==2019? 0.0808609628587791 :
     d==17 and m==8 and y==2019? 0.0805389909192169 :
     d==24 and m==8 and y==2019? 0.0802170189796548 :
     d==31 and m==8 and y==2019? 0.0797022684742928 :
     d==7 and m==9 and y==2019? 0.0779249977286423 :
     d==14 and m==9 and y==2019? 0.0761004322996338 :
     d==21 and m==9 and y==2019? 0.0742758668706253 :
     d==28 and m==9 and y==2019? 0.0724513014416168 :
     d==5 and m==10 and y==2019? 0.0719328264639189 :
     d==12 and m==10 and y==2019? 0.0717619207683073 :
     d==19 and m==10 and y==2019? 0.0715910150726957 :
     d==26 and m==10 and y==2019? 0.0714201093770842 :
     d==2 and m==11 and y==2019? 0.0709366687280973 :
     d==9 and m==11 and y==2019? 0.0699111276025562 :
     d==16 and m==11 and y==2019? 0.0688855864770151 :
     d==23 and m==11 and y==2019? 0.0678600453514739 :
     d==30 and m==11 and y==2019? 0.0669541414141414 :
     d==7 and m==12 and y==2019? 0.0666392649903288 :
     d==14 and m==12 and y==2019? 0.0663032559638943 :
     d==21 and m==12 and y==2019? 0.0659672469374597 :
     d==28 and m==12 and y==2019? 0.0656312379110251 :
     d==4 and m==1 and y==2020? 0.0651329223081883 :
     d==11 and m==1 and y==2020? 0.0646368858800774 :
     d==18 and m==1 and y==2020? 0.0641408494519665 :
     d==25 and m==1 and y==2020? 0.0636448130238556 :
     d==1 and m==2 and y==2020? 0.0631837727272728 :
     d==8 and m==2 and y==2020? 0.0624330317812269 :
     d==15 and m==2 and y==2020? 0.0616822908351811 :
     d==22 and m==2 and y==2020? 0.0609315498891353 :
     d==29 and m==2 and y==2020? 0.0601808089430894 :
     d==7 and m==3 and y==2020? 0.0598767435728412 :
     d==14 and m==3 and y==2020? 0.0595650582289607 :
     d==21 and m==3 and y==2020? 0.0592533728850803 :
     d==28 and m==3 and y==2020? 0.0589416875411998 :
     d==4 and m==4 and y==2020? 0.0586456728603604 :
     d==11 and m==4 and y==2020? 0.0583054476351351 :
     d==18 and m==4 and y==2020? 0.0579652224099099 :
     d==25 and m==4 and y==2020? 0.0576249971846847 :
     d==2 and m==5 and y==2020? 0.0572197474747475 :
     d==9 and m==5 and y==2020? 0.0563079797979798 :
     d==16 and m==5 and y==2020? 0.0553962121212121 :
     d==23 and m==5 and y==2020? 0.0544844444444445 :
     d==30 and m==5 and y==2020? 0.0535726767676768 :
     d==6 and m==6 and y==2020? 0.0530256363025416 :
     d==13 and m==6 and y==2020? 0.0525220914793092 :
     d==20 and m==6 and y==2020? 0.0520185466560769 :
     d==27 and m==6 and y==2020? 0.0515150018328446 :
     d==4 and m==7 and y==2020? 0.051296875 :
     d==11 and m==7 and y==2020? 0.0513260416666667 :
     d==18 and m==7 and y==2020? 0.0513552083333333 :
     d==25 and m==7 and y==2020? 0.051384375 :
     d==1 and m==8 and y==2020? 0.051409375 :
     d==8 and m==8 and y==2020? 0.0513488541666667 :
     d==15 and m==8 and y==2020? 0.0512883333333333 :
     d==22 and m==8 and y==2020? 0.0512278125 :
     d==29 and m==8 and y==2020? 0.0511672916666667 :
     d==5 and m==9 and y==2020? 0.0510874242424243 :
     d==12 and m==9 and y==2020? 0.0510248484848485 :
     d==19 and m==9 and y==2020? 0.0509622727272727 :
     d==26 and m==9 and y==2020? 0.050899696969697 :
     d==3 and m==10 and y==2020? 0.0508960743207941 :
     d==10 and m==10 and y==2020? 0.0507566709117032 :
     d==17 and m==10 and y==2020? 0.0506172675026123 :
     d==24 and m==10 and y==2020? 0.0504778640935214 :
     d==31 and m==10 and y==2020? 0.0502856388888889 :
     d==7 and m==11 and y==2020? 0.0503010087365592 :
     d==14 and m==11 and y==2020? 0.0503182029569893 :
     d==21 and m==11 and y==2020? 0.0503353971774193 :
     d==28 and m==11 and y==2020? 0.0503525913978494 :
     d==5 and m==12 and y==2020? 0.0502937849462366 :
     d==12 and m==12 and y==2020? 0.0502187921146954 :
     d==19 and m==12 and y==2020? 0.0501437992831542 :
     d==26 and m==12 and y==2020? 0.0500688064516129 :
     d==2 and m==1 and y==2021? 0.0498510370370371 :
     d==9 and m==1 and y==2021? 0.0495261851851852 :
     d==16 and m==1 and y==2021? 0.0492013333333334 :
     d==23 and m==1 and y==2021? 0.0488764814814815 :
     d==30 and m==1 and y==2021? 0.048560462962963 :
     d==6 and m==2 and y==2021? 0.0479725925925926 :
     d==13 and m==2 and y==2021? 0.0473847222222222 :
     d==20 and m==2 and y==2021? 0.0467968518518518 :
     d==27 and m==2 and y==2021? 0.0462089814814815 :
     d==6 and m==3 and y==2021? 0.0455328703703704 :
     d==13 and m==3 and y==2021? 0.0447038888888889 :
     d==20 and m==3 and y==2021? 0.0438749074074074 :
     d==27 and m==3 and y==2021? 0.0430459259259259 :
     d==3 and m==4 and y==2021? 0.0425722222222222 :
     d==10 and m==4 and y==2021? 0.0425722222222222 :
     d==17 and m==4 and y==2021? 0.0425722222222222 :
     d==24 and m==4 and y==2021? 0.0425722222222222 :
     d==1 and m==5 and y==2021? 0.0425722222222222 :
     d==8 and m==5 and y==2021? 0.0430975272331155 :
     d==15 and m==5 and y==2021? 0.0436228322440087 :
     d==22 and m==5 and y==2021? 0.044148137254902 :
     d==29 and m==5 and y==2021? 0.0446734422657952 :
     d==5 and m==6 and y==2021? 0.0446461554621849 :
     d==12 and m==6 and y==2021? 0.0443300402661064 : na

    e

energy_value(hr_live) =>

    // HR on TV only has "yesterday's" value --> use "lookahead_on" when running live (on current bar), to pull forward yesterdays data
    hr_hist = security("QUANDL:BCHAIN/HRATE", "D", close,gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
    stock = security("QUANDL:BCHAIN/TOTBC", "D", close,gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
    stock12m = security("QUANDL:BCHAIN/TOTBC", "D", close[365],gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
    
    HR_raw = float(na)
    HR_raw := barstate.isrealtime ? hr_live : hr_hist 
    
    energy_eff = float(na)
    energy_eff := na(energy_efficiency()) ? energy_eff[1] : energy_efficiency()
    energy_in = (HR_raw * 1000) * energy_eff
    
    flow = stock - stock12m
    SF = (stock-lost)/flow
    seconds_per_year = 365.25*24*60*60
    supply_growth_rate = (1.0/SF)/seconds_per_year // supply growth rate per second (inflation rate)
    
    energy_value = (energy_in)/(supply_growth_rate)*(fiat_factor/pow(10,15))
    energy_value
    
// CALCULATE

ev = security("QUANDL:BCHAIN/HRATE", "D", energy_value(close),gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
ev_sma = security("QUANDL:BCHAIN/HRATE", "D", sma(energy_value(close),len),gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)

// PLOT

plot(raw?ev:na,color=color.red,linewidth=1,transp=70,title="")
plot(sma?ev_sma:na,color=color.red,linewidth=2,title="SMA")
//
//
//
//
//************************************************************************************************************************************
//************************************************************************************************************************************
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © capriole_charles

//@version=4

// INPUTS

p_og = input(0.05,"Ave. Electricity Cost $USD KWh (Pre June 2019)")
p_china = input(0.04,"Ave. Electricity Cost $USD KWh (Pre China Exodus 2021")
p_new = input(0.05,"Ave. Electricity Cost $USD KWh (Post May 2021")
elec_pct = input(0.60,"% electricity to Total Mining Costs")
plot_mp = input(true,"Plot BTC Miner Price (Close + Transaction Fee Revenue / BTC)")
plot_tc = input(true,"Plot Production & Electricity Cost Curves")
labels = input(false,"Plot Annual Profit Margin Labels?")

use_f = input(false,"Inflation factor")
f = input(0.06,"Post-Corona Inflation factor")

// 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.0448897733333333 :
     d==8 and m==1 and y==2012? 0.0467732066666667 :
     d==22 and m==1 and y==2012? 0.0499167266666667 :
     d==5 and m==2 and y==2012? 0.0516129533333333 :
     d==19 and m==2 and y==2012? 0.0526943266666667 :
     d==4 and m==3 and y==2012? 0.05538186 :
     d==18 and m==3 and y==2012? 0.0580128533333333 :
     d==1 and m==4 and y==2012? 0.0611542533333333 :
     d==15 and m==4 and y==2012? 0.05900928 :
     d==29 and m==4 and y==2012? 0.05795294 :
     d==13 and m==5 and y==2012? 0.06452278 :
     d==27 and m==5 and y==2012? 0.06054668 :
     d==10 and m==6 and y==2012? 0.0604923266666667 :
     d==24 and m==6 and y==2012? 0.06490944 :
     d==8 and m==7 and y==2012? 0.0670409266666667 :
     d==22 and m==7 and y==2012? 0.0712722266666667 :
     d==5 and m==8 and y==2012? 0.0780710533333333 :
     d==19 and m==8 and y==2012? 0.08622184 :
     d==2 and m==9 and y==2012? 0.0957779533333333 :
     d==16 and m==9 and y==2012? 0.105795213333333 :
     d==30 and m==9 and y==2012? 0.113526666666667 :
     d==14 and m==10 and y==2012? 0.114823266666667 :
     d==28 and m==10 and y==2012? 0.121561133333333 :
     d==11 and m==11 and y==2012? 0.128912466666667 :
     d==25 and m==11 and y==2012? 0.1279656 :
     d==9 and m==12 and y==2012? 0.132210866666667 :
     d==23 and m==12 and y==2012? 0.1144392 :
     d==6 and m==1 and y==2013? 0.120510733333333 :
     d==20 and m==1 and y==2013? 0.115101733333333 :
     d==3 and m==2 and y==2013? 0.117235733333333 :
     d==17 and m==2 and y==2013? 0.103566966666667 :
     d==3 and m==3 and y==2013? 0.109349686666667 :
     d==17 and m==3 and y==2013? 0.124549266666667 :
     d==31 and m==3 and y==2013? 0.172383133333333 :
     d==14 and m==4 and y==2013? 0.213046733333333 :
     d==28 and m==4 and y==2013? 0.241923066666667 :
     d==12 and m==5 and y==2013? 0.275134666666667 :
     d==26 and m==5 and y==2013? 0.306536266666667 :
     d==9 and m==6 and y==2013? 0.394398933333333 :
     d==23 and m==6 and y==2013? 0.4976452 :
     d==7 and m==7 and y==2013? 0.582760133333333 :
     d==21 and m==7 and y==2013? 0.7401676 :
     d==4 and m==8 and y==2013? 0.908156133333333 :
     d==18 and m==8 and y==2013? 1.28042666666667 :
     d==1 and m==9 and y==2013? 0.534310466666667 :
     d==15 and m==9 and y==2013? 0.391327333333333 :
     d==29 and m==9 and y==2013? 0.5588032 :
     d==13 and m==10 and y==2013? 0.764947933333333 :
     d==27 and m==10 and y==2013? 1.3118052 :
     d==10 and m==11 and y==2013? 1.91040266666667 :
     d==24 and m==11 and y==2013? 2.39963266666667 :
     d==8 and m==12 and y==2013? 2.99599333333333 :
     d==22 and m==12 and y==2013? 4.11922 :
     d==5 and m==1 and y==2014? 5.27489066666667 :
     d==19 and m==1 and y==2014? 7.14739266666667 :
     d==2 and m==2 and y==2014? 8.90992733333333 :
     d==16 and m==2 and y==2014? 11.0466933333333 :
     d==2 and m==3 and y==2014? 7.66129533333333 :
     d==16 and m==3 and y==2014? 2.928136 :
     d==30 and m==3 and y==2014? 3.55123333333333 :
     d==13 and m==4 and y==2014? 4.35657733333333 :
     d==27 and m==4 and y==2014? 5.25784666666667 :
     d==11 and m==5 and y==2014? 5.83622733333333 :
     d==25 and m==5 and y==2014? 6.94009533333333 :
     d==8 and m==6 and y==2014? 7.97694666666667 :
     d==22 and m==6 and y==2014? 9.47082666666667 :
     d==6 and m==7 and y==2014? 10.1856406666667 :
     d==20 and m==7 and y==2014? 6.81052933333333 :
     d==3 and m==8 and y==2014? 7.12624 :
     d==17 and m==8 and y==2014? 7.97746666666667 :
     d==31 and m==8 and y==2014? 7.722336 :
     d==14 and m==9 and y==2014? 1.62927266666667 :
     d==28 and m==9 and y==2014? 1.79479733333333 :
     d==12 and m==10 and y==2014? 1.85550666666667 :
     d==26 and m==10 and y==2014? 1.89836 :
     d==9 and m==11 and y==2014? 2.08068933333333 :
     d==23 and m==11 and y==2014? 2.12267133333333 :
     d==7 and m==12 and y==2014? 2.03810866666667 :
     d==21 and m==12 and y==2014? 1.72866733333333 :
     d==4 and m==1 and y==2015? 1.83967066666667 :
     d==18 and m==1 and y==2015? 1.902684 :
     d==1 and m==2 and y==2015? 1.85248266666667 :
     d==15 and m==2 and y==2015? 1.959152 :
     d==1 and m==3 and y==2015? 2.06469933333333 :
     d==15 and m==3 and y==2015? 2.09730533333333 :
     d==29 and m==3 and y==2015? 2.10092 :
     d==12 and m==4 and y==2015? 2.14148066666667 :
     d==26 and m==4 and y==2015? 2.11196933333333 :
     d==10 and m==5 and y==2015? 2.078068 :
     d==24 and m==5 and y==2015? 2.145618 :
     d==7 and m==6 and y==2015? 2.09108066666667 :
     d==21 and m==6 and y==2015? 2.194054 :
     d==5 and m==7 and y==2015? 2.186602 :
     d==19 and m==7 and y==2015? 2.267548 :
     d==2 and m==8 and y==2015? 2.32644866666667 :
     d==16 and m==8 and y==2015? 2.355268 :
     d==30 and m==8 and y==2015? 2.40752866666667 :
     d==13 and m==9 and y==2015? 2.56722266666667 :
     d==27 and m==9 and y==2015? 2.59675 :
     d==11 and m==10 and y==2015? 2.723812 :
     d==25 and m==10 and y==2015? 2.71067066666667 :
     d==8 and m==11 and y==2015? 2.85210933333333 :
     d==22 and m==11 and y==2015? 3.03022866666667 :
     d==6 and m==12 and y==2015? 3.39929466666667 :
     d==20 and m==12 and y==2015? 3.98623133333333 :
     d==3 and m==1 and y==2016? 4.50523 :
     d==17 and m==1 and y==2016? 4.933306 :
     d==31 and m==1 and y==2016? 5.44452133333333 :
     d==14 and m==2 and y==2016? 6.76244066666667 :
     d==28 and m==2 and y==2016? 6.95727866666667 :
     d==13 and m==3 and y==2016? 7.16674333333333 :
     d==27 and m==3 and y==2016? 7.24838533333333 :
     d==10 and m==4 and y==2016? 7.59466 :
     d==24 and m==4 and y==2016? 8.03744733333333 :
     d==8 and m==5 and y==2016? 8.04669333333333 :
     d==22 and m==5 and y==2016? 8.575128 :
     d==5 and m==6 and y==2016? 8.75916066666667 :
     d==19 and m==6 and y==2016? 9.00681933333333 :
     d==3 and m==7 and y==2016? 9.36009866666667 :
     d==17 and m==7 and y==2016? 9.46875666666667 :
     d==31 and m==7 and y==2016? 8.01446666666667 :
     d==14 and m==8 and y==2016? 7.53580333333333 :
     d==28 and m==8 and y==2016? 7.733898 :
     d==11 and m==9 and y==2016? 7.87172933333333 :
     d==25 and m==9 and y==2016? 8.36815333333333 :
     d==9 and m==10 and y==2016? 9.015172 :
     d==23 and m==10 and y==2016? 8.90605 :
     d==6 and m==11 and y==2016? 9.13996866666667 :
     d==20 and m==11 and y==2016? 9.65543933333333 :
     d==4 and m==12 and y==2016? 10.1403306666667 :
     d==18 and m==12 and y==2016? 10.79038 :
     d==1 and m==1 and y==2017? 11.3003666666667 :
     d==15 and m==1 and y==2017? 12.10902 :
     d==29 and m==1 and y==2017? 13.9971133333333 :
     d==12 and m==2 and y==2017? 11.8909206666667 :
     d==26 and m==2 and y==2017? 9.31708666666667 :
     d==12 and m==3 and y==2017? 9.86065 :
     d==26 and m==3 and y==2017? 10.3007733333333 :
     d==9 and m==4 and y==2017? 7.24762 :
     d==23 and m==4 and y==2017? 7.68320933333333 :
     d==7 and m==5 and y==2017? 8.29340133333333 :
     d==21 and m==5 and y==2017? 9.45574933333333 :
     d==4 and m==6 and y==2017? 14.5437 :
     d==18 and m==6 and y==2017? 16.2587 :
     d==2 and m==7 and y==2017? 16.08918 :
     d==16 and m==7 and y==2017? 17.9574866666667 :
     d==30 and m==7 and y==2017? 14.9784533333333 :
     d==13 and m==8 and y==2017? 14.0673133333333 :
     d==27 and m==8 and y==2017? 17.6895533333333 :
     d==10 and m==9 and y==2017? 18.6930666666667 :
     d==24 and m==9 and y==2017? 20.69434 :
     d==8 and m==10 and y==2017? 21.5257466666667 :
     d==22 and m==10 and y==2017? 23.8291866666667 :
     d==5 and m==11 and y==2017? 25.94404 :
     d==19 and m==11 and y==2017? 24.5501666666667 :
     d==3 and m==12 and y==2017? 28.0661066666667 :
     d==17 and m==12 and y==2017? 31.0909466666667 :
     d==31 and m==12 and y==2017? 33.61164 :
     d==14 and m==1 and y==2018? 33.3194666666667 :
     d==28 and m==1 and y==2018? 37.2621666666667 :
     d==11 and m==2 and y==2018? 39.83868 :
     d==25 and m==2 and y==2018? 34.0908333333333 :
     d==11 and m==3 and y==2018? 36.35522 :
     d==25 and m==3 and y==2018? 38.6891266666667 :
     d==8 and m==4 and y==2018? 36.9834333333333 :
     d==22 and m==4 and y==2018? 36.3653933333333 :
     d==6 and m==5 and y==2018? 36.9741866666667 :
     d==20 and m==5 and y==2018? 36.2525266666667 :
     d==3 and m==6 and y==2018? 39.6266866666667 :
     d==17 and m==6 and y==2018? 42.9442866666667 :
     d==1 and m==7 and y==2018? 46.8299533333333 :
     d==15 and m==7 and y==2018? 44.0931866666667 :
     d==29 and m==7 and y==2018? 46.5117733333333 :
     d==12 and m==8 and y==2018? 51.6115333333333 :
     d==26 and m==8 and y==2018? 52.88186 :
     d==9 and m==9 and y==2018? 53.53606 :
     d==23 and m==9 and y==2018? 52.26566 :
     d==7 and m==10 and y==2018? 54.2591266666667 :
     d==21 and m==10 and y==2018? 49.32842 :
     d==4 and m==11 and y==2018? 49.3347733333333 :
     d==18 and m==11 and y==2018? 46.1241 :
     d==2 and m==12 and y==2018? 38.5558133333333 :
     d==16 and m==12 and y==2018? 30.9474466666667 :
     d==30 and m==12 and y==2018? 33.21604 :
     d==13 and m==1 and y==2019? 36.9950533333333 :
     d==27 and m==1 and y==2019? 34.83958 :
     d==10 and m==2 and y==2019? 35.0500933333333 :
     d==24 and m==2 and y==2019? 35.4441 :
     d==10 and m==3 and y==2019? 35.9794066666667 :
     d==24 and m==3 and y==2019? 37.2053866666667 :
     d==7 and m==4 and y==2019? 38.1241066666667 :
     d==21 and m==4 and y==2019? 38.6097133333333 :
     d==5 and m==5 and y==2019? 40.98594 :
     d==19 and m==5 and y==2019? 41.5870866666667 :
     d==2 and m==6 and y==2019? 45.8937066666667 :
     d==16 and m==6 and y==2019? 46.9439933333333 :
     d==30 and m==6 and y==2019? 49.2081066666667 :
     d==14 and m==7 and y==2019? 56.2514066666667 :
     d==28 and m==7 and y==2019? 56.63712 :
     d==11 and m==8 and y==2019? 60.3306466666667 :
     d==25 and m==8 and y==2019? 60.5889466666667 :
     d==8 and m==9 and y==2019? 64.10794 :
     d==22 and m==9 and y==2019? 70.9877866666667 :
     d==6 and m==10 and y==2019? 71.6044066666666 :
     d==20 and m==10 and y==2019? 69.7132133333333 :
     d==3 and m==11 and y==2019? 68.3367666666667 :
     d==17 and m==11 and y==2019? 67.1753266666667 :
     d==1 and m==12 and y==2019? 65.7259666666667 :
     d==15 and m==12 and y==2019? 62.8617666666667 :
     d==29 and m==12 and y==2019? 62.7077733333333 :
     d==12 and m==1 and y==2020? 68.22666 :
     d==26 and m==1 and y==2020? 72.07338 :
     d==9 and m==2 and y==2020? 74.5989666666667 :
     d==23 and m==2 and y==2020? 76.2619933333333 :
     d==8 and m==3 and y==2020? 79.4506866666667 :
     d==22 and m==3 and y==2020? 68.7661266666667 :
     d==5 and m==4 and y==2020? 54.9826466666667 :
     d==19 and m==4 and y==2020? 66.4887 :
     d==3 and m==5 and y==2020? 67.6403466666667 :
     d==17 and m==5 and y==2020? 73.33354 :
     d==31 and m==5 and y==2020? 52.1350333333333 :
     d==14 and m==6 and y==2020? 53.85798 :
     d==28 and m==6 and y==2020? 54.2859733333333 :
     d==12 and m==7 and y==2020? 56.9790133333333 :
     d==26 and m==7 and y==2020? 54.9029866666667 :
     d==9 and m==8 and y==2020? 59.41582 :
     d==23 and m==8 and y==2020? 63.73672 :
     d==6 and m==9 and y==2020? 62.8713933333333 :
     d==20 and m==9 and y==2020? 65.73894 :
     d==4 and m==10 and y==2020? 65.8654733333333 :
     d==18 and m==10 and y==2020? 64.7199866666667 :
     d==1 and m==11 and y==2020? 60.9787333333333 :
     d==15 and m==11 and y==2020? 65.9051533333333 :
     d==29 and m==11 and y==2020? 80.4638533333333 :
     d==13 and m==12 and y==2020? 81.26956 :
     d==27 and m==12 and y==2020? 82.89574 :
     d==10 and m==1 and y==2021? 98.42008 :
     d==24 and m==1 and y==2021? 106.3038 :
     d==7 and m==2 and y==2021? 106.528133333333 :
     d==21 and m==2 and y==2021? 110.938466666667 :
     d==7 and m==3 and y==2021? 111.518733333333 :
     d==21 and m==3 and y==2021? 113.947466666667 :
     d==4 and m==4 and y==2021? 119.531533333333 :
     d==18 and m==4 and y==2021? 120.342066666667 :
     d==2 and m==5 and y==2021? 108.69154 :
     d==16 and m==5 and y==2021? 127.397466666667 :
     d==30 and m==5 and y==2021? 109.282866666667 :
     d==13 and m==6 and y==2021? 102.673546666667 :
     d==27 and m==6 and y==2021? 81.9444333333333 :
     d==11 and m==7 and y==2021? 63.6538866666667 :
     d==25 and m==7 and y==2021? 69.75506 :
     d==8 and m==8 and y==2021? 76.5859266666667 :
     d==22 and m==8 and y==2021? 84.7868666666667 :
     d==5 and m==9 and y==2021? 92.0367733333333 :
     d==19 and m==9 and y==2021? 97.33628 :
     d==3 and m==10 and y==2021? 99.3566133333333 :
     d==17 and m==10 and y==2021? 102.828706666667 :
     d==31 and m==10 and y==2021? 109.406533333333 :
     d==14 and m==11 and y==2021? 115.639 :
     d==28 and m==11 and y==2021? 116.424333333333 :
     d==12 and m==12 and y==2021? 120.5624 :
     d==26 and m==12 and y==2021? 120.825266666667 :
     d==9 and m==1 and y==2022? 122.812266666667 :
     d==23 and m==1 and y==2022? 130.533733333333 :
     d==6 and m==2 and y==2022? 129.958733333333 :
     d==20 and m==2 and y==2022? 135.468666666667 :
     d==6 and m==3 and y==2022? 133.200466666667 : 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

b = barssince(time == timestamp(2020, 3, 1, 00, 00))

btc_elec_cost := (use_f and time >= timestamp(2020, 3, 1, 00, 00) ? pow((1+f),b/365) : 1.0)*btc_elec_cost

total_cost = btc_elec_cost/elec_pct   

margin = ((miner_price/total_cost)-1.0)*100.0
profit_margin = round(sma(margin,365))
m = ''
m := profit_margin > 0 ? "+" : ''

// PLOT

a = plot(plot_tc ? btc_elec_cost : na,color=color.red,linewidth=2,title=" Electricity")
tc = plot(plot_tc ? total_cost : na,color=color.purple,linewidth=1,title=" Total")
fill(a,tc,color=color.red,transp=70)

mp = plot(plot_mp ? miner_price : close,color=(plot_mp ? color.green : na),linewidth=2,title="Price")

clr = ((plot_mp ? miner_price : close) < total_cost) ? color.red : na
fill(mp,tc,color=clr,transp=20)

// LABELS

if labels and (barstate.islast or (dayofmonth ==1 and month ==1) and profit_margin)
    label.new(bar_index, btc_elec_cost, style=label.style_labelup,
         text="Margin (p.a.)\n" + m + tostring(profit_margin) +"%",
         textcolor=color.white,color=color.green)
//
//
//************************************************************************************************************************************
//************************************************************************************************************************************
//
//@version=4
//
// An indicator by @Quantadelic aka Pinescript Pleb aka Stealer of Alpha & Destroyer of Discords
//
// "I drink your milkshake! I drink it up."
//
// No rights reserved, copy, paste, do what you want.
//
// 13/02/2020: edited by @mabonyi to include coloured zones and remove the intercept/slope variables from indicator displays
//
// INPUTS
DarkOrLight =   input("Light", title="Dark mode or Light mode?", options=["Dark", "Light"])
ShowFibs =      input("Y", title="Show Fibonacci Levels?", options=["Y","N"])
ShowExtensions= input("N", title="Show Curve Projections?", options=["Y","N"])
//
// Made fixed so that they aren't clogging up the display
HighIntercept = 1.06930947
HighSlope =     0.00076
LowIntercept =  -3.0269716
LowSlope =      0.001329
//
//HighIntercept = input(1.06930947, title="Top Curve Intercept", step=0.001)
//HighSlope =     input(0.00076, title="Top Curve Slope", step=0.00001)
//LowIntercept =  input(-3.0269716, title="Bottom Curve Intercept", step=0.001)
//LowSlope =      input(0.001329, title="Bottom Curve Slope", step=0.00001)
//
// TIME CALCS
TimeIndex =     time < 1279670400000 ? 3.0 : (time - 1279670400000) / 86400000
Weight =        (log10(TimeIndex + 10) * TimeIndex * TimeIndex - TimeIndex) / 30000
TimeDelta =     time - time[1]

// TOP OF CHANNEL INTERCEPT & SLOPE CALCS
HighSlopeCum =  HighSlope * TimeIndex
HighLogDev =    TimeIndex > 2 ? log(Weight) + HighIntercept + HighSlopeCum : na

// BOTTOM OF CHANNEL INTERCEPT & SLOPE CALCS
LowSlopeCum =   LowSlope * TimeIndex
LowLogDev =     TimeIndex > 2 ? log(Weight) + LowIntercept + LowSlopeCum : na

// TOTAL CHANNEL LOG RANGE
LogRange =      HighLogDev - LowLogDev

// FIBONACCI LEVELS
Fib9098Calc =   (LogRange * 0.9098) + LowLogDev
Fib8541Calc =   (LogRange * 0.8541) + LowLogDev
Fib7639Calc =   (LogRange * 0.7639) + LowLogDev
Fib618Calc =    (LogRange * 0.618) + LowLogDev
MidCalc =       (LogRange * 0.5) + LowLogDev
Fib382Calc =    (LogRange * 0.382) + LowLogDev
Fib2361Calc =   (LogRange * 0.2361) + LowLogDev
Fib1459Calc =   (LogRange * 0.1459) + LowLogDev
Fib0902Calc =   (LogRange * 0.0902) + LowLogDev

NegFib0902Calc = LowLogDev - (LogRange * 0.0902)
NegFib1459Calc = LowLogDev - (LogRange * 0.1459)

// SCALE LOG VARIABLES TO LINEAR
HighDev =       pow(2.718281828459, HighLogDev)
Fib9098Dev =    ShowFibs == "Y" ? pow(2.718281828459, Fib9098Calc) : na
Fib8541Dev =    ShowFibs == "Y" ? pow(2.718281828459, Fib8541Calc) : na
Fib7639Dev =    ShowFibs == "Y" ? pow(2.718281828459, Fib7639Calc) : na
Fib618Dev =     ShowFibs == "Y" ? pow(2.718281828459, Fib618Calc) : na
MidDev =        pow(2.718281828459, MidCalc)
Fib382Dev =     ShowFibs == "Y" ? pow(2.718281828459, Fib382Calc) : na
Fib2361Dev =    ShowFibs == "Y" ? pow(2.718281828459, Fib2361Calc) : na
Fib1459Dev =    ShowFibs == "Y" ? pow(2.718281828459, Fib1459Calc) : na
Fib0902Dev =    ShowFibs == "Y" ? pow(2.718281828459, Fib0902Calc) : na
LowDev =        pow(2.718281828459, LowLogDev)
NegFib0902Dev = ShowFibs == "Y" ? pow(2.718281828459, NegFib0902Calc) : na
NegFib1459Dev = ShowFibs == "Y" ? pow(2.718281828459, NegFib1459Calc) : na

// COLOR SCHEME
ColorScheme =   DarkOrLight == "Dark" ? color(color.white) : color(color.black)

// PLOTS
p10000 = plot(HighDev, color=ColorScheme, transp=0, title="100% - Top of Channel")
p9098 = plot(Fib9098Dev, color=ColorScheme, transp=50, title="90.98% - Fibonacci Level")
p8541 = plot(Fib8541Dev, color=ColorScheme, transp=50, title="85.41% - Fibonacci Level")
p7639 = plot(Fib7639Dev, color=ColorScheme, transp=50, title="76.39% - Fibonacci Level")
p6180 = plot(Fib618Dev, color=ColorScheme, transp=50, title="61.80% - Fibonacci Level")
p5000 = plot(MidDev, color=ColorScheme, transp=0, title="50% - Middle of Channel")
p3820 = plot(Fib382Dev, color=ColorScheme, transp=50, title="38.20% - Fibonacci Level")
p2361 = plot(Fib2361Dev, color=ColorScheme, transp=50, title="23.61% - Fibonacci Level")
p1459 = plot(Fib1459Dev, color=ColorScheme, transp=50, title="14.59% - Fibonacci Level")
p902 = plot(Fib0902Dev, color=ColorScheme, transp=50, title="9.02% - Fibonacci Level")
p0 = plot(LowDev, color=ColorScheme, transp=0, title="0% - Bottom of Channel")
pn0902 = plot(NegFib0902Dev, color=color.white, transp=50, title="-9.02% - Fibonacci Level")
pn1459 = plot(NegFib1459Dev, color=color.white, transp=25, title="-14.59% - Fibonacci Level")

fill(p0, p2361, color=color.black, transp=80, title="Oversold Zone")
fill(p3820, p6180, color=color.white, transp=80, title="Transition Zone")
fill(p8541, p10000, color=color.black, transp=80, title="Overbought Zone")

//////////////////////////////////////////////////////////
//                                                      //
// CURVE EXTENSION DRAWING FUNCTION & HELPER VARIABLES  //
//                                                      //
//////////////////////////////////////////////////////////

// CURVE EXTENSION FUNCTION
ExtenZe(_i, _slope, _intercept) =>
    TI = ((time + TimeDelta *_i) -1279670400000) / 86400000
    W = (log10(TI + 10) * TI * TI - TI) / 30000
    HSC = _slope * TI
    HLD = log(W) + _intercept + HSC
    HD = pow(2.718281828459, HLD)
    HD

// VARIABLES FOR ADJUSTING LENGTH OF CURVE EXTENSIONS RELATIVE TO CHART TIME-FRAME
ForLoopStep = timeframe.ismonthly == 1 ? ceil( 12 / timeframe.multiplier) : 
              timeframe.isweekly == 1 ? ceil(26 / timeframe.multiplier) : 
              timeframe.isdaily == 1 ? ceil(91 / timeframe.multiplier) : 
              (timeframe.isminutes == 1) and (timeframe.multiplier > 59) ? ceil(10080 / timeframe.multiplier) : na
ForLoopMax =    na(ForLoopStep) ? na : ForLoopStep * 13

// MID CURVE SLOPE & INTERCEPT
MidSlope =      (HighSlope + LowSlope) * 0.5
MidIntercept =  (HighIntercept + LowIntercept) * 0.5

// CHECK IF LAST BAR & "Show Curve Projections" IS CHECKED, THEN DRAW CURVE EXTENZES
if barstate.islast and ShowExtensions == "Y"
    for i = 0 to ForLoopMax-1 by ForLoopStep
        line.new(time + TimeDelta * i, ExtenZe(i, HighSlope, HighIntercept), time + TimeDelta * (i + ForLoopStep), ExtenZe(i + ForLoopStep, HighSlope, HighIntercept), xloc=xloc.bar_time, color=ColorScheme)
        line.new(time + TimeDelta * i, ExtenZe(i, LowSlope, LowIntercept), time + TimeDelta * (i + ForLoopStep), ExtenZe(i + ForLoopStep, LowSlope, LowIntercept), xloc=xloc.bar_time, color=ColorScheme)
        line.new(time + TimeDelta * i, ExtenZe(i, MidSlope, MidIntercept), time + TimeDelta * (i + ForLoopStep), ExtenZe(i + ForLoopStep, MidSlope, MidIntercept), xloc=xloc.bar_time, color=ColorScheme)
//************************************************************************************************************************************
//************************************************************************************************************************************