Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
exception handling help.

I wanted to ask if any could give me quick support on coding issue. I noticed that the whole algo stalls when there is nothing to return on

context.output = pipeline_output('ranked_2000')

I would like the "before_trading_start" to return nothing using the typical exception handling mechanism. Could you please modify the code below so that it would accomplish what I am trying to do? I am very novice programmer and did everything in my work life in Excel (basically learned the logic, but don't know how to program well).

def before_trading_start(context, data):

    try:  
        context.output = pipeline_output('ranked_2000')  
    except:  
        continue  

    ranked_2000 = context.output.fillna(0)  
    log.info("\n" + str(len(context.output)))  
    context.len_long_short = min(len(ranked_2000), 30)  
    context.long_list = ranked_2000.sort(['combo_rank'], ascending=False).iloc[:context.len_long_short]  
    #context.long_list = ranked_2000.sort(['testFactor'], ascending=True).iloc[:context.len_long_short]  
    context.short_list = ranked_2000.sort(['combo_rank'], ascending=False).iloc[-0:]  
    update_universe(context.long_list.index.union(context.short_list.index)) 

    print 'open orders'  
    print get_open_orders()  
3 responses

Hi Ujae,

If you change continue to return in your except: block, I think that should do the trick!

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.

Jamie,

Does it get you out of the entire "def before_trading_start(context, data):" block if you use "return"?

Also, what would "pass" in place of "continue" do? Thank you so much for your help.

Hi Ujae,

return will get you out of the before_trading_start() function whereas pass is effectively the same as "do nothing". So just using pass will mean the lines of code underneath will still be run. continue is meant to be used to skip to the next iteration of a loops (for or while).