I use the schedule function to trigger my strategy just before close:
schedule_function(
func=my_handle_data,
date_rule=date_rules.every_day(),
time_rule=time_rules.market_close(minutes=2),
half_days=True
)
I also have the handle data function do nothing:
def handle_data(context, data):
pass
I am expecting to only fill orders in the last 2 minutes of the day, because my universe is the 1% most liquid stocks by dollar volume.
I am however getting a transaction every minute. It is not simply filling the remainder of an incompletely filled order from the previous day, because I get both buys and sells of the same security.
This is how I place orders:
for security in largest:
order_target_percent(security, 0.5/len(largest))
for security in smallest:
order_target_percent(security, -0.5/len(smallest))
And here are just some of the orders from a random day:
date security transaction quantity last sale price $amount
8:16 AM LLTC SELL -1175 38.49 -45223.40
8:17 AM LLTC SELL -1075 38.52 -41406.85
8:18 AM LLTC BUY 3825 39.05 149373.90
8:19 AM LLTC BUY 625 39.05 24407.50
8:20 AM LLTC BUY 4032 39.1 157663.30
8:21 AM LLTC BUY 325 39.13 12718.23
8:22 AM LLTC BUY 1600 39.12 62596.80
8:23 AM LLTC BUY 550 39.11 21512.15
8:24 AM LLTC SELL -1075 38.57 -41459.53
8:25 AM LLTC SELL -825 38.58 -31826.02
8:26 AM LLTC SELL -1815 38.59 -70035.40
8:27 AM LLTC BUY 2387 39.12 93386.60
8:28 AM LLTC BUY 750 39.11 29334.75
8:29 AM LLTC BUY 1950 39.13 76309.35
8:30 AM LLTC BUY 1125 39.17 44069.63
8:31 AM LLTC BUY 3662 39.22 143634.63
8:32 AM LLTC BUY 8566 39.37 337277.68
8:33 AM LLTC SELL -8647 38.84 -335814.89
8:34 AM LLTC SELL -18070 38.98 -704458.95
The problem usually manifests itself suddenly during the backtest (maybe after a month in) and aggregates to enormous positions (2000+%). Most times it is just one security before I stop the simulation, but sometimes it is two that have this behavior. Has anyone else had this problem? and if so what is the solution?