PHPを使って Coincheck のBTC現物注文・新規注文APIのサンプルコードを以下に記します。
当サイトに記載されている会社名、製品名などは一般に各社または団体の商標または登録商標です。
当サイトの資料により直接的および間接的障害が生じても一切責任を負いません。
あらかじめご了承ください。
残高APIはPrivate APIのため、
Coincheck
の口座開設が必要となります。
本PHPサンプルコードでは、BTCの現物売買のサンプルコードを公開しています。
order_typeを変更すればレバレッジ取引にでも使用できると思います。
残高APIの仕様(Coincheck APIページから抜粋)
新規注文 取引所に新規注文を発行します。 たとえば、10 BTC を 30000 JPY/BTC で買いたい場合は、以下のように指定します。 rate: 30000, amount: 10, order_type: "buy", pair: "btc_jpy" 買い注文の場合、指定した価格よりも低い価格での売り注文が既に存在していれば、その売り注文を安い順に決済します。売り注文が足りなかった場合は未決済の注文として残ります。売り注文の場合も同様です。 注文方法について order_type は全部で8つあります。 "buy" 指値注文 現物取引 買い "sell" 指値注文 現物取引 売り "market_buy" 成行注文 現物取引 買い "market_sell" 成行注文 現物取引 売り "leverage_buy" 指値注文 レバレッジ取引新規 買い "leverage_sell" 指値注文 レバレッジ取引新規 売り "close_long" 指値注文 レバレッジ取引決済 売り "close_short" 指値注文 レバレッジ取引決済 買い <省略>
POST /api/exchange/orders PARAMETERS *pair 取引ペア。現在は "btc_jpy" のみです。 *order_type 注文方法 rate 注文のレート。(例)28000 amount 注文での量。(例)0.1 market_buy_amount 成行買で利用する日本円の金額。(例)10000 position_id 決済するポジションのID stop_loss_rate 逆指値レート ( 逆指値とは? ) <省略>
本サンプルソースには、$ACCESS_KEY, $SECRET_ACCESS_KEY にAPIキーを設定していません。
APIキーを準備してください。
// Please set ACCESS_KEY and SECRET_ACCESS_KEY $ACCESS_KEY = ""; $SECRET_ACCESS_KEY = "";
APIキーの作成に関しては以下の記事を参考にしてください。
本サンプルソースを実行するには、APIキーのパーミッションとして「新規注文」にチェックが必要です。
APIキーを作成すると、以下のスクリーンショットのように「アクセスキー」「アクセスシークレットキー」が作成されるので、
サンプルソースの $ACCESS_KEY, $SECRET_ACCESS_KEY に設定してください。
以下に
Coincheck
の残高APIを呼び出すサンプルソースを記します
上記に記述しましたが、「アクセスキー」「アクセスシークレットキー」を準備してください。
このサンプルコードでは、注文入力を行い(&ref(): File not found: "order_ui.php" at page "API/coincheck/新規注文・現物売買(PHP)";)、新規注文を行う(&ref(): File not found: "new_order.php" at page "API/coincheck/新規注文・現物売買(PHP)";)の2つのサンプルコードファイルから構成されています。
また、新規注文を行う(order.php)サンプルコードでは、POSTするデータ形式をクエリー文字列または、JSONを選択できるようになっています。
POSTする注文内容は、クエリー文字列でもJSONでも問題なく動作しました。
JSONの場合は、ヘッダーに Content-Type: application/json が必要になります。
&ref(): File not found: "order_ui.php" at page "API/coincheck/新規注文・現物売買(PHP)";はBTCの数量、価格等を入力するファインクション群のサンプルコードです。
#ref(): File not found: "order_ui.php" at page "API/coincheck/新規注文・現物売買(PHP)"
<?php //---------------------------------------- // order ui functions //---------------------------------------- function order_ui() { $rate = input_rate(); $amount = input_amount(); $order_type = input_buy_sell(); printf( "Rate[BTC/JPY] : %d" . PHP_EOL . "Amount[BTC] : %f" . PHP_EOL . "Price[JPY] : %f" . PHP_EOL . "Order type : %s" . PHP_EOL , $rate, $amount, $rate*$amount, $order_type); if(!yes_no_quit()) { die(0); } $order = array('rate'=>$rate, 'amount'=>$amount, 'order_type'=>$order_type); return $order; } //---------------------------------------- function input_amount() { while(TRUE) { print("Amount[BTC](quit if empty): "); $n = trim(fgets(STDIN)); if(empty($n)) { print("Quit." . PHP_EOL); die(0); } elseif(!is_numeric($n)) { printf("[ERROR] " . $n . " is not numeric." . PHP_EOL); } else { break; } } return $n; } //---------------------------------------- function input_rate() { while(TRUE) { print("Rate[BTC/JPY](quit if empty): "); $rate = trim(fgets(STDIN)); if(empty($rate)) { print("Quit." . PHP_EOL); die(0); } elseif(!ctype_digit($rate)) { printf("[ERROR] " . $rate . " is not integer." . PHP_EOL); } else { break; } } return $rate; } //---------------------------------------- function input_buy_sell() { while(TRUE) { print("Order type [buy/sell](quit if empty): "); $type = trim(fgets(STDIN)); if(empty($type)) { print("Quit." . PHP_EOL); die(0); } elseif(!strcasecmp($type, "buy")) { break; } elseif(!strcasecmp($type, "sell")) { break; } else { print("Please check the input." . PHP_EOL); } } return strtolower($type); } //---------------------------------------- function yes_no_quit() { while (TRUE) { print("OK? [yes/no/quit] :"); $ok = trim(fgets(STDIN)); if(!strcasecmp($ok, "yes")) { $ok = TRUE; break; } elseif(!strcasecmp($ok, "no")) { print("Order canceled." . PHP_EOL); $ok = FALSE; break; } elseif(!strcasecmp($ok, "quit")) { print("Quit." . PHP_EOL); $ok = FALSE; break; } else { print("Please check the input." . PHP_EOL); } } return $ok; }
以下のサンプルコードでは、上記の &ref(): File not found: "order_ui.php" at page "API/coincheck/新規注文・現物売買(PHP)"; を呼び出し、注文内容を入力後、
BTCの売買発注を行うサンプルコードになります。
#ref(): File not found: "new_order.php" at page "API/coincheck/新規注文・現物売買(PHP)"
<?php // API doc : https://coincheck.com/ja/documents/exchange/api#order-new // API url : https://coincheck.com/api/exchange/orders // order ui require("./order_ui.php"); // Please set ACCESS_KEY and SECRET_ACCESS_KEY $ACCESS_KEY = ""; $SECRET_ACCESS_KEY = ""; // BTC/JPY only $currency_pair = "btc_jpy"; // proxy settings $proxy = ""; $proxy_port = ""; // coincheck order api url $url = "https://coincheck.com/api/exchange/orders"; // Post data type $json = FALSE; // $json ? POST data is JSON : POST data is Query string // input order $order = order_ui(); // post data $postdata_array = array( "rate" => $order["rate"], "amount" => $order["amount"], "order_type" => $order["order_type"], "pair" => $currency_pair); if($json) { $postdata = json_encode($postdata_array); // POST data is JSON } else { $postdata = http_build_query($postdata_array); // POST data is Query string } // create signature $nonce = time(); $message = $nonce . $url . $postdata; $signature = hash_hmac("sha256", $message, $SECRET_ACCESS_KEY); // header $headers = array( "ACCESS-KEY: {$ACCESS_KEY}", "ACCESS-SIGNATURE: {$signature}", "ACCESS-NONCE: {$nonce}", ); if ($json) { // $json ? POST data is JSON : POST data is Query string array_push($headers, "Content-Type: application/json"); } $curl = curl_init(); if ($curl == FALSE) { fputs(STDERR, "[ERR] curl_init(): " . curl_error($curl) . PHP_EOL); die(1); } // set proxy server settings if (!empty($proxy) && !empty($proxy_port)) { curl_setopt($curl, CURLOPT_HTTPPROXYTUNNEL, 1); curl_setopt($curl, CURLOPT_PROXY, $proxy . ":" . $proxy_port); curl_setopt($curl, CURLOPT_PROXYPORT, $proxy_port); } curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata); $response = curl_exec($curl); if ($response == FALSE) { fputs(STDERR, "[ERR] curl_exec(): " . curl_error($curl) . PHP_EOL); die(1); } curl_close($curl); // json decode $json_decode = json_decode($response, true); if ($json_decode == NULL) { fputs(STDERR, "[ERR] json_decode(): " . json_last_error_msg() . PHP_EOL); die(1); } // output json_decode print_r($json_decode); exit(0);
以下に、上記サンプルコードを実行した結果を記します。
以下はBTCの注文内容を入力している時の出力です。
$ php new_order.php Rate[BTC/JPY](quit if empty): 300000 Amount[BTC](quit if empty): 0.005 Order type [buy/sell](quit if empty): buy Rate[BTC/JPY] : 300000 Amount[BTC] : 0.005000 Price[JPY] : 1500.000000 Order type : buy OK? [yes/no/quit] :yes
以下、yesを入力しEnterキーを押すことにより注文が発注されました。
Array ( [success] => 1 [id] => 999999999 [amount] => 0.005 [rate] => 300000.0 [order_type] => buy [pair] => btc_jpy [created_at] => 2017-11-25T00:00:00.000Z [market_buy_amount] => [stop_loss_rate] => )
以下、
Coincheck
のWeb画面です。
発注されているのが確認できます。
キャンセルする場合は、Webページでキャンセルしてくださいね。
以下によく発生すると思われるエラーについて記します。
以下のようにERRORが出た場合は、APIキーの設定に誤りがないか確認してください。
$ php balance.php [ERROR] : invalid authentication
APIキーの設定を確認してください。
新規注文にチェックが入っていないためパーミッションエラーとなっています。
$ php balance.php [ERROR] : This api is not permitted, Please set permission. https://coincheck.com/api_settings
その他にも以下のように残高不足などのエラーも出力されます。
$ php new_order.php Rate[BTC/JPY](quit if empty): 900000 Amount[BTC](quit if empty): 0.001 Order type [buy/sell](quit if empty): buy Rate[BTC/JPY] : 900000 Amount[BTC] : 0.001000 Price[JPY] : 900.000000 Order type : buy OK? [yes/no/quit] :yes Array ( [success] => [error] => Amount 量が最低量(0.005 BTC)を下回っています )
$ php new_order.php Rate[BTC/JPY](quit if empty): 1000000 Amount[BTC](quit if empty): 1 Order type [buy/sell](quit if empty): sell Rate[BTC/JPY] : 1000000 Amount[BTC] : 1.000000 Price[JPY] : 1000000.000000 Order type : sell OK? [yes/no/quit] :yes Array ( [success] => [error] => Amount BTC の所持金額が足りません )
以上、 Coincheck の新規注文APIを使ってBTC新規注文をするサンプルコードおよび実行結果でした。