Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Want to Have an 'If Statement' Check for Time Left in Trading Day

Is it possible to write a check that says something along the lines of:

'If there is one hour left in the trading day' : do something

get_datetime() works to get me the current time, but I figure for half days and other occasions I would like to be sure that my check works (instead of just hardcoding 3pm EST into the algorithm)

Thanks!

5 responses
    schedule_function(OneHourLeft, date_rules.every_day(),  
        time_rules.market_close(minutes = 60),  
        half_days=True  
    )

Anything you do in the OneHourLeft function will be done when there is one hour left in the trading day. schedule_function has to go in def initialize(context): function.

Hi Charles,

Thanks for the response, but I think I was unclear in what I was asking. I would like my algorithm to run until 1 hour is left in the trading day, regardless of half-days or any other early closure. In other words, I would like it to begin at market open and run until 1 hour is left in trading. I believe 'schedule_function' sets it so that the function, 'OneHourLeft' in this case, begins running once their is 1 hour left in trading. Is there a way to have it work as how I described in the first sentence? Thanks.

Hi Omar,

Building on Charles' suggestion, you could use schedule_function to set a flag on context to mark the fact that the day is now 1 hour before market close. You could schedule one function at market open that set something like context.run_strategy to True, and another one that sets it to False with 1 hour left in the day. Then, in handle_data you could run your algo logic only when context.run_strategy is True.

Does this help?

Jamie

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.

Charles Witt's approach is the best. Don't go down the rabbit hole using 'if statements'. To run until the last hour then simply schedule functions as needed. While you could manually schedule them an easier approach is to use a loop. Maybe something like this..


def initialize(context):  
    """  
    Called once at the start of the algorithm.  
    """  
    # Set the start time and stop time (both in minutes)  
    # Here we are working backwards from market_close.  
    # To stop 1hr before market_close then set stop_time to 60  
    # There are typically 390 trading minutes in a day  
    # so set start_time to 390 if one wants to start at market open.  
    # Note that the schedule function ignores any pre-market schedules  
    # so this works fine for half days.  
    # Also set the frequency in minutes (in this case every 5 minutes)  
    # Frequency can be set to 1 to mimic the behavior of handle_data  


    start_time, stop_time, frequency = 390, 60, 5  
    for minutes_offset in range(stop_time, start_time, frequency):  
        schedule_function(  
            my_function,  
            date_rules.every_day(),  
            time_rules.market_close(minutes=minutes_offset),  
            half_days = True)

def my_function(context,data):  
    """  
    Do any logic here. Will be executed each time it's scheduled  
    """  
    log.info(get_datetime('US/Eastern').time())  


See the attached algorithm to see this in action. Check the logs which show the times that the scheduled function was executed.Note the 11-25-2016 date in the logs. This was a half day and the algorithm ran as expected until 12:00 (since the market closed at 1:00).

Hi Jamie and Dan,

Both ideas are brilliant! I look forward to testing them both, and thanks for the quick responses! This really simplifies what I was trying to do in my algorithm.