Ejemplo n.º 1
0
    def buy(self, security, cash_amount):  # cash_amount is a string value
        self.timeLog("开始下达火币市价买单...")
        self.timeLog("只保留下单数量的小数点后2位...")
        self.timeLog("原始下单金额:%s" % cash_amount)
        tmp = float(cash_amount)
        tmp = helper.downRound(tmp, 2)
        cash_amount = str(tmp)
        self.timeLog("做完小数点处理后的下单金额:%s" % cash_amount)

        if float(cash_amount) < self.huobi_min_cash_amount:
            self.timeLog("金额:%s 小于交易所最小交易金额(火币最小金额:%.2f元),因此无法下单,此处忽略该信号" %
                         (cash_amount, self.huobi_min_cash_amount),
                         level=logging.WARN)
            return None

        coin_type = helper.coinTypeStructure[
            self.coinMarketType]["huobi"]["coin_type"]
        res = self.huobiService.buyMarket(
            coin_type, cash_amount, None, None,
            helper.coinTypeStructure[self.coinMarketType]["huobi"]["market"],
            BUY_MARKET)
        if helper.componentExtract(res, u"result", "") != u'success':
            self.timeLog(
                "下达火币市价买单(金额:%s)失败, 结果是:%s!" %
                (cash_amount, helper.componentExtract(res, u"result", "")),
                level=logging.ERROR)
            return None
        order_id = res[u"id"]
        # 查询订单执行情况
        order_info = self.huobiService.getOrderInfo(
            coin_type, order_id,
            helper.coinTypeStructure[self.coinMarketType]["huobi"]["market"],
            ORDER_INFO)
        self.timeLog("下达如下火币市价买单,金额:%s" % cash_amount)
        self.timeLog(str(order_info))

        retry_time = 0
        while retry_time < self.huobi_order_query_retry_maximum_times and order_info[
                "status"] != 2:
            self.timeLog("等待%f秒直至订单完成" % self.orderWaitingTime)
            time.sleep(self.orderWaitingTime)
            order_info = self.huobiService.getOrderInfo(
                coin_type, order_id, helper.coinTypeStructure[
                    self.coinMarketType]["huobi"]["market"], ORDER_INFO)
            self.timeLog(str(order_info))
            retry_time += 1

        if float(order_info["processed_price"]) > 0:
            executed_qty = float(order_info["processed_amount"]) / float(
                order_info["processed_price"])
            self.timeLog("火币市价买单已被执行,执行数量:%f,花费的现金:%.2f" %
                         (executed_qty, float(order_info["processed_amount"])))
            # self.dataLog()
            return executed_qty
        else:
            self.timeLog("火币市价买单未被执行", level=logging.WARN)
            return 0
