#author("2018-01-14T13:58:43+09:00","","") #author("2018-01-30T22:34:26+09:00","","") #navi(../) * coincheckの新規注文API(orders)の現物注文サンプルコード(PHP) [#c41d5596] PHPを使って&htmlinsert(coincheck.html);のBTC現物注文・[[新規注文API>https://coincheck.com/ja/documents/exchange/api#order-new]]のサンプルコードを以下に記します。 ---- &color(red){当サイトに記載されている会社名、製品名などは一般に各社または団体の商標または登録商標です。&br;当サイトの資料により直接的および間接的障害が生じても一切責任を負いません。&br;あらかじめご了承ください。}; ---- #htmlinsert(coincheck_wide.html) #contents #htmlinsert(cc-top.html) * 関連記事 [#i35f0a9b] -[[API動作環境構築>API/環境構築]] -[[ティッカー(PHP)>API/coincheck/ティッカー(PHP)]] -[[全取引履歴(PHP)>API/coincheck/全取引履歴(PHP)]] -[[板情報(PHP)>API/coincheck/板情報(PHP)]] -[[レート取得(PHP)>API/coincheck/レート取得(PHP)]] -[[販売レート取得(PHP)>API/coincheck/販売レート取得(PHP)]] -[[CoincheckのAPIキー生成手順>API/coincheck/APIキー生成手順]] -[[アカウントの残高確認(PHP)>API/coincheck/残高確認(PHP)]] -[[新規注文・現物売買(PHP)>API/coincheck/新規注文・現物売買(PHP)]] -[[レバレッジアカウントの残高サンプル(PHP)>API/coincheck/レバレッジアカウントの残高取得]] -[[レバレッジ取引注文サンプルコード(PHP)>API/coincheck/レバレッジ取引注文(PHP)]] -[[レバレッジ取引のポジション一覧を取得する・positions(PHP)>API/coincheck/ポジション一覧(PHP)]] -[[最近の取引履歴を取得する(PHP)>API/coincheck/最近の取引履歴の取得(PHP)]] * 新規注文APIの仕様 [#o91a4146] 残高APIはPrivate APIのため、&htmlinsert(coincheck.html);の口座開設が必要となります。~ 本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 逆指値レート ( 逆指値とは? ) <省略> * 新規注文APIサンプルソースを使用する前の準備 [#q57b35ed] 本サンプルソースには、$ACCESS_KEY, $SECRET_ACCESS_KEY にAPIキーを設定していません。~ APIキーを準備してください。~ // Please set ACCESS_KEY and SECRET_ACCESS_KEY $ACCESS_KEY = ""; $SECRET_ACCESS_KEY = ""; APIキーの作成に関しては以下の記事を参考にしてください。~ -[[CoincheckのAPIキー生成手順>API/coincheck/APIキー生成手順]] 本サンプルソースを実行するには、APIキーのパーミッションとして「新規注文」にチェックが必要です。~ #ref(01.png) #br APIキーを作成すると、以下のスクリーンショットのように「アクセスキー」「アクセスシークレットキー」が作成されるので、~ サンプルソースの $ACCESS_KEY, $SECRET_ACCESS_KEY に設定してください。 #ref(02.png) #br ---- #htmlinsert(coincheck_wide.html) * 新規注文API・BTC現物売買 PHPサンプルソース [#yd0acd3b] 以下に&htmlinsert(coincheck.html);の残高APIを呼び出すサンプルソースを記します~ 上記に記述しましたが、「アクセスキー」「アクセスシークレットキー」を準備してください。 このサンプルコードでは、注文入力を行い(order_ui.php)、新規注文を行う(new_order.php)の2つのサンプルコードファイルから構成されています。~ また、新規注文を行う(new_order.php)サンプルコードでは、POSTするデータ形式をクエリー文字列または、JSONを選択できるようになっています。~ POSTする注文内容は、クエリー文字列でもJSONでも問題なく動作しました。~ JSONの場合は、ヘッダーに Content-Type: application/json が必要になります。 ** 注文入力 [#wdc0caba] order_ui.phpはBTCの数量、価格等を入力するファインクション群のサンプルコードです。 #ref(order_ui.php.zip) <?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; } ** BTC現物注文・サンプルコード [#p3c56ed5] 以下のサンプルコードでは、上記のorder_ui.phpを呼び出し、注文内容を入力後、~ BTCの売買発注を行うサンプルコードになります。 #ref(new_order.php.zip) <?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); * 実行例 [#f17849ce] 以下に、上記サンプルコードを実行した結果を記します。 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-??-??T00:00:00.000Z [market_buy_amount] => [stop_loss_rate] => ) 以下、&htmlinsert(coincheck.html);のWeb画面です。~ 発注されているのが確認できます。~ &color(red){キャンセルする場合は、Webページでキャンセルしてくださいね。}; #ref(03.png) * 新規注文APIのエラーについて [#l49e5d08] 以下によく発生すると思われるエラーについて記します。 ** invalid authentication エラーになった場合 [#m8f9ae49] 以下のようにERRORが出た場合は、APIキーの設定に誤りがないか確認してください。 $ php balance.php [ERROR] : invalid authentication ** This api is not permitted... エラーになった場合 [#f86b3bda] APIキーの設定を確認してください。~ 新規注文にチェックが入っていないためパーミッションエラーとなっています。 $ php balance.php [ERROR] : This api is not permitted, Please set permission. https://coincheck.com/api_settings ** 注文エラー [#g781a11e] その他にも以下のように残高不足などのエラーも出力されます。 -最低量を下回っている場合のエラー出力 $ 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 の所持金額が足りません ) 以上、&htmlinsert(coincheck.html);の新規注文APIを使ってBTC新規注文をするサンプルコードおよび実行結果でした。 ---- #htmlinsert(coincheck_wide.html) #htmlinsert(cc-btm.html)