Exemplo n.º 1
0
    def __build_kline(self, contract_id, quot):
        last_price = self.__get_last_price(quot)
        if last_price != 0.0:
            qty = self.__get_qty(contract_id)

            #买单
            buy_order = Order()
            buy_order.contract_id = contract_id
            buy_order.side = 1
            buy_order.price = to_decimal(float(last_price),
                                         price_precision_dic[contract_id])
            buy_order.quantity = qty

            #卖单
            sell_order = Order()
            sell_order.contract_id = contract_id
            sell_order.side = -1
            sell_order.price = to_decimal(float(last_price),
                                          price_precision_dic[contract_id])
            sell_order.quantity = qty

            flag = random.randint(1, 4)
            if flag % 2 == 0:
                if False == self.__send_order(buy_order):
                    return False
                if False == self.__send_order(sell_order):
                    return False
            else:
                if False == self.__send_order(sell_order):
                    return False
                if False == self.__send_order(buy_order):
                    return False
            return True
Exemplo n.º 2
0
    def __init_levels(self):
        for contract_id in self.levels_params:
            while True:
                if contract_id in golbal_quot_book:
                    break

            quot = Quot()
            quot_lock.acquire()
            quot = copy.deepcopy(golbal_quot_book[contract_id])
            quot_lock.release()

            # 全部撤单
            self.__cancel_all_order(contract_id)

            # 初始档位
            level_param = self.levels_params[contract_id]
            for side in [1, -1]:
                order = Order()
                order.contract_id = contract_id
                order.side = side
                for level in range(level_param.max_level):
                    if order.side == 1:
                        if len(quot.bid_price
                               ) != 0 and quot.bid_price[0] != 0.0:
                            bid_price = quot.bid_price[0]
                        else:
                            bid_price = level_param.bid_price
                        price = bid_price - random.uniform(
                            level_param.step_price[0],
                            level_param.step_price[1])
                        order.price = to_decimal(
                            price, price_precision_dic[contract_id])
                    else:
                        if len(quot.ask_price
                               ) != 0 and quot.ask_price[0] != 0.0:
                            ask_price = quot.ask_price[0]
                        else:
                            ask_price = level_param.ask_price
                        price = ask_price + random.uniform(
                            level_param.step_price[0],
                            level_param.step_price[1])
                        order.price = to_decimal(
                            price, price_precision_dic[contract_id])

                    flag = random.randint(1, 4)
                    if flag % 2 == 0:
                        coeff = 1.0 + random.uniform(1 / (level + 1), 1)
                    else:
                        coeff = 1.0 - random.uniform(1 / (level + 1), 1)
                    min_qty = level_param.step_qty[0] * coeff + qty_size_dic[
                        contract_id]
                    max_qty = level_param.step_qty[1] * coeff + qty_size_dic[
                        contract_id]
                    qty = random.uniform(min_qty, max_qty)
                    order.quantity = to_decimal(qty,
                                                qty_precision_dic[contract_id])
                    self.__send_order(order)
Exemplo n.º 3
0
 def __get_order_price(self, side, level_param, quot):
     if side == 1:
         bid_price = 0.0
         if len(quot.bid_price) != 0:
             bid_price = quot.bid_price[0]
         if bid_price == 0.0:
             bid_price = quot.last_price
             if bid_price == 0.0:
                 bid_price = level_param.bid_price
         price = bid_price - random.uniform(level_param.step_price[0],
                                            level_param.step_price[1])
         return to_decimal(price, price_precision_dic[quot.contract_id])
     else:
         ask_price = 0.0
         if len(quot.ask_price) != 0:
             ask_price = quot.bid_price[0]
         if ask_price == 0.0:
             ask_price = quot.last_price
             if ask_price == 0.0:
                 ask_price = level_param.ask_price
         price = ask_price + random.uniform(level_param.step_price[0],
                                            level_param.step_price[1])
         return to_decimal(price, price_precision_dic[quot.contract_id])
