chrisleekr / binance-trading-bot

Automated Binance trading bot - Trade multiple cryptocurrencies. Buy low/sell high with Grid Trading. Integrated with TradingView technical analysis
MIT License
5k stars 1.09k forks source link

REST API & Websockets #172

Closed andferno closed 3 years ago

andferno commented 3 years ago

@chrisleekr , you have mentioned that API limit affects Web GUI refresh rate because of max credit reached. But you are also using Websockets that have different limits (24h session and 5 requests per second). So I would be happy understanding how Websockets are used to minimize the impact of refreshing in Web GUI refresh. Sometimes I see currency rising on Binance and the Web GUI stuck in an older position when a perfect selling price is reached.

Please explain the logic under API/Websockets usage to be able to help into that matter. I really love this bot and would be happy to improve it..

Thanks.

chrisleekr commented 3 years ago

Hey @andferno

you have mentioned that API limit affects Web GUI refresh rate because of max credit reached

API limit does not impact Web GUI as it is completely separated.

But you are also using Websockets that have different limits (24h session and 5 requests per second).

Binance Websocket does not have a limit except a maximum stream which is not our case. Refer: https://github.com/binance/binance-spot-api-docs/blob/master/web-socket-streams.md#websocket-limits

Sometimes I see currency rising on Binance and the Web GUI stuck in an older position when a perfect selling price is reached.

Ok, let me explain

  1. The bot is relying on the WebSocket stream, which means it will receive the candle when Binance sends. Once receive any candle, it will store in the cache - https://github.com/chrisleekr/binance-trading-bot/blob/feature-monitoring-simultaneously/app/server-binance.js#L40
  2. The WebSocket does not send the data immediately as you can imagine, but it is almost instant.
  3. The coin will be checked buy/sell/wait actions every second; however, it is also depending on your server's performance. It may take 1-5 seconds to process the coin.
  4. During checking the indicator for buy/sell, the job called trailingTrade will retrieve candle cache which is saved from WebSocket - https://github.com/chrisleekr/binance-trading-bot/blob/feature-monitoring-simultaneously/app/cronjob/trailingTrade/step/get-indicators.js#L46
  5. Once the process is finished, then it will save into cache with all processed information - https://github.com/chrisleekr/binance-trading-bot/blob/feature-monitoring-simultaneously/app/cronjob/trailingTrade/step/save-data-to-cache.js#L22
  6. In the frontend, it will call the bot every second to get the latest data - https://github.com/chrisleekr/binance-trading-bot/blob/feature-monitoring-simultaneously/public/js/App.js#L49
  7. The bot receives WebSocket request and retrieve all symbols' cached data and return to the frontend - https://github.com/chrisleekr/binance-trading-bot/blob/feature-monitoring-simultaneously/app/frontend/websocket/handlers/latest.js#L96

You can see there are some gaps between the live price and the timing when it is calculated. It's in fact almost instant (less than 1 sec, max 5 secs gap). But when you see the price in the frontend, that is definitely old data, because that is after processing.

See the below screenshot of my bot. It is also old price.

image

I really love this bot and would be happy to improve it..

Definitely, if I can make it faster, that would be great.

andferno commented 3 years ago

Wow! Very detailed information of the process, thank you. It's very simple and optimized.

What I was suffering is that currency price is stuck for more than 30 seconds (sometimes minutes) and selling opportunities are lost.

I'll try to give some ideas, but can't assure any good result. I'm not so good at this.

chrisleekr commented 3 years ago

@andferno

If your question is answered, let's close the issue.