zipline icon indicating copy to clipboard operation
zipline copied to clipboard

schedule_function every quarter or year?

Open sword134 opened this issue 4 years ago • 4 comments

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

sword134 avatar Jan 15 '21 12:01 sword134

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

everling avatar Jan 19 '21 14:01 everling

Where do I write this code snippet? In events.py or in my algorithm code?

KrishnaRachh avatar Dec 15 '22 13:12 KrishnaRachh

@.***邮箱联系我,谢谢!

andycwang avatar Dec 15 '22 13:12 andycwang

cant see ur message.

KrishnaRachh avatar Dec 15 '22 13:12 KrishnaRachh