示例#1
0
class OrderRouter(object):
    def __init__(self, apikey, secretkey, password):
        self._api = RequestClient(api_key=apikey, secret_key=secretkey)
        self.account_type = AccountType.SPOT

    def _format_instrument_id(self, instrument_id):
        return instrument_id.lower().replace('-', '')

    def _decode_type(self, order_type):
        if not order_type:
            return None, None
        tmp = order_type.split('-')
        side = tmp[0]
        o_type = tmp[1]
        if side not in ['buy', 'sell']:
            side = None

        if o_type not in ['limit', 'market', 'ioc']:
            o_type = None

        return side, o_type

    def _decode_state(self, state):
        if state == 'submitted':
            return 'open'

        return state

    def _timestamp_to_datetime(self, timestamp):
        return datetime.datetime.fromtimestamp((timestamp + 28800000) / 1000)

    def _format_order(self, instrument_id, order_info):
        def fill_obj(o):
            side, o_type = self._decode_type(o.order_type)
            return {
                'instrument_id':
                instrument_id,
                'order_id':
                str(o.order_id),
                'client_oid':
                '',
                'price':
                o.price,
                'size':
                o.amount,
                'timestamp':
                o.created_timestamp,
                'finished_timestamp':
                o.finished_timestamp,
                'finished_datetime':
                self._timestamp_to_datetime(o.finished_timestamp),
                'canceled_timestamp':
                o.canceled_timestamp,
                'datetime':
                self._timestamp_to_datetime(o.created_timestamp),
                'filled_size':
                o.filled_amount,
                'filled_notional':
                o.filled_cash_amount,
                'side':
                side,
                'type':
                o_type,
                'state':
                o.state,
                'status':
                self._decode_state(o.state),
                'created_at':
                datetime.datetime.now(),
                'updated_at':
                datetime.datetime.now()
            }

        if isinstance(order_info, list):
            ret = []
            for o in order_info:
                ret.append(fill_obj(o))
        else:
            ret = fill_obj(order_info)

        return ret

    def check_position(self, symbol):
        psss

    def submit_spot_order(self,
                          client_oid,
                          type,
                          side,
                          instrument_id,
                          price,
                          size,
                          notional,
                          order_type='0',
                          timeout=20,
                          wait_flag=False):

        instrument_id = self._format_instrument_id(instrument_id)
        order_type = '{}-{}'.format(param['side'], param['type'])
        order_id = self._api.create_order(instrument_id, self.account_type,
                                          order_type, size, price)
        return {'order_id': order_id}

    # take orders
    # 市价单
    # params = [
    #   {"client_oid":"20180728","instrument_id":"btc-usdt","side":"sell","type":"market"," size ":"0.001"," notional ":"10001","margin_trading ":"1"},
    #   {"client_oid":"20180728","instrument_id":"btc-usdt","side":"sell","type":"limit"," size ":"0.001","notional":"10002","margin_trading ":"1"}
    # ]

    # 限价单
    # params = [
    #   {"client_oid":"20180728","instrument_id":"btc-usdt","side":"sell","type":"limit","size":"0.001","price":"10001","margin_trading ":"1"},
    #   {"client_oid":"20180728","instrument_id":"btc-usdt","side":"sell","type":"limit","size":"0.001","price":"10002","margin_trading ":"1"}
    # ]
    def submit_orders(self, params):
        ret = []
        for param in params:
            instrument_id = self._format_instrument_id(param['instrument_id'])
            order_type = '{}-{}'.format(param['side'], param['type'])
            try:
                order_id = self._api.create_order(instrument_id, self.account_type,
                                                order_type, param['size'],
                                                param['price'])
                order_id = str(order_id)
                print('submit order id: {}, order_type: {}, price: {}'.format(
                    order_id, order_type, param['price']))
                ret.append({
                    'order_id': order_id,
                    'side': param['side'],
                    'price': param['price']
                })
            except Exception as e:
                print('submit_orders err: {}'.format(e))
                print('account type:{}, order_type:{}, size:{}, price:{}'.format(self.account_type, order_type, param['size'], param['price']))

            time.sleep(0.01)

        return ret

    def get_order_info(self, order_id, instrument_id, client_oid=''):
        order_info = self._api.get_order(
            self._format_instrument_id(instrument_id), order_id)

        return self._format_order(instrument_id, order_info)

    def cancel_order(self, order_id, instrument_id):
        return self._api.cancel_order(
            self._format_instrument_id(instrument_id), order_id)

    # revoke orders

    # params example:
    # [
    #   {"instrument_id":"btc-usdt","order_ids":[1600593327162368,1600593327162369]},
    #   {"instrument_id":"ltc-usdt","order_ids":[243464,234465]}
    # ]
    def cancel_orders(self, params):
        ret = []
        for param in params:
            instrument_id = self._format_instrument_id(param['instrument_id'])
            ret.append(
                self._api.cancel_orders(instrument_id, param['order_ids']))
            time.sleep(0.01)

        return ret

    def get_orders_pending(self, instrument_id):
        ret = []
        open_orders = self._api.get_open_orders(
            self._format_instrument_id(instrument_id),
            self.account_type,
            size=100)
        return self._format_order(instrument_id, open_orders)

    def get_kline(self, instrument_id, start, end, granularity):
        pass

    def get_ticker(self, instrument_id):
        last_trade = self._api.get_last_trade(
            self._format_instrument_id(instrument_id))

        return {'last': last_trade.price}

    def get_coin_info(self, instrument_id='all'):
        # TODO 把sdk方法get_exchange_info的symobl和currency分离出来

        exchange_info = self._api.get_exchange_info()
        symbol_list = exchange_info.symbol_list

        for sl in symbol_list:
            if sl.symbol == self._format_instrument_id(instrument_id):
                return {
                    'instrument_id': instrument_id,
                    'base_currency': sl.base_currency,
                    'quote_currency': sl.quote_currency,
                    'tick_size': sl.price_precision,
                    'size_increment': sl.amount_precision
                }
        return None

    def get_account_info(self):
        account_balances = self._api.get_account_balance_by_account_type(
            self.account_type)
        details_obj = {}
        for b in account_balances.balances:
            if b.balance:
                if b.currency not in details_obj:
                    if b.balance_type == BalanceType.TRADE:
                        details_obj[b.currency] = {
                            'currency': b.currency.upper(),
                            'frozen': 0,
                            'balance': b.balance,
                            'available': b.balance
                        }
                    elif b.balance_type == BalanceType.FROZEN:
                        details_obj[b.currency] = {
                            'currency': b.currency.upper(),
                            'frozen': b.balance,
                            'balance': b.balance,
                            'available': 0
                        }
                else:
                    if b.balance_type == BalanceType.TRADE:
                        details_obj[b.currency]['available'] += b.balance
                        details_obj[b.currency]['balance'] += b.balance

                    elif b.balance_type == BalanceType.FROZEN:
                        details_obj[b.currency]['frozen'] += b.balance
                        details_obj[b.currency]['balance'] += b.balance

        return list(details_obj.values())

    def get_coin_account_info(self, symbol):
        balances = self._api.get_account_balance_by_account_type(
            self.account_type)
        ret = {'currency': symbol, 'frozen': 0, 'balance': 0, 'available': 0}
        for b in balances.get_balance(symbol.lower()):
            if b.balance_type == BalanceType.TRADE:
                ret['balance'] += b.balance
                ret['available'] = b.balance
            elif b.balance_type == BalanceType.FROZEN:
                ret['balance'] += b.balance
                ret['frozen'] = b.balance

        return ret

    def get_coin_balance(self, symbol):
        return self.get_coin_account_info(symbol)['balance']

    def get_coin_available(self, symbol):
        return self.get_coin_account_info(symbol)['available']

    def get_oneday_orders(self, instrument_id, stime, state):
        if stime > datetime.datetime.now():
            return []

        start_date = stime.strftime('%Y-%m-%d')
        end_date = start_date  # (stime + datetime.timedelta(days=1)).strftime('%Y-%m-%d')
        orders = []
        start_id = None
        size = 100
        format_instrument_id = self._format_instrument_id(instrument_id)
        while True:
            order = self._api.get_historical_orders(
                format_instrument_id,
                state,
                start_date=start_date,
                end_date=end_date,
                start_id=start_id,
                size=size)
            #print('Fetched orders, date: {}, pair: {}, total: {}'.format(start_date, instrument_id, len(order)))
            format_orders = self._format_order(instrument_id, order)
            orders += format_orders
            # print('Fetched orders, pair: %s, total: %d', instrument_id, len(format_orders))
            if len(format_orders) < size:
                break
            else:
                start_id = min([o['order_id'] for o in format_orders])
            time.sleep(0.5)
        return orders

    def get_orders(self, instrument_id, stime, etime, state):
        date_list = pd.date_range(
            stime.replace(hour=0, minute=0, second=0, microsecond=0),
            etime.replace(hour=0, minute=0, second=0, microsecond=0))
        orders = []
        for d in date_list:
            orders += self.get_oneday_orders(instrument_id, d, state)

        return [
            o for o in orders
            if o['datetime'] >= stime and o['datetime'] < etime
        ]
