シストレどうですか

  Algorithmic Trading for Dummies

OANDA API 解説編 第8回 口座情報の取得

今回は口座情報の取得方法について学んでみたいと思います。前回の銘柄情報の取得と似ていますので、特に複雑な引数はありません。
OANDA REST-v20の開発ガイド(英語版)はこちら


口座情報を取得する

口座のサマリー情報を取得する

口座情報(明細)を取得する



どちらも同じ引数で呼び出すことが可能ですが、取得できる内容の細かさに違いがあります。共通部分が多いので目的に応じて使い分けられます。

引数の説明

名前 場所 必須 説明
Authorization header string Y 署名なしトークンID
Accept-Datetime-Format header 時刻フォーマットの指定 初期値:RFC3339 (yyyy-mm-ddThh:mm:ss.nnnnnnnnnZ形式)
その他:UNIX (12345678.000000123形式)
accountID path AccountID Y 自分の口座番号

早速この引数を使って銘柄情報を取得してみます。

# 必要なモジュールの読み込み
import requests
import json
  
# 口座情報の設定
API_Token = '********************************-********************************'
API_AccountID = '999-999-99999999-999'
  
# URLの設定 (デモ口座用非ストリーミングURL)
API_URL =  "https://api-fxpractice.oanda.com"
  
# <口座情報取得(サマリー)用URLの変数の設定>
url = API_URL + "/v3/accounts/%s/summary" % str(API_AccountID)
  

# <口座情報取得(明細)用URLの変数の設定>の場合はこちら
#url = API_URL + "/v3/accounts/%s" % str(API_AccountID)
  
# ヘッダー情報の変数の設定
headers = {
             "Authorization": "Bearer " + API_Token
          }
  
# サーバーへの要求
response = requests.get(url, headers=headers)
  
# 処理結果の編集
Response_Body = response.json()
print(json.dumps(Response_Body, indent=2))

すると以下のようなデータを受け取ります。

/v3/account/{account name}/summary の場合

