Esempio n. 1
0
    def __init__(self):
        """ 初始化
        """
        self.strategy = config.strategy
        self.platform = config.accounts[0]["platform"]
        self.account = config.accounts[0]["account"]
        self.access_key = config.accounts[0]["access_key"]
        self.secret_key = config.accounts[0]["secret_key"]
        self.host = config.accounts[0]["host"]
        self.wss = config.accounts[0]["wss"]

        self.future_platform = config.accounts[1]["platform"]
        self.future_account = config.accounts[1]["account"]
        self.future_access_key = config.accounts[1]["access_key"]
        self.future_secret_key = config.accounts[1]["secret_key"]
        self.future_host = config.accounts[1]["host"]
        self.future_wss = config.accounts[1]["wss"]

        self.symbol = config.markets[0]["symbol"]
        self.channels = config.markets[0]["channels"]
        self.orderbook_length = config.markets[0]["orderbook_length"]
        self.orderbooks_length = config.markets[0]["orderbooks_length"]
        self.klines_length = config.markets[0]["klines_length"]
        self.trades_length = config.markets[0]["trades_length"]
        self.market_wss = config.markets[0]["wss"]

        self.future_symbol = config.markets[1]["symbol"]
        self.future_contract_type = config.markets[1]["contract_type"]
        self.future_channels = config.markets[1]["channels"]
        self.future_orderbook_length = config.markets[1]["orderbook_length"]
        self.future_orderbooks_length = config.markets[1]["orderbooks_length"]
        self.future_klines_length = config.markets[1]["klines_length"]
        self.future_trades_length = config.markets[1]["trades_length"]
        self.future_market_wss = config.markets[1]["wss"]

        self.orderbook_invalid_seconds = 100  # orderbook无效时间
        self.spread = 1  # 价差设定
        self.volume = 1  # 每次开仓数量
        self.max_quantity = 10  # 最大仓位数量(多仓的最大仓位数量和空仓的最大数量)
        self.delta_limit = 1  # delta超过多少进行对冲
        self.future_volume_usd = 100  # 交割合约面值

        self.last_bid_price = 0  # 上次的买入价格
        self.last_ask_price = 0  # 上次的卖出价格
        self.last_orderbook_timestamp = 0  # 上次的orderbook时间戳
        self.last_mark_price = 0  # 上次的标记价格

        self.raw_symbol = self.symbol.split('-')[0]
        self.partition_symbol = self.symbol.split('-')[1]

        self.ask1_price = 0
        self.bid1_price = 0
        self.ask1_volume = 0
        self.bid1_volume = 0

        self.mark_price = 0  # 标记价格

        # 交易模块
        cc = {
            "strategy": self.strategy,
            "platform": self.platform,
            "symbol": self.symbol,
            "account": self.account,
            "access_key": self.access_key,
            "secret_key": self.secret_key,
            "host": self.host,
            "wss": self.wss,
            "order_update_callback": self.on_event_order_update,
            "asset_update_callback": self.on_event_asset_update,
            "position_update_callback": self.on_event_position_update,
            "init_success_callback": self.on_event_init_success_callback,
        }
        self.trader = Trade(**cc)

        future_cc = {
            "strategy": self.strategy,
            "platform": self.future_platform,
            "symbol": self.future_symbol,
            "contract_type": self.future_contract_type,
            "account": self.future_account,
            "access_key": self.future_access_key,
            "secret_key": self.future_secret_key,
            "host": self.future_host,
            "wss": self.future_wss,
            "order_update_callback": self.on_event_order_update_future,
            "asset_update_callback": self.on_event_asset_update_future,
            "position_update_callback": self.on_event_position_update_future,
            "init_success_callback":
            self.on_event_init_success_callback_future,
        }
        self.future_trader = Trade(**future_cc)

        # 行情模块
        cc = {
            "platform": self.platform,
            "symbols": [self.symbol],
            "channels": self.channels,
            "orderbook_length": self.orderbook_length,
            "orderbooks_length": self.orderbooks_length,
            "klines_length": self.klines_length,
            "trades_length": self.trades_length,
            "wss": self.market_wss,
            "orderbook_update_callback": self.on_event_orderbook_update,
            "kline_update_callback": self.on_event_kline_update,
            "trade_update_callback": self.on_event_trade_update
        }
        self.market = Market(**cc)

        if self.future_contract_type == "this_week":
            self.future_contract_code = self.future_symbol + "_CW"
        elif self.future_contract_type == "next_week":
            self.future_contract_code = self.future_symbol + "_NW"
        elif self.future_contract_type == "quarter":
            self.future_contract_code = self.future_symbol + "_CQ"

        # 行情模块
        market_cc = {
            "platform": self.future_platform,
            "symbols": [self.future_contract_code],
            "channels": self.future_channels,
            "orderbook_length": self.future_orderbook_length,
            "orderbooks_length": self.future_orderbooks_length,
            "klines_length": self.future_klines_length,
            "trades_length": self.future_trades_length,
            "wss": self.future_market_wss,
            "orderbook_update_callback": self.on_event_orderbook_update_future,
            "kline_update_callback": self.on_event_kline_update_future,
            "trade_update_callback": self.on_event_trade_update_future
        }
        self.future_market = Market(**market_cc)

        # 10秒执行1次
        LoopRunTask.register(self.on_ticker, 10)

        # delta对冲
        LoopRunTask.register(self.delta_hedging, 10)
