Ejemplo n.º 1
0
    async def check_asset_update(self, *args, **kwargs):
        """Fetch asset information."""
        result, error = await self._rest_api.get_account_balance()
        if error:
            logger.warn("platform:",
                        self._platform,
                        "account:",
                        self._account,
                        "get asset info failed!",
                        caller=self)
            return

        assets = {}
        for key, value in result.items():
            name = await self.convert_currency_name(key)
            if not name:
                logger.warn("convert currency error:", key, caller=self)
                continue
            total = float(value)
            assets[name] = {"total": "%.8f" % total, "free": 0, "locked": 0}

        if assets == self._assets:
            update = False
        else:
            update = True
        self._assets = assets

        # Publish AssetEvent.
        timestamp = tools.get_cur_timestamp_ms()
        EventAsset(self._platform, self._account, self._assets, timestamp,
                   update).publish()
        logger.info("platform:",
                    self._platform,
                    "account:",
                    self._account,
                    "asset:",
                    self._assets,
                    caller=self)
Ejemplo n.º 2
0
    async def check_asset_update(self, *args, **kwargs):
        """ 检查账户资金是否更新
        """
        result, error = await self._rest_api.get_user_account()
        if error:
            return

        # 更新资金信息
        assets = {}
        for name, value in result["asset"].items():
            free = float(value.get("available"))
            total = float(value.get("total"))
            if not total:
                continue
            d = {
                "free": "%.8f" % free,
                "locked": "%.8f" % (total - free),
                "total": "%.8f" % total
            }
            assets[name] = d

        if assets == self._assets:
            update = False
        else:
            update = True
        self._assets = assets

        # 推送当前资产
        timestamp = tools.get_cur_timestamp_ms()
        EventAsset(self._platform, self._account, self._assets, timestamp,
                   update).publish()
        logger.info("platform:",
                    self._platform,
                    "account:",
                    self._account,
                    "asset:",
                    self._assets,
                    caller=self)
Ejemplo n.º 3
0
    def __init__(self, platform, account, symbol):
        """ 初始化
        @param platform 交易平台
        @param account 交易账户
        @param symbol 交易对
        """
        self._platform = platform  # 交易平台
        self._account = account  # 交易账户
        self._timestamp = 0  # 资产更新时间戳
        self._symbol = symbol  # 交易对
        self._assets = {}  # 所有资金详情
        self._key_x = self._symbol.split('/')[0]
        self._key_y = self._symbol.split('/')[1]
        self._asset_x = None  # 交易对的资金详情 {"free": 11.11, "locked": 22.22, "total": 33.33}
        self._asset_y = None  # 交易对的资金详情 {"free": 11.11, "locked": 22.22, "total": 33.33}
        self._callback_handlers = []  # 资产有更新的时候,执行的回调函数

        # 初始化资产数据库对象
        self._asset_db = AssetData()

        # 订阅事件 资产更新
        EventAsset(platform, account).subscribe(self._on_event_asset)
        # 从数据库加载初始化资产
        asyncio.get_event_loop().create_task(self._load_asset())