Ejemplo n.º 2
0
    def sell(self, security, quantity):  # quantity is a string value
        self.timeLog("开始下达火币市价卖单...")
        self.timeLog("只保留下单数量的小数点后4位...")
        self.timeLog("原始下单数量:%s" % quantity)
        tmp = float(quantity)
        tmp = helper.downRound(tmp, 4)
        quantity = str(tmp)
        self.timeLog("做完小数点处理后的下单数量:%s" % quantity)
        if float(quantity) < self.huobi_min_quantity:
            self.timeLog("数量:%s 小于交易所最小交易数量(火币最小数量:%f),因此无法下单,此处忽略该信号" %
                         (quantity, self.huobi_min_quantity),
                         level=logging.WARN)
            return None

        coin_type = helper.coinTypeStructure[
            self.coinMarketType]["huobi"]["coin_type"]
        res = self.huobiService.sellMarket(
            coin_type, quantity, None, None,
            helper.coinTypeStructure[self.coinMarketType]["huobi"]["market"],
            SELL_MARKET)
        if helper.componentExtract(res, u"result", "") != u'success':
            self.timeLog(
                "下达火币市价卖单(数量:%s)失败, 结果是: %s!" %
                (quantity, helper.componentExtract(res, u"result", "")),
                level=logging.ERROR)
            return None
        order_id = res[u"id"]
        # 查询订单执行情况
        order_info = self.huobiService.getOrderInfo(
            coin_type, order_id,
            helper.coinTypeStructure[self.coinMarketType]["huobi"]["market"],
            ORDER_INFO)
        self.timeLog("下达如下火币市价卖单,数量:%s" % quantity)
        self.timeLog(str(order_info))

        retry_time = 0
        while retry_time < self.huobi_order_query_retry_maximum_times and order_info[
                "status"] != 2:
            self.timeLog("等待%f秒直至订单完成" % self.orderWaitingTime)
            time.sleep(self.orderWaitingTime)
            order_info = self.huobiService.getOrderInfo(
                coin_type, order_id, helper.coinTypeStructure[
                    self.coinMarketType]["huobi"]["market"], ORDER_INFO)
            self.timeLog(str(order_info))
            retry_time += 1

        executed_qty = float(order_info["processed_amount"])
        self.timeLog("火币市价卖单已被执行,执行数量:%f,收到的现金:%.2f" %
                     (executed_qty,
                      executed_qty * float(order_info["processed_price"])))

        # self.dataLog()
        return executed_qty
    def buy(self, security, cash_amount):  # cash_amount is a string value
        self.timeLog("开始下达火币市价买单...")
        self.timeLog("只保留下单数量的小数点后2位...")
        self.timeLog("原始下单金额:%s" % cash_amount)
        tmp = float(cash_amount)
        tmp = helper.downRound(tmp, 2)
        cash_amount = str(tmp)
        self.timeLog("做完小数点处理后的下单金额:%s" % cash_amount)

        if float(cash_amount) < self.huobi_min_cash_amount:
            self.timeLog("金额:%s 小于交易所最小交易金额(火币最小金额:%.2f元),因此无法下单,此处忽略该信号" % (cash_amount, self.huobi_min_cash_amount),
                         level=logging.WARN)
            return None

        coin_type = helper.coinTypeStructure[self.coinMarketType]["huobi"]["coin_type"]
        res = self.huobiService.buyMarket(coin_type, cash_amount, None, None,
                                          helper.coinTypeStructure[self.coinMarketType]["huobi"]["market"],
                                          BUY_MARKET)
        if helper.componentExtract(res, u"result", "") != u'success':
            self.timeLog("下达火币市价买单(金额:%s)失败, 结果是:%s!" % (cash_amount, helper.componentExtract(res, u"result", "")),
                         level=logging.ERROR)
            return None
        order_id = res[u"id"]
        # 查询订单执行情况
        order_info = self.huobiService.getOrderInfo(coin_type, order_id,
                                                    helper.coinTypeStructure[self.coinMarketType]["huobi"][
                                                        "market"], ORDER_INFO)
        self.timeLog("下达如下火币市价买单,金额:%s" % cash_amount)
        self.timeLog(str(order_info))

        retry_time = 0
        while retry_time < self.huobi_order_query_retry_maximum_times and order_info["status"] != 2:
            self.timeLog("等待%f秒直至订单完成" % self.orderWaitingTime)
            time.sleep(self.orderWaitingTime)
            order_info = self.huobiService.getOrderInfo(coin_type, order_id,
                                                        helper.coinTypeStructure[self.coinMarketType]["huobi"][
                                                            "market"], ORDER_INFO)
            self.timeLog(str(order_info))
            retry_time += 1

        if float(order_info["processed_price"]) > 0:
            executed_qty = float(order_info["processed_amount"]) / float(order_info["processed_price"])
            self.timeLog("火币市价买单已被执行,执行数量:%f,花费的现金:%.2f" % (executed_qty, float(order_info["processed_amount"])))
            # self.dataLog()
            return executed_qty
        else:
            self.timeLog("火币市价买单未被执行", level=logging.WARN)
            return 0
    def sell(self, security, quantity):  # quantity is a string value
        self.timeLog("开始下达火币市价卖单...")
        self.timeLog("只保留下单数量的小数点后4位...")
        self.timeLog("原始下单数量:%s" % quantity)
        tmp = float(quantity)
        tmp = helper.downRound(tmp, 4)
        quantity = str(tmp)
        self.timeLog("做完小数点处理后的下单数量:%s" % quantity)
        if float(quantity) < self.huobi_min_quantity:
            self.timeLog(
                "数量:%s 小于交易所最小交易数量(火币最小数量:%f),因此无法下单,此处忽略该信号" % (quantity, self.huobi_min_quantity),
                level=logging.WARN)
            return None

        coin_type = helper.coinTypeStructure[self.coinMarketType]["huobi"]["coin_type"]
        res = self.huobiService.sellMarket(coin_type, quantity, None, None,
                                           helper.coinTypeStructure[self.coinMarketType]["huobi"]["market"],
                                           SELL_MARKET)
        if helper.componentExtract(res, u"result", "") != u'success':
            self.timeLog("下达火币市价卖单(数量:%s)失败, 结果是: %s!" % (quantity, helper.componentExtract(res, u"result", "")),
                         level=logging.ERROR)
            return None
        order_id = res[u"id"]
        # 查询订单执行情况
        order_info = self.huobiService.getOrderInfo(coin_type, order_id,
                                                    helper.coinTypeStructure[self.coinMarketType]["huobi"][
                                                        "market"], ORDER_INFO)
        self.timeLog("下达如下火币市价卖单,数量:%s" % quantity)
        self.timeLog(str(order_info))

        retry_time = 0
        while retry_time < self.huobi_order_query_retry_maximum_times and order_info["status"] != 2:
            self.timeLog("等待%f秒直至订单完成" % self.orderWaitingTime)
            time.sleep(self.orderWaitingTime)
            order_info = self.huobiService.getOrderInfo(coin_type, order_id,
                                                        helper.coinTypeStructure[self.coinMarketType]["huobi"][
                                                            "market"], ORDER_INFO)
            self.timeLog(str(order_info))
            retry_time += 1

        executed_qty = float(order_info["processed_amount"])
        self.timeLog(
            "火币市价卖单已被执行,执行数量:%f,收到的现金:%.2f" % (executed_qty, executed_qty * float(order_info["processed_price"])))

        # self.dataLog()
        return executed_qty
