Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Timed Restriction On Re-Buying Stock

Hi, I'm new to Quantopian and I have just been playing around with coding a simple algorithm and I was just wondering how I might go about making it so that a stock that has just been sold can't be repurchased for a set time period, eg;

  • if a stock is sold that specific stock can't be re-bought for 60 minutes?

Thanks.

5 responses

Not too sure. Anyone has suggestions?

Chris and Andy, you'd probably have to store this state yourselves in your algorithm. You could store a dictionary of stocks -> times, update the dictionary when you place sell orders, and check the dictionary first before placing buy orders.

Another way to do it is to take advantage of the fact that the order method returns an order id. You can get then store the ids and look up the order using get_order(id) when you need to. This will probably be less performant than storing the times yourself.

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.

Going off of Jean's example you can store your variables in a dictionary like so

def initialize(context):  
    context.stocks_stored = {}

def handle_data(context, data):  
    if #order logic:  
        order(sid(24), 0)  
        context.stocks_stored[sid(24)] = 0  
    context.stocks_stored[sid(24)] += 1  

This would keep track of how many bars you've currently held the stock for and so you can check if context.stocks_stored[sid(24)] > 30 before placing an order again, and remove it from the dictionary afterwards.

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.

Hi Chris,

I've been fiddling with this one, and to do it right, I think you need to work off of the orders. In the case of slippage or thinly traded securities, you can't assume that the order will be filled immediately after the order was submitted. I've attached an example. If you look at the log output, you'll see that BND does not get completely filled immediately. Also, note that the order object has a 'dt' key which applies a datetime stamp to changes in the 'filled' value. So, once a sell order is fully executed, you can apply a timer for that security, to delay any follow-on orders (e.g. for 60 minutes).

Also, note that if you count minutely bars to determine the delay, it is different from computing a time difference. For example, if your sell order is filled near the end of the day, then you may want to allow follow-on trading immediately the next trading day, rather than waiting.

If you haven't sorted it out, just let me know and I'll keep pecking away at an example.

Grant

Thanks so much for the help!

:)