Diabolicus

Volume Weighted Market Mean 3.0 [Dia]

150924 - v3.0
This script is a complete rewrite of the previous version here.
It gathers market data from up to 8 different Bitcoin exchange pairs (8 is maximum due to script limitations).The influence of each single exchange can be customized individually, default is 50% for 0-fee exchanges, 10% for futures and 100% for all others. For each single exchange a standard deviation from the composite mean price and a average price range are determined. To make data from exchanges with different liquidity comparable, all single exchange price ranges are normalized to the range of the composite mean price. To make data from exchanges with different fee structure (= exchanges that constantly trade at a premium / discount) comparable, all single exchange prices are corrected by their respective standard deviations. To account for trade volume , all price data is weighted by the current candles volume .
- optional Heikin Ashi candle display
- optional EMAs
- lots of switches to play around with ;-)
Remove from Favorite Scripts Add to Favorite Scripts
study(title="Volume Weighted Market Mean 3.0 [Dia]", shorttitle="VWMM_Dia 3.0", overlay=true)
// 150924 - v3.0
// The script gathers market data from up to 8 different Bitcoin exchange pairs.
// The influence of each single exchange can be customized by user preference, default is 50% for 0-fee exchanges, 10% for futures and 100% for all others.
// For each single exchange a 100 bar standard deviation from the composite mean price and an 100 bar average price range are determined.
// To make data from exchanges with different liquidity comparable, all single exchange price ranges are normalized to the range of the composite mean price.
// To make data from exchanges with different fee structure (= exchanges that constantly trade at a premium / discount) comparable, all single exchange prices are corrected by their respective standard deviations.
// To account for trade volume, all price data is weighted by the current candles volume.
// - optional Heikin Ashi candle display
// - optional simple arithmetic mean

// =========
// FUNCTIONS
// =========

sym_hl2(s)=>security(s,period,hl2)
sym_hlc3(s)=>security(s,period,hlc3)
sym_ohlc4(s)=>security(s,period,ohlc4)
ohlc(o,h,l,c)=>(o+h+l+c)/4

// ======
// INPUTS
// ======

// exchanges
e_1 = "KRAKEN:XBTEUR"
e_2 = "OKCOIN:BTCCNY"
e_3 = "OKCOIN:BTCUSD1W"
e_4 = "OKCOIN:BTCUSD3M"
e_5 = "HUOBI:BTCCNY"
e_6 = "BITSTAMP:BTCUSD"
e_7 = "BITFINEX:BTCUSD"
e_8 = "BTCE:BTCUSD"

// show
s_1 = input(defval=1.0, minval=0, maxval=1, step=0.1, title="01 Kraken   XBTEUR")
s_2 = input(defval=0.5, minval=0, maxval=1, step=0.1, title="02 OKCoin   BTCCNY")
s_3 = input(defval=0.1, minval=0, maxval=1, step=0.1, title="03 OKCoin   BTCUSD1W")
s_4 = input(defval=0.1, minval=0, maxval=1, step=0.1, title="04 OKCoin   BTCUSD3M")
s_5 = input(defval=0.5, minval=0, maxval=1, step=0.1, title="05 Huobi    BTCCNY")
s_6 = input(defval=1.0, minval=0, maxval=1, step=0.1, title="06 Bitstamp BTCUSD")
s_7 = input(defval=1.0, minval=0, maxval=1, step=0.1, title="07 Bitfinex BTCUSD")
s_8 = input(defval=1.0, minval=0, maxval=1, step=0.1, title="08 BTC-E    BTCUSD")

norm_r   = input(defval=1,  minval=0, maxval=1, step=1, title="Normalize Price Ranges?")
equalize = input(defval=1,  minval=0, maxval=1, step=1, title="Equalize Exchanges?")
tex      = input(defval=1,  minval=1, maxval=9, step=1, title="# of trading exchange (1..8)?")
tx_pm    = input(defval=0,  minval=0, maxval=1, step=1, title="Show normalized trading exchange EMA?")
s_vm     = input(defval=1,  minval=0, maxval=1, step=1, title="Show volume weighted mean EMA?")
s_pm     = input(defval=1,  minval=0, maxval=1, step=1, title="Show simple arithmetic mean EMA?")
mean_ema = input(defval=20, minval=1,           step=1, title="Mean EMA Length?")
ha       = input(defval=1,  minval=0, maxval=1, step=1, title="Draw Heikin Ashi Candles?")

// exchange rates
EURUSD = sym_ohlc4("FX:EURUSD")
USDCNH = sym_ohlc4("FX:USDCNH")

// adjust to current ticker base currency 
base = ticker == "XBTEUR" ? 2 : ticker == "BTCEUR" ? 2 : ticker == "BTCCNY" ? 3 : 1
USD_correct = base==2 ? 1/EURUSD           : base==3 ? USDCNH        : 1
EUR_correct = base==2 ? 1                  : base==3 ? EURUSD*USDCNH : EURUSD
CNY_correct = base==2 ? 1/(USDCNH * EURUSD): base==3 ? 1             : 1/USDCNH

