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).