Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Question about function

Hello, my name is Camilo and I am new in Quantopian. I want to know if exists a method to create a function that run only when the algo start (I know initialize does that) but where I can call pipeline_output().
Thanks

6 responses

Hi - if I understand your questions correctly:

RE: function only runs when algo starts: easiest way is with a short circuit (see https://www.quantopian.com/posts/short-circuit-before-trading-start-number-fundamentals). Depending on what exactly you need it for, may need to modify it a bit, but logic is the same

RE: call pipeline_output(): generic code is:

def before_trading_start(context, data):  
    # Access results using the name passed to `attach_pipeline`.  
    results = pipeline_output('my_pipeline')  
    print results.head(5)

    # Store pipeline results for use by the rest of the algorithm.  
    context.pipeline_results = results  

can then use context.pipeline_results in other functions as needed.

gluck

Hi, Umar thanks for your answer, but I want call pipeline_output('my_pipeline') just one time, if I do in before_trading_start, pipeline_output('my_pipeline') will be called every day. Because of that I want a function that only is called at the start of the algorithm only (like initialize) but where I can use pipeline_output.

Hi Camilo,

Unfortunately, there's not currently a way to run a pipeline only once in an algorithm. Even if you schedule it less frequently than daily, it's computed every day. In the future, we would like to make this customizable, but for now, it must be computed every day of the simulation.

Sorry for the inconvenience.

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.

Hello Camilo,

You just need a little flag/switch thingy in your code:

context.first_time = True # this goes in initialize

if context.first_time:  
    # do something  
    context.first_time = False  

The "do something" would be a copy of the pipeline output or whatever (just be sure to create a copy, e.g. http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.copy.html ).

Make sense?

Ah yes, thank you Grant. I think I misread Camilo's intention. Grant is right that you could simply include boolean logic to call pipeline_output once. However, your pipeline will still be run and computed each day. You can just ignore the output!

Thanks both Jamie and Grant for the answers. I want something like Grant said, sorry if I expressed myself poorly, English is my second language. Thanks all of you guys!!