// ========
// GET DATA
// ========

get_data(e)=>
    cor= e=="KRAKEN:XBTEUR" or e=="BTCE:BTCEUR" ? EUR_correct : 
         e=="OKCOIN:BTCCNY" or e=="HUOBI:BTCCNY" or e=="BTCCHINA:BTCCNY" ? CNY_correct :
         e=="OKCOIN:BTCUSD" or e=="OKCOIN:BTCUSD1W" or e=="OKCOIN:BTCUSD2W" or e=="OKCOIN:BTCUSD3M" or e=="BITFINEX:BTCUSD" or e=="BITSTAMP:BTCUSD" or e=="BTCE:BTCUSD" ? USD_correct : 1
    v = security(e,period,volume)
    h = security(e,period,high)*cor
    l = security(e,period,low)*cor
    c = security(e,period,close)*cor
    o = c[1]//security(e,period,open)*cor
    [v,o,h,l,c,cor]
[v_1,o_1,h_1,l_1,c_1,cor_1] = get_data(e_1)
[v_2,o_2,h_2,l_2,c_2,cor_2] = get_data(e_2)
[v_3,o_3,h_3,l_3,c_3,cor_3] = get_data(e_3)
[v_4,o_4,h_4,l_4,c_4,cor_4] = get_data(e_4)
[v_5,o_5,h_5,l_5,c_5,cor_5] = get_data(e_5)
[v_6,o_6,h_6,l_6,c_6,cor_6] = get_data(e_6)
[v_7,o_7,h_7,l_7,c_7,cor_7] = get_data(e_7)
[v_8,o_8,h_8,l_8,c_8,cor_8] = get_data(e_8)

// calculate totals
s_total = s_1+s_2+s_3+s_4+s_5+s_6+s_7+s_8

// calculate volume (weighted by user settings)
v_total = (v_1*s_1+v_2*s_2+v_3*s_3+v_4*s_4+v_5*s_5+v_6*s_6+v_7*s_7+v_8*s_8)

// calculate means weighted by user settings
v_mean = v_total/s_total
o_mean = (o_1*s_1+o_2*s_2+o_3*s_3+o_4*s_4+o_5*s_5+o_6*s_6+o_7*s_7+o_8*s_8)/s_total
h_mean = (h_1*s_1+h_2*s_2+h_3*s_3+h_4*s_4+h_5*s_5+h_6*s_6+h_7*s_7+h_8*s_8)/s_total
l_mean = (l_1*s_1+l_2*s_2+l_3*s_3+l_4*s_4+l_5*s_5+l_6*s_6+l_7*s_7+l_8*s_8)/s_total
c_mean = (c_1*s_1+c_2*s_2+c_3*s_3+c_4*s_4+c_5*s_5+c_6*s_6+c_7*s_7+c_8*s_8)/s_total
pw_mean = ohlc(o_mean,h_mean,l_mean,c_mean)
pw_mean_1 = ohlc(o_1,h_1,l_1,c_1)
pw_mean_2 = ohlc(o_2,h_2,l_2,c_2)
pw_mean_3 = ohlc(o_3,h_3,l_3,c_3)
pw_mean_4 = ohlc(o_4,h_4,l_4,c_4)
pw_mean_5 = ohlc(o_5,h_5,l_5,c_5)
pw_mean_6 = ohlc(o_6,h_6,l_6,c_6)
pw_mean_7 = ohlc(o_7,h_7,l_7,c_7)
pw_mean_8 = ohlc(o_8,h_8,l_8,c_8)

// normalize exchange prices
norm(x,y)=>
    std_deviation = equalize==1?sum(x-y,100)/100:0
dev_1 = norm(pw_mean_1,pw_mean)
dev_2 = norm(pw_mean_2,pw_mean)
dev_3 = norm(pw_mean_3,pw_mean)
dev_4 = norm(pw_mean_4,pw_mean)
dev_5 = norm(pw_mean_5,pw_mean)
dev_6 = norm(pw_mean_6,pw_mean)
dev_7 = norm(pw_mean_7,pw_mean)
dev_8 = norm(pw_mean_8,pw_mean)

// ranges
range(h,l)=>
    std_range = norm_r==1?sum(h-l,100)/100:1
range_1 = range(h_1,l_1)
range_2 = range(h_2,l_2)
range_3 = range(h_3,l_3)
range_4 = range(h_4,l_4)
range_5 = range(h_5,l_5)
range_6 = range(h_6,l_6)
range_7 = range(h_7,l_7)
range_8 = range(h_8,l_8)
range_total  = (range_1*s_1+range_2*s_2+range_3*s_3+range_4*s_4+range_5*s_5+range_6*s_6+range_7*s_7+range_8*s_8)/s_total
rf_1 = range_total/range_1
rf_2 = range_total/range_2
rf_3 = range_total/range_3
rf_4 = range_total/range_4
rf_5 = range_total/range_5
rf_6 = range_total/range_6
rf_7 = range_total/range_7
rf_8 = range_total/range_8

