Market Hedge Ratio
// 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=5
indicator("Market Hedge Ratio (Capriole Investments)",overlay=false)

market = input.string("all","market to use",options=['all','btc','eth','eth_to_btc'])

len= input(200)
mu = input(1.2)
ml = input(1.2)
smooth=input(true)
len_smooth=input(10)
 
data_available =  time >= timestamp(2019, 7, 1, 00, 00)

//https://coinmarketcap.com/view/stablecoin/

mcap_total = request.security("CRYPTOCAP:TOTAL",timeframe.period,close,lookahead=barmerge.lookahead_on)
mcap_btc = request.security("CRYPTOCAP:BTC",timeframe.period,close,lookahead=barmerge.lookahead_on)
mcap_eth = request.security("CRYPTOCAP:ETH",timeframe.period,close,lookahead=barmerge.lookahead_on)

mcap_usdt = request.security("CRYPTOCAP:USDT",timeframe.period,close,lookahead=barmerge.lookahead_on)
mcap_usdc = request.security("CRYPTOCAP:USDC",timeframe.period,close,lookahead=barmerge.lookahead_on)
//mcap_busd = request.security("CRYPTOCAP:BUSD",timeframe.period,close,lookahead=barmerge.lookahead_on)
mcap_dai = request.security("CRYPTOCAP:DAI",timeframe.period,close,lookahead=barmerge.lookahead_on)

mcap_stables = nz(mcap_usdt,0) + nz(mcap_usdc,0) + nz(mcap_dai,0)

r_btc = mcap_btc/mcap_stables
r_eth = mcap_eth/mcap_stables
r_all = mcap_total/mcap_stables
r_ethbtc = mcap_eth/mcap_btc

m = market=='all' ? r_all :
     market=='btc' ? r_btc :
     market=='eth' ? r_eth : 
     market=='eth_to_btc' ? r_ethbtc : na

m := smooth ? ta.ema(m,len_smooth) : m

sma = ta.sma(m,len)
std = ta.stdev(m,len)

u = sma + mu*std
l = sma - ml*std

plot(data_available ? m : na,color=color.blue,title="ALL")

plot(data_available ? u : na,color=color.gray,title="")
plot(data_available ? l : na,color=color.gray,title="")

buy =  (timeframe.period=='D' and m<l)
     or(timeframe.period!='D' and timeframe.multiplier<=240 and m>u)
     
sell = (timeframe.period=='D' and m>u)
     or(timeframe.period!='D' and timeframe.multiplier<=240 and m<l)

bgcolor(buy?color.new(color.green,50):sell?color.new(color.red,50):na)

// Sourcing
var table_source = table(na)
table_source := table.new(position=position.bottom_left, columns=1, rows=1, bgcolor=color.white, border_width=1)
table.cell(table_source, 0, 0, text="Source: Capriole Investments Limited", bgcolor=color.white, text_color=color.black, width=20, height=7, text_size=size.normal, text_halign=text.align_left)