Hi guys,

Can someone help me with the following error? I am looking to add quantopian's continuous futures but first I need to de-bug.

Thank you in advance.

Something went wrong. Sorry for the inconvenience. Try using the built-in debugger to analyze your code. If you would like help, send us an email.

AssertionError: Number of Block dimensions (1) must equal number of axes (2)

There was a runtime error on line 153.

Clone Algorithm

11

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 math import numpy as np import datetime import pandas as pd from pytz import timezone from sklearn.decomposition import PCA from zipline.utils import tradingcalendar as calendar class LogReturnSeries: def __init__(self, t, n): self.T = t self.N = n self.reset() def reset(self): self.counter = 0 self.log_returns = np.zeros((self.T, self.N)) self.last_prices = [] def add_prices(self, prices): if len(self.last_prices) == 0: self.last_prices = prices return else: log_prices = [] for ii in range(0, len(prices)): log_prices.append(math.log(prices[ii] * 1.0 / self.last_prices[ii])) self.last_prices = prices if self.counter < self.T: self.log_returns[self.counter, :] = log_prices else: self.log_returns = np.delete(self.log_returns, 0, axis=0) self.log_returns = np.vstack((self.log_returns, log_prices)) self.counter += 1 def get_series(self): if self.counter <= self.T: return None else: return np.copy(self.log_returns) class Regime: def __init__(self): pass MOMENTUM = 1 MEAN_REVERSION = 2 NONE = 0 class Strategy: def __init__(self, stocks, t, r, e): self.T = t self.N = stocks self.H = r self.K = e self.log_returns = LogReturnSeries(t, stocks) def reset(self): self.log_returns.reset() def compute_betas_by_ols(self, series, d): future_returns = series[self.T - self.H - 1:self.T - 1, :] future_returns = future_returns.sum(axis=0) d_hat = d[self.T - 2 * self.H - 1:self.T - self.H - 1, :] d_hat = d_hat.sum(axis=0) betas = np.zeros((self.K, self.N)) for ii in range(0, self.N): b = self.compute_betas_by_ols_stock(d_hat, future_returns[ii]) betas[:,ii] = b[0] return betas @staticmethod def compute_betas_by_ols_stock(d_hat, future_return): ret = np.linalg.lstsq(d_hat, [[future_return]])[0] return ret def add_prices(self, prices): self.log_returns.add_prices(prices) ret = self.log_returns.get_series() if ret is None: return Regime.NONE, None m = ret.mean(axis=0) y = ret - m pca = PCA(self.K) comp = pca.fit_transform(y) d = np.matrix(comp) b = self.compute_betas_by_ols(self.log_returns.get_series(), d) d_hat = d[self.T - self.H - 1:self.T - 1, :] d_hat = d_hat.sum(axis=0) v = d_hat * b + m real_return = ret[self.T - self.H - 1:self.T - 1, :] real_return = real_return.sum(axis=0) v = real_return - v e_h_1 = self.get_eh(d, -1 * self.H) e_h_2 = self.get_eh(d, -1 * self.H - 1) return e_h_1 - e_h_2, v #if e_h_1 - e_h_2 > 0.00000: # return Regime.MOMENTUM, v #elif e_h_1 - e_h_2 < -0.00000: # return Regime.MEAN_REVERSION, v #else: # return Regime.NONE, v def get_eh(self, d, begin): sub = d[self.T + begin - 2:self.T + begin - 1 + self.H, :] previous = 0 chis = [] for ii in range(0, self.H): if ii == 0: previous = np.std(sub[ii, :]) continue std_dev = np.std(sub[ii, :]) chi = std_dev - previous previous = std_dev chis.append(chi * chi) return math.pow(sum(chis), 0.5) def initialize(context): context.sids = [sid(14848), sid(3766), sid(24), sid(5061), sid(5692), sid(23709), sid(25006), sid(357), sid(2190), sid(698), sid(8229), sid(3496), sid(39942), sid(20088), sid(16841), sid(26169), sid(24832), sid(1582), sid(4922), sid(23112), sid(4151)] context.strategy = Strategy(21,30,5,4) context.counter = 0 set_slippage(slippage.FixedSlippage(spread = 0.00)) set_commission(commission.PerTrade(0.01)) def handle_data(context, data): if before_close(pd.Timestamp(get_datetime()).tz_convert('US/Eastern'), minutes=10, hours=0): for stock in context.sids: for oo in get_open_orders(sid=stock): cancel_order(oo) order_target(stock, 0) context.strategy.reset() context.counter = 0 return prices = [] for sid in context.sids: prices.append(data[sid].price) regime, valuation = context.strategy.add_prices(prices) if valuation is None: return valuation = np.array(valuation) valuation = valuation.ravel() idx = 0 record(r=regime * 10000) for sid in context.sids: if regime > 0.0001: trade_momentum(sid, context, valuation, idx, data) elif regime < -0.0001: trade_meanreversion(sid, context, valuation , idx, data) idx = idx + 1 if context.counter > 1: context.counter = 0 context.counter += 1 def trade_momentum(sid, context, v, idx, data): #order_target_value(sid, 0) if v[idx] > 0.000 and context.portfolio.positions[sid].amount <= 0: order_target(sid, 3000 // data[sid].price) elif v[idx] < -0.000 and context.portfolio.positions[sid].amount >= 0: order_target(sid, -3000 // data[sid].price) def trade_meanreversion(sid, context, v, idx, data): #order_target_value(sid, 0) if v[idx] < -0.000 and context.portfolio.positions[sid].amount <= 0: order_target(sid, 3000 // data[sid].price) elif v[idx] > 0.000 and context.portfolio.positions[sid].amount >= 0: order_target(sid, -3000 // data[sid].price) def before_close(dt, minutes=0, hours=0): delta_t = datetime.timedelta(minutes=60*hours + minutes) ref = calendar.canonicalize_datetime(dt) open_close = calendar.open_and_closes.T[ref] market_close = open_close['market_close'] return dt > market_close - delta_t