Ejemplo n.º 5
0
    def buy_market(self, security, cash_amount, exchange="huobi", sell_1_price=None):
        if exchange == "huobi":
            self.timeLog("开始下达火币市价买单...")
            self.timeLog("只保留下单数量的小数点后2位...")
            self.timeLog("原始下单金额:%s" % cash_amount)
            tmp = float(cash_amount)
            tmp = helper.downRound(tmp, 2)
            cash_amount = str(tmp)
            self.timeLog("做完小数点处理后的下单金额:%s" % cash_amount)

            if float(cash_amount) < self.huobi_min_cash_amount:
                self.timeLog("金额:%s 小于交易所最小交易金额(火币最小金额:%s元),因此无法下单,此处忽略该信号" % (cash_amount, self.huobi_min_cash_amount),
                             level=logging.WARN)
                return None

            symbol = helper.coinTypeStructure[self.coinMarketType]["huobi"]["coin_type"]
            # market = helper.coinTypeStructure[self.coinMarketType]["huobi"]["market"]

            res = self.HuobiService.sendOrder(None,cash_amount,"api",symbol,HB_BUY_MARKET)
            # res = self.HuobiService.buyMarket(
            #     coin_type, cash_amount, None, None, market, BUY_MARKET)
            if helper.componentExtract(res, u"result", "") != u'success':
                self.timeLog("下达火币市价买单(金额:%s)失败, 结果是:%s!" % (cash_amount, helper.componentExtract(res, u"result", "")),
                             level=logging.ERROR)
                return None
            order_id = res[u"data"]
            # 查询订单执行情况
            order_info = self.HuobiService.getOrderInfo(order_id)
            self.timeLog("下达如下火币市价买单,金额:%s" % cash_amount)
            self.timeLog(str(order_info))

            retry_time = 0
            while retry_time < self.huobi_order_query_retry_maximum_times and order_info["state"] != "filled":
                self.timeLog("等待%f秒直至订单完成" % self.orderWaitingTime)
                time.sleep(self.orderWaitingTime)
                order_info = self.HuobiService.getOrderInfo(order_id)
                self.timeLog(str(order_info))
                retry_time += 1

            if float(order_info["field-cash-amount"]) > 0:
                executed_qty = float(
                    order_info["field-cash-amount"]) / float(order_info["field-cash-amount"])
                self.timeLog("火币市价买单已被执行,执行数量:%f,花费的现金:%.2f" % (
                    executed_qty, float(order_info["field-cash-amount"])))
                return executed_qty
            else:
                self.timeLog("火币市价买单未被执行", level=logging.WARN)
                return 0

        elif exchange == "okcoin":
            if sell_1_price is None:
                raise ValueError(
                    "处理okcoin市价买单之前,需要提供当前Okcoin卖一的价格信息,请检查传入的sell_1_price参数是否完备!")
            self.timeLog("开始下达okcoin市价买单...")
            self.timeLog("只保留下单数量的小数点后2位...")
            self.timeLog("原始下单金额:%s" % cash_amount)
            tmp = float(cash_amount)
            tmp = helper.downRound(tmp, 2)
            cash_amount = str(tmp)
            self.timeLog("做完小数点处理后的下单金额:%s" % cash_amount)

            if float(cash_amount) < self.okcoin_min_quantity * sell_1_price:
                self.timeLog(
                    "金额:%s 不足以购买交易所最小交易数量(okcoin最小数量:%f,当前卖一价格%.2f,最小金额要求:%.2f),因此无法下单,此处忽略该信号" % (
                        cash_amount, self.okcoin_min_quantity, sell_1_price, self.okcoin_min_quantity * sell_1_price),
                    level=logging.WARN)
                return None

            symbol = helper.coinTypeStructure[self.coinMarketType]["okcoin"]["coin_type"]
            res = self.OKCoinService.trade(
                symbol, OKC_BUY_MARKET, price=cash_amount)

            if helper.componentExtract(res, "result") != True:
                self.timeLog("下达okcoin市价买单(金额:%s)失败, 结果是:%s!" % (cash_amount, helper.componentExtract(res, "result")),
                             level=logging.ERROR)
                return None
            order_id = res["order_id"]
            # 查询订单执行情况
            order_info = self.OKCoinService.getOrderInfo(symbol, str(order_id))
            self.timeLog("下达如下okcoin市价买单,金额:%s" % cash_amount)
            self.timeLog(str(order_info))

            retry_time = 0
            while retry_time < self.okcoin_order_query_retry_maximum_times and order_info["orders"][0]["status"] != 2:
                self.timeLog("等待%.1f秒直至订单完成" % self.orderWaitingTime)
                time.sleep(self.orderWaitingTime)
                order_info = self.OKCoinService.getOrderInfo(symbol, str(order_id))
                self.timeLog(str(order_info))
                retry_time += 1

            executed_qty = order_info["orders"][0]["deal_amount"]
            self.timeLog("okcoin市价买单已被执行,执行数量:%f,花费的现金:%.2f" % (
                executed_qty, executed_qty * order_info["orders"][0]["avg_price"]))
            return executed_qty
