Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How would I create a condition for day of the week?

For example, buy stock XYZ on Wednesday and sell on Thursday.

4 responses

Hello Ron,

I expect there is a better way.

Regards,

Peter

Hi Ron,

Looks like Peter set you on the right track. Are you looking to run a backtest on daily or minute bars? There is a subtlety about how the Quantopian backtester works. With daily bars, to have an order filled on Wednesday (at the closing price), you'll need to submit the order on Tuesday. Similarly, for an order to be filled on Thursday, you'll need to submit the order on Wednesday. For minute bars, you have more flexibility, but the logic is the same--an order submitted during minute 0, will be filled during minute 1.

A footnote to all of this is that the backtester is "event-driven" by security, so you can only submit an order and have it filled for a given security if there are event windows in time in the database. So, particularly in daily mode, with thinly traded securities, you might run into problems with your scheme (if it doesn't have additional logic to handle the "non-events").

Grant

Peter and Grant are quite right. Another way to tackle this is like what Taibo did here.

You can run your algo in minutely mode and place your order late in the day. Of course, if you place it at 3:59 you run the risk of the order not being filled at the end of the day if it's not traded.

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 for the information. This is all very helpful.