Exemplo n.º 4
0
    def __rebuild_levels(self, contract_id, level_param, quot):

        # 全部撤单后补单
        self.__cancel_all_order(contract_id)
        for side in [1, -1]:
            order = Order()
            order.contract_id = contract_id
            order.side = side
            for level in range(level_param.max_level):
                order.price = self.__get_order_price(side, level_param, quot)
                flag = random.randint(1, 4)
                if flag % 2 == 0:
                    coeff = 1.0 + random.uniform(1 / (level + 1), 1)
                else:
                    coeff = 1.0 - random.uniform(1 / (level + 1), 1)
                min_qty = level_param.step_qty[0] * coeff
                max_qty = level_param.step_qty[1] * coeff
                qty = random.uniform(min_qty, max_qty)
                order.quantity = to_decimal(qty,
                                            qty_precision_dic[contract_id])
                self.__send_order(order)
Exemplo n.º 5
0
 def __get_qty(self, contract_id):
     line_param = self.lines_params[contract_id]
     if line_param.next_time <= time.time():
         min_qty = round(
             random.uniform(line_param.min_qty[0], line_param.min_qty[1]),
             qty_precision_dic[contract_id])
         max_qty = round(
             random.uniform(line_param.max_qty[0], line_param.max_qty[1]),
             qty_precision_dic[contract_id])
         line_param.period = random.randint(line_param.step_perid[0],
                                            line_param.step_perid[1])
         flag = random.randint(1, 4)
         if flag % 2 == 0:
             step_period = (1.0 + 0.382) * random.randint(
                 line_param.step_perid[0], line_param.step_perid[1])
         else:
             step_period = (1.0 - 0.382) * random.randint(
                 line_param.step_perid[0], line_param.step_perid[1])
         line_param.next_time = time.time() + step_period
         line_param.scope_qty.append(min_qty + qty_size_dic[contract_id])
         line_param.scope_qty.append(max_qty + qty_size_dic[contract_id])
     return to_decimal(
         random.uniform(line_param.scope_qty[0], line_param.scope_qty[1]),
         qty_precision_dic[contract_id])
Exemplo n.º 6
0
TRADE_SERVER_URL = 'tcp://xmu-centos7:20050'
#TRADE_SERVER_URL = 'tcp://118.24.201.203:20060'

variety_vs_symbol = {1: 'btcusdt'}
price_precision = {1: 2}

if __name__ == '__main__':
    context = zmq.Context()
    socket = context.socket(zmq.DEALER)
    socket.connect(TRADE_SERVER_URL)
    py2json = {}
    py2json['message_type'] = 5008
    py2json['appl_id'] = 2
    py2json['commodity_id'] = 2
    py2json['currency_id'] = 7
    py2json['index_price'] = to_decimal(6400)
    #    py2json['index_price'] = to_decimal(5252.4)
    #    py2json['index_price'] = to_decimal(5169.1)
    #    py2json['index_price'] = to_decimal(5169.1)
    #    py2json['index_price'] = to_decimal(6099.2)
    #    py2json['index_price'] = to_decimal(5946.2)
    #    py2json['index_price'] = to_decimal(7381)
    py2json['funding_basis'] = '0'
    py2json['market_id'] = ''
    str_json = json.dumps(py2json).encode('utf-8')
    print(str_json)
    socket.send(str_json)
    ret = socket.recv()
    print(ret)
Exemplo n.º 7
0
#py2json['type'] = 1
#py2json['forbid_trade'] = 0
#py2json['taker_fee_ratio'] = to_decimal(0.002)
#py2json['maker_fee_ratio'] = to_decimal(0.001)
#str_json = json.dumps(py2json).encode('utf-8')
#print(str_json)
#send_msg(str_json)

# 转账
py2json = {}
py2json['message_type'] = 4005
py2json['account_id'] = 13
py2json['currency_id'] = 7
py2json['from_appl_id'] = 5
py2json['to_appl_id'] = 1
py2json['quantity'] = to_decimal(555)
py2json['id'] = 1540727468003
str_json = json.dumps(py2json).encode('utf-8')
#print(str_json)
send_msg(str_json)

