简体   繁体   English

CCXT如何自动刷新价格?

[英]CCXT how to refresh prices automatically?

when querying exchange APIs, to get the latest price do I simply keep calling my fetchPrice() method every minute or so?在查询交易所 API 时,为了获得最新价格,我是否只需每分钟左右调用一次 fetchPrice() 方法? I can get the price once, but is the proper way to update using CCXT to simply keep on fetching?我可以得到一次价格,但是使用 CCXT 进行更新以继续获取的正确方法是什么? The use case is for a simple market scanner用例是一个简单的市场扫描仪

It would have been nice to have some of your code to help you solve this question.如果有一些代码可以帮助您解决这个问题,那就太好了。 Anyway you need to use asyncio event loops.无论如何,您需要使用asyncio事件循环。

There are many tutorials online on how to use asyncio but I found this one particularly helpful when I started out.网上有很多关于如何使用asyncio的教程,但是当我刚开始使用时,我发现这一篇特别有用。

Here is the code below to get the bid / ask every second on deribit exchange but you can reflace that with any exchange that is supported by ccxt and it will work the same way:这是下面的代码,用于在 deribit 交易所每秒获得出价/询问,但您可以使用 ccxt 支持的任何交易所替换它,它的工作方式相同:

import asyncio
import ccxt

async def cctx_prices():
    deribit = ccxt.deribit()
    while True:
        ticker = deribit.fetch_ticker('BTC-PERPETUAL')
        print(ticker)
        
        bid = ticker['bid']
        ask = ticker['ask']
        print(f"{bid} / {ask}")
        
        # pause asyncio for 1 second
        await asyncio.sleep(1) 

loop = asyncio.get_event_loop()
asyncio.ensure_future(cctx_prices())
loop.run_forever()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM