Back to Community
Framework for strategies that trade relatively infrequently

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.

Sunil

Clone Algorithm
20
Loading...
Backtest from to with initial capital
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
# Backtest ID: 5789a7142a3d490f8e6297fc
There was a runtime error.
4 responses

Great code, thanks for sharing!

Thanks for posting this Sunil.
It will take some time to study and make sensible comments.

One thing I noted near the top of your file was: benchmark = symbol('SPY')
If you want to modify the benchmark to be, for example AAPL, then change that to: benchmark = symbol('AAPL')
and add to your initialize section: set_benchmark(benchmark)

Happy to see some interest in my code, and I welcome the feedback. I've been working on my algorithms of course, though I haven't folded some of the recent changes back into the minimal, basic framework. I shall do so when I get the chance. I'm going on vacation starting tomorrow, so I may not get the time.

Peter, thanks for the notes regarding benchmark. I didn't know you could actually change the benchmark that quantopian used in the backtest. In this case, the benchmark variable was something that leaked in from a different use of this algorithm. I shall remove the variable to minimize confusion.

Sunil

I have now updated and fixed up the framework code. I also have a git repo with this code now available:

https://bitbucket.org/smishmash/quantopian-tools

Again, I appreciate any feedback you have to offer. I still have to document and describe the framework, hope to get started with that process soon.

Sunil

Clone Algorithm
20
Loading...
Backtest from to with initial capital
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
# Backtest ID: 57be247d1cf5ea101042a29d
There was a runtime error.