bmino / binance-triangle-arbitrage

Detect in-market cryptocurrency arbitrage
MIT License
1.06k stars 337 forks source link

How much latency is expected to make a profit? #161

Closed winChawakorn closed 3 years ago

winChawakorn commented 3 years ago

Hi, I'm really interested in your project. I tried it with a server with latency around 20 ms. I think it's pretty fast, but still can't make a profit. It traded 12 times with 1 win and 11 lose.

Do I need to reduce the latency or something? Can you please advice?

This is a lose result with age 39ms, and done trading in 78ms.

[5/2/2021, 3:04:01 PM] INFO: execute calculated position
[5/2/2021, 3:04:01 PM] INFO: Attempting to execute USDT-CAKE-BUSD with an age of 39 ms and expected profit of 0.0459%
[5/2/2021, 3:04:01 PM] INFO: Execution
[5/2/2021, 3:04:01 PM] INFO: ENTER LINEAR EXECUTION
[5/2/2021, 3:04:01 PM] INFO: Buying 0.714 CAKEUSDT @ market price
[5/2/2021, 3:04:01 PM] INFO: Successfully bought 0.71400000 CAKEUSDT @ a quote of 30.05868600 in 19 ms
[5/2/2021, 3:04:01 PM] INFO: Selling 0.714 CAKEBUSD @ market price
[5/2/2021, 3:04:01 PM] INFO: Successfully sold 0.71400000 CAKEBUSD @ a quote of 30.07796400 in 15 ms
[5/2/2021, 3:04:01 PM] INFO: Selling 30.07 BUSDUSDT @ market price
[5/2/2021, 3:04:01 PM] INFO: Successfully sold 30.07000000 BUSDUSDT @ a quote of 30.05496500 in 15 ms
[5/2/2021, 3:04:01 PM] INFO: Executed USDT-CAKE-BUSD position in 78 ms
[5/2/2021, 3:04:01 PM] DEBUG: CAKEUSDT Stats:
[5/2/2021, 3:04:01 PM] DEBUG: Expected Conversion:  29.99371200 USDT into 0.71400000 CAKE @ 42.00800000
[5/2/2021, 3:04:01 PM] DEBUG: Observed Conversion:  30.05868600 USDT into 0.71400000 CAKE @ 42.09900000
[5/2/2021, 3:04:01 PM] DEBUG: Price Change:         0.21662540%
[5/2/2021, 3:04:01 PM] DEBUG:
[5/2/2021, 3:04:01 PM] DEBUG: CAKEBUSD Stats:
[5/2/2021, 3:04:01 PM] DEBUG: Expected Conversion:  0.71400000 CAKE into 30.09153000 BUSD @ 42.14500000
[5/2/2021, 3:04:01 PM] DEBUG: Observed Conversion:  0.71400000 CAKE into 30.07796400 BUSD @ 42.12600000
[5/2/2021, 3:04:01 PM] DEBUG: Price Change:         -0.04508245%
[5/2/2021, 3:04:01 PM] DEBUG:
[5/2/2021, 3:04:01 PM] DEBUG: BUSDUSDT Stats:
[5/2/2021, 3:04:01 PM] DEBUG: Expected Conversion:  30.09000000 BUSD into 30.07495500 USDT @ 0.99950000
[5/2/2021, 3:04:01 PM] DEBUG: Observed Conversion:  30.07000000 BUSD into 30.05496500 USDT @ 0.99950000
[5/2/2021, 3:04:01 PM] DEBUG: Price Change:         -0.00000000%
[5/2/2021, 3:04:01 PM] INFO:
[5/2/2021, 3:04:01 PM] INFO: USDT delta:          -0.00372100 (-0.0124%)
[5/2/2021, 3:04:01 PM] INFO: CAKE delta:           0.00000000 ( 0.0000%)
[5/2/2021, 3:04:01 PM] INFO: BUSD delta:           0.00796400 ( 0.0265%)
[5/2/2021, 3:04:01 PM] INFO: BNB fees:    -0.00007237

30.05868600 USDT -> 30.05496500 USDT (Loss: -0.012379%)

bmino commented 3 years ago

Hey, glad to see your interest!

I see you've made some code changes too ;)

Obviously, less latency is better. I used to setup with about the same ~30ms of latency detected on startup and set an age threshold of 10ms. Even then, the time it took to execute the trade was around 20ms per execution which ate up too much time imo and I couldn't find a way to reduce that

winChawakorn commented 3 years ago

Thanks for your idea 😃, and wow how many tickers in your white list for detecting 10 ms opportunities? I found only at least 30ms from around 10 white list tickers.

