Note. Both use JSON format, so they are very compatible.
From here for the time being. Omitting the environment construction of python
Don't forget to put the bin in your PATH. (I forgot) http://qiita.com/yoh-nak/items/f0c429f10347ae7ec98b http://qiita.com/t-koyama/items/9b8804cbec59b3c93eb0
pip install pymongo
pip install oandapy
http://qiita.com/saba1024/items/f2ad56f2a3ba7aaf8521 http://www.cuspy.org/diary/2012-04-17/the-little-mongodb-book-ja.pdf
http://developer.oanda.com/docs/jp/ By the way, I haven't tried using the sandbox environment, so I recommend using the fxTrade Practice environment from the beginning.
http://api.mongodb.com/python/
https://github.com/oanda/oandapy
import oandapy
oanda = oandapy.API(environment="practice", access_token="your_token")
response = oanda.get_history(instrument="EUR_USD",granularity="D",count=500)
EUR_USD_D = response.get("candles")
With this alone, the past daily euro-dollar data (500 steps) can be obtained. You can also specify start and end.
from pymongo import MongoClient
client = MongoClient('localhost',27017)
db = client.ex_rate
collection = db.eur_usd_d
collection.insert_many(d for d in EUR_USD_D)
With this alone, the daily data (500 steps) acquired above in JSON format can be stored in the collection (table) [eur_usd_d] of the MongoDB database [ex_rate]. Even if the contents of MongoDB are empty, they create databases and collections without permission.
import oandapy
import pandas as pd
oanda = oandapy.API(environment="practice", access_token="your_token")
Enter the account id for #account_id (not the login id) response = oanda.get_instruments(account_id="xxxxxxx") insts = response.get("instruments") #In pandas for easy viewing df = pd.DataFrame(list(insts)) df.head()
↓ Output
displayName instrument maxTradeUnits pip
0 AUD/CAD AUD_CAD 10000000 0.0001
1 AUD/CHF AUD_CHF 10000000 0.0001
2 AUD/HKD AUD_HKD 10000000 0.0001
3 AUD/JPY AUD_JPY 10000000 0.01
4 AUD/NZD AUD_NZD 10000000 0.0001
You can get it continuously even if the number you want to get exceeds the upper limit of 5000 by the following procedure. There may be a smarter way. -Get the latest data group and get the top (oldest) "time" -Reformat the "time" data and issue a request again as "end"
import oandapy
import datetime as dt
oanda = oandapy.API(environment="practice",
access_token="your_token")
response1 = oanda.get_history(instrument="EUR_USD",granularity="D",count=2)
EUR_USD_H1 = response1.get("candles")
#Get the time of the first data and convert RFC3339 format to python datetime format dtime = dt.datetime.strptime(EUR_USD_H1[0]['time'],'%Y-%m-%dT%H:%M:%S.%fZ') #Convert to RFC3339 format again rfc_endtime = dtime.isoformat('T')
response2 = oanda.get_history(instrument="EUR_USD",granularity="D",end=rfc_endtime,count=2)
EUR_USD_H2 = response2.get("candles")
for d in EUR_USD_H2:
print(d)
for d in EUR_USD_H1:
print(d)
↓ Output
{'time': '2017-08-28T21:00:00.000000Z', 'openBid': 1.19762, 'openAsk': 1.19787, 'highBid': 1.20698, 'highAsk': 1.20712, 'lowBid': 1.19451, 'lowAsk': 1.19471, 'closeBid': 1.19709, 'closeAsk': 1.19728, 'volume': 73642, 'complete': True}
{'time': '2017-08-29T21:00:00.000000Z', 'openBid': 1.19697, 'openAsk': 1.19731, 'highBid': 1.19839, 'highAsk': 1.19853, 'lowBid': 1.18803, 'lowAsk': 1.18821, 'closeBid': 1.18828, 'closeAsk': 1.18856, 'volume': 56980, 'complete': True}
{'time': '2017-08-30T21:00:00.000000Z', 'openBid': 1.18827, 'openAsk': 1.18857, 'highBid': 1.19119, 'highAsk': 1.19139, 'lowBid': 1.18225, 'lowAsk': 1.18238, 'closeBid': 1.19079, 'closeAsk': 1.19102, 'volume': 59987, 'complete': True}
{'time': '2017-08-31T21:00:00.000000Z', 'openBid': 1.19078, 'openAsk': 1.19103, 'highBid': 1.19794, 'highAsk': 1.19807, 'lowBid': 1.18485, 'lowAsk': 1.18508, 'closeBid': 1.18572, 'closeAsk': 1.18625, 'volume': 76954, 'complete': True}
Recommended Posts