我正试图获得欧元兑美元的历史价格。我正试图获得最后的5000.但我只收到180.
这是我的代码:
pDynamicOffsets
但是df_data只有180行,而不是5000行。
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)
我该如何解决这个问题?
答案 0 :(得分:1)
如何解决这个问题就是将其简化为基础知识。获得一个简单的requests.get()
调用工作并打印文本。一旦你让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)
这样的东西将取代你的大部分代码,没有慢速循环。
答案 1 :(得分:0)
对于此API调用,我确定您应该使用:
stream = False
还要考虑使用可用于访问REST-API的API包装器之一。这些将您的代码减少到几行。 https://github.com/hootnot/oanda-api-v20允许您下载超过5000条记录。
对于v1(2017年12月末?)
https://github.com/oanda/oandapy
或v2:
https://github.com/oanda/v20-python
https://github.com/hootnot/oanda-api-v20
https://github.com/oanda/v20-python
https://github.com/hootnot/oanda-api-v20