BitMEX/sample-market-maker

ws_thread.py funds can return wrong currency

Opened this issue · 0 comments

Had a problem with the bot returning spurious wallet values after the March 23rd site maintenance. Support couldn't help but I eventually found the issue.

The funds() function in ws_thread.py looks like this:

def funds(self):
    return self.data['margin'][0]

It just returns whatever is first in the list of currencies returned in data. But after the March 23rd site update this might not be XBt, for instance I was getting spurious data because the first in the list was actually BMEx. Here's the actual margin currency data returned by ws.

'margin': [
{'account': xxxxxxxxxx, 'currency': 'BMEx', 'riskLimit': 9223372036854775807, 'amount': 12082210, 'prevRealisedPnl': 0, 'grossComm': 0, 'grossOpenCost': 0, 'grossOpenPremium': 0, 'grossExecCost': 0, 'grossMarkValue': 0, 'riskValue': 0, 'initMargin': 0, 'maintMargin': 0, 'targetExcessMargin': 0, 'realisedPnl': 0, 'unrealisedPnl': 0, 'walletBalance': 12082210, 'marginBalance': 12082210, 'marginLeverage': 0.0, 'marginUsedPcnt': 0.0, 'excessMargin': 12082210, 'availableMargin': 12082210, 'withdrawableMargin': 12082210, 'timestamp': '2023-03-28T03:50:00.855Z'},
{'account': xxxxxxxxxx, 'currency': 'XBt', 'riskLimit': 1000000000000, 'amount': 20000, 'prevRealisedPnl': -3444113, 'grossComm': 0, 'grossOpenCost': 0, 'grossOpenPremium': 0, 'grossExecCost': 0, 'grossMarkValue': 0, 'riskValue': 0, 'initMargin': 0, 'maintMargin': 0, 'targetExcessMargin': 0, 'realisedPnl': 0, 'unrealisedPnl': 0, 'walletBalance': 20000, 'marginBalance': 20000, 'marginLeverage': 0.0, 'marginUsedPcnt': 0.0, 'excessMargin': 20000, 'availableMargin': 20000, 'withdrawableMargin': 20000, 'timestamp': '2023-03-24T01:03:11.565Z'}]

As you can see, BMEx is returned first, XBt second, so funds() returns BMEx holdings instead of XBt holdings.

In my own code I changed it to the following. It suits my purposes for now but you might want to do something different.

#was returning erroneous data if XBt wasn't the first currency returned in self.data['margin']
#will now look for XBt and exit if it can't find it.
def funds(self):
    #return self.data['margin'][0]
    for o in self.data['margin']:
        if o['currency']=='XBt':
            return o
            
    self.logger.info('ERROR: ws_thread.py cannot find XBt in margin data.')
    sleep(5)
    self.exit()
    sys.exit(1)