Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Condition(s) to Run a Function

Hello Folks,

I would like to run a function on the premise that a condition is met, I feel like this should be relatively simple though I cannot find the syntax in the documentation. I have shown a simple example below, suppose I want to run the rebalance function if and only if the context.market_cap is above some value. How would I incorporate this conditional into the rebalance function?

 def filter(context, data):

      context.market_cap = '''code here to compute market data for some pre-defined securities'''

 def rebalance(context, data):  

      '''Code/syntax written here'''

      return '''some variable stored'''  
2 responses

Not sure what your programming background is, but the logic is basically:

if some condition is met, then
do something
otherwise
do something else

https://www.tutorialspoint.com/python/python_if_else.htm

More on....python if..else