Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Weird profit test

First I should explain what I am trying to do
I am trying to run the rebalance function everyday and everytime its ran I am puting 90 percent of my total portfolio in the context.asset()(appl stock) and then I am trying to record the apple stock price everyday using record()

Tell me if I am coding it the wrong way please

There is 2 problems:
1) the apple stock price I am tracking seems to start from 300 and then rose to 600 and droped to 90 afterwards while in the real world apple stock price stayed within 55 - 120 the past 5 years (Is there a bug causing wrong price to be tracked? Or Am I tracking the wrong thing)

2) If we are using the price of what we have here, shouldn't we be making massive lost? As we put most of our portfolio into apple at the start when it was priced 300 and at the end its only worth 90. How come we are making a huge profit in this backtest? (Is the profit calculated in some other ways causing a profit to be shown?)

7 responses

And also how can A stupid algorithm like this be making money and how to spot and how to spot any algorithms that generates profit in the back test but actually doesn't in the real world ?

Apple had a 7 to 1 split in June 2014. The algo beats the market because Apple beats the market. This does exactly what you thought.

Hi Philbert,

Andrew's correct about the split. AAPL underwent a 7-for-1 split in Jun 2014. The price dropped by 7x to reflect the split. If you look at your "Daily Positions and Gains" for your backtest, you'll notice that on the day of the price drop, your position in AAPL will increase by 7x as well, so you won't notice the change in your portfolio value (this is what would have happened in real life).

The question about telling whether a backtest will hold up in live trading is a tricky one. There's no concrete way to know if a strategy that has good looking backtests will hold up in the future. It's always a good idea to test a strategy with what's called "out of sample" data. This means that you test a strategy on data that wasn't yet available to you when you developed it. One way to do this is with paper trading.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Welcome to Quantopian. Let's see if I can help out.

1) Apple really did trade in the 500-700 range earlier this decade. What happened in June of 2014 was a 7-for-1 stock split. So when you log the price as-it-happens, you see the high prices of the earlier years, with no split adjustment.

2) On the back end, Quantopian is simulating that split for you . Your portfolio didn't lose money when the price dropped because your number of shares held also grew by the same factor.

3) Your algorithm isn't really making any investment decisions. It's just going long one stock, and staying long the whole time. If you could have known in 2011 to buy Apple and hold it, you could have made a lot of money - your backtest demonstrates that.

You've got a road in front of you - there is lots of stuff for you to learn. Here are some lectures to get you started:

Check out [Introduction to Pairs Trading][2] for it's good, fun example.
See the The Dangers of Overfitting to learn some of the logical mistakes for you to avoid.

If you aren't understanding the research environment in those lectures, you should start with this intro to research

Good luck!

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Dan,

What about this statement?

From Quantopian Overview

When your algorithm calls for historical price or volume data, it is adjusted for splits, mergers, and dividends as of the current simulation date. In other words, if your algorithm asks for a historical window of prices, and there is a split in the middle of that window, the first part of that window will be adjusted for the split. This adjustment is done so that your algorithm can do meaningful calculations using the values in the window.

I do not see any adjustments to prices in the first part of the window .

Vladimir - the key phrase in the quote you made is "is adjusted. . . as of the current simulation date." There are two dates that matter when thinking about price adjustments: 1) the date of the price, and 2) the date that the price is being considered from.

When the price in early 2011 is simulated in early 2011, it comes back as 340, and that gets written down.
When the price in early 2012 is simulated in early 2012, it comes back as 413, and that gets written down.
When the price in early 2013 is simulated in early 2013, it comes back as 535, and that gets written down.
When the price in early 2014 is simulated in early 2014, it comes back as 555, and that gets written down.

When the price in July 2014 is simulated in July 2014, it comes back as 95, and that gets written down.

The reason you don't see any adjustments in the recorded prices is that the prices are written down at the time of the simulation.

If you want to see the adjustments in action, then within the simulation, in July of 2015, ask for the price for 2011 (using data.history). In that case you will be looking at the date of the price from the view of a date much farther in the future, and you will see the price adjustment for the 7:1 split.

Dan,

May be I need another pair of glasses but I do not see any historical price adjustments in the first part of the window.

if your algorithm asks for a historical window of prices,
2011-08-01 to 2016-07-07

and there is a split in the middle of that window,
2014-06-09 7/1 Stock Split

the first part of that window
2011-08-01 - 2014-06-08
will be adjusted for the split
Returns adjusted (top plot) but not the historical prices.
Do you think any algorithm can do meaningful calculations using the values in the window?