Ejemplo n.º 6
0
    def buy_limit(self, security, price, quantity, exchange="huobi"):
        if exchange == "huobi":
            self.timeLog("开始下达火币限价买单...")
            self.timeLog("只保留下单数量的小数点后4位,下单价格的小数点后2位...")
            self.timeLog("原始下单数量:%s" % quantity)
            tmp = float(quantity)
            tmp = helper.downRound(tmp, 4)
            quantity = str(tmp)
            self.timeLog("做完小数点处理后的下单数量:%s" % quantity)

            if float(quantity) < self.huobi_min_quantity:
                self.timeLog(
                    "数量:%s 小于交易所最小交易数量(火币最小数量:%f),因此无法下单,此处忽略该信号" % (
                        quantity, self.huobi_min_quantity),
                    level=logging.WARN)
                return None

            self.timeLog("原始下单价格:%s" % price)
            tmp = float(price)
            tmp = helper.downRound(tmp, 2)
            price = str(tmp)
            self.timeLog("做完小数点处理后的下单价格:%s" % price)

            symbol = helper.coinTypeStructure[self.coinMarketType]["huobi"]["coin_type"]
            # market = helper.coinTypeStructure[self.coinMarketType]["huobi"]["market"]
            res = self.HuobiService.sendOrder(quantity,None,"api",symbol,HB_BUY)
            if helper.componentExtract(res, u"result", "") != u'success':
                self.timeLog(
                    "下达火币限价买单(数量:%s,价格:%s)失败, 结果是:%s!" % (
                        quantity, price, helper.componentExtract(res, u"result", "")),
                    level=logging.ERROR)
                return None
            order_id = res[u"data"]
            # 查询订单执行情况
            order_info = self.HuobiService.getOrderInfo(order_id)
            self.timeLog("下达如下火币限价买单,数量:%s, 价格:%s" % (quantity, price))
            self.timeLog(str(order_info))

            retry_time = 0
            while retry_time < self.huobi_order_query_retry_maximum_times and order_info["state"] != "filled":
                self.timeLog("等待%f秒直至订单完成" % self.orderWaitingTime)
                time.sleep(self.orderWaitingTime)
                order_info = self.HuobiService.getOrderInfo(order_id)
                self.timeLog(str(order_info))
                retry_time += 1

                # if order is not fully filled, cancel order if after timeout, cancel order is synchronized
            if order_info["state"] != 2:
                cancel_result = self.HuobiService.cancelOrder(order_id)
                retry_time = 0
                while retry_time < self.huobi_order_cancel_query_retry_maximum_times and helper.componentExtract(
                        cancel_result, u"result", "") != u'success':
                    self.timeLog("等待%f秒直至订单取消完成" % self.orderWaitingTime)
                    time.sleep(self.orderWaitingTime)
                    cancel_result = self.HuobiService.cancelOrder(order_id)
                    retry_time += 1
                order_info = self.HuobiService.getOrderInfo(order_id)

            if float(order_info["field-cash-amount"]) > 0:
                executed_qty = float(order_info["field-amount"])
                self.timeLog("火币限价买单已被执行,执行数量:%f,花费的现金:%.2f" % (
                    executed_qty, float(order_info["processed_price"]) * executed_qty))
                return executed_qty
            else:
                self.timeLog("火币限价买单未被执行", level=logging.WARN)
                return None

        elif exchange == "okcoin":
            self.timeLog("开始下达okcoin限价买单...")
            self.timeLog("只保留下单数量的小数点后2位...")
            self.timeLog("原始下单数量:%s" % quantity)
            tmp = float(quantity)
            tmp = helper.downRound(tmp, 2)
            quantity = str(tmp)
            self.timeLog("做完小数点处理后的下单数量:%s" % quantity)
            if float(quantity) < self.okcoin_min_quantity:
                self.timeLog(
                    "数量:%s 小于交易所最小交易数量(okcoin最小数量:%f),因此无法下单,此处忽略该信号" % (
                        quantity, self.okcoin_min_quantity),
                    level=logging.WARN)
                return None

            self.timeLog("原始下单价格:%s" % price)
            tmp = float(price)
            tmp = helper.downRound(tmp, 2)
            price = str(tmp)
            self.timeLog("做完小数点处理后的下单价格:%s" % price)

            symbol = helper.coinTypeStructure[self.coinMarketType]["okcoin"]["coin_type"]

            res = self.OKCoinService.trade(symbol, OKC_BUY, price=price, amount=quantity)

            if helper.componentExtract(res, "result") != True:
                self.timeLog(
                    "下达okcoin限价买单(数量:%s,价格:%s)失败, 结果是:%s!" % (
                        quantity, price, helper.componentExtract(res, "result")),
                    level=logging.ERROR)
                return None

            order_id = res["order_id"]
            # 查询订单执行情况
            order_info = self.OKCoinService.getOrderInfo(symbol, str(order_id))
            self.timeLog("下达如下okcoin限价买单,数量:%s,价格:%s" % (quantity, price))
            self.timeLog(str(order_info))

            retry_time = 0
            while retry_time < self.okcoin_order_query_retry_maximum_times and order_info["orders"][0]["status"] != 2:
                self.timeLog("等待%.1f秒直至订单完成" % self.orderWaitingTime)
                time.sleep(self.orderWaitingTime)
                order_info = self.OKCoinService.getOrderInfo(symbol, str(order_id))
                self.timeLog(str(order_info))
                retry_time += 1

            if order_info["orders"][0]["status"] != 2:
                cancel_result = self.OKCoinService.cancelOrder(symbol, str(order_id))
                retry_time = 0
                while retry_time < self.okcoin_order_cancel_query_retry_maximum_times and helper.componentExtract(
                        cancel_result, u"result", "") != True:
                    self.timeLog("等待%f秒直至订单取消完成" % self.orderWaitingTime)
                    time.sleep(self.orderWaitingTime)
                    cancel_result = self.OKCoinService.cancelOrder(symbol, str(order_id))
                    retry_time += 1
                order_info = self.OKCoinService.getOrderInfo(symbol, str(order_id))

            executed_qty = order_info["orders"][0]["deal_amount"]
            self.timeLog("okcoin限价买单已被执行,执行数量:%f,花费的现金:%.2f" % (
                executed_qty, executed_qty * order_info["orders"][0]["avg_price"]))
            return executed_qty
