Here's my initial attempt at an algo for:

Comments/questions/improvements welcome.

Clone Algorithm

47

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 |

# https://www.quantopian.com/papers/risk # Challenge backtest range: 01/04/2014 to 08/29/2018 from quantopian.algorithm import attach_pipeline, pipeline_output from quantopian.pipeline import Pipeline, CustomFactor from quantopian.pipeline.factors import Returns, SimpleBeta, SimpleMovingAverage import quantopian.optimize as opt from quantopian.pipeline.filters import QTradableStocksUS, StaticAssets from quantopian.pipeline.classifiers.morningstar import Sector import pandas as pd WINDOW_LENGTH_REGRESS = 253 # days, length of window for returns forecast model N_STOCKS = 250 # equal long & short, total positions: 2*NSTOCKS def normalize(x): r = x - x.mean() return r/r.abs().sum() def factor_pipeline(): QTU = QTradableStocksUS() sectors = [101,102,103,104,205,206,207,308,309,310,311] returns = Returns(window_length=2, mask=QTU|StaticAssets(symbols('SPY'))) beta = SimpleBeta(target=sid(8554),regression_length=WINDOW_LENGTH_REGRESS,allowed_missing_percentage=1.0) return_SPY = SimpleMovingAverage(inputs=[returns[sid(8554)]],window_length=20) returns_average = SimpleMovingAverage(inputs=[returns],window_length=20) pipeline_columns = {} for s in sectors: alpha = SimpleMovingAverage(inputs=[returns],window_length=WINDOW_LENGTH_REGRESS,mask=QTU&Sector().eq(s))-beta*SimpleMovingAverage(inputs=[returns[sid(8554)]],window_length=WINDOW_LENGTH_REGRESS) returns_forecast = beta*return_SPY + alpha pipeline_columns['sector_'+str(s)] = (returns_forecast-returns_average).zscore() pipe = Pipeline(columns = pipeline_columns, screen = QTU) return pipe def initialize(context): attach_pipeline(factor_pipeline(), 'factor_pipeline') # Schedule my rebalance function schedule_function(func=rebalance, date_rule=date_rules.every_day(), time_rule=time_rules.market_close(), half_days=True) # record my portfolio variables at the end of day schedule_function(func=recording_statements, date_rule=date_rules.every_day(), time_rule=time_rules.market_close(), half_days=True) set_commission(commission.PerShare(cost=0, min_trade_cost=0)) set_slippage(slippage.FixedSlippage(spread=0)) def recording_statements(context, data): record(num_positions=len(context.portfolio.positions)) record(leverage=context.account.leverage) def rebalance(context, data): alpha = pipeline_output('factor_pipeline').sum(axis=1).dropna() alpha = normalize(pd.Series().append(alpha.nlargest(N_STOCKS)).append(alpha.nsmallest(N_STOCKS))) objective = opt.TargetWeights(alpha) order_optimal_portfolio(objective=objective, constraints=[] )