Back to Community
Can anyone explain why this is profitable?

Leverage is kept fairly low, but the returns just keep going up.

Clone Algorithm
9
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: 5a22a06271860d4428b815b5
There was a runtime error.
5 responses

A lot of the blue under the curve would be from margin. By 1p on the first day, cash had already gone negative by -70%. Consider charting cash. By the third trading day, 2003-01-08, the leverage had hit 2.58. The misconception on leverage has to do with the location where leverage is being recorded, right after ordering. The orders have only been placed rather than having had a chance to go through yet, and the leverage that follows when orders are filled can be related to some partial and completely unfilled orders. That invisibly affects a lot of algos. Additionally here, the ordering at .005 has headroom for 200 stocks, the loop is allowing up to 1000. Try 1.0 / len(context.pipeline_results.head(1000)) in place of .005.

One simple way to see leverage a little better is to move its record to before_trading_start, yesterday's end-of-day leverage. Or you could schedule the record of leverage at market_close to be more sure. Or https://www.quantopian.com/posts/max-intraday-leverage will watch every minute and record any new maximum.

Among these you can find ways to limit to higher volume.
Here I'm tracking those that had the lowest PnL (pulled from a 2003-02-04 summary) for awhile.

"Leverage is kept fairly low, but the returns just keep going up" It seams like the recorded leverage is about 2.4 a lot of the times. (Though this value is probably not entirely accurate as @Blue Seahawk posted). 2.4 isn't 'fairly low'. A general retail investor can typically get only 2.

But, that all said, the returns you're seeing are mostly due to the mathematical magic of leverage and compounding. Take a look at the formula for returns below. RETURN is the 'Cumulative Performance' shown on the backtest.

RETURN = ((1 + RxL)^Y) - 1

Y is years  
R is annual rate of return  
L is leverage  

The backtest for this algorithm was performed over 14.9 years (Jan 3 2003 - Dec 1 2017). The benchmark SPY and the algorithm had a return (Cumulative Performance) of 279% and 1301% respectively. Let's plug those into our formula and see what the annual return would be.

1 + (.09 x 1)^14.9  - 1 = 264%  
1 + (.08 x 2.4)^14.9  - 1 = 1294%

From the above it can be seen that SPY had an average annual return of about 9%. The algorithm had only about 8%. The magic of leverage and compounding are responsible for the returns that "keep going up". One could probably get better returns by simply using 2.4 leverage and investing in SPY.

1 + (.09 x 2.4)^14.9  - 1 = 1743%

To simulate this, attached is the same algo from 7-3-2006 to 12-1-2017 benchmarked not against SPY but against SSO which is a 2x leveraged SPY ETF. (the shorter backtest is because SSO wasn't started until 6-19-2006). Notice that the algorithm simply follows SSO pretty much up until Dec 2016. This shows that there is virtually no alpha. All the 'excess' returns are simply due to leverage.

There's one other issue with the algo. This can be seen in the 'Positions and Gains' tab on 12-30-2016 in the attached backtest. Not sure this exact trade shows up in the original but it's indicative. There was a huge incorrect spike in the price of PVAC from $.15 to $49.00 a share. There may be a fault with Q data but the overarching issue is this stock is VERY thinly traded. Typically less than 1500 shares per day with some huge spikes. (see https://finance.yahoo.com/quote/PVAC/history?p=PVAC).

The backtesting, and specifically slippage models, on Q don't model low liquidity stocks very well (there's a few posts on this here in the forums). Trades in real life probably wouldn't work out the way the backtester models. That said, one flaw with the algorithm is that it doesn't have any filters to exclude low liquidity stocks. Not that a low liquidity strategy couldn't be profitable, rather the profitablity can't be validated well with the Q backtester.

Finally, to get a much more in depth understanding of why an algorithm has good (or bad) returns, use the 'View Backtest Analysis' button on the backtest. This will return a treasure trove of insightful data. Especially after the addition of the Q risk model (see https://www.quantopian.com/posts/introduction-to-the-quantopian-risk-model-in-research) this tool is invaluable. Note that one may need to shorten the backtest time (perhaps by a lot) to get the analysis to run without running out of memory.

Good luck!

oops, forgot to attach the backtest...

Clone Algorithm
0
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: 5a23275d90632d430dc40aec
There was a runtime error.

Simply context.account.leverage and I hesitate to argue with that value.
The chart on the other hand is a little squishy, it does smoothing, zoom in on Dan's backtest using the sliders and it shows leverage of 2.75 by 1-05. The same can be done on the original to verify the 2.58 by 1-08. Contest limit is 1.1. (Click pnl legend item to toggle that one off)

Thanks for your help Dan and Blue.