## 批量转账
#last_id = int(time.time())
#while True:
#    py2json = {}
#    py2json['message_type'] = 1001
#    py2json['appl_id'] = 1
#    py2json['contract_id'] = 1
#    py2json['account_id'] = 6
#    py2json['client_order_id'] = str(time.strftime('%H:%M:%S.000'))
#    py2json['side'] = 1
Exemplo n.º 8
0
    def run(self):
        context = zmq.Context()
        socket = context.socket(zmq.REQ)
        socket.connect("tcp://localhost:20050")
        logger.debug('Market Maker is Ready')

        pre_quot = {}
        cancel_orders = {}
        while True:
            for symbol in contract_id_dic:
                quot_lock.acquire()
                quot = Quot()
                if symbol in golbal_quot_book:
                    quot = copy.deepcopy(golbal_quot_book[symbol])


#                    logger.debug(quot.to_string())
                quot_lock.release()

                if symbol not in pre_quot:
                    empty = Quot()
                    pre_quot[symbol] = empty
                change = quot.get_change(pre_quot[symbol])
                pre_quot[symbol] = copy.deepcopy(quot)
                order_list = []
                for key in change:
                    #print(key, change[key])
                    contract_id = contract_id_dic[quot.symbol]
                    if key == 1:
                        qty = round(random.uniform(0.01, 0.02), 4)
                        #                        print(quot.to_string())

                        #买单
                        buy_order = Order()
                        buy_order.contract_id = contract_id
                        buy_order.side = 1
                        buy_order.price = to_decimal(
                            float(quot.last_price),
                            price_precision_dic[contract_id])
                        buy_order.quantity = to_decimal(
                            qty, qty_precision_dic[contract_id])

                        #卖单
                        sell_order = Order()
                        sell_order.contract_id = contract_id
                        sell_order.side = -1
                        sell_order.price = to_decimal(
                            float(quot.last_price),
                            price_precision_dic[contract_id])
                        sell_order.quantity = to_decimal(
                            qty, qty_precision_dic[contract_id])

                        flag = random.randint(0, 3)
                        if flag % 2 == 0:
                            order_list.append(
                                (contract_id, buy_order.to_json()))
                            order_list.append(
                                (contract_id, sell_order.to_json()))
                        else:
                            order_list.append(
                                (contract_id, sell_order.to_json()))
                            order_list.append(
                                (contract_id, buy_order.to_json()))
                    elif key == 2:
                        #买单
                        levels = change[2]
                        for i in range(0, len(levels)):
                            order = Order()
                            order.contract_id = contract_id
                            order.side = 1
                            order.price = to_decimal(
                                float(quot.bid_price[levels[i]]),
                                price_precision_dic[contract_id])
                            order.quantity = to_decimal(
                                float(quot.bid_qty[levels[i]]),
                                qty_precision_dic[contract_id])
                            #order_list.append((order.contract_id, order.to_json()))
                    elif key == 3:
                        #卖单
                        levels = change[3]
                        for i in range(0, len(levels)):
                            order = Order()
                            order.contract_id = contract_id
                            order.side = -1
                            order.price = to_decimal(
                                float(quot.ask_price[levels[i]]),
                                price_precision_dic[contract_id])
                            order.quantity = to_decimal(
                                float(quot.ask_qty[levels[i]]),
                                qty_precision_dic[contract_id])
                            #order_list.append((order.contract_id, order.to_json()))

                # 报单
                pre_cancel_list = []
                if quot.symbol in cancel_orders:
                    pre_cancel_list = copy.deepcopy(cancel_orders[quot.symbol])
                    cancel_orders[quot.symbol].clear()

                for i in range(0, len(order_list)):
                    #                    logger.debug(order_list[i][1])
                    socket.send(order_list[i][1])
                    ret = socket.recv()
                    data = json.loads(ret.decode('utf-8'))
                    #                    logger.debug(data)
                    if quot.symbol not in cancel_orders:
                        orders = []
                        cancel_orders[quot.symbol] = orders
                    cancel_orders[quot.symbol].append(
                        (data['msg'], order_list[i][0]))

                # 撤掉上一轮报单
                for i in range(0, len(pre_cancel_list)):
                    py2json = {}
                    py2json['message_type'] = 1003
                    py2json['appl_id'] = 1
                    py2json['account_id'] = 6
                    py2json['contract_id'] = pre_cancel_list[i][1]
                    py2json['original_order_id'] = pre_cancel_list[i][0]
                    cancel_order = json.dumps(py2json).encode('utf-8')
                    #                    logger.debug(cancel_order)
                    socket.send(cancel_order)
                    socket.recv()
            wait_sec = random.randint(1, 3)
            time.sleep(wait_sec)
