Python用 OANDA v20 REST API ラッパーライブラリ のサンプル v20-python-samples を触る

OANDA V20 python libのサンプルであるv20-python-samplesのメモです。
APIの確認ができるコマンドラインツールのような使い方ができます。

[https://github.com/oanda/v20-python-samples](https://github.com/oanda/v20-python-samples)

# 導入
基本的にlinux環境を前提にした導入手順になっていますが、windowsにも入ります。
手順中の下記コマンドについて、Windwos環境で手動で同じことをするのは下記です

- bootstrap

```bash:linux
make bootstrap
```
```console:windows_python3
virtualenv -p python3 env
env\Scripts\pip install -r requirements/base.txt
```

- activate

```bash:linux
source env/bin/activate
```
```console:windows
env\Scripts\activate.bat
```

# v20-account-details
detailsはアカウントのサマリーと通貨ペアごとの収支が出ます。

```console:v20-account-details
Account xxx-xxx-xxxxxxx-xxx
===========================

===============================  ==============================
Account ID                       xxx-xxx-xxxxxxx-xxx
Alias                            Primary
Home Currency                    JPY
Balance                          374585.0125
Created by User ID               xxxxxxx
Create Time                      2020-07-09T03:27:35.393967207Z
Guaranteed Stop Loss Order Mode  DISABLED
Profit/Loss                      -72865.1901
Resettable Profit/Loss           -72865.1901
Profit/Loss Reset Time           0
Financing                        -2496.2074
Commission                       0.0
Guaranteed Execution Fees        0.0
Margin Rate                      0.04
Open Trade Count                 0
Open Position Count              0
Pending Order Count              0
Hedging Enabled                  False
Unrealized Profit/Loss           0.0
Net Asset Value                  374585.0125
Margin Used                      0.0
Margin Available                 374585.0125
Position Value                   0.0
Closeout UPL                     0.0
Closeout NAV                     374585.0125
Closeout Margin Used             0.0
Margin Closeout Percentage       0.0
Margin Closeout Position Value   0.0
Withdrawal Limit                 374585.0125
Last Transaction ID              6143
===============================  ==============================

13 Open Positions
=================

============  =========  ================  ==========  ==========
Instrument          P/L    Unrealized P/L  Long        Short
============  =========  ================  ==========  ==========
AUD_JPY        -4643.11                 0  0.0 @ None  0.0 @ None
AUD_USD       -13941.5                  0  0.0 @ None  0.0 @ None
EUR_AUD        -4197.54                 0  0.0 @ None  0.0 @ None
EUR_CAD         2187.31                 0  0.0 @ None  0.0 @ None
EUR_GBP        -4444.72                 0  0.0 @ None  0.0 @ None
EUR_JPY       -13217.7                  0  0.0 @ None  0.0 @ None
EUR_USD        13787.9                  0  0.0 @ None  0.0 @ None
GBP_AUD         6820.85                 0  0.0 @ None  0.0 @ None
GBP_JPY       -17401.3                  0  0.0 @ None  0.0 @ None
GBP_USD        -5079.87                 0  0.0 @ None  0.0 @ None
USD_CAD        -5946.91                 0  0.0 @ None  0.0 @ None
USD_CHF        -9440.97                 0  0.0 @ None  0.0 @ None
USD_JPY       -17347.7                  0  0.0 @ None  0.0 @ None
============  =========  ================  ==========  ==========
```

# v20-account-instruments
扱える通貨の一覧と倍率等が出ます

```consoe:v20-account-instruments
71 Instruments
==============

=======  ========  ======  =============
Name     Type         Pip  Margin Rate
=======  ========  ======  =============
AUD_CAD  CURRENCY  0.0001  25:1 (0.04)
AUD_CHF  CURRENCY  0.0001  20:1 (0.05)
AUD_HKD  CURRENCY  0.0001  10:1 (0.1)
AUD_JPY  CURRENCY  0.01    25:1 (0.04)
AUD_NZD  CURRENCY  0.0001  25:1 (0.04)
AUD_SGD  CURRENCY  0.0001  20:1 (0.05)
AUD_USD  CURRENCY  0.0001  25:1 (0.04)
CAD_CHF  CURRENCY  0.0001  20:1 (0.05)
CAD_HKD  CURRENCY  0.0001  10:1 (0.1)
CAD_JPY  CURRENCY  0.01    25:1 (0.04)
CAD_SGD  CURRENCY  0.0001  20:1 (0.05)
CHF_HKD  CURRENCY  0.0001  10:1 (0.1)
CHF_JPY  CURRENCY  0.01    20:1 (0.05)
CHF_ZAR  CURRENCY  0.0001  10:1 (0.1)
EUR_AUD  CURRENCY  0.0001  25:1 (0.04)
EUR_CAD  CURRENCY  0.0001  25:1 (0.04)
EUR_CHF  CURRENCY  0.0001  20:1 (0.05)
EUR_CZK  CURRENCY  0.0001  20:1 (0.05)
EUR_DKK  CURRENCY  0.0001  10:1 (0.1)
EUR_GBP  CURRENCY  0.0001  20:1 (0.05)
EUR_HKD  CURRENCY  0.0001  10:1 (0.1)
EUR_HUF  CURRENCY  0.01    20:1 (0.05)
EUR_JPY  CURRENCY  0.01    25:1 (0.04)
EUR_NOK  CURRENCY  0.0001  25:1 (0.04)
EUR_NZD  CURRENCY  0.0001  25:1 (0.04)
EUR_PLN  CURRENCY  0.0001  20:1 (0.05)
EUR_SEK  CURRENCY  0.0001  25:1 (0.04)
EUR_SGD  CURRENCY  0.0001  20:1 (0.05)
EUR_TRY  CURRENCY  0.0001  10:1 (0.1)
EUR_USD  CURRENCY  0.0001  25:1 (0.04)
EUR_ZAR  CURRENCY  0.0001  10:1 (0.1)
GBP_AUD  CURRENCY  0.0001  20:1 (0.05)
GBP_CAD  CURRENCY  0.0001  20:1 (0.05)
GBP_CHF  CURRENCY  0.0001  20:1 (0.05)
GBP_HKD  CURRENCY  0.0001  10:1 (0.1)
GBP_JPY  CURRENCY  0.01    20:1 (0.05)
GBP_NZD  CURRENCY  0.0001  20:1 (0.05)
GBP_PLN  CURRENCY  0.0001  20:1 (0.05)
GBP_SGD  CURRENCY  0.0001  20:1 (0.05)
GBP_USD  CURRENCY  0.0001  20:1 (0.05)
GBP_ZAR  CURRENCY  0.0001  10:1 (0.1)
HKD_JPY  CURRENCY  0.0001  10:1 (0.1)
NZD_CAD  CURRENCY  0.0001  25:1 (0.04)
NZD_CHF  CURRENCY  0.0001  20:1 (0.05)
NZD_HKD  CURRENCY  0.0001  10:1 (0.1)
NZD_JPY  CURRENCY  0.01    25:1 (0.04)
NZD_SGD  CURRENCY  0.0001  20:1 (0.05)
NZD_USD  CURRENCY  0.0001  25:1 (0.04)
SGD_CHF  CURRENCY  0.0001  20:1 (0.05)
SGD_HKD  CURRENCY  0.0001  10:1 (0.1)
SGD_JPY  CURRENCY  0.01    20:1 (0.05)
TRY_JPY  CURRENCY  0.01    10:1 (0.1)
USD_CAD  CURRENCY  0.0001  25:1 (0.04)
USD_CHF  CURRENCY  0.0001  20:1 (0.05)
USD_CNH  CURRENCY  0.0001  20:1 (0.05)
USD_CZK  CURRENCY  0.0001  20:1 (0.05)
USD_DKK  CURRENCY  0.0001  25:1 (0.04)
USD_HKD  CURRENCY  0.0001  10:1 (0.1)
USD_HUF  CURRENCY  0.01    20:1 (0.05)
USD_INR  CURRENCY  0.01    20:1 (0.05)
USD_JPY  CURRENCY  0.01    25:1 (0.04)
USD_MXN  CURRENCY  0.0001  12:1 (0.08)
USD_NOK  CURRENCY  0.0001  25:1 (0.04)
USD_PLN  CURRENCY  0.0001  20:1 (0.05)
USD_SAR  CURRENCY  0.0001  20:1 (0.05)
USD_SEK  CURRENCY  0.0001  25:1 (0.04)
USD_SGD  CURRENCY  0.0001  20:1 (0.05)
USD_THB  CURRENCY  0.01    20:1 (0.05)
USD_TRY  CURRENCY  0.0001  10:1 (0.1)
USD_ZAR  CURRENCY  0.0001  10:1 (0.1)
ZAR_JPY  CURRENCY  0.01    10:1 (0.1)
=======  ========  ======  =============
```

# v20-transaction-range
トランザクションのヒストリーをトランザクションIDの範囲で確認します。
詳細情報がないので、別に詳細情報を取得しなければならないのが残念というか面倒
あと、日付の範囲でとりたいが、このサンプルにはない。
```v20-transaction-range.exe 4000 9999```

```consoe:v20-transaction-range
Transaction 4000: Create Take Profit Order 4000 (ON_FILL): Close Trade 3999 @ 71.693
Transaction 4001: Create Stop Loss Order 4001 (ON_FILL): Close Trade 3999 @ 68.745
Transaction 4002: Cancel Order 4000
Transaction 4003: Create Take Profit Order 4003 (REPLACEMENT): Close Trade 3999 @ 71.693
Transaction 4004: Cancel Order 4001
Transaction 4005: Create Stop Loss Order 4005 (REPLACEMENT): Close Trade 3999 @ 68.736
Transaction 4006: Fill Order 3998 (STOP_LOSS_ORDER): -14604.0 of GBP_USD @ 1.24422
Transaction 4007: Fill Order 4005 (STOP_LOSS_ORDER): -17552.0 of AUD_JPY @ 68.736
Transaction 4008: Cancel Order 4003
Transaction 4009: Fill Order 3995 (STOP_LOSS_ORDER): -18483.0 of AUD_USD @ 0.64342
Transaction 4010: Create Market Order 4010 (CLIENT_ORDER): -8925.0 of AUD_JPY
Transaction 4011: Fill Order 4010 (MARKET_ORDER): -8925.0 of AUD_JPY @ 68.308
Transaction 4012: Create Take Profit Order 4012 (ON_FILL): Close Trade 4011 @ 65.534
Transaction 4013: Create Stop Loss Order 4013 (ON_FILL): Close Trade 4011 @ 68.64
Transaction 4014: Create Market Order 4014 (CLIENT_ORDER): 8405.0 of EUR_GBP
Transaction 4015: Fill Order 4014 (MARKET_ORDER): 8405.0 of EUR_GBP @ 0.87327
```

タイプ指定することで、トランザクションの種類を絞れます
```v20-transaction-range.exe --type ORDER_FILL 4000 9999```

```consoe
Transaction 4006: Fill Order 3998 (STOP_LOSS_ORDER): -14604.0 of GBP_USD @ 1.24422
Transaction 4007: Fill Order 4005 (STOP_LOSS_ORDER): -17552.0 of AUD_JPY @ 68.736
Transaction 4009: Fill Order 3995 (STOP_LOSS_ORDER): -18483.0 of AUD_USD @ 0.64342
Transaction 4011: Fill Order 4010 (MARKET_ORDER): -8925.0 of AUD_JPY @ 68.308
Transaction 4015: Fill Order 4014 (MARKET_ORDER): 8405.0 of EUR_GBP @ 0.87327
Transaction 4025: Fill Order 4024 (MARKET_ORDER_TRADE_CLOSE): -8405.0 of EUR_GBP @ 0.8733
Transaction 4029: Fill Order 4028 (MARKET_ORDER_TRADE_CLOSE): 8925.0 of AUD_JPY @ 68.372
```

# v20-transaction-get
トランザクションIDで詳細を確認します。

```consoe:v20-transaction-get

id: '4100'
time: '2020-04-19T14:17:16.552497903Z'
userID: 2202754
accountID: xxx-xxx-xxxxxxx-xxx
batchID: '4100'
type: ORDER_FILL
orderID: '4099'
instrument: USD_CAD
units: 16311.0
gainQuoteHomeConversionFactor: 77.674
lossQuoteHomeConversionFactor: 77.693
price: 1.38913
fullVWAP: 1.38913
fullPrice:
  type: PRICE
  bids:
  - price: 1.38894
    liquidity: '10000000'
  asks:
  - price: 1.38913
    liquidity: '10000000'
  closeoutBid: 1.38894
  closeoutAsk: 1.38913
reason: STOP_LOSS_ORDER
pl: -1406.6481
financing: 0.0
commission: 0.0
guaranteedExecutionFee: 0.0
accountBalance: 278334.8247
tradesClosed:
- tradeID: '4098'
  units: 16311.0
  price: 1.38913
  realizedPL: -1406.6481
  financing: 0.0
  guaranteedExecutionFee: 0.0
  halfSpreadCost: 120.3749
halfSpreadCost: 120.3749
```

## 注釈
うっかり余計な情報を載せたので、削除して投稿しなおしました・・・