OANDA Japanの口座が順次v20の取引環境に移行している最中ですが(OANDAからのお知らせ)、OANDA JapanのREST APIもいよいよv1からv20への移行期限が迫っています。
そこでデモアカウントでREST-API v20を使ってドル円の現在価格取得と過去の価格取得を今回はやってみたいと思います。
同じ内容をv1で出力した結果も併せて載せました。出力フォーマットがv20とv1でかなり異なるのでアプリケーションコードの変更はまあまあ有りそうです。
デモ口座の開設
以下のリンク先からデモ口座を開設します。
https://fxtrade.oanda.com/your_account/fxtrade/register/gate?utm_source=oandaapi&utm_medium=link&utm_campaign=devportaldocs_demo
注意点は、現状フォーム入力でCountryを「Japan」にしてしまうとv20用のデモアカウントが作成できないようなので「United States」にしておきます。
Phoneは日本の国番号を選択して、電話番号の頭一桁を除いた番号を入力しておけばOKです。
アカウントIDとアクセストークンの取得
APIを叩くためのアクセスキーを取得します。アカウントIDとアクセストークンがそれに当たります。
以下のリンクから作成したデモ口座にログインしてください
https://fxtrade.oanda.ca/demo-account/login?app_name=SecureSignIn
OANDA Webを開いて「Manage Funds」というリンクから「v20 Account Number」(アカウントID)を確認できます。
そして「API」というリンクからアクセストークンを取得できます。
モジュールのインストール
サードパーティーの「oandapyV20」を使います。
オフィシャルのAPIラッパーより使いやすいです。
$ pip install oandapyV20 Collecting oandapyV20 Using cached https://files.pythonhosted.org/packages/0d/d5/f69c978f485db3048886c447398e39c6ec18041e689656e6d31697ea4e7f/oandapyV20-0.6.3.tar.gz Installing collected packages: oandapyV20 Running setup.py install for oandapyV20 ... done Successfully installed oandapyV20-0.6.3
現在価格の取得
v20でのドル円現在価格取得のサンプルコードです。
import json
from oandapyV20 import API
from oandapyV20.endpoints.pricing import PricingInfo
from oandapyV20.exceptions import V20Error
account_id = "xxx-xxx-xxxxxxxx-xxx"
access_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
api = API(access_token=access_token, environment="practice")
params = {"instruments": "USD_JPY"}
pricing_info = PricingInfo(accountID=account_id, params=params)
try:
api.request(pricing_info)
response = pricing_info.response
print(json.dumps(response, indent=4)) # 出力値を見やすく整形
except V20Error as e:
print("Error: {}".format(e))
上記サンプルコード(v20)の出力値です。
{
"time": "2019-02-17T02:38:01.189006987Z",
"prices": [
{
"type": "PRICE",
"time": "2019-02-16T01:10:08.009928180Z",
"bids": [
{
"price": "110.411",
"liquidity": 10000000
}
],
"asks": [
{
"price": "110.501",
"liquidity": 10000000
}
],
"closeoutBid": "110.396",
"closeoutAsk": "110.516",
"status": "non-tradeable",
"tradeable": false,
"unitsAvailable": {
"default": {
"long": "2500000",
"short": "2500000"
},
"openOnly": {
"long": "2500000",
"short": "2500000"
},
"reduceFirst": {
"long": "2500000",
"short": "2500000"
},
"reduceOnly": {
"long": "0",
"short": "0"
}
},
"quoteHomeConversionFactors": {
"positiveUnits": "0.00904969",
"negativeUnits": "0.00905707"
},
"instrument": "USD_JPY"
}
]
}
同じ内容をV1で出力した値です。V1の方がかなりシンプルです。
{
"prices": [
{
"instrument": "USD_JPY",
"time": "2019-02-15T22:00:00.055412Z",
"bid": 110.421,
"ask": 110.491,
"status": "halted"
}
]
}
過去価格の取得
v20のドル円過去3日分の日足価格取得サンプルコードです。
import json
from oandapyV20 import API
import oandapyV20.endpoints.instruments as instruments
from oandapyV20.exceptions import V20Error
access_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
api = API(access_token=access_token, environment="practice")
params = {
"count": 3, # 足3本取得
"granularity": "D", # 日足を取得
"price": "B", # Bidを取得
}
instruments_candles = instruments.InstrumentsCandles(instrument="USD_JPY", params=params)
try:
api.request(instruments_candles)
response = instruments_candles.response
print(json.dumps(response, indent=4)) # 出力値を見やすく整形
except V20Error as e:
print("Error: {}".format(e))
上記サンプルコード(v20)の出力値です。
{
"instrument": "USD_JPY",
"granularity": "D",
"candles": [
{
"complete": true,
"volume": 11518,
"time": "2019-02-12T22:00:00.000000000Z",
"bid": {
"o": "110.444",
"h": "111.049",
"l": "110.408",
"c": "110.987"
}
},
{
"complete": true,
"volume": 19517,
"time": "2019-02-13T22:00:00.000000000Z",
"bid": {
"o": "110.968",
"h": "111.124",
"l": "110.436",
"c": "110.436"
}
},
{
"complete": false,
"volume": 16099,
"time": "2019-02-14T22:00:00.000000000Z",
"bid": {
"o": "110.431",
"h": "110.638",
"l": "110.227",
"c": "110.411"
}
}
]
}
同じ内容をV1で出力した値です。candles以下の階層構造から違うようです。
{
"instrument": "USD_JPY",
"granularity": "D",
"candles": [
{
"time": "2019-02-12T22:00:00.000000Z",
"openBid": 110.444,
"openAsk": 110.505,
"highBid": 111.049,
"highAsk": 111.063,
"lowBid": 110.408,
"lowAsk": 110.44,
"closeBid": 110.987,
"closeAsk": 111.021,
"volume": 11518,
"complete": true
},
{
"time": "2019-02-13T22:00:00.000000Z",
"openBid": 110.968,
"openAsk": 111.008,
"highBid": 111.124,
"highAsk": 111.138,
"lowBid": 110.436,
"lowAsk": 110.465,
"closeBid": 110.436,
"closeAsk": 110.536,
"volume": 19517,
"complete": true
},
{
"time": "2019-02-14T22:00:00.000000Z",
"openBid": 110.431,
"openAsk": 110.467,
"highBid": 110.638,
"highAsk": 110.652,
"lowBid": 110.227,
"lowAsk": 110.266,
"closeBid": 110.411,
"closeAsk": 110.501,
"volume": 16099,
"complete": true
}
]
}
APIに渡すパラメータ名もv20とv1では異なるのでOANDAのv20マニュアルを確認する必要があります。