Exemplo n.º 9
0
    def run(self):
        # 读取参数
        csv_reader = csv.reader(open("conf_levels.csv"))
        for row in csv_reader:
            level_param = LevelParam()
            level_param.contract_id = int(row[0])
            level_param.max_level = int(row[1])
            level_param.bid_price = float(row[2])
            level_param.ask_price = float(row[3])
            level_param.step_price.append(float(row[4]))
            level_param.step_price.append(float(row[5]))
            level_param.step_qty.append(float(row[6]))
            level_param.step_qty.append(float(row[7]))
            self.levels_params[level_param.contract_id] = level_param

        csv_reader = csv.reader(open("conf_lines.csv"))
        for row in csv_reader:
            line_param = LineParam()
            line_param.contract_id = int(row[0])
            line_param.rate = float(row[1])
            line_param.step_perid.append(int(row[2]))
            line_param.step_perid.append(int(row[3]))
            line_param.min_qty.append(int(row[4]))
            line_param.min_qty.append(int(row[5]))
            line_param.max_qty.append(int(row[6]))
            line_param.max_qty.append(int(row[7]))
            self.lines_params[line_param.contract_id] = line_param

        # 初始档位
        self.__init_levels()

        fill_levels_time = {}
        build_next_time = {}
        cancel_next_time = {}
        while True:
            for contract_id in contract_id_dic:
                if contract_id not in golbal_quot_book:
                    continue
                if contract_id not in self.levels_params:
                    continue

                quot = Quot()
                quot_lock.acquire()
                quot = copy.deepcopy(golbal_quot_book[contract_id])
                quot_lock.release()

                # 构造K线
                if contract_id not in build_next_time:
                    build_next_time[contract_id] = 0
                if build_next_time[contract_id] < time.time():
                    if False == self.__build_kline(contract_id, quot):
                        self.__rebuild_levels(contract_id, level_param, quot)
                    build_next_time[contract_id] = time.time(
                    ) + random.randint(45, 90)

                # 补单
                if contract_id not in fill_levels_time:
                    fill_levels_time[contract_id] = (0, 0)
                if quot.time != fill_levels_time[contract_id][
                        0] and fill_levels_time[contract_id][1] < time.time():
                    fill_levels_time[contract_id] = (quot.time, time.time() +
                                                     random.randint(15, 60))
                    level_param = self.levels_params[contract_id]

                    if len(quot.bid_price) < level_param.max_level:
                        level_num = level_param.max_level - len(quot.bid_price)
                        order = Order()
                        order.contract_id = contract_id
                        order.side = 1
                        for level in range(level_num):
                            order.price = self.__get_order_price(
                                1, level_param, quot)
                            order.quantity = self.__get_qty(contract_id)
                            self.__send_order(order)

                    if len(quot.ask_price) < level_param.max_level:
                        level_num = level_param.max_level - len(quot.ask_price)
                        order = Order()
                        order.contract_id = contract_id
                        order.side = -1
                        for level in range(level_num):
                            order.price = self.__get_order_price(
                                1, level_param, quot)
                            order.quantity = self.__get_qty(contract_id)
                            self.__send_order(order)

                # 随机撤单
                if contract_id not in cancel_next_time:
                    cancel_next_time[contract_id] = 0
                if cancel_next_time[contract_id] < time.time():
                    cancel_next_time[contract_id] = time.time(
                    ) + random.randint(15, 60)

                    if contract_id in self.orders:
                        cancel_orders = []
                        for i in range(random.randint(3, 8)):
                            while True:
                                if len(self.orders[contract_id]) - 1 == 0:
                                    idx = 0
                                else:
                                    idx = random.randint(
                                        0,
                                        len(self.orders[contract_id]) - 1)
                                cancel_order = self.orders[contract_id][idx]
                                py2json = {}
                                py2json['message_type'] = 1003
                                py2json['appl_id'] = 1
                                py2json['account_id'] = ROBOT_USER_ID
                                py2json['contract_id'] = contract_id
                                cancel_orders.append(cancel_order)
                                del self.orders[contract_id][idx]
                                if True == self.__cancel_order(
                                        contract_id, cancel_order[2]):
                                    break

                        level = 0
                        for cancel_order in cancel_orders:
                            order = Order()
                            order.contract_id = contract_id
                            order.side = cancel_order[1]
                            order.price = self.__get_order_price(
                                order.side, level_param, quot)

                            flag = random.randint(1, 4)
                            if flag % 2 == 0:
                                coeff = 1.0 + random.uniform(
                                    1 / (level + 1), 1)
                            else:
                                coeff = 1.0 - random.uniform(
                                    1 / (level + 1), 1)
                            level = level + 1
                            min_qty = level_param.step_qty[
                                0] * coeff + qty_precision_dic[contract_id]
                            max_qty = level_param.step_qty[
                                1] * coeff + qty_precision_dic[contract_id]
                            qty = random.uniform(min_qty, max_qty)
                            order.quantity = to_decimal(
                                qty, qty_precision_dic[contract_id])
                            self.__send_order(order)

                time.sleep(1)
