So I have a contest algo that is supposed to be short only on IVV and QQQ, and be dynamically trading a portfolio
of less than 20 long stocks twice a day.
I ran extensive backtests on it, and it had no problems, and it's run with $1M live-paper-contest trading no problem.
Now, after 2 weeks live-paper-contest trading, at$10M, I have a stock that is short $2M-$5M!!...obviously a huge problem...
Debugging a running contest algo is hard...you can hack pyfolio and get information, but that is a time consuming way to go, as I found out.
I ran a backtest separately for the time period that the algo has been live, and the results don't match up...the backtest runs fine!...hmmm...
I looked at the Log file, and I had enough info in there to get an idea of what might be happening, but need some help from you to validate/set-me-straight on the ways of ordering-logic, which I'm obviously not very good at.
Essentially, the algo trades twice a day by computing a portfolio, and liquidating those stocks from the morning portfolio
in the afternoon that don't appear in the afternoon portfolio. I liquidate by using order_target_percent()...in retrospect,
I obviously don't understand the real-life semantics of any of these ordering logic functions!
So one stock, AWGN, gets bought in day K for M%. The next day, in the morning rebalance, it gets sold... I order a target of 0% (e.g -M% )...
but then in the afternoon, it gets sold again at -M%...this is a mistake, and at the end of day, I seem to own around -2*M% of AWGN, due to liquidity problems of getting rid of the stock...and...it looks like to me that the afternoon ordering transaction does not reflect the partial order fill that got done in the morning rebalance...all orders are cancelled at the end of day, so at least we get a fresh start...but now we need to get rid of twice as much of the illiquid AWGN the next day!!...and this doesn't happen. This bad liquidity cycle keeps going, day after day, and before I know it...I'm short $5M on one stock!...disaster!!
Yes, I can fix this problem in lots of ways, and yes, I'm doing something stupid, but I'd like your help with pointing me towards
practices, documentation and code that'll make it safer to algo trade without disaster.
For example, is there code to do what the HFT people do and test the waters on what liquidity there is in the order book by issuing fill-or-kill orders in a probing kinda way?
Any help or comments appreciated.
alan