PowerShellでBitfinexの板情報(注文一覧)を取得する操作例を以下に記します。
BitfinexのOrderbook APIドキュメントURL
Orderbook API仕様は、以下のURLで確認してください。
https://bitfinex.readme.io/v1/reference#rest-public-orderbook
以下、URLで板情報(注文一覧)を取得できます。
通貨シンボル一覧(通貨ペア一覧)については、以下のリンク記事を参考にしてください。
PowerShellのInvoke-RestMethodコマンドレットと、
Invoke-WebRequest+ConvertFrom-Jsonコマンドレットを使って、\
Bitfinexの板情報(注文一覧)を取得してみます。
Invoke-RestMethodコマンドレットを使用して、Bitfinexの板情報(注文一覧)を取得する操作例になります。
以下の操作例は、ETH/BTCの通貨ペアを取得しています。
通貨シンボル一覧(通貨ペア一覧)については、以下のリンク記事を参考にしてください。
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...
PS C:\> $orderbook.bids.Count 25
PS C:\> $orderbook.asks.Count 25
PS C:\> $orderbook.bids[0] price amount timestamp ----- ------ --------- 0.068902 29.67674981 1531439501.0
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の通貨ペアを取得しています。
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...
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
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
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使うことにより、HTTPステータスや、Content-Typeなどを取得することができます。
取得したContent部分をConvertFrom-Jsonコマンドレットを使うことにより、Invoke-RestMethodと同様に、
JSON形式でアクセスすることができます。
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
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
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...
上記のコマンドレットを実行すると、APIサーバとのやり取りのプログレスメッセージが表示されます。
これを非表示にしたい場合は、以下のようにしてください。
非表示にするには、$ProgressPreferenceにSilentlyContinueを設定します。
変更前は以下のように Continue が設定されています。
PS C:\> $ProgressPreference Continue
非表示にするには、以下のように変更します。
PS C:\> $ProgressPreference = "SilentlyContinue"
以上、PowerShellを使って、BitfinexのOrderbook APIを呼び出す方法でした。