示例#2
0
request_client = RequestClient(api_key="xxxxxx", secret_key="xxxxxx")

order_id = request_client.create_order(symbol=symbol_test,
                                       account_type=AccountType.SPOT,
                                       order_type=OrderType.SELL_LIMIT,
                                       amount=1.0,
                                       price=20.12,
                                       client_order_id=client_order_id_test,
                                       stop_price=12,
                                       operator="gte")
print("create new order id : " + (str(order_id)) + " with client id " +
      client_order_id_test)
print("\n\n")

orderObj = request_client.get_order(symbol=symbol_test, order_id=order_id)
print("get order by order id : " + (str(order_id)))
PrintMix.print_data(orderObj)
print("\n\n")

orderObj = request_client.get_order_by_client_order_id(
    client_order_id=client_order_id_test)
print("get order by client order id : " + client_order_id_test)
PrintMix.print_data(orderObj)
print("\n\n")

request_client.cancel_client_order(client_order_id=client_order_id_test)
print("cancel order by client order id : " + client_order_id_test)
print("\n\n")

orderObj = request_client.get_order_by_client_order_id(
示例#3
0
文件: QsHuobi.py 项目: qiushui777/qsq
class QsHuobi(object):
    """
    qsq系统提供的火币接口,可通过此接口获取数据和交易等
    要想进行交易,必须在qs/config/config.ini中设置好自己的火币api密钥
    """
    def __init__(self):
        [self.api_key, self.secret_key,
         self.client_order_id] = self.get_apikey()
        self.request_client = RequestClient(api_key=self.api_key,
                                            secret_key=self.secret_key)
        #self.subscription_client = SubscriptionClient()

    def get_apikey(self):
        cfg = ConfigParser()
        cfg.read(QsEnv.g_project_config)
        return [
            cfg.get('huobi', 'huobiapi_key'),
            cfg.get('huobi', 'huobisecret_key'),
            cfg.get('huobi', 'client_order_id')
        ]

    def get_24h_trade_statistics(self, pair="btcusdt"):
        """
        param: pair 想要获取的交易对,默认为btcusdt

        获取24小时的市场数据,返回结构体成员包括有
        timestamp, high, low, open, close, volume
        可使用trade_statistics.high这样的方式获取对应数据
        """
        trade_statistics = self.request_client.get_24h_trade_statistics(pair)
        return trade_statistics

    def get_latest_candlestick(self,
                               pair="btcusdt",
                               interval=CandlestickInterval.MIN1,
                               size=10):
        """
        param: pair 想要获取的交易对,默认为btcusdt
        param: interval 时间间隔,默认为MIN1, 可选有MIN1 MIN5 MIN15 MIN30 MIN60 DAY1 MON1 WEEK1 YEAR1 
        param: size 获取的数据条数,默认为10,选择范围为[1,2000]

        获取最近一段时间的K线数据,返回一个list,这个list的每一项的结构体成员包括有
        timestamp, high, low, open, close, volume
        可使用member.high这样的方式获取对应数据
        """
        candlestick_list = self.request_client.get_latest_candlestick(
            pair, interval, size)
        return candlestick_list

    def get_exchange_currencies(self):
        """
        获取火币所有交易币种
        """
        return self.request_client.get_exchange_currencies()

    def get_exchange_info(self):
        """
        获取交易所信息,返回交易对和支持币种,使用案例
        for symbol in exchange_info.symbol_list:
            print(symbol.symbol)
    
        for currency in exchange_info.currencies:
            print(currency)
        """
        return self.request_client.get_exchange_info()

    def get_fee_rate(self, symbol="btcusdt"):
        """
        获取交易手续费,返回一个FeeRate对象,成员包括
        symbol 对应币种 maker_fee 卖方手续费 taker_fee 买方手续费
        实际使用中,symbol也为一个费率
        """
        result = self.request_client.get_fee_rate(symbols=symbol)
        return result[0]

    def get_historical_orders(self,
                              symbol="ethusdt",
                              order_state=OrderState.CANCELED,
                              order_type=None,
                              start_date=None,
                              end_date=None,
                              start_id=None,
                              size=None):
        """
        param: symbol 符号(必须)
        param: order_state 订单状态(必须),可选参数有SUBMITTED PARTIAL_FILLED CANCELLING PARTIAL_CANCELED FILLED CANCELED INVALID
        param: order_type 订单类型(可选),可选参数有SELL_LIMIT BUY_LIMIT BUY_MARKET SELL_MARKET BUY_IOC SELL_IOC BUY_LIMIT_MAKER SELL_LIMIT_MAKER BUY_STOP_LIMIT SELL_STOP_LIMIT INVALID 
        param: start_date 开始日期(可选) 格式为 yyyy-mm-dd
        param: end_date 结束日期(可选) 格式为 yyyy-mm-dd
        param: start_id(可选) 订单起始id,暂时忽略
        param: size(可选) 大小,暂时忽略
        
        获取历史订单
        """
        orders = self.request_client.get_historical_orders(
            symbol=symbol,
            order_state=order_state,
            order_type=order_type,
            start_date=start_date,
            end_date=end_date,
            start_id=start_id,
            size=size)
        return orders

    def get_historical_trade(self, symbol="btcusdt", size=5):
        """
        param: symbol 符号(必须)
        param: size 交易列表的大小(必须)
        获取历史交易数据,返回trade_list对象列表,每个对象有如下几个成员
        timestamp trade_id price amount direction
        """
        trade_list = self.request_client.get_historical_trade(symbol, size)
        return trade_list

    def get_market_trade(self, symbol="btcusdt"):
        """
        param: symbol 符号,默认为btcusdt
        获取当前市场上的交易,返回一个trade对象组成的tradelist列表,每个trade对象的成员有
        price, amount, trade_id timestamp, direction
        """
        trades = self.request_client.get_market_trade(symbol=symbol)
        return trades

    def get_open_orders(self,
                        symbol="htusdt",
                        account_type=AccountType.SPOT,
                        direct="next"):
        """
        获取自己账户当前开放的交易,返回order数组,每个Order对象有
        order_id, symbol, price, amount, account_type, created_timestamp, order_type, filled_amount, filled_cash_amount, filled_fees, source, state
        """
        orders = self.request_client.get_open_orders(symbol=symbol,
                                                     account_type=account_type,
                                                     direct=direct)
        return orders

    def get_order_recent_48hour(self,
                                symbol=None,
                                start_time=None,
                                end_time=None,
                                size=None,
                                direct=None):
        """
        获取自己账户48小时内的交易记录,返回order数组
        """
        orders = self.request_client.get_order_recent_48hour(
            symbol=symbol,
            start_time=start_time,
            end_time=end_time,
            size=size,
            direct=direct)
        return orders

    def get_price_depth(self, symbol, size):
        """
        获取当前市场的交易深度,symbol 为交易对,size为头部的几个交易,一个用例如下
        i = 0
        for entry in depth.bids:
            i = i + 1
            print(str(i) + ": price: " + str(entry.price) + ", amount: " + str(entry.amount))

        i = 0
        for entry in depth.asks:
            i = i + 1
            print(str(i) + ": price: " + str(entry.price) + ", amount: " + str(entry.amount))
        """
        depth = self.request_client.get_price_depth(symbol, size)
        return depth

    def order(self,
              symbol="btcusdt",
              account_type=AccountType.SPOT,
              order_type=OrderType.SELL_LIMIT,
              amount=1.0,
              price=None,
              stop_price=None,
              operator=None):
        """
        下单函数,谨慎使用
        必须提供的参数为symbol,account_type,order_type,amount
        param: account_type  账户类型,包括有SPOT MARGIN OTC POINT 
        param: order_type 订单类型,包括有SELL_LIMIT BUY_LIMIT BUY_MARKET SELL_MARKET BUY_IOC SELL_IOC
        """
        order_id = self.request_client.create_order(symbol, account_type,
                                                    order_type, amount, price,
                                                    self.client_order_id,
                                                    stop_price, operator)
        return order_id

    def cancle_order(self, symbol="btcusdt", order_id="00000"):
        """
        取消某笔订单
        """
        cancle_state = self.request_client.cancel_order(symbol, order_id)
        return cancle_state

    def get_order(self, symbol="btcusdt", order_id="000000"):
        """
        根据order_id获取某笔交易,每个Order对象有
        order_id, symbol, price, amount, account_type, created_timestamp, order_type, filled_amount, filled_cash_amount, filled_fees, source, state
        """
        order = self.request_client.get_order(symbol=symbol, order_id=order_id)
        return order

    def get_order_by_client_order_id(self):
        """
        根据client_order_id来获取订单, 每个Order对象有
        order_id, symbol, price, amount, account_type, created_timestamp, order_type, filled_amount, filled_cash_amount, filled_fees, source, state
        """
        order = self.request_client.get_order_by_client_order_id(
            client_order_id=self.client_order_id)
        return order

    def cancel_client_order(self):
        """
        根据client_order_id取消某笔交易
        """
        self.request_client.cancel_client_order(
            client_order_id=self.client_order_id)
示例#4
0
class KeepBalanceStrategy(BaseStrategy, MailHandler):
    """
    动态平衡策略
    """
    def __init__(self):
        BaseStrategy.__init__(self)
        self.request_client = RequestClient(
            api_key=self.get_config_value("huobi", "api_key"),
            secret_key=self.get_config_value("huobi", "secret_key"))
        self.strategy = 'KeepBalance'

    def signal(self):
        balance_dict = self.get_account_balance()
        for key, value in enumerate(balance_dict):
            if float(balance_dict[value]["amount"]) * 1.03 < float(
                    balance_dict[value]["dollar"]):
                self.buy(value,
                         amount=balance_dict[value]["amount"],
                         dollar=balance_dict[value]["dollar"])
            elif float(balance_dict[value]["dollar"]) * 1.03 < float(
                    balance_dict[value]["amount"]):
                self.sell(value,
                          amount=balance_dict[value]["amount"],
                          dollar=balance_dict[value]["dollar"],
                          price=balance_dict[value]["price"])
            else:
                self.logger.info(
                    f"当前持有{value}合计金额: {balance_dict[value]['amount']}, 对标美元: "
                    f"{balance_dict[value]['dollar']}, 小于阈值不触发交易, "
                    f"买入阈值: {retain_decimals(float(balance_dict[value]['dollar']) / 1.05, 2)}, "
                    f"卖出阈值: {retain_decimals(float(balance_dict[value]['dollar']) * 1.05, 2)}"
                )

    def buy(self, symbol_name, **kwargs):
        self.trade_lock(symbol_name, self.strategy)
        try:
            assert kwargs['dollar'] >= kwargs['amount']
            buy_dollar = retain_decimals(
                (float(kwargs['dollar']) - float(kwargs['amount'])) / 2, 2)
            if float(buy_dollar) >= TradeLimit.USDT:
                # 买入
                order_id = self.request_client.create_order(
                    symbol_name + 'usdt', AccountType.SPOT,
                    OrderType.BUY_MARKET, buy_dollar, None)
                self.update_balance_and_dollar(order_id,
                                               Action.BUY,
                                               symbol_name,
                                               buy_dollar=buy_dollar)
                self.send_mail("触发买入信号",
                               f"动态平衡策略触发买入{buy_dollar}的{symbol_name}")
            else:
                self.logger.warning(
                    f"当前欲买入{buy_dollar}美金的{symbol_name}, 不满足最低交易限制")
        except AssertionError:
            self.logger.error(
                f"AssertionError, symbol={symbol_name}, amount={kwargs['amount']}, "
                f"dollar={kwargs['dollar']}")
        finally:
            self.trade_unlock(symbol_name, self.strategy)

    def sell(self, symbol_name, **kwargs):
        self.trade_lock(symbol_name, self.strategy)
        try:
            assert kwargs['amount'] > kwargs['dollar']

            # 获取交易数目精度
            precision = self.precision_collection.find_one(
                {"symbol": symbol_name + "usdt"}, {"amount_precision": 1})
            amount_precision = float(precision['amount_precision'])

            # 计算欲卖出的数目
            sell_currency = retain_decimals(
                (float(kwargs['amount']) - float(kwargs['dollar'])) / 2 /
                float(kwargs['price']), amount_precision)

            if float(sell_currency) > TradeLimit.BTC:
                order_id = self.request_client.create_order(
                    symbol_name + 'usdt', AccountType.SPOT,
                    OrderType.SELL_MARKET, sell_currency, None)
                self.update_balance_and_dollar(order_id, Action.SELL,
                                               symbol_name)
                self.send_mail("触发卖出信号",
                               f"动态平衡策略触发卖出{sell_currency}的{symbol_name}")
            else:
                self.logger.warning(
                    f"当前欲卖出{sell_currency}美金的{symbol_name}, 不满足最低交易限制")
        except AssertionError:
            self.logger.error(
                f"AssertionError, symbol={symbol_name}, amount={kwargs['amount']}, "
                f"dollar={kwargs['dollar']}")
        finally:
            self.trade_unlock(symbol_name, self.strategy)

    def update_balance_and_dollar(self, order_id, action, symbol_name,
                                  **kwargs):
        order_detail = self.request_client.get_order("symbol", order_id)

        # 验证订单是否执行完成
        while not order_detail.state == OrderState.FILLED:
            time.sleep(1)
            self.logger.info(f"{order_id}还未执行完成, 休眠1秒等待执行完成")
            order_detail = self.request_client.get_order("symbol", order_id)

        # mongodb中减去已使用的美金
        result = self.keep_balance_collection.find_one({"symbol": symbol_name})
        dollar = float(result['amount'])
        if action == 'BUY':
            dollar -= float(kwargs['buy_dollar'])
        # mongodb中加上卖出获得的美金
        elif action == 'SELL':
            dollar += float(order_detail.filled_cash_amount)

        # 格式化美金数
        dollar = retain_decimals(dollar, 2)

        # mongodb更新操作
        self.keep_balance_collection.update_one({"symbol": symbol_name},
                                                {"$set": {
                                                    "amount": dollar
                                                }})
        self.logger.info(f"{symbol_name}美金数置为: {dollar}")

    def get_account_balance(self):
        """
        获取余额大于0的交易对
        :return:
        """
        balances = self.request_client.get_account_balance()
        balance_dict = {}
        for key, value in enumerate(balances[0].balances):
            if value.balance > 0 and value.currency != 'usdt':
                if not self.check_trade_lock_exist(value.currency,
                                                   self.strategy):
                    price, amount = self.get_account_amount(
                        value.currency, value.balance)
                    if float(amount) >= 1:
                        dollar = self.get_mongodb_dollar(
                            value.currency, amount)
                        balance_dict[value.currency] = {
                            "balance": str(value.balance),
                            "price": str(price),
                            "amount": str(amount),
                            "dollar": dollar
                        }
        self.logger.debug(balance_dict)
        return balance_dict

    def get_account_amount(self, symbol_name, symbol_balance):
        price = self.get_price(symbol_name + "usdt")
        amount = str(float(symbol_balance) * float(price))
        amount = retain_decimals(amount, 2)
        return price, amount

    def get_mongodb_dollar(self, symbol_name, symbol_amount):
        result = self.keep_balance_collection.find_one({"symbol": symbol_name})
        if result is None:
            self.keep_balance_collection.insert_one({
                "symbol":
                symbol_name,
                "amount":
                str(symbol_amount)
            })
            return symbol_amount
        else:
            return result['amount']

    def get_price(self, symbol_name):
        """
        获取当前交易对价格
        :param symbol_name:
        :return:
        """
        current_prices = self.request_client.get_latest_candlestick(
            symbol=symbol_name, interval=CandlestickInterval.DAY1, size=1)
        return current_prices[0].close
class KeepBalanceStrategySocket(BaseStrategy, FileReadAndWrite):
    """
    动态平衡策略
    """

    def __init__(self):
        BaseStrategy.__init__(self)
        FileReadAndWrite.__init__(self)
        self.request_client = RequestClient(api_key=self.get_config_value("huobi", "api_key"),
                                            secret_key=self.get_config_value("huobi", "secret_key"))
        self.strategy = 'KeepBalance'
        self.timeout = float(self.get_config_value("strategy", "timeout"))

    def signal(self):
        balance_dict = self.get_account_balance()
        for key, value in enumerate(balance_dict):
            if float(balance_dict[value]["amount"]) * 1.03 < float(balance_dict[value]["dollar"]):
                self.buy(value, amount=balance_dict[value]["amount"], dollar=balance_dict[value]["dollar"])
            elif float(balance_dict[value]["dollar"]) * 1.03 < float(balance_dict[value]["amount"]):
                self.sell(value, amount=balance_dict[value]["amount"], dollar=balance_dict[value]["dollar"],
                          price=balance_dict[value]["price"])
            else:
                self.logger.info(f"当前{value}价格为{balance_dict[value]['price']}, 持有{value}合计金额: "
                                 f"{balance_dict[value]['amount']}, 对标美元: {balance_dict[value]['dollar']}, "
                                 f"小于阈值不触发交易, "
                                 f"买入阈值: {retain_decimals(str(float(balance_dict[value]['dollar']) / 1.03), '2')}, "
                                 f"卖出阈值: {retain_decimals(str(float(balance_dict[value]['dollar']) * 1.03), '2')}")

    def buy(self, symbol_name, **kwargs):
        self.trade_lock(symbol_name, self.strategy)
        try:
            assert kwargs['dollar'] >= kwargs['amount']
            buy_dollar = retain_decimals(str((float(kwargs['dollar']) - float(kwargs['amount'])) / 2), '2')
            if float(buy_dollar) >= TradeLimit.trade_limit_dict['usdt']:
                # 买入
                order_id = self.request_client.create_order(symbol_name + 'usdt', AccountType.SPOT,
                                                            OrderType.BUY_MARKET, buy_dollar, None)
                self.update_balance_and_dollar(order_id, Action.BUY, symbol_name, buy_dollar=buy_dollar)
                # self.send_mail("触发买入信号", f"动态平衡策略触发买入{buy_dollar}的{symbol_name}")
                WeChatRobot.send_message(f"动态平衡策略触发买入{buy_dollar}的{symbol_name}")
            else:
                self.logger.warning(f"当前欲买入{buy_dollar}美金的{symbol_name}, 不满足最低交易限制")
        except AssertionError:
            self.logger.error(f"AssertionError, symbol={symbol_name}, amount={kwargs['amount']}, "
                              f"dollar={kwargs['dollar']}")
        except Exception as E:
            self.logger.error(E)
        finally:
            self.trade_unlock(symbol_name, self.strategy)

    def sell(self, symbol_name, **kwargs):
        self.trade_lock(symbol_name, self.strategy)
        try:
            assert kwargs['amount'] > kwargs['dollar']

            # 获取交易数目精度
            precision = self.precision_collection.find_one({"symbol": symbol_name + "usdt"}, {"amount_precision": 1})
            amount_precision = float(precision['amount_precision'])

            # 计算欲卖出的数目
            sell_currency = retain_decimals(
                str((float(kwargs['amount']) - float(kwargs['dollar'])) / 2 / float(kwargs['price'])),
                str(amount_precision))

            if float(sell_currency) > TradeLimit.trade_limit_dict[symbol_name]:
                order_id = self.request_client.create_order(symbol_name + 'usdt', AccountType.SPOT,
                                                            OrderType.SELL_MARKET, sell_currency, None)
                self.update_balance_and_dollar(order_id, Action.SELL, symbol_name)
                # self.send_mail("触发卖出信号", f"动态平衡策略触发卖出{sell_currency}的{symbol_name}")
                WeChatRobot.send_message(f"动态平衡策略触发卖出{sell_currency}的{symbol_name}")
            else:
                self.logger.warning(f"当前欲卖出{sell_currency}美金的{symbol_name}, 不满足最低交易限制")
        except AssertionError:
            self.logger.error(f"AssertionError, symbol={symbol_name}, amount={kwargs['amount']}, "
                              f"dollar={kwargs['dollar']}")
        except Exception as E:
            self.logger.error(E)
        finally:
            self.trade_unlock(symbol_name, self.strategy)

    def update_balance_and_dollar(self, order_id, action, symbol_name, **kwargs):
        order_detail = self.request_client.get_order("symbol", order_id)

        # 验证订单是否执行完成
        while not order_detail.state == OrderState.FILLED:
            time.sleep(1)
            self.logger.info(f"{order_id}还未执行完成, 休眠1秒等待执行完成")
            order_detail = self.request_client.get_order("symbol", order_id)

        # mongodb中减去已使用的美金
        result = self.keep_balance_collection.find_one({"symbol": symbol_name})
        dollar = float(result['amount'])
        if action == 'BUY':
            dollar -= float(kwargs['buy_dollar'])
        # mongodb中加上卖出获得的美金
        elif action == 'SELL':
            dollar += float(order_detail.filled_cash_amount)

        # 格式化美金数
        dollar = retain_decimals(str(dollar), '2')

        # mongodb更新操作
        self.keep_balance_collection.update_one({"symbol": symbol_name}, {"$set": {"amount": dollar}})
        self.logger.info(f"{symbol_name}美金数置为: {dollar}")

    def get_account_balance(self):
        """
        获取余额大于0的交易对
        :return:
        """
        balances = self.request_client.get_account_balance()
        balance_dict = {}
        for key, value in enumerate(balances[0].balances):
            if value.balance > 0 and value.currency != 'usdt':
                price, amount = self.get_account_amount(value.currency, value.balance)
                if float(amount) >= 1:
                    if not self.check_trade_lock_exist(value.currency, self.strategy):
                        dollar = self.get_mongodb_dollar(value.currency, amount)
                        balance_dict[value.currency] = {"balance": str(value.balance), "price": str(price),
                                                        "amount": str(amount), "dollar": dollar}
        self.logger.debug(balance_dict)
        return balance_dict

    def get_account_amount(self, symbol_name, symbol_balance):
        price = self.get_price(symbol_name + "usdt")
        amount: str = str(float(symbol_balance) * float(price))
        amount: str = retain_decimals(str(amount), '2')
        return price, amount

    def get_mongodb_dollar(self, symbol_name, symbol_amount):
        result = self.keep_balance_collection.find_one({"symbol": symbol_name})
        if result is None:
            self.keep_balance_collection.insert_one({"symbol": symbol_name, "amount": str(symbol_amount)})
            return symbol_amount
        else:
            return result['amount']

    def get_price(self, symbol_name: str) -> str:
        """
        获取当前交易对价格
        :param symbol_name:
        :return:
        """
        try:
            file = FileReadAndWrite.read(f"{self.get_config_value('strategy', 'price_file_locate')}{symbol_name}.txt")
            time_stamp, price = file.split(",")
            # time.time()获取的时间为0时区时间, 火币为东8区时间, 因此减去28800秒
            current_time = time.time() - 28800
            if abs(current_time - float(time_stamp) / 1000) < self.timeout:
                return price
            else:
                self.logger.error("动态平衡策略获取时间戳出错, 价格超时")
                os.system("/root/miniconda3/bin/supervisorctl -u gator -p zhangpei529\!\@ -c "
                          "/usr/supervisor/supervisord.conf restart collect_price_data")
                self.logger.info("重启价格获取程序")
                raise TimeoutError
        except FileNotFoundError:
            self.logger.error(f"{self.get_config_value('strategy', 'price_file_locate')}{symbol_name}.txt文件不存在")
            raise FileNotFoundError