- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2018-07-13T09:14:52+09:00","","")
#navi(../)
* BitfinexのOrderbook APIをPowerShellで取得する(API v1) [#iad13fca]
PowerShellでBitfinexの板情報(注文一覧)を取得する操作例を以下に記します。~
#contents
#htmlinsert(cc-top.html)
* 参考資料 [#uf0624fe]
BitfinexのOrderbook APIドキュメントURL~
Orderbook API仕様は、以下のURLで確認してください。~
https://bitfinex.readme.io/v1/reference#rest-public-orderbook
* 関連記事 [#l03a6798]
-[[Bitfinexの通貨シンボル一覧の取得(API v1)(PowerShell)>API/Bitfinex/通貨ペアのシンボル一覧の取得(API v1)(PowerShell)]]
-[[BitfinexのTickerの取得(API v1)(PowerShell)>API/Bitfinex/Tickerの取得(API v1)(PowerShell)]]
-[[Bitfinexで指定した通貨ペアの取引量を取得する(API v1)(PowerShell)>API/Bitfinex/指定した通貨ペアの取引量を取得する(API v1)(PowerShell)]]
-[[Bitfinexのファンディング情報を取得する(API v1)(PowerShell)>API/Bitfinex/ファンディング情報を取得する(API v1)(PowerShell)]]
-[[Bitfinexの情報(注文一覧)を取得する(API v1)(PowerShell)>API/Bitfinex/情報情報(注文一覧)を取得する(API v1)(PowerShell)]]
* ブラウザでアクセスしてみる [#x6f332db]
以下、URLで板情報(注文一覧)を取得できます。~
-BTC/USD~
https://api.bitfinex.com/v1/book/btcusd~
Firefoxでアクセスした時のスクリーンショットです。~
|''JSON''|
|&ref(01.png);|
|''生データ''|
|&ref(02.png);|
#br
-ETH/USD
https://api.bitfinex.com/v1/book/ethusd~
Firefoxでアクセスした時のスクリーンショットです。~
|''JSON''|''生データ''|
|&ref(03.png);|&ref(04.png);|
''通貨シンボル一覧(通貨ペア一覧)については、以下のリンク記事を参考にしてください。
-[[Bitfinexの通貨シンボル一覧の取得(API v1)(PowerShell)>API/Bitfinex/通貨ペアのシンボル一覧の取得(API v1)(PowerShell)]]
* PowerShellによるOrderbook API呼び出しの操作例 [#m9e86ba4]
PowerShellの''Invoke-RestMethod''コマンドレットと、~
''Invoke-WebRequest''+''ConvertFrom-Json''コマンドレットを使って、\~
Bitfinexの板情報(注文一覧)を取得してみます。
** ''Invoke-RestMethod''コマンドレットでOrderbookを取得してみる [#u4af09e3]
''Invoke-RestMethod''コマンドレットを使用して、Bitfinexの板情報(注文一覧)を取得する操作例になります。~
以下の操作例は、''ETH/BTC''の通貨ペアを取得しています。~
''通貨シンボル一覧(通貨ペア一覧)については、以下のリンク記事を参考にしてください。
-[[Bitfinexの通貨シンボル一覧の取得(API v1)(PowerShell)>API/Bitfinex/通貨ペアのシンボル一覧の取得(API v1)(PowerShell)]]
+Orderbook APIを呼び出し板情報(注文一覧)を取得する
PS C:\> $v1_orderbook_api = "https://api.bitfinex.com/v1/book/"
PS C:\> $symbol = "ethbtc"
PS C:\> $orderbook = Invoke-RestMethod -UseBasicParsing ($v1_orderbook_api + $symbol)
PS C:\> $orderbook
bids
----
{@{price=0.068902; amount=29.67674981; timestamp=1531439501.0}, @{price=0.068901; amount=0.8700781; timestamp=153143...
+asksの情報数を確認
PS C:\> $orderbook.bids.Count
25
+asksの情報数を確認
PS C:\> $orderbook.asks.Count
25
+bidsの1番目を表示する
PS C:\> $orderbook.bids[0]
price amount timestamp
----- ------ ---------
0.068902 29.67674981 1531439501.0
+asksの1番目を表示する
PS C:\> $orderbook.asks[0]
price amount timestamp
----- ------ ---------
0.068903 0.00000632 1531439501.0
Orderbook APIのパラメータを指定して実行した例を以下に記します。~
指定できるパラメータは以下のようになっています。(bitfinexのAPIページより抜粋)
|''Key''|''Required''|''Type''|''Default''|''Description''|
|limit_bids|false|[int]|50|Limit the number of bids returned. May be 0 in which case the array of bids is empty|
|limit_asks|false|[int]|50|Limit the number of asks returned. May be 0 in which case the array of asks is empty|
|group|false|[0/1]|1|If 1, orders are grouped by price in the orderbook. If 0, orders are not grouped and sorted individually|
以下の操作例は、''XRP/USD''の通貨ペアを取得しています。~
+limit_bids, limit_asksに2を指定して実行してみます。
PS C:\> $v1_orderbook_api = "https://api.bitfinex.com/v1/book/"
PS C:\> $symbol = "xrpusd"
PS C:\> $limit_bids = 2
PS C:\> $limit_asks = 2
PS C:\> $orderbook = Invoke-RestMethod -UseBasicParsing ($v1_orderbook_api + $symbol + "?limit_bids=${limit_bids}&limit_asks=${limit_asks}")
PS C:\> $orderbook
bids asks
---- ----
{@{price=0.43691; amount=31830.683; timestamp=1531440140.0}, @{price=0.43678; amount=22; timestamp=1531440140.0}} {@{price=0.43698; amount=10199.9999592; ti...
+bids, asksの取得件数と取得内容を表示しています。
PS C:\> $orderbook.bids.Count
2
PS C:\> $orderbook.asks.Count
2
PS C:\> $orderbook.bids
price amount timestamp
----- ------ ---------
0.43691 31830.683 1531440140.0
0.43678 22 1531440140.0
PS C:\> $orderbook.asks
price amount timestamp
----- ------ ---------
0.43698 10199.9999592 1531440140.0
0.43727 3540.06244959 1531440140.0
+groupを指定してみます。
+group=1を指定した操作例です。
PS C:\> $group = 1
PS C:\> $orderbook = Invoke-RestMethod -UseBasicParsing ($v1_orderbook_api + $symbol + "?group=${group}")
PS C:\> $orderbook.bids | Select-Object -First 10
price amount timestamp
----- ------ ---------
0.43722 5000 1531440480.0
0.43706 2367.60426413 1531440480.0
0.43694 826.2 1531440480.0
0.43693 142.28 1531440480.0
0.43689 22 1531440480.0
0.43668 5199.87000324 1531440480.0
0.43667 4269.75738489 1531440480.0
0.43666 484 1531440480.0
0.43663 4937.93 1531440480.0
0.4366 1200 1531440480.0
+group=0を指定した操作例です。
PS C:\> $group = 0
PS C:\> $orderbook = Invoke-RestMethod -UseBasicParsing ($v1_orderbook_api + $symbol + "?group=${group}")
PS C:\> $orderbook.bids | Select-Object -First 10
price amount timestamp
----- ------ ---------
0.43722 5000 1531440491.0
0.43693 826.2 1531440491.0
0.43693 650 1531440491.0
0.4369 5199.87000324 1531440491.0
0.43689 22 1531440491.0
0.43685 142.28 1531440491.0
0.43685 2303.17109746 1531440491.0
0.43676 45674 1531440491.0
0.43674 52858 1531440491.0
0.43668 4269.78165981 1531440491.0
** ''Invoke-WebRequest''+''ConvertFrom-Json''コマンドレットで板情報(注文一覧)を取得する [#e335548c]
''Invoke-WebRequest''使うことにより、HTTPステータスや、Content-Typeなどを取得することができます。~
取得したContent部分を''ConvertFrom-Json''コマンドレットを使うことにより、Invoke-RestMethodと同様に、~
JSON形式でアクセスすることができます。
+ Invoke-WebRequestでOrderbook APIをアクセス
PS C:\> $btcusd_book_url = "https://api.bitfinex.com/v1/book/btcusd"
PS C:\> $response = Invoke-WebRequest -UseBasicParsing $btcusd_book_url
+ 取得したレスポンスを表示します。
PS C:\> $response
StatusCode : 200
StatusDescription : OK
Content : {"bids":[{"price":"6229.3","amount":"19.62830209","timestamp":"1531440653.0"},{"price":"6229.2","amount":"0.23","timestamp":"1531440653.0"}
,{"price":"6229","amount":"4.56","timestamp":"1531440653.0"},...
RawContent : HTTP/1.1 200 OK
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
X-Frame-Options: SAMEORIGIN,SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-...
Forms :
Headers : {[Transfer-Encoding, chunked], [Connection, keep-alive], [Vary, Accept-Encoding], [X-Frame-Options, SAMEORIGIN,SAMEORIGIN]...}
Images : {}
InputFields : {}
Links : {}
ParsedHtml :
RawContentLength : 3186
+ステータスコード、HTTP Header, Content-Typeなどの値にアクセスしてみます。
PS C:\> $response.StatusCode
200
PS C:\> $response.Headers
Key Value
--- -----
Transfer-Encoding chunked
Connection keep-alive
Vary Accept-Encoding
X-Frame-Options SAMEORIGIN,SAMEORIGIN
X-XSS-Protection 1; mode=block
X-Content-Type-Options nosniff
X-Request-Id 6aa3d30a-da97-4d40-8181-bf11e5240bb7
X-Runtime 0.010887
Strict-Transport-Security max-age=31536000
Expect-CT max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
CF-RAY 4397877f4feb9427-NRT
Cache-Control max-age=0, private, must-revalidate
Content-Type application/json; charset=utf-8
Date Fri, 13 Jul 2018 00:10:55 GMT
ETag W/"b788bc4945a619cf64929682034736c7"
Set-Cookie __cfduid=d13699a975ffee3990b7e45dcc2005b7c1531440655; expires=Sat, 13-Jul-19 00:10:55 GMT; path=/; domain=.bitfinex.com; HttpOnly
Server cloudflare
PS C:\> $response.Headers.'Content-Type'
application/json; charset=utf-8
+ ConvertFrom-Jsonコマンドレットを使うことにより、JSON形式に変換することができます。
PS C:\> $json = ConvertFrom-Json $response.Content
PS C:\> $json
bids
----
{@{price=6229.3; amount=19.62830209; timestamp=1531440653.0}, @{price=6229.2; amount=0.23; timestamp=1531440653.0}, @{price=6229; amount=4.56; timestamp=153...
* Webの応答を読み取っています。(Waiting for response)を非表示にする方法 [#e691a36e]
上記のコマンドレットを実行すると、APIサーバとのやり取りのプログレスメッセージが表示されます。~
#ref(51.png)
これを非表示にしたい場合は、以下のようにしてください。
非表示にするには、$ProgressPreferenceにSilentlyContinueを設定します。~
変更前は以下のように Continue が設定されています。
PS C:\> $ProgressPreference
Continue
非表示にするには、以下のように変更します。
PS C:\> $ProgressPreference = "SilentlyContinue"
以上、PowerShellを使って、BitfinexのOrderbook APIを呼び出す方法でした。
#htmlinsert(cc-btm.html)