Ejemplo n.º 7
0
    def sell_market(self, security, quantity, exchange="huobi"):  # quantity is a string value
        if exchange == "huobi":
            self.timeLog("开始下达火币市价卖单...")
            self.timeLog("只保留下单数量的小数点后4位...")
            self.timeLog("原始下单数量:%s" % quantity)
            tmp = float(quantity)
            tmp = helper.downRound(tmp, 4)
            quantity = str(tmp)
            self.timeLog("做完小数点处理后的下单数量:%s" % quantity)
            if float(quantity) < self.huobi_min_quantity:
                self.timeLog(
                    "数量:%s 小于交易所最小交易数量(火币最小数量:%f),因此无法下单,此处忽略该信号" % (
                        quantity, self.huobi_min_quantity),
                    level=logging.WARN)
                return None

            symbol = helper.coinTypeStructure[self.coinMarketType]["huobi"]["coin_type"]
            # market = helper.coinTypeStructure[self.coinMarketType]["huobi"]["market"]

            res = self.HuobiService.sendOrder(quantity,None,"api",symbol,HB_SELL_MARKET)
            # res = self.HuobiService.sellMarket(
            #     coin_type, quantity, None, None, market, SELL_MARKET)
            if helper.componentExtract(res, u"result", "") != u'success':
                self.timeLog("下达火币市价卖单(数量:%s)失败, 结果是: %s!" % (quantity, helper.componentExtract(res, u"result", "")),
                             level=logging.ERROR)
                return None
            order_id = res[u"data"]
            # 查询订单执行情况
            order_info = self.HuobiService.getOrderInfo(order_id)
            self.timeLog("下达如下火币市价卖单,数量:%s" % quantity)
            self.timeLog(str(order_info))

            retry_time = 0
            while retry_time < self.huobi_order_query_retry_maximum_times and order_info["state"] != "filled":
                self.timeLog("等待%f秒直至订单完成" % self.orderWaitingTime)
                time.sleep(self.orderWaitingTime)
                order_info = self.HuobiService.getOrderInfo(order_id)
                self.timeLog(str(order_info))
                retry_time += 1

            executed_qty = float(order_info["field-amount"])
            self.timeLog(
                "火币市价卖单已被执行,执行数量:%f,收到的现金:%.2f" % (executed_qty, executed_qty * float(order_info["field-cash-amount"])))
            return executed_qty

        elif exchange == "okcoin":
            self.timeLog("开始下达okcoin市价卖单...")
            self.timeLog("只保留下单数量的小数点后2位...")
            self.timeLog("原始下单数量:%s" % quantity)
            tmp = float(quantity)
            tmp = helper.downRound(tmp, 2)
            quantity = str(tmp)
            self.timeLog("做完小数点处理后的下单数量:%s" % quantity)
            if float(quantity) < self.okcoin_min_quantity:
                self.timeLog(
                    "数量:%s 小于交易所最小交易数量(火币最小数量:%f),因此无法下单,此处忽略该信号" % (
                        quantity, self.okcoin_min_quantity),
                    level=logging.WARN)
                return None

            symbol = helper.coinTypeStructure[self.coinMarketType]["okcoin"]["coin_type"]

            res = self.OKCoinService.trade(symbol, OKC_SELL_MARKET, amount=quantity)
            if helper.componentExtract(res, "result") != True:
                self.timeLog("下达okcoin市价卖单(数量:%s)失败, 结果是:%s" % (quantity, helper.componentExtract(res, "result")),
                             level=logging.ERROR)
                return None
            order_id = res["order_id"]
            # 查询订单执行情况
            order_info = self.OKCoinService.getOrderInfo(symbol, str(order_id))
            self.timeLog("下达如下okcoin市价卖单,数量:%s" % quantity)
            self.timeLog(str(order_info))

            retry_time = 0
            while retry_time < self.okcoin_order_query_retry_maximum_times and order_info["orders"][0]["status"] != 2:
                self.timeLog("等待%.1f秒直至订单完成" % self.orderWaitingTime)
                time.sleep(self.orderWaitingTime)
                order_info = self.OKCoinService.getOrderInfo(symbol, str(order_id))
                self.timeLog(str(order_info))
                retry_time += 1

            executed_qty = order_info["orders"][0]["deal_amount"]
            self.timeLog("okcoin市价卖单已被执行,执行数量:%f,收到的现金:%.2f" % (
                executed_qty, executed_qty * order_info["orders"][0]["avg_price"]))
            return executed_qty
    def buy(self,
            security,
            cash_amount,
            exchange="huobi",
            sell_1_price=None):  # cash_amount is a string value
        if exchange == "huobi":
            self.timeLog("开始下达火币市价买单...")
            self.timeLog("只保留下单数量的小数点后2位...")
            self.timeLog("原始下单金额:%s" % cash_amount)
            tmp = float(cash_amount)
            tmp = helper.downRound(tmp, 2)
            cash_amount = str(tmp)
            self.timeLog("做完小数点处理后的下单金额:%s" % cash_amount)

            if float(cash_amount) < self.huobi_min_cash_amount:
                self.timeLog("金额:%s 小于交易所最小交易金额(火币最小金额:1元),因此无法下单,此处忽略该信号" %
                             (cash_amount, self.huobi_min_cash_amount))
                return None

            coin_type = helper.coinTypeStructure[
                self.coinMarketType]["huobi"]["coin_type"]
            res = self.huobiService.buyMarket(coin_type, cash_amount)
            if res[u"status"] == u'error':
                self.timeLog("下达火币市价買单(数量:%s)失败, 结果是: %s!" %
                             (quantity, res[u'err-msg']),
                             level=logging.ERROR)
                return None
            order_id = res[u"data"]
            # 查询订单执行情况
            # 查询订单执行情况
            order_info = self.HuobiService.getOrderInfo(order_id, market)
            order_info = order_info["data"][0]
            self.timeLog("下达如下火币限价卖单,数量:%s, 价格:%s" % (quantity, price))
            self.timeLog(str(order_info))

            retry_time = 0
            while retry_time < self.huobi_order_query_retry_maximum_times and order_info[
                    "state"] != "filled":
                self.timeLog("等待 1 秒直至订单完成" % self.orderWaitingTime)
                time.sleep(self.orderWaitingTime)
                order_info = self.HuobiService.getOrderInfo(order_id, market)
                self.timeLog(str(order_info))
                retry_time += 1

            # if order is not fully filled, cancel order if after timeout, cancel order is synchronized
            if order_info["status"] != "filled":
                cancel_result = self.HuobiService.cancelOrder(order_id)
                retry_time = 0
                while retry_time < self.huobi_order_cancel_query_retry_maximum_times and helper.componentExtract(
                        cancel_result, u"result", "") != u'success':
                    self.timeLog("等待%f秒直至订单取消完成" % self.orderWaitingTime)
                    time.sleep(self.orderWaitingTime)
                    cancel_result = self.HuobiService.cancelOrder(order_id)
                    retry_time += 1
                order_info = self.HuobiService.getOrderInfo(order_id, market)

            executed_qty = float(order_info["amount"])
            self.timeLog("火币市价买单已被执行,执行数量:%f,花费的现金:%.2f" %
                         (executed_qty, float(order_info["price"])))
            self.dataLog()
            return executed_qty
        elif exchange == "okcoin":
            if sell_1_price is None:
                raise ValueError(
                    "处理okcoin市价买单之前,需要提供当前Okcoin卖一的价格信息,请检查传入的sell_1_price参数是否完备!"
                )
            self.timeLog("开始下达okcoin市价买单...")
            self.timeLog("只保留下单数量的小数点后2位...")
            self.timeLog("原始下单金额:%s" % cash_amount)
            tmp = float(cash_amount)
            tmp = helper.downRound(tmp, 2)
            cash_amount = str(tmp)
            self.timeLog("做完小数点处理后的下单金额:%s" % cash_amount)

            if float(cash_amount) < self.okcoin_min_quantity * sell_1_price:
                self.timeLog(
                    "金额:%s 不足以购买交易所最小交易数量(okcoin最小数量:%f,当前卖一价格%.2f,最小金额要求:%.2f),因此无法下单,此处忽略该信号"
                    % (cash_amount, self.okcoin_min_quantity, sell_1_price,
                       self.okcoin_min_quantity * sell_1_price))
                return None
            res = self.okcoinService.trade(helper.coinTypeStructure[
                self.coinMarketType]["okcoin"]["coin_type"],
                                           1,
                                           price=cash_amount)

            if res["msg"] != "success":
                self.timeLog("下达okcoin市价買单(数量:%s)失败" % quantity)
                return None
            order_id = res["data"]
            # 查询订单执行情况
            order_info = self.okcoinService.orderinfo(
                helper.coinTypeStructure[self.coinMarketType]["okcoin"]
                ["coin_type"], str(order_id))
            self.timeLog("下达如下okcoin市价买单,金额:%s" % cash_amount)
            self.timeLog(str(order_info))
            if order_info["data"]["status"] != 2:
                self.timeLog("等待%.1f秒直至订单完成" % self.orderWaitingTime)
                time.sleep(self.orderWaitingTime)
                order_info = self.okcoinService.orderinfo(
                    helper.coinTypeStructure[self.coinMarketType]["okcoin"]
                    ["coin_type"], str(order_id))
                self.timeLog(str(order_info))
            executed_qty = order_info["data"]["quantity"]
            self.timeLog(
                "okcoin市价买单已被执行,执行数量:%f,花费的现金:%.2f" %
                (executed_qty, executed_qty * order_info["data"]["price"]))
            self.dataLog()
            return executed_qty
    def sell(self,
             security,
             quantity,
             exchange="huobi"):  # quantity is a string value
        if exchange == "huobi":
            self.timeLog("开始下达火币市价卖单...")
            self.timeLog("只保留下单数量的小数点后4位...")
            self.timeLog("原始下单数量:%s" % quantity)
            tmp = float(quantity)
            tmp = helper.downRound(tmp, 4)
            quantity = str(tmp)
            self.timeLog("做完小数点处理后的下单数量:%s" % quantity)
            if float(quantity) < self.huobi_min_quantity:
                self.timeLog("数量:%s 小于交易所最小交易数量(火币最小数量:%f),因此无法下单,此处忽略该信号" %
                             (quantity, self.huobi_min_quantity))
                return None

            coin_type = helper.coinTypeStructure[
                self.coinMarketType]["huobi"]["sell_type"]
            res = self.huobiService.sellMarket(coin_type, quantity)
            if res[u"status"] == u'error':
                self.timeLog("下达火币市价卖单(数量:%s)失败, 结果是: %s!" %
                             (quantity, res[u'err-msg']),
                             level=logging.ERROR)
                return None
            order_id = res[u"data"]
            # 查询订单执行情况
            order_info = self.HuobiService.getOrderInfo(order_id, market)
            self.timeLog("下达如下火币限价卖单,数量:%s, 价格:%s" % (quantity, price))
            self.timeLog(str(order_info))

            retry_time = 0
            while retry_time < self.huobi_order_query_retry_maximum_times and order_info[
                    "state"] != "filled":
                self.timeLog("等待 1 秒直至订单完成" % self.orderWaitingTime)
                time.sleep(self.orderWaitingTime)
                order_info = self.HuobiService.getOrderInfo(order_id, market)
                self.timeLog(str(order_info))
                retry_time += 1

            # if order is not fully filled, cancel order if after timeout, cancel order is synchronized
            if order_info["state"] != "filled":
                cancel_result = self.HuobiService.cancelOrder(order_id)
                retry_time = 0
                while retry_time < self.huobi_order_cancel_query_retry_maximum_times and helper.componentExtract(
                        cancel_result, u"result", "") != u'success':
                    self.timeLog("等待%f秒直至订单取消完成" % self.orderWaitingTime)
                    time.sleep(self.orderWaitingTime)
                    cancel_result = self.HuobiService.cancelOrder(order_id)
                    retry_time += 1
                order_info = self.HuobiService.getOrderInfo(order_id, market)

            executed_qty = float(order_info["amount"])
            self.timeLog(
                "火币限价卖单已被执行,执行数量:%f,收到的现金:%.2f" %
                (executed_qty, executed_qty * float(order_info["price"])))
            return executed_qty
        elif exchange == "okcoin":
            self.timeLog("开始下达okcoin市价卖单...")
            self.timeLog("只保留下单数量的小数点后2位...")
            self.timeLog("原始下单数量:%s" % quantity)
            tmp = float(quantity)
            tmp = helper.downRound(tmp, 2)
            quantity = str(tmp)
            self.timeLog("做完小数点处理后的下单数量:%s" % quantity)
            if float(quantity) < self.okcoin_min_quantity:
                self.timeLog("数量:%s 小于交易所最小交易数量(火币最小数量:%f),因此无法下单,此处忽略该信号" %
                             (quantity, self.okcoin_min_quantity))
                return None
            res = self.okcoinService.trade(helper.coinTypeStructure[
                self.coinMarketType]["okcoin"]["coin_type"],
                                           -1,
                                           amount=quantity)
            if res["msg"] != "success":
                self.timeLog("下达okcoin市价卖单(数量:%s)失败" % quantity)
                return None
            order_id = res["data"]
            # 查询订单执行情况
            order_info = self.okcoinService.orderinfo(
                helper.coinTypeStructure[self.coinMarketType]["okcoin"]
                ["coin_type"], str(order_id))
            self.timeLog("下达如下okcoin市价卖单,数量:%s" % quantity)
            self.timeLog(str(order_info))
            if order_info["data"]["status"] != 2:
                self.timeLog("等待%.1f秒直至订单完成" % self.orderWaitingTime)
                time.sleep(self.orderWaitingTime)
                order_info = self.okcoinService.orderinfo(
                    helper.coinTypeStructure[self.coinMarketType]["okcoin"]
                    ["coin_type"], str(order_id))
                self.timeLog(str(order_info))
            executed_qty = order_info["data"]["quantity"]
            self.timeLog(
                "okcoin市价卖单已被执行,执行数量:%f,收到的现金:%.2f" %
                (executed_qty, executed_qty * order_info["data"]["price"]))
            self.dataLog()
            return executed_qty
