Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Selecting A Day of the Week

I have a feeling my algorithm executes on Mondays, but I'd like it to execute on Wednesdays. Does anyone know how I can do that?

1 response

Hi Addison,

The Python datetime library makes working with dates pretty simple, I'd recommend importing and using that library along with Pandas in most algorithms.

Attached is an example (in minute mode) of a rebalance algorithm that rebalances every week on Wednesday at market open. I'm using the date.weekday() attribute, which returns the day of the day of the week as an integer starting from Monday as 0 (Wednesday == 2).

The relevant snippet for timing the rebalance is:

# Get the current exchange time, in local timezone  
    exchange_time = pd.Timestamp(get_datetime()).tz_convert('US/Eastern')  
    if exchange_time.weekday() == 2: #If today is Wednesday, rebalance  
        rebalance(context,data,exchange_time)  
        context.rebalance_date = exchange_time #update the state variable holding the last rebalance date  

Hopefully this is in line with what you were looking for - if not let me know and I'm happy to help iterate your algo to get it working the way you'd like.

Best wishes, Jess

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.