darkerego/bmcli

Autostop and lag issues

Opened this issue · 2 comments

Great work with the library! Is it still in use/active development?
I found a couple bugs in today's testing, I'll list them below rather than opening multiple issues:

  1. Running commands in terminal, for example python3 ./bmcli.py -a 0.001 0.005 25, constantly reports lag errors as such:
    [+] bitmex is lagging 86399 second(s)
    [+] Websocket is lagging 86399 seconds.
    This is exactly 24h so I'm assuming that the ticker['timestamp'] is reporting set to 1 day in the future? I didn't do extensive testing yet, just commented everything below if self.lag >= 30: in bitmex_ws.py.

2.1) I'm running into some issues with the autostop function (great idea BTW). The trailing stop part seems to work fine, but the stop loss does not. More specifically, after running python3 ./bmcli.py -a 0.001 0.001 20 and the stop loss threshold being reached, this is the output:
[+] [ Params: 0.001|0.001 | 25.0] | [ High: 36924|Diff: 26.91399999999703 ]
[+] [ Unrealized PNL: -5e-08 | Realized PNL: -3.7e-07 | Balance: xxxxxxxx]
[+] [ 2021-01-16 15:29:08, Position: 1 | Entry Price: 36914.0 ]
[+] quote {'symbol': 'XBTUSD', 'id': 8796313950, 'side': 'Sell', 'size': 730214, 'price': 36860.5}
[!] Error: 'bid'
The script exits immediately after that and does not execute any sales.

2.2) The second, minor issue I observed with the autostop function is that the script doesn't terminate when autostop function successfully closes a position using the trailing stop and I need to use Ctrl+C combination to escape manually.

  1. The addition of SyBrain is interesting, but I've been running into the same problem as described here. However, seems this is an external and seemingly abandoned library this is just a minor comment.

Let me know if you're open to pull requests, I'd be happy to contribute.

Hey there, thanks for the feedback. I may have broken some things while updating this at some point and as an civilian of the United States I don't have access to bitmex anymore, so this is no longer in development by myself. Also I tend to feel that bitmex is kind of shady so I moved to Deribit before they started enforcing KYC and now I am on FTX while that lasts. Interesting find with the lag, it could be the case. Here is some code that I use for FTX that seems to work really well:

  `    def monitor(self):
          c = 0
          tt = 0
          order_manager = OrderManager(sql=self.sql)
          _orders = []
          while True:
              orders = (self.ws.get_orders())
              try:
                  ticker = self.ws.get_ticker(market=market)
              except Exception as fuck:
                  self.logger.error(f'Ticker Error {fuck}, lets hope its transient!')
              else:
                  if not ticker:
                      pass
                  else:
                      last_time = datetime.datetime.utcfromtimestamp(ticker['time']).second
                      current_time = datetime.datetime.utcnow().second.real
                      self.lag = last_time - current_time

              if self.lag >= 10 and current_time > 0:
                  self.logger.critical('WS is lagging {} second(s), we are going down!'.format(self.lag))
                  return False`

Maybe there is an answer in there. Regarding the autostop, strange , i think you need to just change that to ['bidPrice'] or something. It's my custom API lib. I probably broke something. Finally, I can't remember but I think its supposed to keep monitoring for newly open positioned, which is why it doesn't exit but again, I forget...