Yesterday all our funds were taken by some huge whales.

Our funds, mostly mine (but also friends and charity) went from 8 Bitcoins to zero in a few hours in my overly high risk fund.

If the Bitcoin spike had been 40% instead of 43% on the daily we would have made a huge profit, but this hasn't happened since 2011 so nobody anticipated it.

After the initial shock, I decided that losing 67000 USD was not enough. Since the robot had made 720% up until that point, there obviously was moneys to be made. Although I need to go about it in a slightly lower pace.

Looking at what went wrong revealed, as is the case in any sufficiently complex system, errors. Because according to my backtesting, I should have been in the free! I found two serious bugs. One in the robot itself, and another in the simulation. Both only visible under extreme situations like this one.

The bot bug had to do with replacing a taken order too quickly, so it doesn't match the simulation (over-buying). I've code in place to handle that scenario, but obviously not as effective as I would have wanted. Should be a simple fix. The other one was in the simulation, where I've tried to approximate BitMEX's liquidation formula with an oversimplified one parameter, one degree polygon (I've never really understood financial instruments).

I've noted both these bugs before, but didn't think they'd be crucial. Ouch.

**BitMEX's cross margin liquidation formula**
The calculator on the BitMEX page is incorrect according to some, but it's the best I've got. So I

implemented it in Python, which took several hours.

This 100 line giant is nothing I can use in my backtesting for "all the minutes since 2017". I need to optimize it. But it's surprisingly non-linear to the input parameters wallet size, order quantity and price. For polynomial fitting I want to solve

Ax=B

Where x contains samples of wallet size, order quantity, price and a constant, and B the actual liquidation prices as calculated with the calculator above. A will contain the 1-degree coefficients when using this Numpy code:

p,q,r = df[['wallet','contracts','price']].T.values
A = np.column_stack([p, q, r, np.ones(len(p))])
B = df['liquidation-price'].values
(a,b,c,d),_,_,_ = np.linalg.lstsq(A, B, rcond=None)

Projecting on a price of 3000 USD and wallet of 1 Bitcoin and plotting both long and short liquidation prices along with our fit plane doesn't look too good:

Fitting a 2D polynomial to that surface proved more difficult than I had thought. In particular I don't want my poly to result in points below the curve (for longs), as that would yields false negatives. Too close to the entry price, and I can hardly take any trade. Hm. I'm going to have to spend some time on this. But then I'm ready to waste some more hard-earned cash. :-/