Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法获取Oanda历史价格_Python_Python 2.7 - Fatal编程技术网

Python 无法获取Oanda历史价格

Python 无法获取Oanda历史价格,python,python-2.7,Python,Python 2.7,我想知道欧兰达欧元兑美元的历史价格。我想拿到最后5000英镑。但我只收到180英镑 这是我的代码: def get_data_oanda(num_periods, **keyword_parameters): """ Environment Description fxTrade (Live) The live (real money) environment fxTrade Practice (Dem

我想知道欧兰达欧元兑美元的历史价格。我想拿到最后5000英镑。但我只收到180英镑

这是我的代码:

def get_data_oanda(num_periods, **keyword_parameters):
    """
    Environment                 Description 
    fxTrade (Live)              The live (real money) environment 
    fxTrade Practice (Demo)     The demo (simulated money) environment 
    """
    domainDict = { 'live' : 'api-fxtrade.oanda.com','demo' : 'api-fxpractice.oanda.com' }
    environment = 'demo'
    domain = domainDict[environment]
    access_token = 'xxxx'
    account_id = 'xxxx'
    instruments = 'EUR_USD'
    count = num_periods
    granularity = "M1"
    try:
        s = requests.Session()
        url = "https://" + domain + "/v1/candles"
        headers = {'Authorization' : 'Bearer ' + access_token,
                   # 'X-Accept-Datetime-Format' : 'unix'
                  }
        params = {'instrument' : instruments, 'accountId' : account_id, 'count' : count, 'granularity' : granularity}
        req = requests.Request('GET', url, headers = headers, params = params)
        pre = req.prepare()
        resp = s.send(pre, stream = True, verify = True)
        return resp
    except Exception as e:
        s.close()
        print()
        print("Caught exception when connecting to stream\n" + str(e))


num_periods = 5000
my_date = datetime.datetime.now(pytz.timezone('America/Sao_Paulo')).strftime('%Y-%m-%dT%H:%M:%S')
timezone = 'America/Sao_Paulo' 
response = get_data_oanda(num_periods)
msg = json.loads(response.text)
candles = msg['candles']
for candle in candles:
    df_data = df_data.append({
            'date': datetime.datetime.strptime(candle['time'], '%Y-%m-%dT%H:%M:%S.000000Z').replace(tzinfo=pytz.utc).astimezone(local_tz).strftime('%Y-%m-%d %H:%M:%S'),
            'instrument': msg['instrument'],
            "open": candle['openAsk'],
            "high": candle['highAsk'],
            "low": candle['lowAsk'],
            "close": candle['closeAsk'],
            "volume": candle['volume']
        },ignore_index=True)
但df_数据只有180行,而不是5000行

[180 rows x 7 columns]

我该如何解决这个问题呢?

解决这个问题的方法是把它从最基本的部分剥离出来。获取一个简单的
请求。Get()
调用working并打印文本。一旦API返回了所需的结果,就可以在Pandas端工作。如果API没有返回您需要的结果,请联系服务提供商

在Pandas中加载JSON的正确方法要简单得多,如下所示:

resp = requests.get(url, headers=headers, params=params, stream=True)
df = pd.read_json(resp.raw)
df_data = pd.io.json.json_normalize(df.candles)
df_data['time'] = pd.to_datetime(df_data.time)

类似的东西将替换大部分代码,不会出现慢循环。

对于此API调用,我确信您应该使用:

    stream = False

还考虑使用可访问REST-API的API包装器之一。这些将代码减少到几行。 还允许您下载5000多条记录

v1(2017年12月寿命结束?)

或v2:


你能试着提出我在OANDAAPI上的请求吗?我已经给他们发了一封电子邮件,但是他们没有回复我,只是把请求本身和结果发布在你的问题中。并非所有这些额外的代码都是无关的。
https://github.com/oanda/v20-python
https://github.com/hootnot/oanda-api-v20