Ejemplo n.º 10
0
    def buy_market(self, security, cash_amount, exchange="huobi", sell_1_price=None):  # cash_amount is a string value
        if exchange == "huobi":
            self.timeLog("开始下达火币市价买单...")
            self.timeLog("只保留下单数量的小数点后2位...")
            self.timeLog("原始下单金额:%s" % cash_amount)
            tmp = float(cash_amount)
            tmp = helper.downRound(tmp, 2)
            cash_amount = str(tmp)
            self.timeLog("做完小数点处理后的下单金额:%s" % cash_amount)

            if float(cash_amount) < self.huobi_min_cash_amount:
                self.timeLog("金额:%s 小于交易所最小交易金额(火币最小金额:1元),因此无法下单,此处忽略该信号" % (cash_amount, self.huobi_min_cash_amount),
                             level=logging.WARN)
                return None

            coin_type = helper.coinTypeStructure[self.coinMarketType]["huobi"]["coin_type"]
            market = helper.coinTypeStructure[self.coinMarketType]["huobi"]["market"]

            res = self.HuobiService.buyMarket(coin_type, cash_amount, None, None, market, BUY_MARKET)
            if helper.componentExtract(res, u"result", "") != u'success':
                self.timeLog("下达火币市价买单(金额:%s)失败, 结果是:%s!" % (cash_amount, helper.componentExtract(res, u"result", "")),
                             level=logging.ERROR)
                return None
            order_id = res[u"id"]
            # 查询订单执行情况
            order_info = self.HuobiService.getOrderInfo(coin_type, order_id, market, ORDER_INFO)
            self.timeLog("下达如下火币市价买单,金额:%s" % cash_amount)
            self.timeLog(str(order_info))

            retry_time = 0
            while retry_time < self.huobi_order_query_retry_maximum_times and order_info["status"] != 2:
                self.timeLog("等待%f秒直至订单完成" % self.orderWaitingTime)
                time.sleep(self.orderWaitingTime)
                order_info = self.HuobiService.getOrderInfo(coin_type, order_id, market, ORDER_INFO)
                self.timeLog(str(order_info))
                retry_time += 1

            if float(order_info["processed_price"]) > 0:
                executed_qty = float(order_info["processed_amount"]) / float(order_info["processed_price"])
                self.timeLog("火币市价买单已被执行,执行数量:%f,花费的现金:%.2f" % (executed_qty, float(order_info["processed_amount"])))
                return executed_qty
            else:
                self.timeLog("火币市价买单未被执行", level=logging.WARN)
                return 0

        elif exchange == "okcoin":
            if sell_1_price is None:
                raise ValueError("处理okcoin市价买单之前,需要提供当前Okcoin卖一的价格信息,请检查传入的sell_1_price参数是否完备!")
            self.timeLog("开始下达okcoin市价买单...")
            self.timeLog("只保留下单数量的小数点后2位...")
            self.timeLog("原始下单金额:%s" % cash_amount)
            tmp = float(cash_amount)
            tmp = helper.downRound(tmp, 2)
            cash_amount = str(tmp)
            self.timeLog("做完小数点处理后的下单金额:%s" % cash_amount)

            if float(cash_amount) < self.okcoin_min_quantity * sell_1_price:
                self.timeLog(
                    "金额:%s 不足以购买交易所最小交易数量(okcoin最小数量:%f,当前卖一价格%.2f,最小金额要求:%.2f),因此无法下单,此处忽略该信号" % (
                        cash_amount, self.okcoin_min_quantity, sell_1_price, self.okcoin_min_quantity * sell_1_price),
                    level=logging.WARN)
                return None

            coin_type = helper.coinTypeStructure[self.coinMarketType]["okcoin"]["coin_type"]
            res = self.OKCoinService.trade(coin_type, "buy_market", price=cash_amount)

            if helper.componentExtract(res, "result") != True:
                self.timeLog("下达okcoin市价买单(金额:%s)失败, 结果是:%s!" % (cash_amount, helper.componentExtract(res, "result")),
                             level=logging.ERROR)
                return None
            order_id = res["order_id"]
            # 查询订单执行情况
            order_info = self.OKCoinService.orderInfo(coin_type, str(order_id))
            self.timeLog("下达如下okcoin市价买单,金额:%s" % cash_amount)
            self.timeLog(str(order_info))

            retry_time = 0
            while retry_time < self.okcoin_order_query_retry_maximum_times and order_info["orders"][0]["status"] != 2:
                self.timeLog("等待%.1f秒直至订单完成" % self.orderWaitingTime)
                time.sleep(self.orderWaitingTime)
                order_info = self.OKCoinService.orderInfo(coin_type, str(order_id))
                self.timeLog(str(order_info))
                retry_time += 1

            executed_qty = order_info["orders"][0]["deal_amount"]
            self.timeLog("okcoin市价买单已被执行,执行数量:%f,花费的现金:%.2f" % (
                executed_qty, executed_qty * order_info["orders"][0]["avg_price"]))
            return executed_qty
