Put the exchange rate obtained from Oanda REST API in Python into MongoDB

Note. Both use JSON format, so they are very compatible.

Environment

From here for the time being. Omitting the environment construction of python

Install MongoDB on Windows

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

Install pymongo

pip install pymongo

Install oandapy (python wrapper for oanda REST API)

pip install oandapy

Instructions

MongoDB Primer

http://qiita.com/saba1024/items/f2ad56f2a3ba7aaf8521 http://www.cuspy.org/diary/2012-04-17/the-little-mongodb-book-ja.pdf

How to use Oanda API

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.

How to use pymongo

http://api.mongodb.com/python/

How to use oandapy

https://github.com/oanda/oandapy

Code example

Hit the API with 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.

Put data in MongoDB

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.

Get a list of available stocks

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

If you want to get more than 5000 steps of data

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

Put the exchange rate obtained from Oanda REST API in Python into MongoDB
Getting the arXiv API in Python
Hit the Sesami API in Python
Hit the web API in Python
Use the Flickr API from Python
Quickly implement REST API in Python
Access the Twitter API in Python
Get exchange rates from open exchange rates in Python
Try using the Kraken API in Python
Tweet using the Twitter API in Python
MongoDB for the first time in Python
Try hitting the YouTube API in Python
Touch the sample v20-python-samples of the OANDA v20 REST API wrapper library for Python
Put text scraped in Python into Google Sheets
Try using the BitFlyer Ligntning API in Python
Tips for hitting the ATND API in Python
[WP REST API v2] Upload images in Python
Hit the Firebase Dynamic Links API in Python
Try accessing the YQL API directly from Python 3
Try using the DropBox Core API in Python
Import the schedule obtained from "Schedule-kun" into Google Calendar
Python beginners tried implementing REST API in one day
Operate mongoDB from python in ubuntu environment ① Introduction of mongoDB
Translator in Python from Visual Studio 2017 (Microsoft Translator Text API)
[Python] Get the text of the law from the e-GOV Law API
Initial settings when using the foursquare API in python
Get LEAD data using Marketo's REST API in Python
Find the part that is 575 from Wikipedia in Python
Hit Watson's REST API from Python on IBM Bluemix
Using the National Diet Library Search API in Python
A little bit from Python using the Jenkins API
Put Ubuntu in Raspi, put Docker on it, and control GPIO with python from the container
C API in Python 3
I tried to create API list.csv in Python from swagger.yaml
The first API to make with python Djnago REST framework
Get the value while specifying the default value from dict in Python
Manipulate the clipboard in Python and paste the table into Excel
Hit REST in Python to get data from New Relic
Specification generation and code generation in REST API development (Python edition)
What beginners learned from the basics of variables in python