tiagosiebler / TriangularArbitrage

Detect triangular arbitrage opportunities within Binance price tickers
545 stars 171 forks source link

Possible inconsistency in DB data for arbitrage_ticks #32

Closed tiagosiebler closed 6 years ago

tiagosiebler commented 6 years ago

If you look at the ticks_arbitrage table at a random row. You can see that (for example) the 'a_step_from: BTC' and 'a_step_to:ETH' . But if you collapse the A: Object then stepFrom: ETH and StepTo: BTC so it is the otherway around, that does not seem right? Step B and Step C do not have this issue. I already checked the code and that seems correctly, as a_step_from is assigned to StepFrom... but the output does not match

Example object:

    "a" : {
        "e" : "24hrTicker",
        "E" : 1514987934215.0,
        "s" : "BNBBTC",
        "p" : "-0.00003470",
        "P" : "-5.538",
        "w" : "0.00058985",
        "x" : "0.00062672",
        "c" : "0.00059186",
        "Q" : "9.00000000",
        "b" : "0.00059013",
        "B" : "476.00000000",
        "a" : "0.00059185",
        "A" : "203.00000000",
        "o" : "0.00062656",
        "h" : "0.00063062",
        "l" : "0.00052900",
        "v" : "4510157.00000000",
        "q" : "2660.31029749",
        "O" : 1514901534215.0,
        "C" : 1514987934215.0,
        "F" : 3924337,
        "L" : 4037342,
        "n" : 113006,
        "_id" : ObjectId("5a4ce19eb0de5e54e65c95b7"),
        "key" : "BTC",
        "startsWithKey" : true,
        "endsWithKey" : false,
        "flipped" : false,
        "rate" : "0.00059013",
        "stepFrom" : "BNB",
        "stepTo" : "BTC"
    },
    "a_symbol" : "BNBBTC",
    "a_step_from" : "BTC",
    "a_step_to" : "BNB",

internal stepFrom mismatches external a_step_from, why?

tiagosiebler commented 6 years ago

Document is prepared here before insert: https://github.com/tiagosiebler/TriangularArbitrage/blob/master/lib/CurrencyCore.js#L130

tiagosiebler commented 6 years ago

So in binance, assuming we’re trading with the BTC silo, the tickers follow the xxxBTC format. So for example BNBBTC means you can buy bnb with btc, or sell bnb for btc.

However, by default I’m reading in the opposite format and only switching when no match is found:

  var currency = stream.obj[fromCur + toCur];

  if(currency){
    currency.flipped = false;
    currency.rate = currency.a;

    // BTCBNB
  }else{
    currency = stream.obj[toCur + fromCur];
    if(!currency){
      return false;
    }
    currency.flipped = true;
    currency.rate = (1/currency.b);
    // BNBBTC
  }

Most of the time it won’t find it the first time, but on some occasions it’s something like BTCBNB where both currencies are tradeable silos, therefore the BNB silo does have a ticker to buy BTC using BNB and will match first.

That’s when this inconsistency is coming from. It doesn’t affect all rows, but it does affect any that involve a transition across one of the main silos.