Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Misleading results?

How could it be that my algorithm buying Facebook stocks is outperforming itself if I set Facebook itself as a benchmark (Total Returns 0.77 vs. Benchmark returns 0.58)?

6 responses

It's an example of the achilles heel: Margin becomes illusory profit.

Notice in the beginning the returns curve is below bench. That's because not all of the initial capital had been spent yet.
Then when cash goes negative, returns appear to rise above the benchmark, however the broker is owed then, so the profits are not real.

If you negate commissions you'll see the pure version of that.

set_commission(commission.PerTrade(cost = 0.0))

The simplest way to protect from letting accidental margin lead one to believe that a change is positive when it really isn't (and visa-versa) would be to always record cash:

record(cash = context.portfolio.cash)

To illustrate the point in terms that might be easier to understand, this backtest appears to be a terrific strategy at 20% profit in 20 days.
Take a look at the code by clicking on the 'Source Code' tab.

Simply recording cash, while advisable, has some limitations. A precise solution and more info can be found here and maybe start with the minimal version. This adds something like that to your code (hover over the custom chart) and commissions are off. I would have expected the two traces in the main chart to cross over at the point where all capital was used, not sure why it isn't so. Note however, when 100% of initial capital is used, pvr matches the Algorithm trace. It is at all times indicating what I would call risk-adjusted returns except that term appears to be taken already in the industry and tied to benchmarks.

It is incredibly useful to be able to see when a change is good or bad. My real money algo has more than doubled in 5 months. Perhaps the tool has something to do with that, being able to see clearly.

Thank you Blue, that was crystal clear!

What do you think about this Blue?
I limited the amount of stock to buy so that I never go below zero with cash and resell just the stock I already have in my portfolio. Still my system outperforms the benchmark itself.

Some comments in the code. This adds a loop so you can experiment with more than one stock to some degree. Think toward pipeline.
You'll want to direct future questions to the community as my personal fitness haha trainer fee is market. :|
The scheduling of cash_low is time-consuming so you can turn that off by uncommenting the break, to prevent that, the other loop.
Hopefully you'll find some of this exercise educational, useful.

Thanks man, really appreciate that!