首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >blp.live与Pyxll Asyncio RTD在python中的应用

blp.live与Pyxll Asyncio RTD在python中的应用
EN

Stack Overflow用户
提问于 2022-02-23 05:28:48
回答 1查看 187关注 0票数 0

我对Pyxll和Asyncio很陌生,很难完成下面的代码。我一直在电子表格上得到初始值=0,这一点也不刷新。你能帮忙让我知道我做错了什么吗?下面是Pyxll教程中的示例:https://www.pyxll.com/docs/userguide/rtd.html#using-the-asyncio-event-loop

代码语言:javascript
复制
from pyxll import RTD, xl_func, xl_app
from xbbg import blp
import asyncio

class AsyncRTDExample(RTD):

    def __init__(self, ticker):
        super().__init__(value=0)
        self.__stopped = False
        self.__ticker = ticker

    async def connect(self):
        while not self.__stopped:
            # Yield to the event loop for 1s
            await asyncio.sleep(1)

            # Update value (which notifies Excel)
            async for t in blp.live(self.__ticker, flds = ['LAST_PRICE'], info=['LAST_PRICE']):
                self.value = t["LAST_PRICE"]

    async def disconnect(self):
        self.__stopped = True

@xl_func("string ticker: rtd<float>", recalc_on_open=True)
def async_rtd_price(ticker):
    return AsyncRTDExample(ticker)

真的很感激!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-23 17:49:18

我用xbbg blp.bdp函数计算出来,它做类似的事情。这是一个很好的替代,如果你有一个庞大的bbg功能拉RT价格。PyXLL允许您输入一组代码,这将节省大量时间。我希望这能节省一些时间:)

代码语言:javascript
复制
from pyxll import RTD, xl_func, xl_app
import logging
import sys
from xbbg import blp
import asyncio

_log = logging.getLogger(__name__)

class AsyncPriceRTD(RTD):

    def __init__(self, ticker):
        self.__ticker = ticker
        super().__init__(value=blp.bdp(self.__ticker, flds=['Last_Price']))
        self.__stopped = False
        
    async def bdp(self):
        return (blp.bdp(self.__ticker, flds=['Last_Price']))

    async def connect(self):
        try:
            while not self.__stopped:
                await asyncio.sleep(0.5)
                self.value = await self.bdp()

        except:
            self.set_error(*sys.exc_info())

    async def disconnect(self):
        self.__stopped = True


@xl_func("string[] array: rtd<dataframe<index=False,columns=False>>", recalc_on_open=True)
def async_rtd_price(ticker):
    return AsyncPriceRTD(ticker)


@xl_func("int interval: var")
def rtd_set_interval(interval):
    """Set Excel's RTD throttle interval (in milliseconds).

    When real time data objects notify Excel that they have changed
    the displayed value in Excel doesn't actually update until
    Excel refreshes. How often Excel refreshes due to RTD updates
    defaults to every 2 seconds, and so to see data refresh more
    frequently this function may be used.
    """
    xl = xl_app()
    xl.RTD.ThrottleInterval = interval
    return "Refresh interval: {} seconds.".format(interval/1000)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71231777

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档