Back to Community
How to call a function only ONCE ?

Hello,

Besides having the code in initialize (context), is there any way I can call a function only once?
I know schedule_function(), but this calls the function every month or day, etc.

All I want is the function to be called only once.

Thanks in advanced.

4 responses

Hi Leon,

Assuming you want to call the function on the first day of trading, I suggest setting a boolean to check if the function has been called once already, like in this snippet. Let me know if I can help further.


def initialize(context):  
    #create a boolean value:  
    context.function_bool = True

    schedule_function(func=function,  
                      date_rule=date_rules.every_day(),  
                      time_rule=time_rules.market_open())  


def function(context, data):

    if context.function_bool == True:  
            #function logic goes here  
            context.function_bool == False

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.

Note that if the function doesn't need to be called during the trading day, this may be a better option:

def initialize(context):  
    #create a boolean value:  
    context.function_bool = True

def before_trading_start(context, data):

    if context.function_bool == True:  
            function(context,data)  
            context.function_bool == False

def function(context,data)  
    #function logic goes here  

Also, even for calling the function within the trading day, I wouldn't necessarily embed the execution control as illustrated. For example, consider:

def initialize(context):  
    #create a boolean value:  
    context.function_bool = True

    schedule_function(func=function,  
                      date_rule=date_rules.every_day(),  
                      time_rule=time_rules.market_open())  


def function(context, data):

    if context.function_bool == True:  
            my_function(context,data)  
            context.function_bool == False

def my_function(context,data)  
    #function logic goes here  

This way, the actual function that does the computing is separated from the function call control (I ain't no computer programmer, but it seems that one would want to avoid the "don't do anything" option within a function, and not call the function in the first place).

Great tip. Thank you all =)

Another consideration would be once the function is called to delete it and free up the resources. I don't know how to do this in Python and the Quantopian API but it would seem to be a best practice from a coding standpoint. Perhaps someone can provide guidance?