Esempio n. 2
0
    def __init__(self):
        """ 初始化
        """
        self.strategy = config.strategy
        self.platform = config.accounts[0]["platform"]
        self.account = config.accounts[0]["account"]
        self.access_key = config.accounts[0]["access_key"]
        self.secret_key = config.accounts[0]["secret_key"]
        self.host = config.accounts[0]["host"]
        self.wss = config.accounts[0]["wss"]
        self.symbol = config.symbol
        self.contract_type = config.contract_type
        self.channels = config.markets[0]["channels"]
        self.orderbook_length = config.markets[0]["orderbook_length"]
        self.orderbooks_length = config.markets[0]["orderbooks_length"]
        self.klines_length = config.markets[0]["klines_length"]
        self.trades_length = config.markets[0]["trades_length"]
        self.market_wss = config.markets[0]["wss"]

        self.orderbook_invalid_seconds = 5

        self.last_bid_price = 0  # 上次的买入价格
        self.last_ask_price = 0  # 上次的卖出价格
        self.last_orderbook_timestamp = 0  # 上次的orderbook时间戳

        self.raw_symbol = self.symbol.split(
            '_')[0] if self.contract_type != 'SWAP' else self.symbol.split(
                '-')[0]

        self.ask1_price = 0
        self.bid1_price = 0
        self.ask1_volume = 0
        self.bid1_volume = 0

        if self.contract_type == "this_week":
            self.future_contract_code = self.symbol + "_CW"
        elif self.contract_type == "next_week":
            self.future_contract_code = self.symbol + "_NW"
        elif self.contract_type == "quarter":
            self.future_contract_code = self.symbol + "_CQ"

        # 交易模块
        cc = {
            "strategy": self.strategy,
            "platform": self.platform,
            "symbol": self.symbol,
            "contract_type": self.contract_type,
            "account": self.account,
            "access_key": self.access_key,
            "secret_key": self.secret_key,
            "host": self.host,
            "wss": self.wss,
            "order_update_callback": self.on_event_order_update,
            "asset_update_callback": self.on_event_asset_update,
            "position_update_callback": self.on_event_position_update,
            "init_success_callback": self.on_event_init_success_callback,
        }
        self.trader = Trade(**cc)

        # 行情模块
        cc = {
            "platform": self.platform,
            "symbols": [self.future_contract_code],
            "channels": self.channels,
            "orderbook_length": self.orderbook_length,
            "orderbooks_length": self.orderbooks_length,
            "klines_length": self.klines_length,
            "trades_length": self.trades_length,
            "wss": self.market_wss,
            "orderbook_update_callback": self.on_event_orderbook_update,
            "kline_update_callback": self.on_event_kline_update,
            "trade_update_callback": self.on_event_trade_update
        }
        self.market = Market(**cc)

        # 60秒执行1次
        LoopRunTask.register(self.on_ticker, 60)
