Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
algorithm execution based on list of minutes

An illustration of how to restrict code execution based on a list of hard-coded specific historical minutes (in this case, arbitrarily chosen). Ultimately, I'm trying to sort out how to incorporate external data into Quantopian. One way to synch external time series is with the (inefficient) for-loop approach I use. For each minute of the backtest, the code iterates through the list of historical minutes I've provided looking for a match. If a match is found, then a buy/sell order is submitted (as an illustration only).

Eventually, I'd like to provide the backtester with a file of time-series data with UTC minutely time stamps.

3 responses

Cool, thanks for exploring this. It's not an easy problem.

The obvious complication with the for-loop approach is if you had a list of thousands of events. But I'm not sure if there is a good and simple alternative to that (maybe a dict if there is associated information to each event).

One question, from line 19 to 25: why do you parse out the datetime object and recreate it?

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.

Thomas,

Regarding line 19 to 25, I tried a simpler approach (event_time = data[context.security].datetime) and got an error:

Runtime exception: TypeError: can't compare offset-naive and offset-aware datetimes

The error is generated on line 32 of the code above, when event_time is compared to et (an event time in my list).

Please let me know if there is a resolution.

Thomas,

I removed the for-loops and used searches instead:


    #algorithm #1 execution  
    if event_time in context.alg1_days and notional < context.max_notional:  
        order(context.security,+100)  
        log.debug(event_time)  
        log.debug('Buy order submitted.')  
    #algorithm #2 execution  
    if event_time in context.alg2_days and notional > context.min_notional:  
        order(context.security,-100)  
        log.debug(event_time)  
        log.debug('Sell order submitted.')  

The code works the same. Any idea how efficient it is in Python to do an "if x in y" search? Must be better than my original for-loop approach.

You might be interested in this related discussion, as well.