This is not a real algorithm but a demonstration of how one might assemble an algorithm. It breaks down the trading strategy into steps: portfolio selection, portfolio adjustment, and trade execution.
In portfolio selection (implemented by the
StockSelectionAlgorithm) the expected output is a
PortfolioAllocation that is a map of stocks to percent allocations. Value must be from -1 to 1, negative values indicating short positions.
I have a separate optional
StockAllocationAdjustment step that is responsible for applying a hedging strategy. The thought here is that you might want to try the same hedging strategy against more than one algorithm. The default implementation does nothing, you'd be subclassing the
StockAllocationAdjustment class if you really wanted to use this. The hedging strategy is executed at a different frequency than the selection algorithm, potentially. And you might want to keep track of how you've hedged each position in your portfolio, and reverse that hedge as precisely as possible, before re-applying hedging. This isn't as well thought out...
Once you have a
PortfolioAllocation you like, the framework computes the difference between this and the current portfolio, and executes transactions with as few transactions as possible. Unfortunately the ordering strategy is quite poor right now, it uses market value transactions which if used with real money might move the stock value for low liquidity stocks quite a bit. For the backtest, the order once opened remains open for the rest of the day. On the following day the order executor again computes the difference between the current portfolio and the target, and places more orders. I've put in code to make sure we don't exceed the target leverage value as the orders are placed. But this is not well tested for short orders. Also, the leverage limit can be easily exceeded as a shorted position gains value.
I am missing a couple of knobs and controls, for instance an adjustable means for selecting long vs short allocation. There probably are bugs in here as well. But on the whole this approach has worked well for me when dealing with orders for thinly traded stocks that cannot be satisfied in a single day given the 2.5% of volume trading limit that Quantopian imposes in its slippage model.
Hope this is of use to some of you out there.
|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|