Esempio n. 3
0
    def __init__(self, **kwargs):
        """ 初始化
        """
        self.strategy = config.strategy
        self.platform = config.accounts[0]["platform"]
        self.account = config.accounts[0]["account"]
        self.access_key = config.accounts[0]["access_key"]
        self.secret_key = config.accounts[0]["secret_key"]
        self.host = config.accounts[0]["host"]
        self.wss = config.accounts[0]["wss"]
        self.contract_type = config.contract_type
        self.channels = config.markets[0]["channels"]
        self.orderbook_length = config.markets[0]["orderbook_length"]
        self.orderbooks_length = config.markets[0]["orderbooks_length"]
        self.klines_length = config.markets[0]["klines_length"]
        self.trades_length = config.markets[0]["trades_length"]
        self.market_wss = config.markets[0]["wss"]

        self.symbol = kwargs.get("symbol")
        self.contract_size = kwargs.get("contract_size")

        self.orderbook_invalid_seconds = 10

        self.last_bid_price = 0  # 上次的买入价格
        self.last_ask_price = 0  # 上次的卖出价格
        self.last_orderbook_timestamp = 0  # 上次的orderbook时间戳

        self.raw_symbol = self.symbol.split('-')[1].upper()

        self.ask1_price = 0
        self.bid1_price = 0
        self.ask1_volume = 0
        self.bid1_volume = 0

        self.total_count = 0
        self.exceed_1s_count = 0
        self.exceed_100ms_count = 0
        self.exceed_50ms_count = 0

        self.orderids = {}

        # 交易模块
        cc = {
            "strategy": self.strategy,
            "platform": self.platform,
            "symbol": self.symbol,
            "contract_type": self.contract_type,
            "account": self.account,
            "access_key": self.access_key,
            "secret_key": self.secret_key,
            "host": self.host,
            "wss": self.wss,
            "order_update_callback": self.on_event_order_update,
            "asset_update_callback": self.on_event_asset_update,
            "position_update_callback": self.on_event_position_update,
            "init_success_callback": self.on_event_init_success_callback,
        }
        self.trader = Trade(**cc)

        # 行情模块
        cc = {
            "platform": self.platform,
            "symbols": [self.symbol],
            "channels": self.channels,
            "orderbook_length": self.orderbook_length,
            "orderbooks_length": self.orderbooks_length,
            "klines_length": self.klines_length,
            "trades_length": self.trades_length,
            "wss": self.market_wss,
            "orderbook_update_callback": self.on_event_orderbook_update,
            "kline_update_callback": self.on_event_kline_update,
            "trade_update_callback": self.on_event_trade_update
        }
        self.market = Market(**cc)

        # 60秒执行1次
        LoopRunTask.register(self.on_ticker, 2)

        LoopRunTask.register(self.on_showresult, 10)
