cryptoeax / arbbot

Arbitrator, a bitcoin/altcoin arbitrage trading bot
https://gitter.im/cryptoeax-arbbot/Lobby
GNU General Public License v3.0
199 stars 78 forks source link

Double check the trade matching for multi-trade orders #41

Closed cryptoeax closed 6 years ago

cryptoeax commented 6 years ago

From https://gitter.im/cryptoeax-arbbot/Lobby?at=5a39ea0200d569114bc90165:

Scorpic1 @Scorpic1 23:41
Hello! @cryptoeax P & L now has to correctly calculate if the order is executed in several parts, more than one?

POT/BTC    Sell    Exchange    0.00001936    52.00000000    0.00000252 BTC (0.25%)    0.00100420 BTC    2017-12-19 20:43:38
POT/BTC Sell Exchange 0.00001936 93.74455999 0.00000454 BTC (0.25%) 0.00181035 BTC 2017-12-19 20:43:38

Scorpic1 @Scorpic1 23:46
But bot calculate in P&L only first order 93.74455999 POT. Each 52.00000000 POT not calculate P&L
2017-12-19 23:44:46 POT BTTRX PLNX 93.74455999 0.00280562 0.00181489 0.00099073 0.01000000 0.00100247

Scorpic1 @Scorpic1 23:52

20:43:37: TRADING POT-BTC FROM BITTREX TO POLONIEX

= FUNDS ============================================
SOURCE : 0.03059030 BTC
TARGET : 145.74655999 POT

= SIMULATION =======================================
TARGET MAX : 145.74655999
SOURCE MAX : 519.75051975
BUY RATE : 0.00001924
BUY AMOUNT : 145.74655999
BUY PRICE : 0.00281117
SELL RATE : 0.00001936
SELL AMOUNT : 145.74455999
SELL PRICE : 0.00281456
PROFIT : 0.00000339

= TRADE ============================================
SELL ORDER : 145.74455999 POT @ 0.00001888 BTC
BUY ORDER : 145.74655999 POT @ 0.00001972 BTC

20:43:38: Placed sell order (POLONIEX ID: BTC_POT:20581640831)
20:43:41: Placed buy order (BITTREX ID: 20137711-8c6b-4079-967f-8ac3dfbffff4)
20:43:41: Waiting for order execution...
20:44:41: [POLONIEX] Cancelling order BTC_POT:20581640831
20:44:42: [BITTREX] Cancelling order 20137711-8c6b-4079-967f-8ac3dfbffff4
20:44:42: Checking trade results (1)...
20:44:43: Received 145.74655999 POT from the exchange while our wallets show a balance difference of 145.74655999 (145.74655999 - 0.00000000 finished deposits)
20:44:45: Received 93.74455999 POT from the exchange while our wallets show a balance difference of 145.74455999 (145.74455999 - 0.00000000 finished deposits)
20:44:46: TRADE SUMMARY:
PAIR: POT vs BTC
DIRECTION: BITTREX TO POLONIEX
BALANCES BEFORE / AFTER

BITTREX:
POT: 0.00000000 => 145.74655999 = 145.74655999
BTC: 0.03059030 => 0.02777767 = -0.00281263

POLONIEX:
POT: 0.02010101 => 0.00200000 = -145.74455999
BTC: 0.02010101 => 0.02191136 = 0.00181035

TOTAL:
BTC: -0.00100228
POT: 0.00200000
POT(-tx) : 0.00000000

(Transfer fee is 0.00200000)

20:44:46: Calculated P&L: -0.00100247
20:44:46: Transfering 145.74655999 POT BITTREX => POLONIEX
cryptoeax commented 6 years ago

@Scorpic1 do you have other similar examples by any chance?

Scorpic1 commented 6 years ago

@Scorpic1 do you have other similar examples by any chance?

12:21:16: TRADING LBC-BTC FROM POLONIEX TO BITTREX

= FUNDS ============================================
     SOURCE :     0.02291558 BTC
     TARGET :   301.77281291 LBC

