Can someone check my logic? Trying to calculate top gainers and losers by percentage each day

I have an algo and I am trying to calculate the top gainers and losers throughout a day where yesterdays_data is the closing price from the day before for all the stocks in the universe, and todays_data is the current price of all of the stocks in the universe.

  # Calculate the percentage gained since closing yesterday for all the stocks
todays_gain_since_open = (todays_data['price'] / context.yesterdays_data['close']) * 100.0
# Get the top 5 gainers and losers for the day
top_gainers = todays_gain_since_open.nlargest(5)
top_losers = todays_gain_since_open.nsmallest(5)
# Here we log the data but could also do more logic
log.info('top gainers {} top losers {}'.format(top_gainers, top_losers))


I think the logic makes sense, but the numbers don't look right. My losers aren't negative as I would expect. Quick checking against other resources like Yahoo finance the numbers don't look right either.
Any ideas what I am doing wrong here?

3 responses

The main reason why gainers aren't positive and losers aren't negative (as you expected) is your 'gain' calculation. The algo above uses

    todays_gain_since_open = (todays_data['price'] / context.yesterdays_data['close']) * 100.0



If one want's positive and negative values, then subtract 1. Like this.

    todays_gain_since_open = ((todays_data['price'] / context.yesterdays_data['close']) -1 )* 100.0



However, there is one other issue. Don't ever store price or volume data to use in subsequent days. The values won't be adjusted for splits or dividends and therefore may give incorrect results. In the algo this is being done with 'context.yesterdays_data'. Since you are using pipeline to get a list of stocks, the simplest way to get the closing price is to add a column in your pipeline. These values will be adjusted as of the current day.

Attached is a revised algo which I believe does what you want.

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.

Thank you Dan! Your numbers look much closer to what I was hoping for. The advice about "Don't ever store price or volume data to use in subsequent days" is good. I will remember that.

The pipeline column you set up

 pipe = Pipeline(
columns={'yesterdays_close': USEquityPricing.close.latest},
screen=stock_universe
)


Makes a lot of sense. Seems obvious now that you point it out, but was counter intuitive looking at it yesterday. I was over thinking what the "latest" closing price of an equity meant in context.

Hi Dan & Aaron,

This is so cool! I would love to trade the same list of stocks. If I want to scan all stocks that are traded on the NYSE and NASDAQ (not just QTradableStocksUS, Q1500US, Q500US), how could I widen the universe like that? I thought I would have to use 'quantopian.pipeline.domain.US_EQUITIES,' but I am very new to python and can't seem to get that to work when cloning this algorithm. Any pointers would be greatly appreciated.

Thank you!
Sean