Esempio n. 4
0
    def __init__(self):
        """ 初始化
        """
        self.strategy = config.strategy
        self.platform = config.accounts[0]["platform"]
        self.account = config.accounts[0]["account"]
        self.access_key = config.accounts[0]["access_key"]
        self.secret_key = config.accounts[0]["secret_key"]
        self.host = config.accounts[0]["host"]
        self.wss = config.accounts[0]["wss"]

        self.swap_platform = config.accounts[1]["platform"]
        self.swap_account = config.accounts[1]["account"]
        self.swap_access_key = config.accounts[1]["access_key"]
        self.swap_secret_key = config.accounts[1]["secret_key"]
        self.swap_host = config.accounts[1]["host"]
        self.swap_wss = config.accounts[1]["wss"]

        self.symbol = config.markets[0]["symbol"]
        self.channels = config.markets[0]["channels"]
        self.orderbook_length = config.markets[0]["orderbook_length"]
        self.orderbooks_length = config.markets[0]["orderbooks_length"]
        self.klines_length = config.markets[0]["klines_length"]
        self.trades_length = config.markets[0]["trades_length"]
        self.market_wss = config.markets[0]["wss"]

        self.swap_symbol = config.markets[1]["symbol"]
        self.swap_contract_type = config.markets[1]["contract_type"]
        self.swap_channels = config.markets[1]["channels"]
        self.swap_orderbook_length = config.markets[1]["orderbook_length"]
        self.swap_orderbooks_length = config.markets[1]["orderbooks_length"]
        self.swap_klines_length = config.markets[1]["klines_length"]
        self.swap_trades_length = config.markets[1]["trades_length"]
        self.swap_market_wss = config.markets[1]["wss"]

        self.orderbook_invalid_seconds = config.orderbook_invalid_seconds
        self.spread = config.spread
        self.volume = config.quantity
        self.max_quantity = config.max_quantity
        self.delta_limit = config.delta_limit
        self.swap_volume_usd = config.swap_volume_usd

        self.last_bid_price = 0  # 上次的买入价格
        self.last_ask_price = 0  # 上次的卖出价格
        self.last_orderbook_timestamp = 0  # 上次的orderbook时间戳
        self.last_mark_price = 0  # 上次的标记价格

        self.raw_symbol = self.symbol.split('-')[0]
        self.partition_symbol = self.symbol.split('-')[1]

        self.ask1_price = 0
        self.bid1_price = 0
        self.ask1_volume = 0
        self.bid1_volume = 0

        self.mark_price = 0  # 标记价格

        # 交易模块
        cc = {
            "strategy": self.strategy,
            "platform": self.platform,
            "symbol": self.symbol,
            "account": self.account,
            "access_key": self.access_key,
            "secret_key": self.secret_key,
            "host": self.host,
            "wss": self.wss,
            "order_update_callback": self.on_event_order_update,
            "asset_update_callback": self.on_event_asset_update,
            "position_update_callback": self.on_event_position_update,
            "init_success_callback": self.on_event_init_success_callback,
        }
        self.trader = Trade(**cc)

        swap_cc = {
            "strategy": self.strategy,
            "platform": self.swap_platform,
            "symbol": self.swap_symbol,
            "contract_type": self.swap_contract_type,
            "account": self.swap_account,
            "access_key": self.swap_access_key,
            "secret_key": self.swap_secret_key,
            "host": self.swap_host,
            "wss": self.swap_wss,
            "order_update_callback": self.on_event_order_update_swap,
            "asset_update_callback": self.on_event_asset_update_swap,
            "position_update_callback": self.on_event_position_update_swap,
            "init_success_callback": self.on_event_init_success_callback_swap,
        }
        self.swap_trader = Trade(**swap_cc)

        # 行情模块
        cc = {
            "platform": self.platform,
            "symbols": [self.symbol],
            "channels": self.channels,
            "orderbook_length": self.orderbook_length,
            "orderbooks_length": self.orderbooks_length,
            "klines_length": self.klines_length,
            "trades_length": self.trades_length,
            "wss": self.market_wss,
            "orderbook_update_callback": self.on_event_orderbook_update,
            "kline_update_callback": self.on_event_kline_update,
            "trade_update_callback": self.on_event_trade_update
        }
        self.market = Market(**cc)

        # 行情模块
        market_cc = {
            "platform": self.swap_platform,
            "symbols": [self.swap_symbol],
            "channels": self.swap_channels,
            "orderbook_length": self.swap_orderbook_length,
            "orderbooks_length": self.swap_orderbooks_length,
            "klines_length": self.swap_klines_length,
            "trades_length": self.swap_trades_length,
            "wss": self.swap_market_wss,
            "orderbook_update_callback": self.on_event_orderbook_update_swap,
            "kline_update_callback": self.on_event_kline_update_swap,
            "trade_update_callback": self.on_event_trade_update_swap
        }
        self.swap_market = Market(**market_cc)

        # 10秒执行1次
        LoopRunTask.register(self.on_ticker, 10)

        # delta对冲
        LoopRunTask.register(self.delta_hedging, 10)
