Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Variable trade schedluing at close

Hi, I'd like to set different delays of "buy market on close" by the size of the portfolio (see below). However, starting_dollars is only called once with Initialize. Any thoughts?

def initialize(context):
context.stock = sid(stock)
starting_dollars = context.portfolio.portfolio_value
limit = starting_dollars / 25000

schedule_function(close_positions, date_rules.every_day(), time_rules.market_close(minutes = limit))  
1 response

Maybe if you make the global variables set to zero under initialize (just to establish their existence):
context.limit = 0
context.starting_collars = 0

Then in a "before trading start" function or create a new function that runs at market open, put in your formulas so that they are reset daily.

def initialize(context:
context.starting_dollars = 0
context.limit = 0

schedule function(close_position, date_rules.every_day(), time_rules.market_close(minutes = context.limit)

def before_trading_start(context, data):

context.starting_dollars = context.portfolio.portfolio_value
context.limit = context.starting_dollars / 25000

I think this will allow you to reset your variables each day and make them globally available in any functions

Not sure it will work, but give it a try.