I'm attempting to capture the month end rebalance effect in a Momentum strategy. I'd like the rebalance to occur on the 17th trading day of the month (out of 21). I used the following code in the "def_initialize" and "schedule_function" to offset the rebalance from the "month_start" but in the backtest the trades are consistently occurring later in the month. Any guidance would be appreciated.
def initialize(context):
algo.set_benchmark(algo.symbol("SPY"))
algo.attach_pipeline(make_pipeline(), "pipe")
# rebalance monthly
algo.schedule_function(
rebalance,
algo.date_rules.month_start(17),
algo.time_rules.market_close(),
)