#author("2018-07-10T09:24:12+09:00","","")
#author("2018-07-13T09:22:50+09:00","","")
#navi(../)
* BitfinexのTickerをPowerShellで取得する(API v1) [#d4b8ae3f]
PowerShellでBitfinexのTickerを取得する例を以下に記します。~

#contents
#htmlinsert(cc-top.html)

* 参考資料 [#lde09015]
Bitfinexの通Ticker取得するAPIドキュメントURL~
BitfinexのTicker取得するAPIドキュメントURL~
API仕様は、以下のURLで確認してください。~
https://bitfinex.readme.io/v1/reference#rest-public-ticker


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



* ブラウザでアクセスしてみる [#fd7b976b]
以下、URLでTickerを取得できます。~
-BTC/USD~
https://api.bitfinex.com/v1/pubticker/btcusd
https://api.bitfinex.com/v1/pubticker/btcusd~
Firefoxでアクセスした時のスクリーンショットです。~
|''JSON''|''生データ''|
|&ref(01.png);|&ref(02.png);|
#br
-ETH/USD
https://api.bitfinex.com/v1/pubticker/ethusd
https://api.bitfinex.com/v1/pubticker/ethusd~
Firefoxでアクセスした時のスクリーンショットです。~
|''JSON''|''生データ''|
|&ref(03.png);|&ref(04.png);|


''通貨シンボル一覧(通貨ペア一覧)については、以下のリンク記事を参考にしてください。
''通貨シンボル一覧(通貨ペア一覧)''については、以下のリンク記事を参考にしてください。
-[[Bitfinexの通貨シンボル一覧の取得(API v1)(PowerShell)>API/Bitfinex/通貨ペアのシンボル一覧の取得(API v1)(PowerShell)]]


* PowerShellによるアクセス [#d8d368dd]
PowerShellの''Invoke-RestMethod''コマンドレットと、~
''Invoke-WebRequest''+''ConvertFrom-Json''コマンドレットを使って、\~
BitfinexのTickerを取得してみます。

** ''Invoke-RestMethod''コマンドレットでTickerを取得してみる [#ne8aa42c]
''Invoke-RestMethod''コマンドレットを使用して、BitfinexのTickerを取得する操作例になります。~

以下の操作例は、''ETH/BTC''と''XLM/JPY''の通貨ペアを取得しています。~
''通貨シンボル一覧(通貨ペア一覧)については、以下のリンク記事を参考にしてください。
''通貨シンボル一覧(通貨ペア一覧)''については、以下のリンク記事を参考にしてください。
-[[Bitfinexの通貨シンボル一覧の取得(API v1)(PowerShell)>API/Bitfinex/通貨ペアのシンボル一覧の取得(API v1)(PowerShell)]]

 PS C:\> $v1_ticker_api = "https://api.bitfinex.com/v1/pubticker/"
 PS C:\> $symbol = "ethbtc"PS C:\> $ticker = Invoke-RestMethod -UseBasicParsing ($v1_ticker_api + $symbol)
 PS C:\> $symbol = "ethbtc"
 PS C:\> $ticker = Invoke-RestMethod -UseBasicParsing ($v1_ticker_api + $symbol)
 PS C:\> $ticker
 
 mid        : 0.0706165
 bid        : 0.070575
 ask        : 0.070658
 last_price : 0.070658
 low        : 0.070411
 high       : 0.072696
 volume     : 12980.205281710008
 timestamp  : 1531181760.7016923
 
 PS C:\> $symbol = "xlmjpy"
 PS C:\> $ticker = Invoke-RestMethod -UseBasicParsing ($v1_ticker_api + $symbol)
 PS C:\> $ticker
 
 mid        : 22.7525
 bid        : 22.698
 ask        : 22.807
 last_price : 22.69807138
 low        : 22.69807138
 high       : 23.54399956
 volume     : 10046.78230199
 timestamp  : 1531181831.9344094


** ''Invoke-WebRequest''+''ConvertFrom-Json''コマンドレットでTickerを取得する [#p36d5ebd]
''Invoke-WebRequest''使うことにより、HTTPステータスや、Content-Typeなどを取得することができます。~
取得したContent部分を''ConvertFrom-Json''コマンドレットを使うことにより、Invoke-RestMethodと同様に、~
JSON形式でアクセスすることができます。
+ Invoke-WebRequestでsymbols APIをアクセス
 PS C:\> $btcusd_ticker_url = "https://api.bitfinex.com/v1/ticker/btcusd"
+ Invoke-WebRequestでTickerAPIをアクセス
 PS C:\> $btcusd_ticker_url = "https://api.bitfinex.com/v1/pubticker/btcusd"
 PS C:\> $response = Invoke-WebRequest -UseBasicParsing $btcusd_ticker_url
+ 取得したレスポンスを表示します。
 PS C:\> $response
 
 StatusCode        : 200
 StatusDescription : OK
 Content           : {"mid":"6660.15","bid":"6660.1","ask":"6660.2","last_price":"6660.1","timestamp":"1531181966.189357
                     593"}
 Content           : {"mid":"6350.95","bid":"6350.9","ask":"6351.0","last_price":"6350.4","low":"6286.3","high":"6407.9"
                     ,"volume":"14856.667564400002","timestamp":"1531358483.7853873"}
 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: 0bc8dfe5-1a04-4bab-ae67-8add9427fec1
                     X-Request-Id: 6451c7f8-d06d-4234-989b-7641e87a17bd
                     X...
 Forms             :
 Headers           : {[Connection, keep-alive], [X-Frame-Options, SAMEORIGIN,SAMEORIGIN], [X-XSS-Protection, 1; mode=blo
                     ck], [X-Content-Type-Options, nosniff]...}
 Images            : {}
 InputFields       : {}
 Links             : {}
 ParsedHtml        :
 RawContentLength  : 104
 RawContentLength  : 163
+ステータスコード、HTTP Header, Content-Typeなどの値にアクセスしてみます。
 PS C:\> $response.StatusCode
 200
#br
 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              0bc8dfe5-1a04-4bab-ae67-8add9427fec1
 X-Runtime                 0.005799
 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                    437edbd18d589559-NRT
 Content-Length            104
 Cache-Control             max-age=0, private, must-revalidate
 Content-Type              application/json; charset=utf-8
 Date                      Tue, 10 Jul 2018 00:19:26 GMT
 ETag                      W/"d19fa253552728a626c37d4221f14969"
 Set-Cookie                __cfduid=d581c9a228f8c52ac4d92403d2bc0d6ac1531181966; expires=Wed, 10-Jul-19 00:19:26 GMT;...
 Server                    cloudflare
#br
 PS C:\> $response.Headers.'Content-Type'
 application/json; charset=utf-8
+ ConvertFrom-Jsonコマンドレットを使うことにより、JSON形式に変換することができます。
 PS C:\> $json = ConvertFrom-Json $response.Content
 PS C:\> $json
 
 mid        : 6660.15
 bid        : 6660.1
 ask        : 6660.2
 last_price : 6660.1
 timestamp  : 1531181966.189357593
 mid        : 6350.95
 bid        : 6350.9
 ask        : 6351.0
 last_price : 6350.4
 low        : 6286.3
 high       : 6407.9
 volume     : 14856.667564400002
 timestamp  : 1531358483.7853873

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

#ref(51.png)

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

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

 PS C:\> $ProgressPreference
 Continue

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

 PS C:\> $ProgressPreference = "SilentlyContinue"

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

#htmlinsert(cc-btm.html)


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