shift(o,h,l,c,rf,dev)=>
    ctr = (h-l)/2+l
    h_new = (h-ctr)*rf+ctr-dev
    l_new = (l-ctr)*rf+ctr-dev
    o_new = (o-ctr)*rf+ctr-dev
    c_new = (c-ctr)*rf+ctr-dev
    [o_new,h_new,l_new,c_new]
[on_1,hn_1,ln_1,cn_1]=shift(o_1,h_1,l_1,c_1,rf_1,dev_1)
[on_2,hn_2,ln_2,cn_2]=shift(o_2,h_2,l_2,c_2,rf_2,dev_2)
[on_3,hn_3,ln_3,cn_3]=shift(o_3,h_3,l_3,c_3,rf_3,dev_3)
[on_4,hn_4,ln_4,cn_4]=shift(o_4,h_4,l_4,c_4,rf_4,dev_4)
[on_5,hn_5,ln_5,cn_5]=shift(o_5,h_5,l_5,c_5,rf_5,dev_5)
[on_6,hn_6,ln_6,cn_6]=shift(o_6,h_6,l_6,c_6,rf_6,dev_6)
[on_7,hn_7,ln_7,cn_7]=shift(o_7,h_7,l_7,c_7,rf_7,dev_7)
[on_8,hn_8,ln_8,cn_8]=shift(o_8,h_8,l_8,c_8,rf_8,dev_8)

// calculate means weighted by user settings and volume
hv_m  = (s_1*hn_1*v_1+s_2*hn_2*v_2+s_3*hn_3*v_3+s_4*hn_4*v_4+s_5*hn_5*v_5+s_6*hn_6*v_6+s_7*hn_7*v_7+s_8*hn_8*v_8) / v_total
lv_m  = (s_1*ln_1*v_1+s_2*ln_2*v_2+s_3*ln_3*v_3+s_4*ln_4*v_4+s_5*ln_5*v_5+s_6*ln_6*v_6+s_7*ln_7*v_7+s_8*ln_8*v_8) / v_total
cv_m  = (s_1*cn_1*v_1+s_2*cn_2*v_2+s_3*cn_3*v_3+s_4*cn_4*v_4+s_5*cn_5*v_5+s_6*cn_6*v_6+s_7*cn_7*v_7+s_8*cn_8*v_8) / v_total
ov_m  = (s_1*on_1*v_1+s_2*on_2*v_2+s_3*on_3*v_3+s_4*on_4*v_4+s_5*on_5*v_5+s_6*on_6*v_6+s_7*on_7*v_7+s_8*on_8*v_8) / v_total
vw_m  = ohlc(ov_m,hv_m,lv_m,cv_m)
cv_mean = ha==1? vw_m : cv_m
ov_mean = ha==1? (na(ov_mean[1])?ov_m:ov_mean[1]+cv_mean[1])/(2-na(ov_mean[1])) : ov_m
hv_mean = ha==1?  max(hv_m,max(ov_mean,cv_mean)): hv_m
lv_mean = ha==1?  min(lv_m,min(ov_mean,cv_mean)): lv_m
vw_mean = ohlc(ov_mean,hv_mean,lv_mean,cv_mean)

// traded exchange
tx_on = tex==1?on_1:tex==2?on_2:tex==3?on_3:tex==4?on_4:tex==5?on_5:tex==6?on_6:tex==7?on_7:on_8
tx_hn = tex==1?hn_1:tex==2?hn_2:tex==3?hn_3:tex==4?hn_4:tex==5?hn_5:tex==6?hn_6:tex==7?hn_7:hn_8
tx_ln = tex==1?ln_1:tex==2?ln_2:tex==3?ln_3:tex==4?ln_4:tex==5?ln_5:tex==6?ln_6:tex==7?ln_7:ln_8
tx_cn = tex==1?cn_1:tex==2?cn_2:tex==3?cn_3:tex==4?cn_4:tex==5?cn_5:tex==6?cn_6:tex==7?cn_7:cn_8
tx_pwn_mean = ohlc(tx_on,tx_hn,tx_ln,tx_cn)

// ====
// PLOT
// ====

mean_vw = plot(s_vm ? ema(vw_mean,mean_ema) : na,color=red,linewidth=1)
mean_pw = plot(s_pm ? ema(pw_mean,mean_ema) : na,color=orange,linewidth=1)
mean_tx = plot(tx_pm ? ema(tx_pwn_mean,mean_ema) : na,color=blue,linewidth=1)
mean_vw_candle = plotcandle(ov_mean,hv_mean,lv_mean,cv_mean,title="PW_SHIFT",color=cv_mean<ov_mean?#FF3300:#FF9900)
Ideas Scripts Chart
United States
United Kingdom
India
España
Italia
Brasil
Россия
Türkiye
日本
한국
Home Stock Screener Economic Calendar How It Works Chart Features House Rules Moderators For the WEB Widgets Stock Charting Library Priority Support Feature Request Blog & News FAQ Help & Wiki Twitter
Private Messages Chat Ideas Published Followers Following Priority Support Public Profile Profile Settings Billing Sign Out