Exemplo n.º 10
0
#py2json['appl_id'] = 2
#py2json['contract_id'] = 100
#py2json['account_id'] = 6
#py2json['margin'] = to_decimal(-1)
#str_json = json.dumps(py2json).encode('utf-8')
###print(str_json)
#send_msg(str_json)

py2json = {}
py2json['message_type'] = 1001
py2json['appl_id'] = 2
py2json['contract_id'] = 100
py2json['account_id'] = 6
py2json['client_order_id'] = str(time.strftime('%H:%M:%S.000'))
py2json['side'] = 1 
py2json['price'] = to_decimal(6400)
py2json['quantity'] = to_decimal(80)
py2json['order_type'] = 1
py2json['time_in_force'] = 1
py2json['position_effect'] = 1
#py2json['margin_type'] = 2
#py2json['margin_rate'] = to_decimal(0.05)
py2json['margin_type'] = 1
py2json['margin_rate'] = "0"
str_json = json.dumps(py2json).encode('utf-8')
#print(str_json)
send_msg(str_json)

py2json = {}
py2json['message_type'] = 1001
py2json['appl_id'] = 2
Exemplo n.º 11
0
    def run(self):
        context = zmq.Context()
        socket = context.socket(zmq.REQ)
        socket.connect(TRADE_FRONT_URL)

        # 全部撤单
        for user_id in contract_vs_user:
            py2json = {}
            py2json['message_type'] = 1003
            py2json['appl_id'] = 1
            py2json['account_id'] = contract_vs_user[user_id]
            py2json['contract_id'] = 0
            cancel_order = json.dumps(py2json).encode('utf-8')
            socket.send(cancel_order)
            socket.recv()

        pre_quot = {}
        cancel_orders = {}
        while True:
            for symbol in contract_id_dic:
                contract_id = 0
                quot_lock.acquire()
                quot = Quot()
                if symbol in golbal_quot_book:
                    quot = copy.deepcopy(golbal_quot_book[symbol])
                    contract_id = quot.contract_id
