Risk Parity with Foreign Stocks

Hi all,

I found Georges Bilan's post about Risk Parity (https://www.quantopian.com/posts/risk-parity-slash-slash-all-weather-portfolio) and wanted to adapt it to my portfolios with global stocks. But I learned that it is not possible to get global stock prices through context.sid in the IDE. Is there a way to work around that in the codes?

Thanks so much!!

4
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
 Returns 1 Month 3 Month 6 Month 12 Month
 Alpha 1 Month 3 Month 6 Month 12 Month
 Beta 1 Month 3 Month 6 Month 12 Month
 Sharpe 1 Month 3 Month 6 Month 12 Month
 Sortino 1 Month 3 Month 6 Month 12 Month
 Volatility 1 Month 3 Month 6 Month 12 Month
 Max Drawdown 1 Month 3 Month 6 Month 12 Month
import numpy as np
import scipy

def initialize(context):
context.stocks = [ sid(16841),
sid(47740),
sid(46631),
sid(5061)]

context.x0 = 1.0*np.ones_like(context.stocks)/len(context.stocks)

schedule_function(func= getin,date_rule=date_rules.month_end(),
time_rule=time_rules.market_open())

def getin(context, data):

prices = data.history(context.stocks,'price',180,'1d').as_matrix(context.stocks) #22 = 1 month
ret = np.diff(prices,axis=0) # daily \$ returns, current day price minus the day before price
ret = np.divide(ret,np.amax(np.absolute(ret)))

bnds = ((0,1),(0,1),(0,1),(0,1)) #bounds for weights (number of bounds  = to number of assets)
cons = ({'type': 'eq', 'fun': lambda x:  np.sum(x)-1.0})

res= scipy.optimize.minimize(fitnessERC, context.x0, args=ret,method='SLSQP',constraints=cons,bounds=bnds)

if res.success:
allocation = res.x
allocation[allocation<0]=0
denom = np.sum(allocation)
if denom != 0:         #normalization process
allocation = allocation/denom
else:
allocation = context.x0

context.x0 = allocation

total=allocation[0]+allocation[1]+allocation[2]+allocation[3]
w1=allocation[0]/total
w2=allocation[1]/total
w3=allocation[2]/total
w4=allocation[3]/total

order_target_percent(sid(16841),w1)
order_target_percent(sid(47740),w2)
order_target_percent(sid(46631),w3)
order_target_percent(sid(5061),w4)

def variance(x,*args):
p = np.squeeze(np.asarray(args))
Acov = np.cov(p.T)
return np.dot(x,np.dot(Acov,x))

def fitnessERC(x, *args):
N = x.shape[0]
p = np.squeeze(np.asarray(args))
Acov = np.cov(p.T)
Acov = np.matrix(Acov)
x = np.matrix(x)
y = np.array(x) * ( np.array( Acov * x.T ).T )
#risk contribution
var = x * Acov * x.T
b = var/N
fval = 0
y = np.squeeze(np.asarray(y))
for i in range(0,N):
xij  = (y[i]/var - b) * (y[i]/var - b)
fval = fval + xij*xij
return fval
There was a runtime error.