- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2018-07-22T20:46:14+09:00","","")
#navi(../)
* Bitfinexの取引情報(取引リスト)を取得する(API v1) [#s32e1393]
PowerShellでBitfinexのTradesを呼び出し、指定した仮想通貨ペアの取引情報を取得する操作例を以下に記します。~
#contents
#htmlinsert(cc-top.html)
* 参考資料 [#u0addad1]
BitfinexのTrades取得するAPIドキュメントURL~
API仕様は、以下のURLで確認してください。~
https://bitfinex.readme.io/v1/reference#rest-public-trades
* 関連記事 [#n50b9f5c]
-[[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)]]
-[[Bitfinexの取引情報(取引リスト)を取得する(API v1)(PowerShell)>API/Bitfinex/取引情報を取得する(API v1)(PowerShell)]]
* Trades APIをブラウザでアクセスしてみる [#na22404a]
以下、URLで取引情報を取得できます。~
-BTC/USD~
https://api.bitfinex.com/v1/trades/btcusd~
Firefoxでアクセスした時のスクリーンショットです。~
|''JSON''|''生データ''|
|&ref(01.png);|&ref(02.png);|
#br
-ETH/USD
https://api.bitfinex.com/v1/trades/ethusd~
Firefoxでアクセスした時のスクリーンショットです。~
|''JSON''|''生データ''|
|&ref(03.png);|&ref(04.png);|
''通貨シンボル一覧(通貨ペア一覧)''については、以下のリンク記事を参考にしてください。
-[[Bitfinexの通貨シンボル一覧の取得(API v1)(PowerShell)>API/Bitfinex/通貨ペアのシンボル一覧の取得(API v1)(PowerShell)]]
* PowerShellによるTrades APIアクセス [#da3939fe]
PowerShellの''Invoke-RestMethod''コマンドレットと、~
''Invoke-WebRequest''+''ConvertFrom-Json''コマンドレットを使って、~
BitfinexのTrades APIを呼び出しレスポンスを取得してみます。
** ''Invoke-RestMethod''コマンドレットで取引情報を取得してみる [#nead3e8a]
''Invoke-RestMethod''コマンドレットを使用して、Bitfinexの取引情報を取得する操作例になります。~
以下の操作例は、''BTC/USD''の通貨ペアを取得しています。~
''通貨シンボル一覧(通貨ペア一覧)''については、以下のリンク記事を参考にしてください。
-[[Bitfinexの通貨シンボル一覧の取得(API v1)(PowerShell)>API/Bitfinex/通貨ペアのシンボル一覧の取得(API v1)(PowerShell)]]
+以下の構文でBTCUSDの取引情報を取得します。
PS C:\> $v1_trades_api = "https://api.bitfinex.com/v1/trades/"
PS C:\> $symbol = "BTCUSD"
PS C:\> $trades = Invoke-RestMethod -UseBasicParsing ($v1_trades_api + $symbol)
+取得した取引情報数を表示しています。
PS C:\> $trades.Count
100
+Select-Objectコマンドレットを使って最初の2件を表示してみます。
PS C:\> $trades | Select-Object -First 2
timestamp : 1532234014
tid : 271279029
price : 7392.7
amount : 0.057797
exchange : bitfinex
type : buy
timestamp : 1532234013
tid : 271279027
price : 7392.7
amount : 0.25
exchange : bitfinex
type : buy
+Format-Tableコマンドレットを使って表形式に整形し、5行表示しています。
PS C:\> $trades | Format-Table | Select-Object -First 5
timestamp tid price amount exchange type
--------- --- ----- ------ -------- ----
1532234014 271279029 7392.7 0.057797 bitfinex buy
1532234013 271279027 7392.7 0.25 bitfinex buy
1532234007 271279020 7392.6 0.1 bitfinex sell
*** パラメータを指定してみる [#n5740fad]
Trades APIの詳細を見ると以下の通り、時間と取得数を設定するパラメータがあります。~
このパラメータを使ってみます。~
Bitfinex Trades APIページから抜粋~
''Request Details''
|Key|Required|Type|Default|Description|
|timestamp|false|[time]|Only show trades at or after this timestamp|
limit_trades|false|[int]|50|Limit the number of trades returned. Must be >= 1|
-''timestampを現在時刻-10秒として実行してみる''~
timestampはUNIX時間を指定します。~
UNIX時間については、以下のリンク記事を参照してください。~
-[[PowerShellでUNIXTIMEを取得する方法>http://win.just4fun.biz/?PowerShell/UNIXTIME%E3%82%92%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95]]
PS C:\> $btcusd_trades_url = "https://api.bitfinex.com/v1/trades/btcusd"
PS C:\> $timestamp = [int](((Get-Date) - (Get-Date("1970/1/1 0:0:0 GMT"))).TotalSeconds - 10)
PS C:\> $trades = Invoke-RestMethod -UseBasicParsing ($btcusd_trades_url + "?timestamp=$timestamp")
PS C:\> $timestamp
1532258886
PS C:\> $trades | Format-Table
timestamp tid price amount exchange type
--------- --- ----- ------ -------- ----
1532258893 271325392 7454.92241801 0.05 bitfinex sell
1532258888 271325378 7454.8 0.1 bitfinex sell
指定したtimestamp以降の取引情報のみ取得できているのが確認できます。
-''取得数(limit_trades)を指定して取得する''~
limit_tradesを指定して取引データを取得してみます。
PS C:\> $limit = 20
PS C:\> $btcusd_trades_url = "https://api.bitfinex.com/v1/trades/btcusd"
PS C:\> $trades = Invoke-RestMethod -UseBasicParsing ($btcusd_trades_url + "?limit_trades=$limit")
PS C:\> $trades.Count
20
PS C:\> $limit = 5
PS C:\> $trades = Invoke-RestMethod -UseBasicParsing ($btcusd_trades_url + "?limit_trades=$limit")
PS C:\> $trades | Format-Table
timestamp tid price amount exchange type
--------- --- ----- ------ -------- ----
1532259347 271326252 7457.0 0.004 bitfinex buy
1532259347 271326251 7456.7 0.01 bitfinex buy
1532259340 271326234 7456.7 0.005 bitfinex buy
1532259333 271326231 7456.5 0.276295 bitfinex sell
1532259323 271326224 7456.1 0.17939914 bitfinex sell
-''timestamp, limit_tradesの両方を指定し取得してみる''~
PS C:\> $btcusd_trades_url = "https://api.bitfinex.com/v1/trades/btcusd"
PS C:\> $limit = 20
PS C:\> $timestamp = [int](((Get-Date) - (Get-Date("1970/1/1 0:0:0 GMT"))).TotalSeconds - 60)
PS C:\> $trades = Invoke-RestMethod -UseBasicParsing ($btcusd_trades_url + "?timestamp=${timestamp}&limit_trades=${limit}")
PS C:\> $trades | Format-Table
timestamp tid price amount exchange type
--------- --- ----- ------ -------- ----
1532259754 271326683 7457.4 0.00235 bitfinex buy
1532259752 271326673 7457.4 0.119 bitfinex sell
1532259740 271326646 7457.5 0.81046561 bitfinex buy
1532259740 271326645 7457.3 0.23922987 bitfinex buy
1532259740 271326644 7457.2 0.8 bitfinex buy
1532259739 271326641 7455.7 0.005 bitfinex sell
** ''Invoke-WebRequest''+''ConvertFrom-Json''コマンドレットで取引情報を取得する [#y15dcb5c]
''Invoke-WebRequest''使うことにより、HTTPステータスや、Content-Typeなどを取得することができます。~
取得したContent部分を''ConvertFrom-Json''コマンドレットを使うことにより、Invoke-RestMethodと同様に、~
JSON形式でアクセスすることができます。
+ Invoke-WebRequestでTrades APIにアクセス
PS C:\> $btcusd_trades_url = "https://api.bitfinex.com/v1/trades/btcusd"
PS C:\> $response = Invoke-WebRequest -UseBasicParsing $btcusd_trades_url
+ 取得したレスポンスを表示します。
PS C:\> $response
StatusCode : 200
StatusDescription : OK
Content : [{"timestamp":1532235786,"tid":271282004,"price":"7394.5","amount":"1.0","exchange":"bitfinex","typ
e":"sell"},{"timestamp":1532235783,"tid":271281988,"price":"7394.5","amount":"0.0095","exchange":"b
it...
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 : 11180
+ステータスコード、HTTP Header, Content-Typeなどの値にアクセスしてみます。
PS C:\> $response.StatusCode
200
#br
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 790f7088-d9d9-4726-82f3-2e4d6a6fa0cb
X-Runtime 0.006105
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 43e35c21b8f39547-NRT
Cache-Control max-age=0, private, must-revalidate
Content-Type application/json; charset=utf-8
Date Sun, 22 Jul 2018 05:03:17 GMT
ETag W/"3bc822405766a7e950aad440c40bbbf9"
Set-Cookie __cfduid=de11180575c3cdec4b4b287d1776345091532235796; expires=Mon, 22-Jul-19 05:03:16 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.Count
100
PS C:\> $json | Select-Object -First 5 | Format-Table
timestamp tid price amount exchange type
--------- --- ----- ------ -------- ----
1532235786 271282004 7394.5 1.0 bitfinex sell
1532235783 271281988 7394.5 0.0095 bitfinex sell
1532235782 271281987 7394.5 0.0095 bitfinex sell
1532235770 271281953 7394.5 0.1 bitfinex sell
1532235767 271281950 7394.5 0.01352 bitfinex sell
* Webの応答を読み取っています。(Waiting for response)を非表示にする方法 [#uf6c2cb4]
上記のコマンドレットを実行すると、APIサーバとのやり取りのプログレスメッセージが表示されます。~
#ref(51.png)
これを非表示にしたい場合は、以下のようにしてください。
非表示にするには、$ProgressPreferenceにSilentlyContinueを設定します。~
変更前は以下のように Continue が設定されています。
PS C:\> $ProgressPreference
Continue
非表示にするには、以下のように変更します。
PS C:\> $ProgressPreference = "SilentlyContinue"
以上、PowerShellを使って、BitfinexのTrades APIを呼び出す方法でした。
#htmlinsert(cc-btm.html)