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

        assets = {}
        for item in result["info"]:
            symbol = item["instrument_id"].split("-")[0]
            total = float(item["equity"])
            locked = float(item["margin"])
            if total > 0:
                assets[symbol] = {
                    "total": "%.8f" % total,
                    "free": "%.8f" % (total - locked),
                    "locked": "%.8f" % locked
                }

        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):
        """Fetch asset information."""
        result, error = await self._rest_api.get_accounts("trade")
        if error:
            return

        assets = {}
        for item in result:
            name = item["currency"]
            total = float(item["balance"])
            free = float(item["available"])
            locked = float(item["holds"])
            if not total:
                continue
            d = {
                "free": "%.8f" % free,
                "locked": "%.8f" % locked,
                "total": "%.8f" % total
            }
            assets[name] = d

        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.º 3
0
    async def check_asset_update(self, *args, **kwargs):
        """ 检查账户资金是否更新
        """
        result, error = await self._rest_api.get_user_account()
        if error:
            logger.warn("platform:", self._platform, "account:", self._account, "get asset info failed!", caller=self)
            return

        assets = {}
        for name, item in result["info"].items():
            symbol = name.upper()
            total = float(item["equity"])
            locked = float(item["margin"])
            if total > 0:
                assets[symbol] = {
                    "total": "%.8f" % total,
                    "free": "%.8f" % (total - locked),
                    "locked": "%.8f" % locked
                }

        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.º 4
0
    async def check_asset_update(self, *args, **kwargs):
        """Fetch asset information."""
        result, error = await self._rest_api.get_user_account()
        if error:
            logger.warn("platform:", self._platform, "account:", self._account, "get asset info failed!", caller=self)
            return

        assets = {}
        for item in result:
            symbol = item["currency"]
            total = float(item["balance"])
            free = float(item["available"])
            locked = float(item["frozen"])
            if total > 0:
                assets[symbol] = {
                    "total": "%.8f" % total,
                    "free": "%.8f" % free,
                    "locked": "%.8f" % locked
                }

        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.º 5
0
 def __init__(self, platform, account, callback):
     """ Initialize. """
     if platform == "#" or account == "#":
         multi = True
     else:
         multi = False
     from quant.event import EventAsset
     EventAsset(platform, account).subscribe(callback, multi)
Ejemplo n.º 6
0
 def __init__(self, platform, account, callback):
     """ 初始化
     @param platform 交易平台
     @param account 交易账户
     @param callback 资产更新回调函数,必须是async异步函数,回调参数为 Asset 对象,比如: async def on_event_account_update(asset: Asset): pass
     """
     from quant.event import EventAsset
     EventAsset(platform, account).subscribe(callback)
Ejemplo n.º 7
0
 async def _publish_asset(self, *args, **kwargs):
     """ 推送资产信息
     """
     if self._last_assets == self._assets:
         update = False
     else:
         update = True
     self._last_assets = self._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.º 8
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

        temps = {}
        for item in result.get("list"):
            name = item.get("currency").upper()
            t = item.get("type")
            b = float(item.get("balance"))
            if name not in temps:
                temps[name] = {}
            if t == "trade":
                temps[name]["free"] = b
            else:
                temps[name]["locked"] = b

        assets = {}
        for name, item in temps.items():
            total = item["free"] + item["locked"]
            if total <= 0:
                continue
            assets[name] = {
                "free": "%.8f" % item["free"],
                "locked": "%.8f" % item["locked"],
                "total": "%.8f" % total
            }

        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.º 9
0
 async def _on_event_asset(self, event):
     """ 资产更新事件
     @param event 事件对象
     """
     asset = EventAsset().duplicate(event)
     if asset.platform != self._platform:
         return
     if asset.account != self._account:
         return
     self._assets = asset.assets
     self._timestamp = asset.timestamp
     self._asset_x = self._assets.get(self._key_x)
     self._asset_y = self._assets.get(self._key_y)
     for func in self._callback_handlers:
         asyncio.get_event_loop().create_task(func(self.assets))
     logger.debug('assets updated. assets:', self._assets, caller=self)
Ejemplo n.º 10
0
    async def check_asset_update(self, *args, **kwargs):
        """ 检查账户资金是否更新
        """
        result, error = await self._rest_api.get_user_account()
        if error:
            logger.warn("platform:",
                        self._platform,
                        "account:",
                        self._account,
                        "get asset info failed!",
                        caller=self)
            return

        assets = {}
        for item in result["balances"]:
            name = item.get("asset")
            free = float(item.get("free"))
            locked = float(item.get("locked"))
            total = free + locked
            if total > 0:
                assets[name] = {
                    "total": "%.8f" % total,
                    "free": "%.8f" % free,
                    "locked": "%.8f" % locked
                }

        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.º 11
0
    async def check_asset_update(self, *args, **kwargs):
        """ 检查账户资金是否更新
        """
        result, error = await self._rest_api.get_margin()
        if error:
            logger.warn("platform:",
                        self._platform,
                        "account:",
                        self._account,
                        "get asset info failed!",
                        caller=self)
            return

        assets = {}
        name = "XBT"
        free = result["availableMargin"] / 100000000.0
        total = result["marginBalance"] / 100000000.0
        locked = total - free
        assets[name] = {
            "total": "%.8f" % total,
            "free": "%.8f" % free,
            "locked": "%.8f" % locked
        }

        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.º 12
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 item in result.get("data"):
            name = item.get("currency").upper()
            b = float(item.get("balance"))
            if b <= 0:
                continue
            assets[name] = {
                "free": "%.8f" % item["available"],
                "locked": "%.8f" % (b - float(item["available"])),
                "total": "%.8f" % b
            }

        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.º 13
0
    async def check_asset_update(self, *args, **kwargs):
        """Fetch asset information."""
        success, error = await self._rest_api.get_user_account()
        if error or not success["result"]:
            logger.warn("platform:",
                        self._platform,
                        "account:",
                        self._account,
                        "get asset info failed!",
                        caller=self)
            return

        assets = {}
        for key, value in success["available"].items():
            free = float(value)
            locked = float(success["locked"][key])
            assets[key] = {
                "total": "%.8f" % (free + locked),
                "free": "%.8f" % free,
                "locked": "%.8f" % locked
            }

        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.º 14
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.º 15
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.º 16
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())