Esempio n. 5
0
    def __init__(self):
        """ 初始化
        """
        print(config)
        self.strategy = config.strategy
        self.platform = config.accounts[0]["platform"]
        self.account = config.accounts[0]["account"]
        self.access_key = config.accounts[0]["access_key"]
        self.secret_key = config.accounts[0]["secret_key"]
        self.host = config.accounts[0]["host"]
        self.wss = config.accounts[0]["wss"]
        self.symbol = config.symbol
        self.lever_rate = config.lever_rate
        self.place_num = config.place_num
        self.contract_type = config.contract_type
        self.channels = config.markets[0]["channels"]
        self.orderbook_length = config.markets[0]["orderbook_length"]
        self.orderbook_step = config.markets[0]["orderbook_step"]
        self.orderbooks_length = config.markets[0]["orderbooks_length"]
        self.klines_length = config.markets[0]["klines_length"]
        self.klines_period = config.markets[0]["klines_period"]
        self.trades_length = config.markets[0]["trades_length"]
        self.market_wss = config.markets[0]["wss"]

        self.orderbook_invalid_seconds = 0.5

        self.last_bid_price = 0  # 上次的买入价格
        self.last_ask_price = 0  # 上次的卖出价格
        self.last_orderbook_timestamp = 0  # 上次的orderbook时间戳
        self.last_open_order_time = 0  #上次开单时间

        self.raw_symbol = self.symbol.split('-')[0]

        self.ask1_price = 0
        self.bid1_price = 0
        self.ask1_volume = 0
        self.bid1_volume = 0

        self.periods = [5, 10]

        self.trade_init_result = False

        # rest api
        if self.platform == HUOBI_SWAP:
            from alpha.platforms.swap.huobi_swap_api import HuobiSwapRestAPI
            self._rest_api = HuobiSwapRestAPI(self.host, self.access_key,
                                              self.secret_key)
        elif self.platform == HUOBI_DELIVERY:
            from alpha.platforms.delivery.huobi_delivery_api import HuobiDeliveryRestAPI
            self._rest_api = HuobiDeliveryRestAPI(self.host, self.access_key,
                                                  self.secret_key)

        # 交易模块
        cc = {
            "strategy": self.strategy,
            "platform": self.platform,
            "symbol": self.symbol,
            "contract_type": self.contract_type,
            "account": self.account,
            "access_key": self.access_key,
            "secret_key": self.secret_key,
            "host": self.host,
            "wss": self.wss,
            "rest_api": self._rest_api,
            "order_update_callback": self.on_event_order_update,
            "asset_update_callback": self.on_event_asset_update,
            "position_update_callback": self.on_event_position_update,
            "init_success_callback": self.on_event_init_trade_success_callback,
        }
        self.trader = Trade(**cc)

        # 行情模块
        cc = {
            "platform": self.platform,
            "symbol": self.symbol,
            "contract_type": self.contract_type,
            "channels": self.channels,
            "orderbook_length": self.orderbook_length,
            "orderbook_step": self.orderbook_step,
            "orderbooks_length": self.orderbooks_length,
            "klines_length": self.klines_length,
            "klines_period": self.klines_period,
            "trades_length": self.trades_length,
            "wss": self.market_wss,
            "rest_api": self._rest_api,
            "orderbook_update_callback": self.on_event_orderbook_update,
            "kline_update_callback": self.on_event_kline_update,
            "trade_update_callback": self.on_event_trade_update,
        }
        self.market = Market(**cc)

        # 1秒执行1次
        LoopRunTask.register(self.on_ticker, 5)
