このエントリーをはてなブックマークに追加


BitfinexのOrderbook APIをPowerShellで取得する(API v1)

PowerShellでBitfinexの板情報(注文一覧)を取得する操作例を以下に記します。


スポンサーリンク

参考資料

BitfinexのOrderbook APIドキュメントURL
Orderbook API仕様は、以下のURLで確認してください。
https://bitfinex.readme.io/v1/reference#rest-public-orderbook

関連記事

ブラウザでアクセスしてみる

以下、URLで板情報(注文一覧)を取得できます。

通貨シンボル一覧(通貨ペア一覧)については、以下のリンク記事を参考にしてください。

PowerShellによるOrderbook API呼び出しの操作例

PowerShellのInvoke-RestMethodコマンドレットと、
Invoke-WebRequest+ConvertFrom-Jsonコマンドレットを使って、\
Bitfinexの板情報(注文一覧)を取得してみます。

Invoke-RestMethodコマンドレットでOrderbookを取得してみる

Invoke-RestMethodコマンドレットを使用して、Bitfinexの板情報(注文一覧)を取得する操作例になります。

以下の操作例は、ETH/BTCの通貨ペアを取得しています。
通貨シンボル一覧(通貨ペア一覧)については、以下のリンク記事を参考にしてください。

  1. 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...
  2. asksの情報数を確認
    PS C:\> $orderbook.bids.Count
    25
  3. asksの情報数を確認
    PS C:\> $orderbook.asks.Count
    25
  4. bidsの1番目を表示する
    PS C:\> $orderbook.bids[0]
    price    amount      timestamp
    -----    ------      ---------
    0.068902 29.67674981 1531439501.0
  5. asksの1番目を表示する
    PS C:\> $orderbook.asks[0]
    
    price    amount     timestamp
    -----    ------     ---------
    0.068903 0.00000632 1531439501.0

Orderbook APIのパラメータを指定して実行した例を以下に記します。
指定できるパラメータは以下のようになっています。(bitfinexのAPIページより抜粋)

KeyRequiredTypeDefaultDescription
limit_bidsfalse[int]50Limit the number of bids returned. May be 0 in which case the array of bids is empty
limit_asksfalse[int]50Limit the number of asks returned. May be 0 in which case the array of asks is empty
groupfalse[0/1]1If 1, orders are grouped by price in the orderbook. If 0, orders are not grouped and sorted individually

以下の操作例は、XRP/USDの通貨ペアを取得しています。

  1. 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...
  2. 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
  3. groupを指定してみます。
  4. 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
  5. 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コマンドレットで板情報(注文一覧)を取得する

Invoke-WebRequest使うことにより、HTTPステータスや、Content-Typeなどを取得することができます。
取得したContent部分をConvertFrom-Jsonコマンドレットを使うことにより、Invoke-RestMethodと同様に、
JSON形式でアクセスすることができます。

  1. 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
  2. 取得したレスポンスを表示します。
    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
  3. ステータスコード、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
  4. 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)を非表示にする方法

上記のコマンドレットを実行すると、APIサーバとのやり取りのプログレスメッセージが表示されます。

51.png

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

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

PS C:\> $ProgressPreference
Continue

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

PS C:\> $ProgressPreference = "SilentlyContinue"

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


スポンサーリンク

添付ファイル: file51.png 275件 [詳細] file02.png 290件 [詳細] file01.png 286件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2018-07-13 (金) 09:21:28