Exemple #1
0
 def make_order(self, my_order_info):
     logger.info(
         u'\n-------------------------------------------spot order------------------------------------------------'
     )
     try:
         result = self.API.send_order(self.ACCOUNT_ID, my_order_info.amount,
                                      my_order_info.symbol,
                                      my_order_info.orderType,
                                      my_order_info.price,
                                      my_order_info.offset)
     except Exception as e:
         logger.error("***send_order:{}".format(e, traceback.format_exc()))
         send_msg("%s:send_order failed:%s" % (my_order_info.symbol, e))
         exit()
         result = self.check_order_list(my_order_info)
     if result is not None and result.get('status') == 'ok':
         logger_join("OrderId", result['data'], my_order_info.symbol,
                     my_order_info.orderType, my_order_info.price,
                     my_order_info.amount, "  ", from_time_stamp())
         return result['data']
     else:
         logger_join("order failed!", my_order_info.symbol,
                     my_order_info.orderType, my_order_info.price,
                     my_order_info.amount)
         return "-1"
Exemple #2
0
 def make_order(self, my_order_info):
     logger.info(
         '-----------------------------------------make order----------------------------------------------'
     )
     result = {}
     try:
         result = self.TradeApi.place_order(my_order_info.symbol, "cross",
                                            my_order_info.orderType, "fok",
                                            my_order_info.amount,
                                            my_order_info.offset,
                                            my_order_info.price)
     except Exception as e:
         logger.error("***trade:%s" % e)
     if result is not None and result.get('code') == "0" and result.get(
             'data'):
         logger.info("Order {} {} {} {} {} {}".format(
             result['data'][0]['ordId'], my_order_info.symbol,
             my_order_info.orderType, my_order_info.price,
             my_order_info.amount, from_time_stamp()))
         return result['data'][0]['ordId']
     else:
         logger.error("order failed!{} {} {} {} {}".format(
             my_order_info.symbol, my_order_info.orderType,
             my_order_info.price, my_order_info.amount,
             round(my_order_info.price * my_order_info.amount, 3)))
         return -1
Exemple #3
0
 def __init__(self, symbol="", order_type="", price=0, amount=0, base=0, trigger=0):
     self.orderId = ""
     self.symbol = symbol
     self.orderType = order_type
     self.price = price
     self.totalAmount = amount
     self.base = base
     self.totalDealAmount = 0
     self.amount = 0
     self.dealAmount = 0
     self.avgPrice = 0
     self.transaction = 0
     self.count = 0
     self.trigger = self.get_trigger(trigger)
     self.timestamp = from_time_stamp()
     self.canceled = 0
Exemple #4
0
 def make_order(cls, my_order_info):
     logger.info('-----------------------------------------spot order----------------------------------------------')
     result = {}
     try:
         result = spotAPI.take_order(my_order_info.orderType, my_order_info.symbol, 2, my_order_info.price,
                                     my_order_info.amount)
     except Exception as e:
         logger.error("***trade:%s" % e)
     if result is not None and result.get('result'):
         logger.info(
             "Order {} {} {} {} {} {}".format(result['order_id'], my_order_info.symbol, my_order_info.orderType,
                                              my_order_info.price, my_order_info.amount,
                                              from_time_stamp()))
         return result['order_id']
     else:
         logger.error(
             "order failed!{} {} {} {} {}".format(my_order_info.symbol, my_order_info.orderType, my_order_info.price,
                                                  my_order_info.amount,
                                                  round(my_order_info.price * my_order_info.amount, 3)))
         return -1
Exemple #5
0
def analyze_log():
    yy_mm_dd = str(from_time_stamp())[0:10]
    order_list = []
    f = open(r'log.txt')
    line = f.readline()
    while line and (line.find(yy_mm_dd) != -1 or line.find("canceled") != -1):
        search_canceled = re.search("order ([0-9]+) canceled", line)
        if search_canceled:
            order_id_canceled = search_canceled.group(1)
            for order in order_list:
                if order.orderId == order_id_canceled:
                    order.canceled = 1
        else:
            order = MyOrderInfo()
            order.from_log(line)
            order_list.append(order)
        line = f.readline()
    f.close()
    order_list = order_list[::-1]
    return order_list
Exemple #6
0
 def make_order(self, my_order_info):
     print(
         u'\n-------------------------------------------spot order------------------------------------------------'
     )
     try:
         result = send_order(self.ACCOUNT_ID, my_order_info.amount,
                             my_order_info.symbol, my_order_info.orderType,
                             my_order_info.price)
     except Exception as e:
         print("***send_order:%s" % e)
         send_email("%s:send_order failed:%s" % (my_order_info.symbol, e))
         result = self.check_order_list(my_order_info)
     if result is not None and result.get('status') == 'ok':
         print("OrderId", result['data'], my_order_info.symbol,
               my_order_info.orderType, my_order_info.price,
               my_order_info.amount, "  ", from_time_stamp())
         return result['data']
     else:
         print("order failed!", my_order_info.symbol,
               my_order_info.orderType, my_order_info.price,
               my_order_info.amount)
         return "-1"
Exemple #7
0
def analyze_log():
    yy_mm_dd = str(from_time_stamp())[0:10]
    order_list = []
    try:
        with open(r'log.txt') as f:
            line = f.readline()
            while line and (line.find(yy_mm_dd) != -1
                            or line.find("canceled") != -1):
                search_canceled = re.search("order ([0-9]+) canceled", line)
                if search_canceled:
                    order_id_canceled = search_canceled.group(1)
                    for order in order_list:
                        if order.orderId == order_id_canceled:
                            order.canceled = 1
                else:
                    order = MyOrderInfo()
                    order.from_log(line)
                    order_list.append(order)
                line = f.readline()
        order_list = order_list[::-1]
    except FileNotFoundError:
        logger.info("LogNotFound!")
    return order_list
