BitMEX / api-connectors

Libraries for connecting to the BitMEX API.
https://www.bitmex.com/app/restAPI
909 stars 798 forks source link

get_ticker() is buggy and the tickLog is wrong #342

Open axeldomingues opened 5 years ago

axeldomingues commented 5 years ago

I understand calling get_instrument() before get_ticker() acts as a workaround, but why not add tickLog to the dictionary when calling get_ticker() to avoid the bug and having to workaround?

def get_ticker(self):
    '''Return a ticker object. Generated from quote and trade.'''
    lastQuote = self.data['quote'][-1]
    lastTrade = self.data['trade'][-1]
    ticker = {
        "last": lastTrade['price'],
        "buy": lastQuote['bidPrice'],
        "sell": lastQuote['askPrice'],
        "mid": (float(lastQuote['bidPrice'] or 0) + float(lastQuote['askPrice'] or 0)) / 2
    }

    # The instrument has a tickSize. Use it to round values.
    instrument = self.data['instrument'][0]
    instrument['tickLog'] = int(math.fabs(math.log10(instrument['tickSize'])))  # Add this for fix.
    return {k: round(float(v or 0), instrument['tickLog']) for k, v in ticker.items()}

I think there is an error in calculating tickLog:

#Now round operation makes wrong price:
tickSize = 0.5
tickLog = int(math.fabs(math.log10(tickSize)))
round(123.5, tickLog)
Out[]: 124.0

#Correct round operation
tickLog = math.ceil(math.fabs(math.log10(tickSize)))
round(123.5, tickLog)
Out[]: 123.5

Originally posted by @r8m in https://github.com/BitMEX/api-connectors/issues/149#issuecomment-470542331

intgsull commented 4 years ago

Can you release this update please? This breaks any automated trading completely. This is a minute, easy to fix, but SERIOUS bug.

axeldomingues commented 4 years ago

I would like to merge this into bitmex repo, but unfortunately, I'm not part of their repo. But you can see my fix proposal at https://github.com/axeldomingues/api-connectors/commit/bf47bff8d4b17fb2a4f8cf29bab11f5802404811