= SIMULATION =======================================
 TARGET MAX :   301.77281291
 SOURCE MAX :   220.94564737
   BUY RATE :     0.00004526
 BUY AMOUNT :   220.94564737
  BUY PRICE :     0.01002500
  SELL RATE :     0.00004600
SELL AMOUNT :   220.93564737
 SELL PRICE :     0.01013763
     PROFIT :     0.00011263

= TRADE ============================================
 SELL ORDER : 220.93564737 LBC @ 0.00004485 BTC
  BUY ORDER : 220.94564737 LBC @ 0.00004639 BTC

12:21:17: Placed sell order (BITTREX ID: c22b138a-2315-4a9c-8df0-3a63085420ec)
12:21:18: Placed buy order (POLONIEX ID: BTC_LBC:29179851107)
12:21:18: Waiting for order execution...
12:22:18: [BITTREX] Cancelling order c22b138a-2315-4a9c-8df0-3a63085420ec
12:22:25: [POLONIEX] Cancelling order BTC_LBC:29179851107
12:22:27: Checking trade results (1)...
12:22:31: Received 158.28705072 LBC from the exchange while our wallets show a balance difference of 220.39328326 (220.39328326 - 0.00000000 finished deposits)
12:22:31: WARNING: Got an unrelated trade while trying to perfrom post-trade tasks: %s of %.8f %s at %.8f, saved but will ignore
12:22:34: Received 220.93564737 LBC from the exchange while our wallets show a balance difference of 220.93564737 (220.93564737 - 0.00000000 finished deposits)
12:22:37: TRADE SUMMARY:
PAIR: LBC vs BTC
DIRECTION: POLONIEX TO BITTREX
BALANCES BEFORE / AFTER

POLONIEX:
  LBC:     0.00000000 =>   220.39328326 =   220.39328326
  BTC:     0.02291558 =>     0.02231271 =    -0.00060287

BITTREX:
  LBC:     0.02777767 =>    80.83716554 =  -220.93564737
  BTC:     0.02777767 =>     0.03784918 =     0.01007151

TOTAL:
        BTC:     0.00946864
        LBC:    -0.54236411
  LBC(-tx) :    -0.55236411

(Transfer fee is 0.01000000)

12:22:37: Calculated P&L: 0.00946772
12:22:37: Transfering 220.94564737 LBC POLONIEX => BITTREX

Bot sell 220.39328326 LBC on Bittrex and buy on Poloniex in two order 13.28705072 LBC + 207.65859665 LBC

History trade from Poloniex:

Market Type Category Price/Share Amount Fee Total Transaction Date
  LBC/BTC Buy Exchange 0.00004526 207.65859665 0.51914649 LBC (0.25%) 0.00939862 BTC 2017-12-20 12:21:17
  LBC/BTC Buy Exchange 0.00004526 13.28705072 0.03321763 LBC (0.25%) 0.00060137 BTC 2017-12-20 12:21:17

History trade from Bittrex

CLOSED DATE OPENED DATE TYPE BID/ASK UNITS FILLED LBC UNITS TOTAL LBC ACTUAL RATE COST / PROCEEDS
12/20/2017 03:21:18 PM 12/20/2017 03:21:18 PM Limit Sell 0.00004485 220.93564737 220.93564737 0.00004569 0.01007151

P&L from bot:

Date Coin Source Target Amount Bought Sold Revenue Tx Fee Profit/Loss
2017-12-20 15:22:37 LBC PLNX BTTRX 220.93564737 0.00060137 0.01009675 0.00949538 0.02000000 0.00946772

I see calculate is not correct.

12:22:31: WARNING: Got an unrelated trade while trying to perfrom post-trade tasks: %s of %.8f %s at %.8f, saved but will ignore

what does this message mean?

Thanks.

cryptoeax commented 6 years ago

Thanks!

That message is a debug log output that basically tells me the trade matching engine isn't 100% bullet-proof yet. :-) But it's mostly harmless. I have fixed the bot to emit a proper message without the "%" garbage in it but this message was logged when you were running an older buggy version of the bot.

All side effects of testing a non-stable branch, but they're all benign and nothing to worry about. Thanks for sticking with me. :-)

cryptoeax commented 6 years ago

This should now be fixed on the pl-rewrite branch.