Esempio n. 6
0
class MaStrategy:
    def __init__(self):
        """ 初始化
        """
        print(config)
        self.strategy = config.strategy
        self.platform = config.accounts[0]["platform"]
        self.account = config.accounts[0]["account"]
        self.access_key = config.accounts[0]["access_key"]
        self.secret_key = config.accounts[0]["secret_key"]
        self.host = config.accounts[0]["host"]
        self.wss = config.accounts[0]["wss"]
        self.symbol = config.symbol
        self.lever_rate = config.lever_rate
        self.place_num = config.place_num
        self.contract_type = config.contract_type
        self.channels = config.markets[0]["channels"]
        self.orderbook_length = config.markets[0]["orderbook_length"]
        self.orderbook_step = config.markets[0]["orderbook_step"]
        self.orderbooks_length = config.markets[0]["orderbooks_length"]
        self.klines_length = config.markets[0]["klines_length"]
        self.klines_period = config.markets[0]["klines_period"]
        self.trades_length = config.markets[0]["trades_length"]
        self.market_wss = config.markets[0]["wss"]

        self.orderbook_invalid_seconds = 0.5

        self.last_bid_price = 0  # 上次的买入价格
        self.last_ask_price = 0  # 上次的卖出价格
        self.last_orderbook_timestamp = 0  # 上次的orderbook时间戳
        self.last_open_order_time = 0  #上次开单时间

        self.raw_symbol = self.symbol.split('-')[0]

        self.ask1_price = 0
        self.bid1_price = 0
        self.ask1_volume = 0
        self.bid1_volume = 0

        self.periods = [5, 10]

        self.trade_init_result = False

        # rest api
        if self.platform == HUOBI_SWAP:
            from alpha.platforms.swap.huobi_swap_api import HuobiSwapRestAPI
            self._rest_api = HuobiSwapRestAPI(self.host, self.access_key,
                                              self.secret_key)
        elif self.platform == HUOBI_DELIVERY:
            from alpha.platforms.delivery.huobi_delivery_api import HuobiDeliveryRestAPI
            self._rest_api = HuobiDeliveryRestAPI(self.host, self.access_key,
                                                  self.secret_key)

        # 交易模块
        cc = {
            "strategy": self.strategy,
            "platform": self.platform,
            "symbol": self.symbol,
            "contract_type": self.contract_type,
            "account": self.account,
            "access_key": self.access_key,
            "secret_key": self.secret_key,
            "host": self.host,
            "wss": self.wss,
            "rest_api": self._rest_api,
            "order_update_callback": self.on_event_order_update,
            "asset_update_callback": self.on_event_asset_update,
            "position_update_callback": self.on_event_position_update,
            "init_success_callback": self.on_event_init_trade_success_callback,
        }
        self.trader = Trade(**cc)

        # 行情模块
        cc = {
            "platform": self.platform,
            "symbol": self.symbol,
            "contract_type": self.contract_type,
            "channels": self.channels,
            "orderbook_length": self.orderbook_length,
            "orderbook_step": self.orderbook_step,
            "orderbooks_length": self.orderbooks_length,
            "klines_length": self.klines_length,
            "klines_period": self.klines_period,
            "trades_length": self.trades_length,
            "wss": self.market_wss,
            "rest_api": self._rest_api,
            "orderbook_update_callback": self.on_event_orderbook_update,
            "kline_update_callback": self.on_event_kline_update,
            "trade_update_callback": self.on_event_trade_update,
        }
        self.market = Market(**cc)

        # 1秒执行1次
        LoopRunTask.register(self.on_ticker, 5)

    async def on_ticker(self, *args, **kwargs):
        if not self.trade_init_result:
            logger.error("trade not init.", caller=self)
            return
        if not self.market.init_data():
            logger.error("not init market data.", caller=self)
            return
        if not self.trader.init_data():
            logger.error("not init trader data.", caller=self)
            return
        klines = self.market.klines
        if klines[-1].id == self.last_open_order_time:
            logger.info("haved order. ordertime:",
                        self.last_open_order_time,
                        caller=self)
            return
        ma_point = interval_handler(values=klines,
                                    periods=self.periods,
                                    vtype="close")
        if ma_point[self.periods[0]][1] < ma_point[self.periods[1]][1]:
            if ma_point[self.periods[0]][0] >= ma_point[self.periods[1]][0]:
                print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
                      "开多平空")
        elif ma_point[self.periods[0]][1] > ma_point[self.periods[1]][1]:
            if ma_point[self.periods[0]][0] <= ma_point[self.periods[1]][0]:
                print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
                      "开空平多")
        # await self.cancel_orders()
        # await self.place_orders()

    async def cancel_orders(self):
        """  取消订单
        """
        order_nos = [orderno for orderno in self.trader.orders]
        if order_nos and self.last_ask_price != self.ask1_price:
            _, errors = await self.trader.revoke_order(*order_nos)
            if errors:
                logger.error(self.strategy,
                             "cancel future order error! error:",
                             errors,
                             caller=self)
            else:
                logger.info(self.strategy,
                            "cancel future order:",
                            order_nos,
                            caller=self)

    async def place_orders(self):
        """ 下单
        """
        orders_data = []
        if self.trader.position and self.trader.position.short_quantity:
            # 平空单
            price = self.ask1_price - 0.1
            quantity = -self.trader.position.short_quantity
            action = ORDER_ACTION_BUY
            new_price = str(price)  # 将价格转换为字符串,保持精度
            if quantity:
                orders_data.append({
                    "price": new_price,
                    "quantity": quantity,
                    "action": action,
                    "order_type": ORDER_TYPE_LIMIT,
                    "lever_rate": self.lever_rate
                })
                self.last_ask_price = self.ask1_price
        if self.trader.assets and self.trader.assets.assets.get(
                self.raw_symbol):
            # 开空单
            price = self.bid1_price + 0.1
            volume = float(
                self.trader.assets.assets.get(
                    self.raw_symbol).get("free")) * price // 100
            if volume >= 1:
                quantity = -volume  #  空1张
                action = ORDER_ACTION_SELL
                new_price = str(price)  # 将价格转换为字符串,保持精度
                if quantity:
                    orders_data.append({
                        "price": new_price,
                        "quantity": quantity,
                        "action": action,
                        "order_type": ORDER_TYPE_LIMIT,
                        "lever_rate": self.lever_rate
                    })
                    self.last_bid_price = self.bid1_price

        if orders_data:
            order_nos, error = await self.trader.create_orders(orders_data)
            if error:
                logger.error(self.strategy,
                             "create future order error! error:",
                             error,
                             caller=self)
            logger.info(self.strategy,
                        "create future orders success:",
                        order_nos,
                        caller=self)

    async def on_event_orderbook_update(self, orderbook: Orderbook):
        """  orderbook更新
            self.market.orderbooks 是最新的orderbook组成的队列,记录的是历史N次orderbook的数据。
            本回调所传的orderbook是最新的单次orderbook。
        """
        logger.debug("orderbook:", orderbook, caller=self)
        if orderbook.asks:
            self.ask1_price = float(orderbook.asks[0][0])  # 卖一价格
            self.ask1_volume = float(orderbook.asks[0][1])  # 卖一数量
        if orderbook.bids:
            self.bid1_price = float(orderbook.bids[0][0])  # 买一价格
            self.bid1_volume = float(orderbook.bids[0][1])  # 买一数量
        self.last_orderbook_timestamp = orderbook.timestamp

    async def on_event_order_update(self, order: Order):
        """ 订单状态更新
        """
        print("on_event_order_update")
        print(order.__str__())

    async def on_event_asset_update(self, asset: Asset):
        """ 资产更新
        """
        print("on_event_asset_update")
        print(asset.__str__())

    async def on_event_position_update(self, position: Position):
        """ 仓位更新
        """
        print("on_event_position_update")
        print(position.__str__())

    async def on_event_kline_update(self, kline: Kline):
        """ kline更新
            self.market.klines 是最新的kline组成的队列,记录的是历史N次kline的数据。
            本回调所传的kline是最新的单次kline。
        """
        print("on_event_kline_update")
        print(kline.__str__())

    async def on_event_trade_update(self, trade: MarketTrade):
        """ market trade更新
            self.market.trades 是最新的逐笔成交组成的队列,记录的是历史N次trade的数据。
            本回调所传的trade是最新的单次trade。
        """
        print("on_event_trade_update")
        print(trade.__str__())

    async def on_event_init_trade_success_callback(self, success: bool,
                                                   error: Error, **kwargs):
        """ init success callback
        """
        if not success:
            logger.error("init trade error callback update:",
                         success,
                         error,
                         kwargs,
                         caller=self)
        else:
            self.trade_init_result = True
    def __init__(self):
        """ 初始化
        """
        self.strategy = config.strategy
        self.platform = config.accounts[0]["platform"]
        self.account = config.accounts[0]["account"]
        self.access_key = config.accounts[0]["access_key"]
        self.secret_key = config.accounts[0]["secret_key"]
        self.host = config.accounts[0]["host"]
        self.wss = config.accounts[0]["wss"]
        self.symbol = config.symbol
        self.contract_type = config.contract_type
        self.channels = config.markets[0]["channels"]
        self.orderbook_length = config.markets[0]["orderbook_length"]
        self.orderbooks_length = config.markets[0]["orderbooks_length"]
        self.klines_length = config.markets[0]["klines_length"]
        self.trades_length = config.markets[0]["trades_length"]
        self.market_wss = config.markets[0]["wss"]

        self.contract_size = 0.01
        self.orderbook_invalid_seconds = 10

        self.last_bid_price = 0 # 上次的买入价格
        self.last_ask_price = 0 # 上次的卖出价格
        self.last_orderbook_timestamp = 0 # 上次的orderbook时间戳

        self.raw_symbol = self.symbol.split('-')[0]

        self.ask1_price = 0
        self.bid1_price = 0
        self.ask1_volume = 0
        self.bid1_volume = 0


        # Trade module 交易模块
        cc = {
            "strategy": self.strategy,
            "platform": self.platform,
            "symbol": self.symbol,
            "contract_type": self.contract_type,
            "account": self.account,
            "access_key": self.access_key,
            "secret_key": self.secret_key,
            "host": self.host,
            "wss": self.wss,
            "order_update_callback": self.on_event_order_update,
            "asset_update_callback": self.on_event_asset_update,
            "position_update_callback": self.on_event_position_update,
            "init_success_callback": self.on_event_init_success_callback,
        }
        self.trader = Trade(**cc)

        # quotes module 行情模块
        cc = {
            "platform": self.platform,
            "symbols": [self.symbol],
            "channels": self.channels,
            "orderbook_length": self.orderbook_length,
            "orderbooks_length": self.orderbooks_length,
            "klines_length": self.klines_length,
            "trades_length": self.trades_length,
            "wss": self.market_wss,
            "orderbook_update_callback": self.on_event_orderbook_update,
            "kline_update_callback": self.on_event_kline_update,
            "trade_update_callback": self.on_event_trade_update
        }
        self.market = Market(**cc)
                
        # 60秒执行1次
        LoopRunTask.register(self.on_ticker, 1)