示例#1
0
    def __init__(self, user=BITMEX['test'], sandbox=1):
        """
        每次只允许一个请求
        """

        self.name = user['name']
        self.user = user
        self.sandbox = sandbox
        self.api_key = user['key']
        self.api_secret = user['secret']
        self.test_net = user['test_net']
        self.session_num = 3

        self.orders = {}

        # 锁
        self.is_lock = 0
        # 最小执行锁
        self.min_lock = 1
        # 最后完成时间
        self.last_time = master.mictime()
        # 连续错误次数
        self.error_time = 0
        # 最后错误信息
        self.last_error = ''
        # 最后错误码
        self.last_code = 0

        self.dd = None
        self.bmxRest = None
        self.pub_redis = None
        self.redis = None
    def wss_action(self, action, table, symbol, data):
        master.check_gid()
        try:
            fullData = self.btWss.data[table][symbol] if table in self.btWss.data and symbol in self.btWss.data[
                table] else []

            if table == 'margin':
                fullData = self.btWss.data[table] if table in self.btWss.data else {}
                self.pri_redis.set(table, json.dumps(fullData))
                self.pri_redis.set('time', master.mictime())
            elif table in self.pri_sub:
                self.pri_redis.hset(table, self.symbol, json.dumps(fullData))
                self.pri_redis.set('time', master.mictime())
            else:
                self.pri_redis.hset(table, self.symbol, json.dumps(fullData))
                self.pub_redis.set('time', master.mictime())
        except:
            logging.info('{} pri wss try except'.format(self.name))
            master.sysError()
示例#3
0
 def set_time(self):
     self.last_time = master.mictime()
示例#4
0
    def init(self):
        master.set_pro_name(self.PRO_CNAME)

        importlib.reload(config.user)
        from config.user import USER_CONFIG

        self.initUser(USER_CONFIG)
        while True:
            try:
                time.sleep(1)
                master.check_gid()
                now_time = master.mictime()

                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'])]

                    # 获取策略
                    strategy = conf['strategy'](user, pub_redis, pri_redis,
                                                symbol)
                    # 获取开仓订单
                    orders = strategy.getOrder()

                    # 沙盒环境
                    if conf['sandbox'] > 0:
                        strategy.runBox()
                        continue

                    status = pri_redis.get(self.IS_TRUST)

                    # 60秒仓位日志
                    if int(now_time / 1000000) % 300 == 0:
                        logging.info({
                            'title': 'strategyProcessLog',
                            'name': name,
                            'sign': strategy.sign,
                            'status': status,
                            'orders': orders,
                            'data': strategy.data
                        })

                    # 用户订单仓位未更新
                    lastInfo = pri_redis.hget(self.TRUST, 'info')

                    if not orders or 'info' not in orders or lastInfo == orders[
                            'info']:
                        continue

                    # 正在下单不处理
                    if status in ['1', '2']:
                        continue
                    # 状态为0 新下单
                    elif status == '0' and len(orders) > 2:
                        logging.info({
                            'title': 'strategyCreateOrder',
                            'name': name,
                            'sign': strategy.sign,
                            'status': status,
                            'orders': orders,
                            'data': strategy.data
                        })
                        pri_redis.hdelete(self.TRUST)
                        pri_redis.hmset(self.TRUST, orders)
                        pri_redis.set(self.IS_TRUST, 1)
            except:
                master.sysError()