#author("2018-07-13T01:08:55+09:00","","")
#author("2018-07-13T09:23:23+09:00","","")
#navi(../)
* Bitfinexのファンディング情報を取得する(API v1)(PowerShell) [#r2565278]
PowerShellでBitfinexのファンディング情報(lendbook)を取得する操作例を以下に記します。~
Bitfinexでは、レンディングではなくファンディングと呼んでいるようですね。~
Fundingbook APIのページを見ると ''lend''bookとなっていますが…

#contents
#htmlinsert(cc-top.html)

* 参考資料 [#v30735ca]
Bitfinexのファンディング情報(lendbook)を取得するAPIドキュメントURL~
Fundingbook API仕様は、以下のURLで確認してください。~
https://bitfinex.readme.io/v1/reference#rest-public-fundingbook


* 関連記事 [#db3f03b2]
-[[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)]]


* ブラウザでアクセスしてみる [#da3f2cc7]
以下、URLでBTCのファンディング(レンディング)情報が取得できます。~
https://api.bitfinex.com/v1/lendbook/BTC

|''JSON''|
|&ref(01.png);|
|''生データ''|
|&ref(02.png);|

* PowerShellによるアクセス [#se9db58e]
PowerShellの''Invoke-RestMethod''コマンドレットと、''Invoke-WebRequest''+''ConvertFrom-Json''コマンドレットを使って、~
指定した通貨のファンディング(レンディング)情報を取得してみます。

** ''Invoke-RestMethod''コマンドレット [#ae9027e0]
''Invoke-RestMethod''コマンドレットを使用して、指定した通貨のファンディング(レンディング)情報を取得する操作例になります。~
 PS C:\> $v1_lendbook_api = "https://api.bitfinex.com/v1/lendbook/"
 PS C:\> $currency = "BTC"
 PS C:\> $lendbook = Invoke-RestMethod -UseBasicParsing ($v1_lendbook_api + $currency)
 PS C:\> $lendbook
 
 bids
 ----
 {@{rate=5.548; amount=85.02918116; period=30; timestamp=1530817703.0; frr=No}, @{rate=5.548; amount=101.74978106; pe...

bits, asksの情報はデフォルトでは、最大50件となっていたので、取得件数を確認してみます。
 PS C:\> $lendbook.bids.Count
 50
 PS C:\> $lendbook.asks.Count
 50

パラメータとして、limit_bids, limit_asks があります。~
これは、bids, asks の最大取得件数を指定するパラメータになります。~
bids, asksともに2件と指定し、取得してみます。
 PS C:\> $currency = "ETH"
 PS C:\> $limit_bids = 2
 PS C:\> $limit_asks = 2
 PS C:\> $lendbook = Invoke-RestMethod -UseBasicParsing ($v1_lendbook_api + $currency + "?limit_bids=${limit_bids}&limit_asks=${limit_asks}")
 PS C:\> $lendbook
 
 bids                                                                                                                                                           asks
 ----                                                                                                                                                           ----
 {@{rate=3.285; amount=677.4577947; period=30; timestamp=1531314687.0; frr=No}, @{rate=2.993; amount=1840.63028393; period=30; timestamp=1530807405.0; frr=No}} {@{rate=1.8826; amount=0.6076321; perio d=2; time...

 PS C:\> $lendbook.bids.Count
 2
 PS C:\> $lendbook.asks.Count
 2

 PS C:\> $lendbook.bids
 
 rate      : 3.285
 amount    : 677.4577947
 period    : 30
 timestamp : 1531314687.0
 frr       : No
 
 rate      : 2.993
 amount    : 1840.63028393
 period    : 30
 timestamp : 1530807405.0
 frr       : No

 PS C:\> $lendbook.asks
 
 rate      : 1.8826
 amount    : 0.6076321
 period    : 2
 timestamp : 1531409769.0
 frr       : No
 
 rate      : 1.8826
 amount    : 0.420347
 period    : 2
 timestamp : 1531409848.0
 frr       : No

bids, asksともに指定した件数が取得されているのが確認できます。~
上記の例では、currencyにBTCとETHを指定しました。

** ''Invoke-WebRequest''+''ConvertFrom-Json''コマンドレット [#f0fdcf21]
''Invoke-WebRequest''使うことにより、HTTPステータスや、Content-Typeなどを取得することができます。~
取得したContent部分を''ConvertFrom-Json''コマンドレットを使うことにより、Invoke-RestMethodと同様に、~
JSON形式でアクセスすることができます。
+ Invoke-WebRequestでFundingbook APIをアクセス
 PS C:\> $v1_lendbook_api = "https://api.bitfinex.com/v1/lendbook/"
 PS C:\> $currency = "XRP"
 PS C:\> $limit_bids = 1
 PS C:\> $limit_asks = 1
 PS C:\> $response = Invoke-WebRequest -UseBasicParsing ($v1_lendbook_api + $currency + "?limit_bids=${limit_bids}&limit_asks=${limit_asks}")
+ 取得したレスポンスを表示します。
 PS C:\> $response
 
 StatusCode        : 200
 StatusDescription : OK
 Content           : {"bids":[{"rate":"0.0219","amount":"12183.20629104","period":30,"timestamp":"1531288055.0","frr":"No"}],"asks":[{"rate":"0.0107","amount":"27999.95578344","period":2,"timestamp":"1531340089.
                     0","frr":"...
 RawContent        : HTTP/1.1 200 OK
                     Connection: keep-alive
                     X-Frame-Options: SAMEORIGIN,SAMEORIGIN
                     X-XSS-Protection: 1; mode=block
                     X-Content-Type-Options: nosniff
                     X-Request-Id: a13c409a-6ba4-4134-92fc-831a67d49c55
                     X...
 Forms             :
 Headers           : {[Connection, keep-alive], [X-Frame-Options, SAMEORIGIN,SAMEORIGIN], [X-XSS-Protection, 1; mode=block], [X-Content-Type-Options, nosniff]...}
 Images            : {}
 InputFields       : {}
 Links             : {}
 ParsedHtml        :
 RawContentLength  : 206
+HTTPステータスコード、ヘッダー、Content-Typeなどにアクセスしてみます。
 PS C:\> $response.StatusCode
 200
 
 PS C:\> $response.Headers
 
 Key                       Value
 ---                       -----
 Connection                keep-alive
 X-Frame-Options           SAMEORIGIN,SAMEORIGIN
 X-XSS-Protection          1; mode=block
 X-Content-Type-Options    nosniff
 X-Request-Id              a13c409a-6ba4-4134-92fc-831a67d49c55
 X-Runtime                 0.111515
 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                    4394ac61ce696ef9-NRT
 Content-Length            206
 Cache-Control             max-age=0, private, must-revalidate
 Content-Type              application/json; charset=utf-8
 Date                      Thu, 12 Jul 2018 15:51:49 GMT
 ETag                      W/"2307dfec05c81f6dec04bca6736668d9"
 Set-Cookie                __cfduid=d8e96e5fad527ff5c6808a2c4275200b51531410708; expires=Fri, 12-Jul-19 15:51:48 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                                                                               asks
 ----                                                                               ----
 {@{rate=0.0219; amount=12183.20629104; period=30; timestamp=1531288055.0; frr=No}} {@{rate=0.0107; amount=27999.95578344; period=2; timestamp=1531340089.0; frr=No}}

* Webの応答を読み取っています。(Waiting for response)を非表示にする方法 [#oa4afca0]
上記のコマンドレットを実行すると、APIサーバとのやり取りのプログレスメッセージが表示されます。~

#ref(51.png)

これを非表示にしたい場合は、以下のようにしてください。

非表示にするには、$ProgressPreferenceにSilentlyContinueを設定します。~
変更前は以下のように Continue が設定されています。

 PS C:\> $ProgressPreference
 Continue

非表示にするには、以下のように変更します。

 PS C:\> $ProgressPreference = "SilentlyContinue"

以上、PowerShellを使って、BitfinexのFundingbook APIを呼び出す方法でした。

#htmlinsert(cc-btm.html)



トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS