Ekliptor/WolfBot

Bitmex Position / Order and Price inconsistencies

SpiritOfLogic opened this issue · 14 comments

For testing purposes I ran the default BitmexFisher strategy live. As you can see in the attached screenshots the orders supposedly executed and logged by WolfBot diverge from those actually executed on Bitmex - not only by price but fatally even by side/direction.

Screenshot 2020-03-14 at 09 42 17
Screenshot 2020-03-14 at 09 41 59

Looking more carefully I realized that for those trades (Mar 14, 2020, 8:46:01 AM, Mar 14, 2020, 8:47:03 AM) on the wrong side also the prices diverge - apparently WolfBot received an incorrect market price, submitted a limit order on the "wrong" side of the book (without the "Post-only" flag which should be used for limit orders on Bitmex if they are not supposed to convert to market orders) and thus triggering a market order at the wrong price and wrong side.

Furthermore the order at Mar 14, 2020, 9:00:04 AM was canceled for unknown reasons but appears as a trade in WolfBot history.

The order at Mar 14, 2020, 9:04:04 AM does not appear in WolfBot history at all.

The corresponding logfile excerpt (zipped *.rtf file):

log.rtf.zip

The order side buy or sell is not determined by the rate of the order (regardless if market or limit orders). It is determined by an additional parameter, see Bitmex.ts line 457.

I can't reproduce your error, working for me on bitmex. The log alone looks fine. However I just added some more checks to ensure the amount submitted is always correct (in PortfolioTrader.ts).
Please let me know if the bug persists

Thanks for looking into it - I am not sure that there is no bug though:

`2020-03-14 08:46:00 - verbose: 1min BITMEX candle USD_BTC: high 5465.50000000, low 5458.50000000, close 5459.00000000, volume 375349.00000000, trades 185

2020-03-14 08:46:00 - verbose: 1min BITMEX candle USD_BTC: high 5459.00000000, low 5459.00000000, close 5459.00000000, volume 0.00000000, trades 0

2020-03-14 08:46:00 - info: FishingNet USD_BTC: Skipped increasing SHORT position because the last sell rate was higher: 5470.00000000 > 5458.50000000

2020-03-14 08:46:00 - info: FishingNet USD_BTC: Insufficient volume (relative to avg) to increase existing SHORT position. Volume: 295365.25000000 USD, spike 0.00000000x

2020-03-14 08:46:01 - info: Moved pending USD_BTC order on BitMEX {"success":true,"message":"","orderNumber":"2e566ac2-80ac-86be-abff-a1f2d0d6827f","resultingTrades":{}}`

resulted in a BUY at 5460.0 which was 0.5 above last price and due to missing "Post-only" flag executed as marked order on Bitmex.

But the underlying problem seems to be that "Moved pending" somehow switched the side.

I think as a consequence from this first issue another followup problem happened: Because of the orders on the wrong side the profit was miscalculated and subsequent take profit trailing stops where initiated at wrong profit % and when triggered trade was not in profit in reality so they failed with the error "skipping stop BUY because of required position state: profit".

log2.rtf.zip

That's unrelated because WolfBot doesn't "remember" positions, it constantly queries the exchange to sync positions.

Please try again with latest version and let me know.

I think the problem might stem from this code:

return this.moveMarginOrder(pendingOrder.order.currencyPair, pendingOrder.order.orderID, pendingOrder.order.rate, /*pendingOrder.order.amount*/0, pendingOrder.orderParameters);

Here it does not forward the amount. And then later at

side: amount < 0.0 ? "Sell" : "Buy"

the code you mentioned doesn't work as intended since amount == 0

That's unrelated because WolfBot doesn't "remember" positions, it constantly queries the exchange to sync positions.

Please try again with latest version and let me know.

I guess then indeed there might actually be another bug regarding the retrieval of position profit on Bitmex - will monitor that one. Is WolfBot supposed to see a position on Bitmex that was opened manually (not by WolfBot)?

EDIT: Just reproduced - the profit shown under Strategies (Live trading) is definitely wrong - seems to be the exact opposite. Seems not to update regularly though, therefore hard to tell. Any way to make this update on every tick?