#                    logger.debug(quot.to_string())
                quot_lock.release()

                if symbol not in pre_quot:
                    empty = Quot()
                    pre_quot[symbol] = empty

                # 构造K线
                if contract_id != 0:
                    exquot = Quot()
                    exquot_lock.acquire()
                    if contract_id in golbal_exquot_book:
                        exquot = copy.deepcopy(golbal_exquot_book[contract_id])
                    exquot_lock.release()

                    last_price = self.__get_last_price(exquot, quot)
                    if last_price != 0.0:
                        qty = float(quot.last_qty) * random.uniform(
                            0.001, 0.01)
                        if qty < qty_size_dic[contract_id]:
                            qty = qty_size_dic[contract_id] * random.randint(
                                5, 10)
                        qty = to_decimal(float(qty),
                                         qty_precision_dic[contract_id])

                        #买单
                        buy_order = Order(contract_id)
                        buy_order.side = 1
                        buy_order.price = to_decimal(
                            float(last_price),
                            price_precision_dic[contract_id])
                        buy_order.quantity = qty
                        #                        print(buy_order.to_json())

                        #卖单
                        sell_order = Order(contract_id)
                        sell_order.side = -1
                        sell_order.price = to_decimal(
                            float(last_price),
                            price_precision_dic[contract_id])
                        sell_order.quantity = qty
                        #                        print(sell_order.to_json())

                        flag = random.randint(1, 4)
                        if flag % 2 == 0:
                            self.__send_order(socket, buy_order)
                            self.__send_order(socket, sell_order)
                        else:
                            self.__send_order(socket, sell_order)
                            self.__send_order(socket, buy_order)

                # 构造价格档位
                change = quot.get_change(pre_quot[symbol])
                pre_quot[symbol] = copy.deepcopy(quot)
                order_list = []
                for key in change:
                    if key == 2:
                        #买单
                        levels = change[2]
                        for i in range(0, len(levels)):
                            order = Order(contract_id)
                            order.side = 1
                            order_price = get_price(quot.bid_price[levels[i]],
                                                    -1, self.level_adj_rate)
                            order.price = to_decimal(
                                order_price, price_precision_dic[contract_id])
                            order_qty = self.__get_order_qty(i)
                            order.quantity = to_decimal(
                                order_qty, qty_precision_dic[contract_id])
                            order_list.append(
                                (order.contract_id, order.to_json()))
                    elif key == 3:
                        #卖单
                        levels = change[3]
                        for i in range(0, len(levels)):
                            order = Order(contract_id)
                            order.side = -1
                            order_price = get_price(quot.ask_price[levels[i]],
                                                    1, self.level_adj_rate)
                            order.price = to_decimal(
                                order_price, price_precision_dic[contract_id])
                            order_qty = self.__get_order_qty(i)
                            order.quantity = to_decimal(
                                order_qty, qty_precision_dic[contract_id])
                            order_list.append(
                                (order.contract_id, order.to_json()))

                # 报单
                pre_cancel_list = []
                if quot.symbol in cancel_orders:
                    pre_cancel_list = copy.deepcopy(cancel_orders[quot.symbol])
                    cancel_orders[quot.symbol].clear()

                for i in range(0, len(order_list)):
                    #                    logger.debug(order_list[i][1])
                    socket.send(order_list[i][1])
                    ret = socket.recv()
                    data = json.loads(ret.decode('utf-8'))
                    #                    logger.debug(data)
                    if 0 == data['code']:
                        if quot.symbol not in cancel_orders:
                            orders = []
                            cancel_orders[quot.symbol] = orders
                        cancel_orders[quot.symbol].append(
                            (data['msg'], order_list[i][0]))
                    else:
                        logger.debug('LevelMaker')
                        logger.debug(order_list[i][1])
                        logger.debug(data)

                # 撤掉上一轮报单
                for i in range(0, len(pre_cancel_list)):
                    contract_id = pre_cancel_list[i][1]
                    py2json = {}
                    py2json['message_type'] = 1003
                    py2json['appl_id'] = 1
                    py2json['account_id'] = contract_vs_user[contract_id]
                    py2json['contract_id'] = contract_id
                    py2json['original_order_id'] = pre_cancel_list[i][0]
                    cancel_order = json.dumps(py2json).encode('utf-8')
                    #                    logger.debug(cancel_order)
                    socket.send(cancel_order)
                    ret = socket.recv()
                    data = json.loads(ret.decode('utf-8'))


#                    if 0 != data['code']:
#                        logger.debug(data)

            wait_sec = random.randint(3, 6)
            time.sleep(wait_sec)