自动更新 GET 请求 python

Automatically updating GET request python

我有一个汇率请求,我想每秒更新一次。截至目前,我必须重新加载程序以刷新速率。我将如何在 Python 中执行此操作?

任何帮助将不胜感激,在此先感谢..

脚本:

import oandapy

oanda = oandapy.API(environment="practice", access_token="xxxxxxxxxxxx")

response = oanda.get_prices(instruments="EUR_USD")
prices = response.get("prices")
asking_price = prices[0].get("ask")

stop = asking_price - .001

每个答案:

while True:
    response = oanda.get_prices(instruments="EUR_USD")
    prices = response.get("prices")
    asking_price = prices[0].get("ask")

    stop = asking_price - .001

time.sleep(1)

一般的方法是将整个事情包装在一个无限循环中,并在请求之间等待:

while True:
    # ... do and print request
    time.sleep(1) # then wait one second

确保您的 API 访问令牌允许每秒发送一个请求。


但是,经过快速 Google 我发现您使用的 API 支持流媒体速率:https://github.com/oanda/oandapy#rates-streaming