Example #1
0
 async def _on_message(self, msg):
     if msg == {"event": "pong"}:
         logger.info("get pong from okexserver")
     else:
         for data_item in msg:
             try:
                 if "ticker" in data_item["channel"]:
                     getattr(self.update_handler, "ticker")(data_item)
                 elif "kline" in data_item["channel"]:
                     getattr(self.update_handler, "k_line")(data_item)
                 elif "depth" in data_item["channel"]:
                     getattr(self.update_handler, "depth")(data_item)
                 elif "addChannel" == data_item["channel"]:
                     logger.info(f"频道{data_item['data']['channel']}订阅成功")
             except Exception as e:
                 logger.exception("异常")
Example #2
0
                async def begin_trade(build_type="buy_long", price=None):
                    if build_type == "buy_long":
                        self.current_flag = "买多建仓"
                    if build_type == "buy_short":
                        self.current_flag = "买空平仓"
                    if build_type == "sell_long":
                        self.current_flag = "卖多平仓"
                    if build_type == "sell_short":
                        self.current_flag = "卖空建仓"

                    logger.info(f"当前的交易标志:{self.current_flag}")
                    # 预设交易信息
                    trade = self.okex_market.api.trade().lever_rate(10).symbol(
                        "btc_usd").contract_type("quarter")
                    # 开仓或者平仓,以市价成交
                    aa = build_type.split("_")
                    # 采用定价方式,使用这种方式即可
                    # trade.start(aa[0], aa[1]).amount(1).price(price)
                    trade.start(aa[0], aa[1]).amount(1).as_market_price()
                    result = await trade.go()
                    # 创建order
                    order = self.okex_market.api.order(result["order_id"],
                                                       symbol="btc_usd",
                                                       contract_type="quarter")

                    # 更新order信息
                    # 0等待成交 1部分成交 2全部成交 -1撤单 4撤单处理中 5撤单中
                    # 等待单全部完成
                    while True:
                        await order.info()
                        if order.status == 2:
                            break
                        else:
                            await asyncio.sleep(30)

                    # 如果当前是平仓的操作,则重置交易标志位
                    if "平仓" in self.current_flag:
                        logger.debug("平仓成功,重置标志位,进行下一轮")
                        self.current_flag = None
Example #3
0
    async def start_rule(self):
        # 订阅频道

        await self.okex_market.api.sub_channel(
            "ok_sub_futureusd_btc_ticker_quarter")
        await self.okex_market.api.sub_channel(
            "ok_sub_futureusd_btc_kline_quarter_1min")
        # 如果无需订阅当前频道,可注释
        # await self.okex_market.api.sub_channel("ok_sub_futureusd_btc_depth_this_week")

        # 保持程序运行
        while True:
            # 每隔60s苏醒一次
            await asyncio.sleep(60)
            logger.info("开始尝试规则")
            if len(self.kline) > 20:
                M50 = list(itertools.islice(self.kline, 0, 20))
                M10 = M50[0:10]

                def compute_avg(data_list):
                    # 如果需要计算的话,可以讲func a改写成一些基本的计算函数
                    def a(item):
                        return item

                    return mean(map(lambda x: x.open_price, data_list))

                MV50 = compute_avg(M50)
                MV10 = compute_avg(M10)
                logger.info(f"计算MV50和MV10:{MV50}---{MV10}")

                # build_type buy_long,buy_short,sell_long,sell_short
                async def begin_trade(build_type="buy_long", price=None):
                    if build_type == "buy_long":
                        self.current_flag = "买多建仓"
                    if build_type == "buy_short":
                        self.current_flag = "买空平仓"
                    if build_type == "sell_long":
                        self.current_flag = "卖多平仓"
                    if build_type == "sell_short":
                        self.current_flag = "卖空建仓"

                    logger.info(f"当前的交易标志:{self.current_flag}")
                    # 预设交易信息
                    trade = self.okex_market.api.trade().lever_rate(10).symbol(
                        "btc_usd").contract_type("quarter")
                    # 开仓或者平仓,以市价成交
                    aa = build_type.split("_")
                    # 采用定价方式,使用这种方式即可
                    # trade.start(aa[0], aa[1]).amount(1).price(price)
                    trade.start(aa[0], aa[1]).amount(1).as_market_price()
                    result = await trade.go()
                    # 创建order
                    order = self.okex_market.api.order(result["order_id"],
                                                       symbol="btc_usd",
                                                       contract_type="quarter")

                    # 更新order信息
                    # 0等待成交 1部分成交 2全部成交 -1撤单 4撤单处理中 5撤单中
                    # 等待单全部完成
                    while True:
                        await order.info()
                        if order.status == 2:
                            break
                        else:
                            await asyncio.sleep(30)

                    # 如果当前是平仓的操作,则重置交易标志位
                    if "平仓" in self.current_flag:
                        logger.debug("平仓成功,重置标志位,进行下一轮")
                        self.current_flag = None

                if MV10 >= MV50:
                    if self.current_flag is None:
                        logger.info(f"逻辑进入:买多建仓")
                        await begin_trade("buy_long")
                else:
                    if self.current_flag == "sell_short":
                        logger.info(f"逻辑进入:买空平仓")
                        await begin_trade("buy_short")

                if MV10 < MV50:
                    if self.current_flag is None:
                        logger.info(f"逻辑进入:卖空建仓")
                        await begin_trade("sell_short")
                else:
                    if self.current_flag == "sell_short":
                        logger.info(f"逻辑进入:卖多平仓")
                        await begin_trade("sell_long")
Example #4
0
 async def sub_info(self):
     # 订阅市场消息
     # ok_sub_futureusd_X_ticker_Y 订阅合约行情
     # ok_sub_futureusd_X_trade_Y 订阅合约交易信息
     logger.info("订阅市场消息")
     await self.ws_api.sub_channels("ok_sub_futureusd_btc_ticker_quarter")
Example #5
0
 async def sub_info(self):
     logger.info("订阅频道")
     # await self.wsapi.sub_channels("instrument", "quote")
     await self.ws_api.sub_channels("quote")