Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
how to speed up my code

Hi everyone,

WIth the help of the community i was able to make this code work as it is supposed to be. But now i found that the code is terribly slow, even so slow that it can not complete the 2 year backtest within two hours as required for the quantopian open. I sitll need to make the algorithm better, but this is quite hard if every test takes hours. Are there any tricks i can use to make my algorithm more efficient?
Tips for the algorithm itself are ofcourse also welcome!

Kind regards,
Dennis

7 responses

Sorry that you weren't able to make it into the Quantopian Open this month. Looking at your code, I think all you need to do is remove the bulk of your code in handle_data. It looks to me like you are having it do all that work only to order 0 shares, and you commit your real orders inreevaluate(). It's hard for me to tell, but if that isn't code you can remove, I would try to trim your operations in handle_data. Making many calls to history() can be slow, along with transforming that data.

Another option is to have that code in handle_data run only every 15 minutes, for example.

Does that help?

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.

Thank you for the advice! It needs to determine the sthochastic indicator whether or not it needs to sell a stock, not only to buy, you are right that buying is only done in reevaluate. But doing int every 15 min would be a good choice in this case i ques, but i am not sure how i could implement this, is there an easy way to do this or do i need to make a counter like function that counds every 15 minutes?

Regards,
Dennis

Sure. Generally checking every 15 minutes, or even every 5-10 minutes will be essentially the same unless you are making high frequency trades. I think the best way to do this is to use schedule_function, like this:

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  
            schedule_function(  
            myfunc,  
                date_rules.every_day(),  
                time_rules.market_open(minutes=i),  
                True  
            )

def myfunc(context,data):  
    pass

def handle_data(context,data):  
    pass  

(code is from the help doc)

Another thing I just noticed is that you are getting 100 bars of history. Do you need this, or just need the days indicated in your stochastic function? Again, I haven't looked at your code, but you may want to consider only computing a certain part every 15 minutes. If you were really concerned, you may be able to make a modification to getStoch (I think this is the function that is taking up time?) so that it could give you the difference from the previous data given data from a new bar - this way it wouldn't have to go through extra days recomputing what it has already found. Probably not worth the effort though!

Hi,

I was not able to implement your solution for quite some time due to having no time at all for coding. But now i'm trying to implement it, it constantly gives the following error:
Runtime exception: ValueError: offset must be in between 1 minute and 6 hours and 30 minutes inclusive. Even when removing all my own code and just testing this:

def initialize(context):  
  set_universe(universe.DollarVolumeUniverse(floor_percentile=99.2,ceiling_percentile=99.5))  
  # 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  
      schedule_function(  
      myfunc,  
        date_rules.every_day(),  
        time_rules.market_open(minutes=i),  
        True  
      )

def myfunc(context,data):  
  pass

def handle_data(context,data):  
  pass  

This is literally the code copied from the quantiopian document page. Anyone knows why it is giving this error?

It gives the error since i in range(total minutes): starts from i=0 and i%30=0.

changing the logical test to if (i % 30 == 0 )and (i != 0):

may solve your issue

Thank you that worked!

Strange that they do not have this solution in the examples

thasdThants amazing