在 python 中将 blp.live 与 Pyxll Asyncio RTD 结合使用

发布于 2025-01-09 03:45:08 字数 1096 浏览 5 评论 0原文

我是 Pyxll 和 Asyncio 的新手,无法运行以下代码。我一直在电子表格上得到初始值 = 0,但它并不刷新。你能帮助我并让我知道我做错了什么吗?我按照 Pyxll 教程中的示例进行操作: https ://www.pyxll.com/docs/userguide/rtd.html#using-the-asyncio-event-loop

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)

非常感谢!

I am new to Pyxll and Asyncio and having trouble get the following code going. I kept getting the initial value = 0 on the spreadsheet and it's not refreshing. Could you help and let me know what I did wrong? I followed the example from Pyxll's tutorial here: https://www.pyxll.com/docs/userguide/rtd.html#using-the-asyncio-event-loop

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)

Really appreciate it!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独木成林 2025-01-16 03:45:08

我发现 xbbg blp.bdp 函数可以做类似的事情。如果你有一个巨大的 bbg 函数来拉动 RT 价格,那么这是一个很好的替代品。 PyXLL 允许您输入股票代码数组,这可以节省大量时间。我希望这可以节省某人一些时间:)

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)

I figured out with xbbg blp.bdp function which does similar thing. This is a good substitute if you have a massive bbg function pulling RT price. PyXLL allows you to input an array of tickers which saves a lot of time. I hope this could save someone some time :)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文