Thanks for the move order issue. I committed a fix. However according to your log there were no moved orders on the first few trades, so this didn't happen with you.

About profit updating:
You are using Bitmex in cross margin mode? The profit in percent is wrong then yes (different from what it sas on Bitmex website), because in WolfBot you just increase your trading capital instead of specifying leverage.
However, if profit is positive or negative must be correct. If not that is a bug. Do you see any Bitmex errors in your log?

About profit updating:
You are using Bitmex in cross margin mode? The profit in percent is wrong then yes (different from what it sas on Bitmex website), because in WolfBot you just increase your trading capital instead of specifying leverage.
However, if profit is positive or negative must be correct. If not that is a bug. Do you see any Bitmex errors in your log?

I restarted WolfBot and currently at least the profit sign is correct but as I said the profit in WolfBot doesn't update:

Screenshot 2020-03-14 at 14 34 13

Screenshot 2020-03-14 at 14 34 20

Thanks for the move order issue. I committed a fix. However according to your log there were no moved orders on the first few trades, so this didn't happen with you.

You are welcome - hope we can figure this out. In the logs I see:

2020-03-14 08:46:01 - info: Moved pending USD_BTC order on BitMEX {"success":true,"message":"","orderNumber":"2e566ac2-80ac-86be-abff-a1f2d0d6827f","resultingTrades":{}}

and

2020-03-14 08:47:03 - info: Moved pending USD_BTC order on BitMEX {"success":true,"message":"","orderNumber":"5cc500ff-59e4-bf18-04e8-3026f7d7a6b7","resultingTrades":{}}

These resulted in the orders on the wrong side on Bitmex:

Mar 14, 2020, 8:46:01 AM | XBTUSD | Buy | 1 | 5460.0 | 1 | 0 | -.-- | 5459.0 | 0.0001 XBT | Limit | Filled | Submitted via API. -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | --

and

Mar 14, 2020, 8:47:03 AM | XBTUSD | Buy | 1 | 5459.0 | 1 | 0 | -.-- | 5458.5 | 0.0001 XBT | Limit | Filled | Submitted via API. -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | --

Hmm now this is also weird:

Screenshot 2020-03-14 at 15 37 58

WolfBot went long...

Screenshot 2020-03-14 at 15 39 09

But it is not in trade history...

Screenshot 2020-03-14 at 15 39 50

Shows position though...

Screenshot 2020-03-14 at 15 40 36

Strategy emitted stop sell but was never executed?

A general note about the stop in FishingNet:

  1. Fishing net increases a position repeatedly to get a better average entry price on that position (also called martingale trading).
  2. after your configured profit target is reached it places a trailing stop on that position to close it with profit.

Your "emitting sell" message is from that profit stop message. You can change profitPercent (the target to place the trailing stop) and trailingStopPerc.
I don't see why it did not get executed though, I would need the log for that. However I just committed a Bitmex USD contracts order quantity fix, so for opening amounts this could hacve been an issue (bitmex won't let you trade with invalid amounts including too many decimal places)

A) This is the log regarding the profit stop message:

I figured that one out I guess - trading was paused. What I think happened is that I paused trading before restarting WolfBot. After the restart for some reason it entered the long but otherwise suspended all trades. Not sure how the initial long went through though...

log3.rtf.zip

B) I still don't understand how those moved pending orders caused buys instead of sells - also "moving" would imply not opening additional orders to me but that was the case.

C) How often is Profit/Loss in the Strategies view supposed to update? Seems extremely rare to me.

trading was paused

Then it shouldn't have made that initial long neither.

B) I still don't understand how those moved pending orders caused buys instead of sells - also "moving" would imply not opening additional orders to me but that was the case.

There was a bug that you found for Bitmex when moving orders amount got 0. Other than that it should not happen.

How often is Profit/Loss in the Strategies view supposed to update?

You can set that value in bit-models/serverConfig.ts. The default is updatePortfolioSec = 120; which loads all open positions from the exchange.

Still any issues or questions? Otherwise I will close it