Comments and improvements welcome. --Grant

Clone Algorithm

607

Loading...

There was an error loading this backtest.

Backtest from
to
with
initial capital

Cumulative performance:

Algorithm
Benchmark

Custom data:

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(8554), # SPY, SPDR S&P 500 ETF TRUST sid(23921), # TLT, ISHARES 20+ YEAR TREASURY BOND sid(33651)] # BSV, VANGUARD SHORT-TERM BOND ETF context.x0 = 1.0*np.ones_like(context.stocks)/len(context.stocks) schedule_function(trade,date_rules.week_start(days_offset=1),time_rules.market_open(minutes=60)) set_commission(commission.PerTrade(cost=0)) set_long_only() def handle_data(context,data): record(leverage = context.account.leverage) def trade(context, data): prices = history(20*390,'1m', 'price') ret = prices.pct_change()[-1:].as_matrix(context.stocks) ret_mean = prices.pct_change().mean() ret_std = prices.pct_change().std() ret_norm = ret_mean/ret_std ret_norm = ret_norm.as_matrix(context.stocks) bnds = [] limits = [0,1] for stock in context.stocks: bnds.append(limits) bnds = tuple(tuple(x) for x in bnds) cons = ({'type': 'eq', 'fun': lambda x: np.sum(x)-1.0}, {'type': 'ineq', 'fun': lambda x: np.dot(x,ret_norm)}) res= scipy.optimize.minimize(variance, context.x0, args=ret,jac=jac_variance, method='SLSQP',constraints=cons,bounds=bnds) if res.success: allocation = res.x allocation[allocation<0]=0 denom = np.sum(allocation) if denom != 0 and np.dot(allocation,ret_norm) > 0: allocation = allocation/denom else: # return allocation = np.zeros_like(context.stocks) context.x0 = allocation if get_open_orders(): return for i,stock in enumerate(context.stocks): order_target_percent(stock,allocation[i]) record(SPY = allocation[0]) record(TLT = allocation[1]) record(BSV = allocation[2]) def variance(x,*args): p = np.squeeze(np.asarray(args)) Acov = np.cov(p.T) return np.dot(x,np.dot(Acov,x)) def jac_variance(x,*args): p = np.squeeze(np.asarray(args)) Acov = np.cov(p.T) return 2*np.dot(Acov,x)

We have migrated this algorithm to work with a new version of the Quantopian API. The code is different than the original version, but the investment rationale of the algorithm has not changed. We've put everything you need to know here on one page.