Btw, I heard that AWS has something called direct connect which can reduce the latency to a single digit by making a request direct to another service on AWS without passing the internet. I'll try and raise the PR, if I can make it work.

bmino commented 3 years ago

iiiiiiiiiinteresting, I will look into that as well. First time I'm hearing about that

jurgenBE commented 3 years ago

Hi bmino, first of all great application, but I am new to js so I rewrote it to C# .net core, but the same as the problems here, there is only 1 of the 3 (at best 2) of the 3 that have the same or better odds, (if you have them in your white list - even on AWS Tokyo) but that one with worse odds, i think there is a little error in the program (or it could be in mine too :) but a lot of the time I saw like 0.20$ profit (when trading with startpair USDT), I started to notice that I had always less ETH or less BTC after a trade, so i started to investigate and came to the conclusion that the 0.20 profit like calculated in the calculated/actual was in real -0.45$ instead of the projected 0.20$. You must in fact take the actual.a.spent - actual.a.earned - fees in bnb minus the difference from for example ETH => convert this to USDT and substract that from your 'real profit' => suddenly all my little profits became negative, and sometimes really negative like -0.80$ with like startcapital of 33$, so that's much. I think that's a bug in the program but i don't know for sure as i converted it to .net core code, but i don't see in your code something that converts the negative eth. to the startcoin.

Then another point : I use real-time updates instead of per 100 ms with the websocket function Individual Symbol Book Ticker Streams Pushes any update to the best bid or ask's price or quantity in real-time for a specified symbol. Stream Name: @bookTicker Update Speed: Real-time

This pushes the best ask/bid price per 1 ms instead of 100 ms, so I find a lot of arbitrage opps, but still the speed isn't enough to make consistent profit's so I am about to give it up, but first i want to test your program, but this option how would that need to be implemented in here + can you make a function to calculate the max amount over the 3 arbs so that you can make your loop shorter for example if the max bet is 400$ and your balance is for example 50$, start the loop from min_invest = 48.5 to 49.9 (i always take some margin as to not take the real max amount, otherwise you get stuck) PS : I am talking about paralell execution where there is no recalculateTradeLeg after each order...

bmino commented 3 years ago

Hi bmino, first of all great application, but I am new to js so I rewrote it to C# .net core, but the same as the problems here, there is only 1 of the 3 (at best 2) of the 3 that have the same or better odds, (if you have them in your white list - even on AWS Tokyo) but that one with worse odds, i think there is a little error in the program (or it could be in mine too :) but a lot of the time I saw like 0.20$ profit (when trading with startpair USDT), I started to notice that I had always less ETH or less BTC after a trade, so i started to investigate and came to the conclusion that the 0.20 profit like calculated in the calculated/actual was in real -0.45$ instead of the projected 0.20$. You must in fact take the actual.a.spent - actual.a.earned - fees in bnb minus the difference from for example ETH => convert this to USDT and substract that from your 'real profit' => suddenly all my little profits became negative, and sometimes really negative like -0.80$ with like startcapital of 33$, so that's much. I think that's a bug in the program but i don't know for sure as i converted it to .net core code, but i don't see in your code something that converts the negative eth. to the startcoin.

Then another point : I use real-time updates instead of per 100 ms with the websocket function Individual Symbol Book Ticker Streams Pushes any update to the best bid or ask's price or quantity in real-time for a specified symbol. Stream Name: @bookTicker Update Speed: Real-time

This pushes the best ask/bid price per 1 ms instead of 100 ms, so I find a lot of arbitrage opps, but still the speed isn't enough to make consistent profit's so I am about to give it up, but first i want to test your program, but this option how would that need to be implemented in here + can you make a function to calculate the max amount over the 3 arbs so that you can make your loop shorter for example if the max bet is 400$ and your balance is for example 50$, start the loop from min_invest = 48.5 to 49.9 (i always take some margin as to not take the real max amount, otherwise you get stuck) PS : I am talking about paralell execution where there is no recalculateTradeLeg after each order...

@jurgenBE SMART GUY and good eye! I've actually had the suggestion to convert the depth cache engine from depth() to book tickers for exactly that reason but have since moved on to some other things. I would looove to chat on discord! Happy to help your development and trade some ideas. Let's leave this issue for the stated question which can be closed now.

@winChawakorn I looked into direct connection and it applies to business who own an application running in AWS and need to connect to it with less latency at their brick and mortar office. Unfortunately doesn't apply to this use case

jurgenBE commented 3 years ago

Hey bmino you told me to discuss further on discord, I send you a friend request some months ago already : discord name = jurgen_verhasselt#7186