zipline
zipline copied to clipboard
schedule_function every quarter or year?
Hello, I want to use the following schedule_function to instead of running every month, to run every year or 4 months. I am using it for portfolio optimization and I think rebalancing every month is a bit overkill. I looked over the documentation but it doesn't look like there is a year_end() or quarter_end() function.
schedule_function(handle_data,
date_rules.month_end(),
time_rules.market_close())
There is no built-in functionality for quarterly rebalancing, but you can easily make your own rules:
import zipline.utils.events as Events
class IsNMonth(Events.StatelessRule):
"""
Event that triggers if month_number is evenly divisble with n.
To make quarterly rebalancing: date_rules.month_start() & IsNMonth(n=3)
"""
def __init__(self, n=3):
self.n = n
def should_trigger(self, dt):
return dt.month % self.n == 0
Where do I write this code snippet? In events.py or in my algorithm code?
@.***邮箱联系我,谢谢!
cant see ur message.