Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Unexplained number of stocks

Hi,
my algo is designed to enter in the market with a fixed number of stocks and it works like this:

for each sector:
- select good and bad stocks
- check if the number of good and bad stocks is enough (greather than c.num_stocks)
- save those stocks in a dict
update universe with those stocks

now for the order part...

  1. check if the stocks is delisted
  2. check if the stocks has open orders
  3. exit all previous positions no matter what (to be shure that i'm flat every rebalance day)
  4. order stocks

The problem i'm stuck with:
As you can see i never have the same amount of positions for the long side and for the short side as i would expect since the correct number of stocks is available for shure.
The leverage is never the same maybe this is related to the number of stocks....

What i have done to avoid this:
filter the pipeline with this

    price_filter   = (price >= 1.0)  
    volume_filter  = (volume >= 100 * 60 * 6.30)  
    mkt_cap_filter =  mkt_cap > 3000e6  
    dollar_volume_filter = dollar_volume > 10**7  

and as i have written before i have avoided open orders stocks and delisted stocks.

Thank you very much for any help..

2 responses

Hi Giuseppe,

One of the issues here is that when you are exiting all of your previous positions, an order is then sometimes placed (either long or short) in one of the positions that was just exited. Since this is being done in the same bar (there is no else before a long or short positions is opened), the ordering functions end up over ordering. Since order_target() or order_target_percent() converts to an order in number of shares behind the scenes, if an order is placed to close a position, and then an order is placed to open a new position, the order for the new position will calculate the number of shares to order based on the current position as opposed to the closed (i.e. 0 shares) position.

I've attached a version of your backtest that fixes this - see line 122 for my change.

Does this make sense?

It also appears that c.longs and c.shorts are not always the same length coming out of before_trading_start. Was this something that you had noticed before? I couldn't really tell from your question!

I'm not sure what you want to do

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.

this really helped me a lot, i thought that exiting all position to start new orders from scratch was the simplest way to go and i was screwing the things up instead :)

However i haven't noticed that c.longs and c.shorts are not the same length every time, but i can correct them.

This is my first attempt to write a mkt neutral algo and the idea is to use fundamental factors to split good stocks from bad ones but inside each sector.
So actually from each sector i expect to find 2 good stocks and 2 bad stocks, finally combine the good stocks all together and go long in them and then do the same thing for the bad ones and short them, so yes c.longs and c.shorts has to be same length.

Thank you very much again :)