Exemple #8
0
 def make_order(cls, my_order_info):
     print(
         u'\n-------------------------------------------spot order------------------------------------------------'
     )
     result = {}
     try:
         result = okcoinSpot.trade(my_order_info.symbol,
                                   my_order_info.orderType,
                                   my_order_info.price,
                                   my_order_info.amount)
     except Exception as e:
         print("***trade:%s" % e)
     if result is not None and result.get('result'):
         print("OrderId", result['order_id'], my_order_info.symbol,
               my_order_info.orderType, my_order_info.price,
               my_order_info.amount, "  ", from_time_stamp())
         return result['order_id']
     else:
         print("order failed!", my_order_info.symbol,
               my_order_info.orderType, my_order_info.price,
               my_order_info.amount,
               round(my_order_info.price * my_order_info.amount, 3))
         return -1
Exemple #9
0
def generate_email(order_list):
    symbols = json.loads(config.get("trade", "symbol"))
    email_content = "<html>\n<body>\n<div>"
    email_content += "\n<h2>收益统计 {}</h2>".format(str(from_time_stamp())[0:10])
    # 交易币种
    for symbol in symbols:
        order_list_symbol = list(filter(lambda x: True if x.symbol == symbol else False, order_list))
        # 总交易次数
        trx_count_total = len(order_list_symbol)
        # 策略交易次数
        trx_count_valid = 0
        # 策略交易次数(买)
        trx_count_valid_buy = 0
        # 策略交易次数(卖)
        trx_count_valid_sell = 0
        # 反转触发次数
        trx_count_reverse = 0
        # 插针触发次数
        trx_count_needle = 0
        # 部分成交撤单次数
        trx_count_canceled = 0
        # 总成交金额
        trx_usdt_total = 0
        # 总成交数量
        trx_quantity_total = 0
        # 总成交数量(买)
        trx_quantity_buy = 0
        # 总成交数量(卖)
        trx_quantity_sell = 0
        # 总交易均价
        trx_price_avg = 0
        # 余额变化
        usdt_change = 0
        # 持仓变化
        quantity_change = 0
        # 持仓增减均价
        quantity_change_price = 0
        # 理论收益
        reward_weight_usdt = 0
        # 吃单费率
        fee_rate = 0.0015
        # 点卡消耗
        points_consume = 0
        # 交易单
        order_sheet = ""
        for order in order_list_symbol:
            order_sheet += "\n<p>" + order.__str__() + "</p>"
            if order.canceled == 0:
                trx_count_valid += 1
                if order.orderType == "buy":
                    trx_count_valid_buy += 1
                    trx_quantity_buy += order.totalAmount
                else:
                    trx_count_valid_sell += 1
                    trx_quantity_sell += order.totalAmount
                if order.trigger == "reverse":
                    trx_count_reverse += 1
                elif order.trigger == "needle":
                    trx_count_needle += 1
                trx_usdt_total += abs(order.transaction)
                trx_quantity_total += order.totalAmount
                usdt_change += order.transaction
                reward_weight_usdt += order.count
            else:
                trx_count_canceled += 1
        if trx_quantity_total != 0:
            trx_price_avg = trx_usdt_total / trx_quantity_total
        quantity_change = trx_quantity_buy - trx_quantity_sell
        if usdt_change > 0 and quantity_change > 0:
            quantity_change_price = -trx_price_avg
        else:
            if quantity_change != 0:
                quantity_change_price = abs(usdt_change / quantity_change)
        points_consume = trx_usdt_total * fee_rate
        if quantity_change >= 0:
            change_type = "增"
        else:
            change_type = "减"
        email_content += "\n<div>"
        email_content += "\n<h3>%s</h3>" % symbol
        email_content += "\n<table>"
        email_content += get_tr("总交易次数", trx_count_total)
        email_content += get_tr("部分成交撤单次数", trx_count_canceled)
        email_content += get_tr("策略交易次数", trx_count_valid)
        email_content += get_tr("策略交易次数(买)", trx_count_valid_buy)
        email_content += get_tr("策略交易次数(卖)", trx_count_valid_sell)
        email_content += get_tr("反转触发次数", trx_count_reverse)
        email_content += get_tr("插针触发次数", trx_count_needle)
        email_content += get_tr("总成交金额$", trx_usdt_total)
        email_content += get_tr("总成交数量", trx_quantity_total)
        email_content += get_tr("总成交数量(买)", trx_quantity_buy)
        email_content += get_tr("总成交数量(卖)", trx_quantity_sell)
        email_content += get_tr("总交易均价$", trx_price_avg)
        email_content += get_tr("余额变化$", usdt_change)
        email_content += get_tr("持仓变化", quantity_change)
        email_content += get_tr("{}仓均价$".format(change_type), quantity_change_price)
        email_content += get_tr("理论收益$", reward_weight_usdt)
        email_content += get_tr("点卡消耗", points_consume)
        email_content += "\n</table>"
        email_content += "\n</div><br/>"
        email_content += "\n<h4>交易单</h4>"
        email_content += "\n<div>" + order_sheet + "\n</div>"
    email_content += "\n</div>\n</body>\n</html>"
    return email_content