Going 10% long on top 3-day momentum, and 90% short on 3-day bottom momentum US equities during Coronavirus (COVID-19) outbreak.

Clone Algorithm

642

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 |

from quantopian.algorithm import order_optimal_portfolio from quantopian.algorithm import attach_pipeline, pipeline_output from quantopian.pipeline import Pipeline from quantopian.pipeline import CustomFactor from quantopian.pipeline.data.builtin import USEquityPricing from quantopian.pipeline.factors import SimpleMovingAverage from quantopian.pipeline.filters import QTradableStocksUS import quantopian.optimize as opt class Momentum(CustomFactor): # Default inputs inputs = [USEquityPricing.close] # Compute momentum def compute(self, today, assets, out, close): out[:] = close[-1] / close[0] def initialize(context): # Schedule our rebalance function to run at the start of # each week, when the market opens. schedule_function( my_rebalance, date_rules.every_day(), time_rules.market_open() ) # Create our pipeline and attach it to our algorithm. my_pipe = make_pipeline() attach_pipeline(my_pipe, 'my_pipeline') def make_pipeline(): """ Create our pipeline. """ # Base universe set to the QTradableStocksUS. base_universe = QTradableStocksUS() my_momentum = Momentum(window_length=3) # Filter to select securities to short. shorts = my_momentum.top(10) # Filter to select securities to long. longs = my_momentum.bottom(10) # Filter for all securities that we want to trade. securities_to_trade = (shorts | longs) return Pipeline( columns={ 'longs': longs, 'shorts': shorts }, screen=(base_universe) ) def compute_target_weights(context, data): """ Compute ordering weights. """ # Initialize empty target weights dictionary. # This will map securities to their target weight. weights = {} # If there are securities in our longs and shorts lists, # compute even target weights for each security. if context.longs and context.shorts: long_weight = 0.1 / len(context.longs) short_weight = -0.9 / len(context.shorts) else: return weights # Exit positions in our portfolio if they are not # in our longs or shorts lists. for security in context.portfolio.positions: if security not in context.longs and security not in context.shorts and data.can_trade(security): weights[security] = 0 for security in context.longs: weights[security] = long_weight for security in context.shorts: weights[security] = short_weight return weights def before_trading_start(context, data): """ Get pipeline results. """ # Gets our pipeline output every day. pipe_results = pipeline_output('my_pipeline') # Go long in securities for which the 'longs' value is True, # and check if they can be traded. context.longs = [] for sec in pipe_results[pipe_results['longs']].index.tolist(): if data.can_trade(sec): context.longs.append(sec) # Go short in securities for which the 'shorts' value is True, # and check if they can be traded. context.shorts = [] for sec in pipe_results[pipe_results['shorts']].index.tolist(): if data.can_trade(sec): context.shorts.append(sec) def my_rebalance(context, data): """ Rebalance weekly. """ # Calculate target weights to rebalance target_weights = compute_target_weights(context, data) # If we have target weights, rebalance our portfolio if target_weights: order_optimal_portfolio( objective=opt.TargetWeights(target_weights), constraints=[], )