LoginSignup
7
7

More than 3 years have passed since last update.

oandapyV20を使って為替データを取得

Last updated at Posted at 2019-10-22

為替の値動きを分析するためにpythonを使ってプログラミングしています。
今回は、リアルタイムの為替情報を取得するためのコードを紹介します。


事前準備

コーディング環境としてjupyter notebookを使っています。
anaconda導入時にいろいろなライブラリが自動的に入っていると思いますので、
今回はoandaのライブラリのみインストールすれば良いです。

APItest.py
!pip install oandapyV20

古い記事だとoandapyV20ではなく、oandapyをインストールしてますが、そちらをやってみると、インストールまではいけますが、APIの取得がなかなかうまくいきません。
下記のエラーメッセージが返ってきます
AttributeError: module 'oandapy' has no attribute 'API'

以下は実際のコードです

必要なライブラリ導入

APItest.py
from oandapyV20 import API
import oandapyV20.endpoints.instruments as instruments
import pandas as pd

APIアクセス用情報登録

APItest.py
access_token = "XXXXXXXXX" #自身のaccess_tokenを記載
accountID = "XXX-XXX-XXXXXXX-XXX" #自身のaccountIDを記載
api = API(access_token=access_token)

実際のデータ取得

APItest.py
# 1時間間隔で500データ(ドル円)を取得
r = instruments.InstrumentsCandles(instrument="USD_JPY", params={"count": 500, "granularity": "H1"})
api.request(r)

instrumentは通貨ペア
countは取得するデータ数(最大5000らしい)
granularityは時間足
を意味します。

7
7
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7