Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Calculating up and down days

Hi, I have been looking around trying to find a guide how to set up a calculator to count the number of days a share have traded up or down for a number of days but so far without success. For example, if a share have closed higher for three days in a row then the system sells. Can anyone please give me a hand?

7 responses

Hello Jonas,

I don't have time right now to work up a more specific example, but you can have a look at the attached algorithm for guidance.

Grant

You should be aware that dividends, splits and reverse splits can affect an algorithm like this pretty badly.

Really you want to be working with the adjusted closing price. As it is you are comparing today's price in dollars with yesterday's price in dollars (but not adjusted dollars).

I believe the built-in functions (e.g. returns(), mavg(), batch_transform) are all adjusted for splits and dividends. But when you keep a single variable with the previous price it is not adjusted by the backtester.

https://www.quantopian.com/help#ide-dividends
https://www.quantopian.com/help#overview-datasources

I modified Grant's sample algo to use returns() instead of manually recording the previous price. It makes no difference for NFLX in 2012 since they had no dividends. But if you were to backtest farther back you might be surprised by the 2:1 stock split in 2004.

Thanks Dennis,

I'll have to dig into the issues you highlight. I thought that all of the Quantopian data are adjusted:

Our data uses adjusted close prices. That means that the effect of all stock splits and merger activity are applied to price and volume data. As an example: The stock you're looking at is trading at $100, and has a 2:1 split. The new price is $50, and all past price data is retroactively updated 2:1. In effect, that means you can ignore stock splits unless the stock you are looking at will split in the near future - as soon as it does, that will be applied retroactively to all data.

Incorrect?

With respect to dividends, it sounds like some algorithms need to account for them in analyzing price changes.

Grant

@Grant, you're probably right. I was reading "in the near future" as meaning the adjustment took place during the backtest. But it could be referring to data not yet in the database. Which means the adjustment of prices occurs before the backtest is started.

It does make me wonder how live paper trading is handling adjusted prices.

Also, very long backtests might have extremely low prices at the beginning due to the retroactive adjustments. Something to consider if you normally screen out penny stocks. The retroactive adjustment would mean that you are actually screening out some stocks that were not penny stocks at the time the backtest started. This would introduce a type of survivorship bias.

Neat thread.

  • Grant's read of how splits are handled is correct. (That phrase "in the near future" is confusing and I'll fix it).
  • Dennis is right about dividends. Unfortunately, even returns don't catch dividends on day-over-day because of the separation between the ex date and pay date.
  • Dennis is right that Grant's implementation would fall down during a paper trading simulation if there was a stock split. context.previous_price would be recorded pre-split.

I don't think there is a bulletproof way to implement Jonas's idea until we update the dividend code so that you can check for dividends.

But both Grant and Dennis have given examples that might be "good enough" for particular applications.

(my humble opinion)

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.

Thanks Dan,

Jonas, I would just forge ahead regardless of the potential impact of dividends. I could give you a hand, but I don't expect to have much time. Probably the best approach is for you to do the coding, and then post your code for comment. If you can't get the code to run, just paste it into the editor so we can have a look. Or write up the logic in pseudo-code.

Grant