Cinderella-Man / hands-on-elixir-and-otp-cryptocurrency-trading-bot-source-code

Resources related to the "Hands-on Elixir & OTP: Cryptocurrency trading bot" book
https://elixircryptobot.com
75 stars 24 forks source link

Order does not exist #1

Closed noozo closed 3 years ago

noozo commented 3 years ago

Hi, thanks for sharing the code for the book.

I was doing some experiments and this happened:

18:53:28.073 [info]  Rebuy triggered for XRPUSDT by the trader(1612896801530)

18:53:28.073 [info]  Starting new trader for XRPUSDT

18:53:28.074 [info]  Initializing new trader(1612896808074) for XRPUSDT

18:53:28.082 [info]  The trader(1612896808074) is placing a BUY order for XRPUSDT @ 0.47422, quantity: 24.2

18:53:28.756 [error] GenServer #PID<0.428.0> terminating
** (MatchError) no match of right hand side value: {:error, %{"code" => -2013, "msg" => "Order does not exist."}}
    (naive 0.1.0) lib/naive/trader.ex:117: Naive.Trader.handle_info/2
    (stdlib 3.13.2) gen_server.erl:680: :gen_server.try_dispatch/4
    (stdlib 3.13.2) gen_server.erl:756: :gen_server.handle_msg/6
    (stdlib 3.13.2) proc_lib.erl:226: :proc_lib.init_p_do_apply/3

Any idea in what situations the order can fail to be found?

Thanks!

noozo commented 3 years ago

Wonder if someone posted the buy order, but immediately deleted it but the event still reached my streamer. In those situations i seem to end up with one of the workers stuck. What would you do in that situation?

Cinderella-Man commented 3 years ago

Hello @noozo

Thanks for raising an issue. Would you be able to point me to which branch are you using? I can see that rebuy is happening so it needs to be after the 9th chapter. Did you make any modifications? Looks like you are using the real Binance exchange(not BinanceMock)?

Cinderella-Man commented 3 years ago

Hello again @noozo

It happens as you place a buy order and it gets filled instantly (you instantly get a message through WebSocket with matching buyer_order_id), at that moment the Naive.Trader (at line 117) tries to fetch the order from Binance but Binance hasn't had time to persist it yet.

Here's the full explanation: https://dev.binance.vision/t/faq-error-message-order-does-not-exist/46

In normal trading, this would happen at stop loss and when using MARKET order (buying/selling at whatever price is best)

An alternative implementation that would fix this issue would be to drop fetching completely and calculate the remaining amount inside the bot as it gets trade events. This is quite an error prone approach and requires taking into consideration fee as well as fee currency(you can pay a smaller fee(0.075%) if you have BNB tokens).

I hope that helps