- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2018-05-17T20:19:36+09:00","","")
#navi(../)
* PowerShellでbitFlyerのTickerを取得・getgetticker [#h1c47038]
&color(red){当サイトに記載されている会社名、製品名などは一般に各社または団体の商標または登録商標です。&br;当サイトの資料により直接的および間接的障害が生じても一切責任を負いません。&br;あらかじめご了承ください。};
----
PowerShellを使ってbitFlyerの[[マーケットの一覧>https://lightning.bitflyer.jp/docs]]APIの使用例を以下に記します。
#contents
#htmlinsert(bitflyer_full.html)
* 関連記事 [#vcaccade]
-[[板情報(PowerShell)>API/bitFlyer/板情報の取得(PowerShell)]]
-[[マーケットの一覧(PowerShell)>API/bitFlyer/マーケットの一覧の取得(PowerShell)]]
-[[Ticker(PowerShell)>API/bitFlyer/Tickerの取得(PowerShell)]]
* TickerAPIの仕様など [#ie9464f3]
HTTP Public APIなのでbitFlyerに取引口座を開設していなくても使用することができます。
TickerAPI仕様(bitFlyer APIページから抜粋) ~
リクエスト
GET /v1/getticker
GET /v1/ticker
クエリパラメータ
product_code: マーケットの一覧で取得できる product_code または alias のいずれかを指定してください。
省略した場合の値は BTC_JPY です。
レスポンス
{
"product_code": "BTC_JPY",
"timestamp": "2015-07-08T02:50:59.97",
"tick_id": 3579,
"best_bid": 30000,
"best_ask": 36640,
"best_bid_size": 0.1,
"best_ask_size": 5,
"total_bid_depth": 15.13,
"total_ask_depth": 20,
"ltp": 31690,
"volume": 16819.26,
"volume_by_product": 6819.26
}
timestamp: 時刻は UTC(協定世界時)で表されます。
ltp: 最終取引価格
volume: 24 時間の取引量
* ブラウザでTicker(getmarkets)を取得する [#af13928a]
GETなのでブラウザでTickerを取得することができます。~
以下のようなURLになります。~
product_codeを指定しています。
- BTC_JPY~
https://api.bitflyer.jp/v1/getticker?product_code=BTC_JPY
-FX_BTC_JPY~
https://api.bitflyer.jp/v1/getticker?product_code=FX_BTC_JPY
-ETH_BTC~
https://api.bitflyer.jp/v1/getticker?product_code=ETH_BTC
-BCH_BTC~
https://api.bitflyer.jp/v1/getticker?product_code=BCH_BTC
product_codeに関しては以下の記事を参照してください。~
-[[マーケットの一覧(PowerShell)>API/bitFlyer/マーケットの一覧の取得(PowerShell)]]
* ブラウザでマーケットの一覧APIにアクセスした結果 [#ef984bc9]
上記のURLをブラウザでクリックした結果は以下の通りです。~
FirefoxではJSONが整形され表示されています。~
見やすいですね。~
(生データをクリックすると上記同様にJSON形式の返却された文字列が表示されます。)
- BTC_JPY~
https://api.bitflyer.jp/v1/getticker?product_code=BTC_JPY
#ref(01.png)
#br
-FX_BTC_JPY~
https://api.bitflyer.jp/v1/getticker?product_code=FX_BTC_JPY
#ref(02.png)
#br
-ETH_BTC~
https://api.bitflyer.jp/v1/getticker?product_code=ETH_BTC
#ref(03.png)
#br
-BCH_BTC~
https://api.bitflyer.jp/v1/getticker?product_code=BCH_BTC
#ref(04.png)
#br
* Invoke-RestMethodコマンドレットでbitFlyerのTickerを取得する [#u3b04513]
+Tickerを取得するURL(API)を設定します。
PS C:\> $ticker_api = "https://api.bitflyer.jp/v1/getticker"
+product_codeにBTC_JPYを指定します。
PS C:\> $product_code = "BTC_JPY"
+Invoke-RestMethodコマンドレットを使用し、返却されたTicker情報を$ticker変数に代入します。
PS C:\> $ticker = Invoke-RestMethod -UseBasicParsing -Uri ($ticker_api + "?product_code=" + $product_code)
+取得したTicker情報を表示しています。
PS C:\> $ticker
product_code : BTC_JPY
timestamp : 2018-05-17T11:05:12.143
tick_id : 1387764
best_bid : 921501.0
best_ask : 921586.0
best_bid_size : 0.02804128
best_ask_size : 0.02
total_bid_depth : 2051.2378956
total_ask_depth : 3858.77283227
ltp : 921501.0
volume : 211785.4498822
volume_by_product : 15084.44930353
*HTTPステータスなどを一緒に取得したい場合 [#i880c368]
上記では、Invoke-RestMethodにてJSONをPowerShellで扱いやすい状態に変換してくれます。
しかし、Webサーバから返却された値を変換せず、取得後JSONを別のコマンドレットで変換する操作手順を以下に記します。
+Tickerを取得するURL(API)を設定します。
PS C:\> $ticker_api = "https://api.bitflyer.jp/v1/getticker"
+product_codeにBTC_JPYを指定します。
PS C:\> $product_code = "BTC_JPY"
+ Invoke-WebRequestコマンドレットでサーバからのレスポンスを$resに格納します。
PS C:\> $res = Invoke-WebRequest -UseBasicParsing -Uri ($ticker_api + "?product_code=" + $product_code)
+ 取得したレスポンスは以下の通りです。
PS C:\> $res
StatusCode : 200
StatusDescription : OK
Content : {"product_code":"BTC_JPY","timestamp":"2018-05-17T11:11:12.047","tick_id":1395884,"best_bid":920920
.0,"best_ask":921515.0,"best_bid_size":0.01,"best_ask_size":0.04,"total_bid_depth":2109.47686593,"t
ot...
RawContent : HTTP/1.1 200 OK
Connection: keep-alive
Pragma: no-cache
Request-Context: appId=cid-v1:e4fbc941-a2df-48ac-bbac-f2180e904002
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Frame...
Forms :
Headers : {[Connection, keep-alive], [Pragma, no-cache], [Request-Context, appId=cid-v1:e4fbc941-a2df-48ac-bb
ac-f2180e904002], [X-Content-Type-Options, nosniff]...}
Images : {}
InputFields : {}
Links : {}
ParsedHtml :
RawContentLength : 303
+HTTPステイタスを取得しています。
PS C:\> $res.StatusCode
200
+Contentに取得した情報が格納されています。
PS C:\> $json = ($res.Content | ConvertFrom-Json)
PS C:\> $json
product_code : BTC_JPY
timestamp : 2018-05-17T11:11:12.047
tick_id : 1395884
best_bid : 920920.0
best_ask : 921515.0
best_bid_size : 0.01
best_ask_size : 0.04
total_bid_depth : 2109.47686593
total_ask_depth : 3852.16647046
ltp : 920915.0
volume : 211442.00348458
volume_by_product : 15059.23366309
以上、PowerShellを使って、bitFlyerのマーケットの一覧を取得・gettickerからの返却値を表示する方法でした。
#htmlinsert(bitflyer_full.html)