{
  {
  "account": {
    "guaranteedStopLossOrderMode": "DISABLED",
    "hedgingEnabled": false,
    "id": "xxx-xxx-xxxxxxxx-xxx",
    "createdTime": "2019-08-29T22:10:15.690927808Z",
    "currency": "USD",
    "createdByUserID": xxxxxxxx,
    "alias": "Test Account",
    "marginRate": "0.05",
    "lastTransactionID": "806",
    "balance": "98891.7496",
    "openTradeCount": 1,
    "openPositionCount": 1,
    "pendingOrderCount": 0,
    "pl": "-1111.1150",
    "resettablePL": "-1111.1150",
    "resettablePLTime": "0",
    "financing": "2.8646",
    "commission": "0.0000",
    "dividendAdjustment": "0",
    "guaranteedExecutionFees": "0.0000",
    "unrealizedPL": "0.0015",
    "NAV": "98891.7511",
    "marginUsed": "0.5000",
    "marginAvailable": "98891.2511",
    "positionValue": "10.0000",
    "marginCloseoutUnrealizedPL": "0.0022",
    "marginCloseoutNAV": "98891.7518",
    "marginCloseoutMarginUsed": "0.5000",
    "marginCloseoutPositionValue": "10.0000",
    "marginCloseoutPercent": "0.00000",
    "withdrawalLimit": "98891.2511",
    "marginCallMarginUsed": "0.4000",
    "marginCallPercent": "0.00000"
  },
  "lastTransactionID": "806"
}

/v3/account/{account name} の場合

{
  "account": {
    "guaranteedStopLossOrderMode": "DISABLED",
    "hedgingEnabled": false,
    "id": "xxx-xxx-xxxxxxxx-xxx",
    "createdTime": "2019-08-29T22:10:15.690927808Z",
    "currency": "USD",
    "createdByUserID": xxxxxxxx,
    "alias": "Test Account",
    "marginRate": "0.05",
    "lastTransactionID": "806",
    "balance": "98891.7496",
    "openTradeCount": 1,
    "openPositionCount": 1,
    "pendingOrderCount": 0,
    "pl": "-1111.1150",
    "resettablePL": "-1111.1150",
    "resettablePLTime": "0",
    "financing": "2.8646",
    "commission": "0.0000",
    "dividendAdjustment": "0",
    "guaranteedExecutionFees": "0.0000",
    "orders": [],
    "positions": [
      {
        "instrument": "EUR_USD",
        "long": {
          "units": "0",
          "pl": "-0.0034",
          "resettablePL": "-0.0034",
          "financing": "0.0000",
          "dividendAdjustment": "0.0000",
          "guaranteedExecutionFees": "0.0000",
          "unrealizedPL": "0.0000"
        },
        "short": {
          "units": "0",
          "pl": "-1111.1050",
          "resettablePL": "-1111.1050",
          "financing": "2.8648",
          "dividendAdjustment": "0.0000",
          "guaranteedExecutionFees": "0.0000",
          "unrealizedPL": "0.0000"
        },
        "pl": "-1111.1084",
        "resettablePL": "-1111.1084",
        "financing": "2.8648",
        "commission": "0.0000",
        "dividendAdjustment": "0.0000",
        "guaranteedExecutionFees": "0.0000",
        "unrealizedPL": "0.0000",
        "marginUsed": "0.0000"
      },
      {
        "instrument": "USD_JPY",
        "long": {
          "units": "0",
          "pl": "-0.0035",
          "resettablePL": "-0.0035",
          "financing": "-0.0002",
          "dividendAdjustment": "0.0000",
          "guaranteedExecutionFees": "0.0000",
          "unrealizedPL": "0.0000"
        },
        "short": {
          "units": "-10",
          "averagePrice": "106.759",
          "pl": "-0.0031",
          "resettablePL": "-0.0031",
          "financing": "0.0000",
          "dividendAdjustment": "0.0000",
          "guaranteedExecutionFees": "0.0000",
          "tradeIDs": [
            "806"
          ],
          "unrealizedPL": "0.0025"
        },
        "pl": "-0.0066",
        "resettablePL": "-0.0066",
        "financing": "-0.0002",
        "commission": "0.0000",
        "dividendAdjustment": "0.0000",
        "guaranteedExecutionFees": "0.0000",
        "unrealizedPL": "0.0025",
        "marginUsed": "0.5000"
      }
    ],
    "trades": [
      {
        "id": "806",
        "instrument": "USD_JPY",
        "price": "106.759",
        "openTime": "2020-08-28T00:11:29.452857796Z",
        "initialUnits": "-10",
        "initialMarginRequired": "0.5000",
        "state": "OPEN",
        "currentUnits": "-10",
        "realizedPL": "0.0000",
        "financing": "0.0000",
        "dividendAdjustment": "0.0000",
        "unrealizedPL": "0.0025",
        "marginUsed": "0.5000"
      }
    ],
    "unrealizedPL": "0.0025",
    "NAV": "98891.7521",
    "marginUsed": "0.5000",
    "marginAvailable": "98891.2521",
    "positionValue": "10.0000",
    "marginCloseoutUnrealizedPL": "0.0031",
    "marginCloseoutNAV": "98891.7527",
    "marginCloseoutMarginUsed": "0.5000",
    "marginCloseoutPositionValue": "10.0000",
    "marginCloseoutPercent": "0.00000",
    "withdrawalLimit": "98891.2521",
    "marginCallMarginUsed": "0.4000",
    "marginCallPercent": "0.00000"
  },
  "lastTransactionID": "806"
}


とりあえあず使えそうな値についての説明。

  
Key 名前 説明
id 口座番号
marginRate 証拠金率 例:0.05=5%, 1÷0.05=20倍   
balance 口座残高
pl 実現損益
unlializedPL 未実現損益
NAV Net Asset Value = balance + unrealizedPL
marginUsed = positionValue × marginRate

これらに加えて/v3/account/{account name} の場合は、通貨ペア毎のポジションや損益、それに加え現在保有のオープンポジションやオーダーの取引情報なども取得できます。
取引する際にLotサイズの計算が必要な場合は、balanceやNAVの値を使って計算できますね。
この口座内で一つしか稼働させていない、かつ一つ以上のポジションを保有しないようなシステムであれば、ここから現在保有しているポジションがあるかどうかもわかります。



まとめ

今回は口座情報の取得方法についての解説でした。
単純な口座情報を取得するだけなら/v3/accounts/{accountID}/summary で十分です。 各通貨ペア毎の損益やポジションやオーダー情報などは他のエンドポイントからでも同じものを取得できます。 (/v3/accounts/{accountID}/positions, /v3/accounts/{accountID}/orders等)
呼び出すタイミングや他に必要な情報の組み合わせなどで使い分けてください。