Zipline live error after manual executions

Hi Brian,

After making a manual trade yesterday while data was unavailable, I'm getting this error during my zipline live trading. It occurs right before before_trading_starts is executed, while zipline is syncing with the blotter:

quantrocket_zipline_1|replaying live order and transaction history for divStrat in account XXXXXXXX to bring Zipline's internal data structures up-to-date
quantrocket_flightlog_1|2021-07-15 06:17:08 quantrocket.zipline: ERROR Traceback (most recent call last):
quantrocket_flightlog_1|2021-07-15 06:17:08 quantrocket.zipline: ERROR   File "sym://qrocket_log_py", line 34, in wrapped
quantrocket_flightlog_1|2021-07-15 06:17:08 quantrocket.zipline: ERROR   File "sym://qrocket_qrzipline_trade_trade_py", line 57, in mule_trade_algo
quantrocket_flightlog_1|2021-07-15 06:17:08 quantrocket.zipline: ERROR   File "sym://qrocket_qrzipline_trade_trade_py", line 179, in trade_algo
quantrocket_flightlog_1|2021-07-15 06:17:08 quantrocket.zipline: ERROR   File "/opt/conda/lib/python3.8/site-packages/zipline/algorithm.py", line 652, in run
quantrocket_flightlog_1|2021-07-15 06:17:08 quantrocket.zipline: ERROR     for perf in self.get_generator():
quantrocket_flightlog_1|2021-07-15 06:17:08 quantrocket.zipline: ERROR   File "sym://qrocket_qrzipline_trade_algorithm_py", line 50, in get_generator
quantrocket_flightlog_1|2021-07-15 06:17:08 quantrocket.zipline: ERROR KeyError: '0:0'
quantrocket_flightlog_1|2021-07-15 06:17:08 quantrocket.zipline: ERROR 

When I made the manual trades, I used the orderRef of the strategy name in IBKR TWS. Now they are showing up in executions with OrderId 0:0, which is causing the error.

I tried registering the manual trades with record_executions, but that just created additional execution entries. The entries with OrderId 0:0 are still there, and I'm not sure how to get rid of them.

My strategy can't load unless I wipe the slate clean by selling all positions and changing the strategy name (which I'd strongly prefer not to do). What else can I do?

You could delete the 0:0 record from the executions database.

I'm not super familiar with sqlite, is there a method like record_executions that can do that?

UPDATE: I googled a quick sqlite tutorial. For anyone else looking to manually remove executions:

  1. Enter the sqlite database as described here in the docs: Usage Guide

  2. The command to remove is as follows. Make sure there's a semicolon at the end, or nothing will happen. If it says the database is locked when you execute, try exiting sqlite (with .exit command) and re-entering.

DELETE FROM Execution WHERE OrderId = '0:0' AND OrderRef = '[your strat]';

I just wanted to mention that sometimes hanging positions can be inside the LatestPosition Table, so if you're querying/checking the blotter you via quantrocket.blotter import download_positions you might see these.

For example: I still had an old execution for the old Sid system & SPY was IB756733 so in order to remove it I had to run this:

DELETE FROM LatestPosition WHERE Sid = 'IB756733' AND OrderRef = 'uncategorized' AND Account = 'XXXXX';

Please CONFIRM your query before pressing enter!