The code rebalances monthly within the "rebalance" procedure, which runs 20 min after the opening of the exchange.
On a monthly bases, only at he beginning of the trading day, before_trading_starts also runs (by default, it tries to run every day), and delivers a fresh set of ranked securities. "Rebalance" then sorts them and keeps the top few (actually, the ones with the minimum value of the combined ranks).
If, however, the short term median is below the long-term one, the resulting list of securities to be ordered is replaced by a single bond ETF.
Throughout the month the selected securities are then ordered in the "buy" procedure, which runs daily (at 30 mins after open).