def on_error_ws(self, exception_type, exception_value, tb):
     text = "".join(
         traceback.format_exception(exception_type, exception_value, tb)
     )
     logging.error(text)
     master.ding('{} pri wss 进程终止'.format(self.name))
     master.kill_me()
Beispiel #2
0
    def boxCheckMargin(self):
        errMsg = ''
        for k, v in self.data['margin'].items():
            if v < 0:
                errMsg = '沙盒资产计算错误'
        # if self.data['margin']['orderCost'] > 0 and len(self.data['order']) == 0:
        #     errMsg = '沙盒订单资产错误'
        maxNum = int(self.data['margin']['allCost'] *
                     self.data['position']['lv'] *
                     self.data['market']['lastPrice'] / self.FLOAT_SIZE)
        if maxNum < bitmexManage.MIN_NUM:
            errMsg = '沙盒全仓已爆仓'

        if errMsg:
            logging.info({
                'id': self.runId,
                'errMsg': errMsg,
                'orders': self.order,
                'data': self.data,
                'kline': self.lastKline
            })
            master.stop()
            master.kill_me()

        return True
Beispiel #3
0
    def init(self):
        master.set_pro_name(self.PRO_CNAME)
        self.initBitmex()

        from config.user import USER_CONFIG
        for conf in USER_CONFIG:
            user = conf['user']
            name = user['name']
            symbol = conf['symbol']

            pri_redis = self.redis[name + str(conf['sandbox'])]
            pub_redis = self.redis[conf['site'] + str(user['test_net'])]

            baseBoxDate = bitmexSandbox.KLINE_LIST[0]

            # 从沙盒120根基础k线运行时间开始 当前测试k线
            s_date = self.dataframe[baseBoxDate].iloc[0:bitmexMarket.
                                                      MAX_LINE].index[-1]
            # 2019-07-03 11:00:00  5m线 第一个成交
            for index, row in self.dataframe[
                    self.BASE_DATE].loc[s_date:].iterrows():
                self.updateNowKline(index, row)

                if len(self.kline[baseBoxDate]) < bitmexMarket.MAX_LINE:
                    continue
                if len(self.kline[baseBoxDate]) > bitmexMarket.MAX_LINE:
                    self.kline[baseBoxDate] = self.kline[baseBoxDate][
                        -bitmexMarket.MAX_LINE:]

                # 获取策略
                self.strategy = conf['strategy'](user, pub_redis, pri_redis,
                                                 symbol)
                self.strategy.realMode = False
                self.strategy.backMode = True

                if self.strategy.realMode:
                    sell = row['open']
                else:
                    sell = 0
                self.setBitmexMarket(pub_redis, symbol, row['close'], sell)
                self.setBitmexKline(pub_redis, symbol)

                order = self.strategy.getOrder()
                self.strategy.runBox()

                # todo 图表观察
                # myPlot.kline(self.kline['1h'])

        master.stop()
        master.kill_me()
    def checkRest(self):
        try:
            # 模拟环境不开启请求
            if self.sandbox > 0:
                return

            if self.is_lock != 0:
                return
            if self.error_time > 2:
                master.sysError()
                master.kill_me()
            elif self.last_error:
                self.dd is not None and self.dd.msg(self.last_error)
                self.last_error = ''

            orders = self.redis.hgetall(self.TRUST)
            status = self.redis.get(self.IS_TRUST)

            # 0准备下单 1下单到用户进程 2远程下单 3下单失败
            if status == '1':
                logging.info({
                    'title': 'bitmexRestLog status 1',
                    'self.last_time': self.last_time,
                    'self.orders': self.orders,
                    'status': status,
                    'orders': orders
                })
                if not orders or 'time' not in orders:
                    self.log('time not in orders')
                    return

                if self.last_time >= int(orders['time']):
                    self.log(
                        'status 1 stop: last_time: {} orders[time]: {}'.format(
                            self.last_time, orders['time']))
                    return

                # self.log('status 1 start')
                self.lock()
                self.last_time = int(orders['time'])
                self.redis.set(self.IS_TRUST, 2)
                self.orders = orders
                self.pushOrder()
                self.unlock()
                # self.log('status 1 end')

            elif status == '2':
                # self.log('status 2 start')
                if len(self.orders) > 2:
                    logging.info({
                        'title': 'bitmexRestLog status 2',
                        'self.last_time': self.last_time,
                        'self.orders': self.orders,
                        'status': status,
                        'orders': orders
                    })
                    if self.last_code == 400 or self.last_time - int(
                            orders['time']) > 30000000:
                        for key in self.orders.keys():
                            self.log(
                                'order fail : del order key {}'.format(key))
                            # 失败订单不在重试
                            self.redis.hdel(self.TRUST, key)
                        self.log('status 2 del key end')
                        self.redis.set(self.IS_TRUST, 0)
                    else:
                        self.lock()
                        self.pushOrder()
                        self.unlock()
                else:
                    self.redis.set(self.IS_TRUST, 0)
                # self.log('status 2 end')
        except:

            t, v, tb = sys.exc_info()
            text = "".join(traceback.format_exception(t, v, tb))
            self.log(text)
            self.unlock()
            master.sysError()