Ejemplo n.º 11
0
    def buy_limit(self, security, price, quantity, exchange="huobi"):
        if exchange == "huobi":
            self.timeLog("开始下达火币限价买单...")
            self.timeLog("只保留下单数量的小数点后4位,下单价格的小数点后2位...")
            self.timeLog("原始下单数量:%s" % quantity)
            tmp = float(quantity)
            tmp = helper.downRound(tmp, 4)
            quantity = str(tmp)
            self.timeLog("做完小数点处理后的下单数量:%s" % quantity)

            if float(quantity) < self.huobi_min_quantity:
                self.timeLog(
                    "数量:%s 小于交易所最小交易数量(火币最小数量:%f),因此无法下单,此处忽略该信号" % (quantity, self.huobi_min_quantity),
                    level=logging.WARN)
                return None

            self.timeLog("原始下单价格:%s" % price)
            tmp = float(price)
            tmp = helper.downRound(tmp, 2)
            price = str(tmp)
            self.timeLog("做完小数点处理后的下单价格:%s" % price)

            coin_type = helper.coinTypeStructure[self.coinMarketType]["huobi"]["coin_type"]
            market = helper.coinTypeStructure[self.coinMarketType]["huobi"]["market"]

            res = self.HuobiService.buy(coin_type, price, quantity, None, None, market, BUY)
            if helper.componentExtract(res, u"result", "") != u'success':
                self.timeLog(
                    "下达火币限价买单(数量:%s,价格:%s)失败, 结果是:%s!" % (quantity, price, helper.componentExtract(res, u"result", "")),
                    level=logging.ERROR)
                return None
            order_id = res[u"id"]
            # 查询订单执行情况
            order_info = self.HuobiService.getOrderInfo(coin_type, order_id, market, ORDER_INFO)
            self.timeLog("下达如下火币限价买单,数量:%s, 价格:%s" % (quantity, price))
            self.timeLog(str(order_info))

            retry_time = 0
            while retry_time < self.huobi_order_query_retry_maximum_times and order_info["status"] != 2:
                self.timeLog("等待%f秒直至订单完成" % self.orderWaitingTime)
                time.sleep(self.orderWaitingTime)
                order_info = self.HuobiService.getOrderInfo(coin_type, order_id, market, ORDER_INFO)
                self.timeLog(str(order_info))
                retry_time += 1

                # if order is not fully filled, cancel order if after timeout, cancel order is synchronized
            if order_info["status"] != 2:
                cancel_result = self.HuobiService.cancelOrder(coin_type, order_id, market, CANCEL_ORDER)
                retry_time = 0
                while retry_time < self.huobi_order_cancel_query_retry_maximum_times and helper.componentExtract(
                        cancel_result, u"result", "") != u'success':
                    self.timeLog("等待%f秒直至订单取消完成" % self.orderWaitingTime)
                    time.sleep(self.orderWaitingTime)
                    cancel_result = self.HuobiService.cancelOrder(coin_type, order_id, market, CANCEL_ORDER)
                    retry_time += 1
                order_info = self.HuobiService.getOrderInfo(coin_type, order_id, market, ORDER_INFO)

            if float(order_info["processed_price"]) > 0:
                executed_qty = float(order_info["processed_amount"])
                self.timeLog("火币限价买单已被执行,执行数量:%f,花费的现金:%.2f" % (
                    executed_qty, float(order_info["processed_price"]) * executed_qty))
                return executed_qty
            else:
                self.timeLog("火币限价买单未被执行", level=logging.WARN)
                return None

        elif exchange == "okcoin":
            self.timeLog("开始下达okcoin限价买单...")
            self.timeLog("只保留下单数量的小数点后2位...")
            self.timeLog("原始下单数量:%s" % quantity)
            tmp = float(quantity)
            tmp = helper.downRound(tmp, 2)
            quantity = str(tmp)
            self.timeLog("做完小数点处理后的下单数量:%s" % quantity)
            if float(quantity) < self.okcoin_min_quantity:
                self.timeLog(
                    "数量:%s 小于交易所最小交易数量(okcoin最小数量:%f),因此无法下单,此处忽略该信号" % (quantity, self.okcoin_min_quantity),
                    level=logging.WARN)
                return None

            self.timeLog("原始下单价格:%s" % price)
            tmp = float(price)
            tmp = helper.downRound(tmp, 2)
            price = str(tmp)
            self.timeLog("做完小数点处理后的下单价格:%s" % price)

            coin_type = helper.coinTypeStructure[self.coinMarketType]["okcoin"]["coin_type"]

            res = self.OKCoinService.trade(coin_type, "buy", price=price, amount=quantity)

            if helper.componentExtract(res, "result") != True:
                self.timeLog(
                    "下达okcoin限价买单(数量:%s,价格:%s)失败, 结果是:%s!" % (quantity, price, helper.componentExtract(res, "result")),
                    level=logging.ERROR)
                return None

            order_id = res["order_id"]
            # 查询订单执行情况
            order_info = self.OKCoinService.orderInfo(coin_type, str(order_id))
            self.timeLog("下达如下okcoin限价买单,数量:%s,价格:%s" % (quantity, price))
            self.timeLog(str(order_info))

            retry_time = 0
            while retry_time < self.okcoin_order_query_retry_maximum_times and order_info["orders"][0]["status"] != 2:
                self.timeLog("等待%.1f秒直至订单完成" % self.orderWaitingTime)
                time.sleep(self.orderWaitingTime)
                order_info = self.OKCoinService.orderInfo(coin_type, str(order_id))
                self.timeLog(str(order_info))
                retry_time += 1

            if order_info["orders"][0]["status"] != 2:
                cancel_result = self.OKCoinService.cancelOrder(coin_type, str(order_id))
                retry_time = 0
                while retry_time < self.okcoin_order_cancel_query_retry_maximum_times and helper.componentExtract(
                        cancel_result, u"result", "") != True:
                    self.timeLog("等待%f秒直至订单取消完成" % self.orderWaitingTime)
                    time.sleep(self.orderWaitingTime)
                    cancel_result = self.OKCoinService.cancelOrder(coin_type, str(order_id))
                    retry_time += 1
                order_info = self.OKCoinService.orderInfo(coin_type, str(order_id))

            executed_qty = order_info["orders"][0]["deal_amount"]
            self.timeLog("okcoin限价买单已被执行,执行数量:%f,花费的现金:%.2f" % (
                executed_qty, executed_qty * order_info["orders"][0]["avg_price"]))
            return executed_qty