Back to Community
end of day

How do you determine when end of day is?
I would like to allow vwap orders every half hour like this
end_date=now + timedelta(minutes=30)))

but the backtest crashes every once in awhile saying that I am placing vwap orders that will finish after the close

5 responses

Someone gave me this method to determine if its near close. Maybe it can be of use to you.

def before_close(dt, minutes=0, hours=0):
delta_t = datetime.timedelta(minutes=60*hours + minutes)
ref = calendar.canonicalize_datetime(dt)
open_close = calendar.open_and_closes.T[ref]
market_close = open_close['market_close']
return dt > market_close - delta_t

how do you import calendar?

from zipline.utils import tradingcalendar as calendar

thank you

You can also use schedule_function to run the code every 30 minutes. It will be smart enough not to run the function if it's the end of day. Here's an example:

def initialize(context):  
  # For every minute available (max is 6 hours and 30 minutes)  
  total_minutes = 6*60 + 30

  for i in range(total_minutes):  
    # Every 30 minutes run schedule  
    if i % 30 == 0:  
      # This will start at 9:31AM and will run every 30 minutes  

def myfunc(context,data):  

def handle_data(context,data):  

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.