From 881d3d9d5ad224566fdf3b3e2ad317bf2d6924c2 Mon Sep 17 00:00:00 2001 From: rc1021 Date: Sat, 27 Feb 2021 20:33:57 +0800 Subject: [PATCH 01/39] add extension --- composer.json | 1 + extensions/margin-api.php | 42 +++++++++++++++++++++++++++++++++++++++ php-binance-api.php | 2 ++ 3 files changed, 45 insertions(+) create mode 100644 extensions/margin-api.php diff --git a/composer.json b/composer.json index 2486b834..2ef250c4 100755 --- a/composer.json +++ b/composer.json @@ -24,6 +24,7 @@ }, "autoload": { "classmap": [ + "extensions/", "php-binance-api.php", "php-binance-api-rate-limiter.php" ] diff --git a/extensions/margin-api.php b/extensions/margin-api.php new file mode 100644 index 00000000..97c82cfe --- /dev/null +++ b/extensions/margin-api.php @@ -0,0 +1,42 @@ + $asset, + "symbol" => $symbol, + "transFrom" => $transFrom, + "transTo" => $transTo, + "amount" => $amount, + ]; + + // someone has preformated there 8 decimal point double already + // dont do anything, leave them do whatever they want + if (gettype($amount) !== "string") { + // for every other type, lets format it appropriately + $amount = number_format($amount, 8, '.', ''); + } + + if (is_numeric($amount) === false) { + // WPCS: XSS OK. + echo "warning: amount expected numeric got " . gettype($amount) . PHP_EOL; + } + + $qstring = "/sapi/v1/margin/isolated/transfer"; + return $this->httpRequest($qstring, "POST", $opt, true); + } +} \ No newline at end of file diff --git a/php-binance-api.php b/php-binance-api.php index 015b26f7..910bb2a4 100755 --- a/php-binance-api.php +++ b/php-binance-api.php @@ -27,6 +27,8 @@ */ class API { + use Margin; + protected $base = 'https://api.binance.com/api/'; // /< REST endpoint for the currency exchange protected $wapi = 'https://api.binance.com/wapi/'; // /< REST endpoint for the withdrawals protected $sapi = 'https://api.binance.com/sapi/'; // /< REST endpoint for the supporting network API From c88f41d39d9202f7fcf42725a9b8b7c85517ca56 Mon Sep 17 00:00:00 2001 From: rc1021 Date: Sat, 27 Feb 2021 23:34:57 +0800 Subject: [PATCH 02/39] update margin --- extensions/margin-api.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/extensions/margin-api.php b/extensions/margin-api.php index 97c82cfe..9ebc9124 100644 --- a/extensions/margin-api.php +++ b/extensions/margin-api.php @@ -17,6 +17,7 @@ trait Margin public function marginIsolatedTransfer(string $asset, string $symbol, string $transFrom, string $transTo, $amount) { $opt = [ + "sapi" => true, "asset" => $asset, "symbol" => $symbol, "transFrom" => $transFrom, @@ -36,7 +37,7 @@ public function marginIsolatedTransfer(string $asset, string $symbol, string $tr echo "warning: amount expected numeric got " . gettype($amount) . PHP_EOL; } - $qstring = "/sapi/v1/margin/isolated/transfer"; + $qstring = "v1/margin/isolated/transfer"; return $this->httpRequest($qstring, "POST", $opt, true); } } \ No newline at end of file From fdc178183b017a29fadc98c775f3fef3bec00623 Mon Sep 17 00:00:00 2001 From: rc1021 Date: Sun, 28 Feb 2021 22:23:09 +0800 Subject: [PATCH 03/39] =?UTF-8?q?=E6=A7=93=E6=A1=BF=E5=B8=B3=E6=88=B6?= =?UTF-8?q?=E4=B8=8B=E5=96=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/margin-api.php | 142 +++++++++++++++++++++++++++++++++++++- 1 file changed, 141 insertions(+), 1 deletion(-) diff --git a/extensions/margin-api.php b/extensions/margin-api.php index 9ebc9124..30598f15 100644 --- a/extensions/margin-api.php +++ b/extensions/margin-api.php @@ -3,6 +3,146 @@ trait Margin { + /** + * 杠杆账户逐倉下单 (TRADE) + * + * @param $symbol string BTC + * @param $side ENUM 订单方向 : BUY / SELL + * @param $type ENUM 订单类型 (orderTypes, type) + * @param $quantity DECIMAL + * @param $quoteOrderQty DECIMAL + * @param $price DECIMAL + * @param $stopPrice DECIMAL 与STOP_LOSS, STOP_LOSS_LIMIT, TAKE_PROFIT, 和 TAKE_PROFIT_LIMIT 订单一起使用. + * @param $newClientOrderId string 客户自定义的唯一订单ID。若未发送自动生成。 + * @param $icebergQty DECIMAL 与 LIMIT, STOP_LOSS_LIMIT, 和 TAKE_PROFIT_LIMIT 一起使用创建 iceberg 订单. + * @param $newOrderRespType ENUM 设置响应: JSON. ACK, RESULT, 或 FULL; MARKET 和 LIMIT 订单类型默认为 FULL, 所有其他订单默认为 ACK. + * @param $sideEffectType ENUM NO_SIDE_EFFECT, MARGIN_BUY, AUTO_REPAY;默认为 NO_SIDE_EFFECT. + * @param $timeInForce ENUM GTC,IOC,FOK + * @return array containing the response + * @throws \Exception + */ + public function marginIsolatedOrder(string $symbol, string $side = "BUY", string $type = "LIMIT", $quantity = null, $quoteOrderQty = null, $price = null, $stopPrice = null, $newClientOrderId = null, $icebergQty = null, $newOrderRespType = null, $sideEffectType = null, $timeInForce = null) + { + return $this->marginOrder($symbol, $side, $type, true, $quantity, $quoteOrderQty, $price, $stopPrice, $newClientOrderId, $icebergQty, $newOrderRespType, $sideEffectType, $timeInForce); + } + + /** + * 杠杆账户下单 (TRADE) + * + * @param $symbol string BTC + * @param $side ENUM 订单方向 : BUY / SELL + * @param $type ENUM 订单类型 (orderTypes, type) + * @param $isIsolated bool 是否為逐倉交易 + * @param $quantity DECIMAL + * @param $quoteOrderQty DECIMAL + * @param $price DECIMAL + * @param $stopPrice DECIMAL 与STOP_LOSS, STOP_LOSS_LIMIT, TAKE_PROFIT, 和 TAKE_PROFIT_LIMIT 订单一起使用. + * @param $newClientOrderId string 客户自定义的唯一订单ID。若未发送自动生成。 + * @param $icebergQty DECIMAL 与 LIMIT, STOP_LOSS_LIMIT, 和 TAKE_PROFIT_LIMIT 一起使用创建 iceberg 订单. + * @param $newOrderRespType ENUM 设置响应: JSON. ACK, RESULT, 或 FULL; MARKET 和 LIMIT 订单类型默认为 FULL, 所有其他订单默认为 ACK. + * @param $sideEffectType ENUM NO_SIDE_EFFECT, MARGIN_BUY, AUTO_REPAY;默认为 NO_SIDE_EFFECT. + * @param $timeInForce ENUM GTC,IOC,FOK + * @return array containing the response + * @throws \Exception + */ + public function marginOrder(string $symbol, string $side = "BUY", string $type = "LIMIT", $isIsolated = false, $quantity = null, $quoteOrderQty = null, $price = null, $stopPrice = null, $newClientOrderId = null, $icebergQty = null, $newOrderRespType = null, $sideEffectType = null, $timeInForce = null) + { + // 类型 | 强制要求的参数 + // LIMIT | timeInForce, quantity, price + // MARKET | quantity or quoteOrderQty + // STOP_LOSS | quantity, stopPrice + // STOP_LOSS_LIMIT | timeInForce, quantity, price, stopPrice + // TAKE_PROFIT | quantity, stopPrice + // TAKE_PROFIT_LIMIT | timeInForce, quantity, price, stopPrice + // LIMIT_MAKER | quantity, price + + $opt = [ + "sapi" => true, + "symbol" => $symbol, + "side" => $side, + "type" => $type, + "isIsolated" => $isIsolated, + ]; + + switch($type) { + case "LIMIT": + $opt = array_merge($opt, compact('timeInForce', 'quantity', 'price')); + break; + case "MARKET": + if(!is_null($quoteOrderQty)) + $opt['quoteOrderQty'] = $quoteOrderQty; + else + $opt['quantity'] = $quantity; + break; + case "STOP_LOSS": + $opt = array_merge($opt, compact('quantity', 'stopPrice')); + break; + case "STOP_LOSS_LIMIT": + $opt = array_merge($opt, compact('timeInForce', 'quantity', 'price', 'stopPrice')); + break; + case "TAKE_PROFIT": + $opt = array_merge($opt, compact('quantity', 'stopPrice')); + break; + case "TAKE_PROFIT_LIMIT": + $opt = array_merge($opt, compact('timeInForce', 'quantity', 'price', 'stopPrice')); + break; + case "LIMIT_MAKER": + $opt = array_merge($opt, compact('quantity', 'price')); + break; + } + + $qstring = "v1/margin/order"; + return $this->httpRequest($qstring, "POST", $opt, true); + } + + /** + * 杠杆账户撤销订单 (TRADE) + * + * @param $symbol string BTC + * @param $isIsolated bool 是否為逐倉交易 + * @param $orderId LONG + * @param $origClientOrderId string + * @param $newClientOrderId string + * @return array containing the response + * @throws \Exception + */ + public function marginDeleteOrder(string $symbol, $isIsolated = false, $orderId = null, string $origClientOrderId = null, string $newClientOrderId = null) + { + $opt = [ + "sapi" => true, + "symbol" => $symbol, + "isIsolated" => $isIsolated, + ]; + + if(!is_null($origClientOrderId)) + $opt['origClientOrderId'] = $origClientOrderId; + else + $opt['orderId'] = $orderId; + + $qstring = "v1/margin/order"; + return $this->httpRequest($qstring, "DELETE", $opt, true); + } + + /** + * 杠杆账户撤销单一交易对的所有挂单 (TRADE) + * + * @param $symbol string BTC + * @param $isIsolated bool 是否為逐倉交易 + * @return array containing the response + * @throws \Exception + */ + public function marginDeleteOpenOrders(string $symbol, $isIsolated = false) + { + $opt = [ + "sapi" => true, + "symbol" => $symbol, + "isIsolated" => $isIsolated, + ]; + + $qstring = "v1/margin/openOrders"; + return $this->httpRequest($qstring, "DELETE", $opt, true); + } + /** * 杠杆逐仓账户划转 (MARGIN) * @@ -10,7 +150,7 @@ trait Margin * @param $symbol string 逐仓 symbol * @param $transFrom string "SPOT", "ISOLATED_MARGIN" * @param $transTo string "SPOT", "ISOLATED_MARGIN" - * @param $amount DECIMAL is determined by the symbol bu typicall LIMIT, STOP_LOSS_LIMIT etc. + * @param $amount DECIMAL 划转数量 * @return array containing the response * @throws \Exception */ From eee4b79ab4464f95e2b6197eae85fe62416f0fbb Mon Sep 17 00:00:00 2001 From: rc1021 Date: Sun, 28 Feb 2021 23:52:12 +0800 Subject: [PATCH 04/39] =?UTF-8?q?=E8=87=AA=E5=8B=95=E5=80=9F=E6=AC=BE/?= =?UTF-8?q?=E9=82=84=E6=AC=BE=E8=A8=AD=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/margin-api.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/extensions/margin-api.php b/extensions/margin-api.php index 30598f15..7edd4feb 100644 --- a/extensions/margin-api.php +++ b/extensions/margin-api.php @@ -45,7 +45,7 @@ public function marginIsolatedOrder(string $symbol, string $side = "BUY", string * @return array containing the response * @throws \Exception */ - public function marginOrder(string $symbol, string $side = "BUY", string $type = "LIMIT", $isIsolated = false, $quantity = null, $quoteOrderQty = null, $price = null, $stopPrice = null, $newClientOrderId = null, $icebergQty = null, $newOrderRespType = null, $sideEffectType = null, $timeInForce = null) + public function marginOrder(string $symbol, string $side = "BUY", string $type = "LIMIT", $isIsolated = false, $quantity = null, $quoteOrderQty = null, $price = null, $stopPrice = null, $newClientOrderId = null, $icebergQty = null, $newOrderRespType = null, $sideEffectType = "NO_SIDE_EFFECT", $timeInForce = null) { // 类型 | 强制要求的参数 // LIMIT | timeInForce, quantity, price @@ -62,6 +62,7 @@ public function marginOrder(string $symbol, string $side = "BUY", string $type = "side" => $side, "type" => $type, "isIsolated" => $isIsolated, + "sideEffectType" => $sideEffectType ]; switch($type) { From 1fa2bb7462d723dfc183967e5549025baa4c1e8e Mon Sep 17 00:00:00 2001 From: rc1021 Date: Mon, 1 Mar 2021 00:01:22 +0800 Subject: [PATCH 05/39] =?UTF-8?q?=E8=87=AA=E5=8B=95=E5=80=9F=E6=AC=BE/?= =?UTF-8?q?=E9=82=84=E6=AC=BE=E8=A8=AD=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/margin-api.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/extensions/margin-api.php b/extensions/margin-api.php index 7edd4feb..6da28fc5 100644 --- a/extensions/margin-api.php +++ b/extensions/margin-api.php @@ -21,9 +21,9 @@ trait Margin * @return array containing the response * @throws \Exception */ - public function marginIsolatedOrder(string $symbol, string $side = "BUY", string $type = "LIMIT", $quantity = null, $quoteOrderQty = null, $price = null, $stopPrice = null, $newClientOrderId = null, $icebergQty = null, $newOrderRespType = null, $sideEffectType = null, $timeInForce = null) + public function marginIsolatedOrder(string $symbol, string $side = "BUY", string $type = "LIMIT", $quantity = null, $quoteOrderQty = null, $price = null, $stopPrice = null, $newClientOrderId = null, $icebergQty = null, $newOrderRespType = null, $sideEffectType = "NO_SIDE_EFFECT", $timeInForce = "GTC") { - return $this->marginOrder($symbol, $side, $type, true, $quantity, $quoteOrderQty, $price, $stopPrice, $newClientOrderId, $icebergQty, $newOrderRespType, $sideEffectType, $timeInForce); + return $this->marginOrder($symbol, $side, $type, "TRUE", $quantity, $quoteOrderQty, $price, $stopPrice, $newClientOrderId, $icebergQty, $newOrderRespType, $sideEffectType, $timeInForce); } /** @@ -45,7 +45,7 @@ public function marginIsolatedOrder(string $symbol, string $side = "BUY", string * @return array containing the response * @throws \Exception */ - public function marginOrder(string $symbol, string $side = "BUY", string $type = "LIMIT", $isIsolated = false, $quantity = null, $quoteOrderQty = null, $price = null, $stopPrice = null, $newClientOrderId = null, $icebergQty = null, $newOrderRespType = null, $sideEffectType = "NO_SIDE_EFFECT", $timeInForce = null) + public function marginOrder(string $symbol, string $side = "BUY", string $type = "LIMIT", $isIsolated = "FALSE", $quantity = null, $quoteOrderQty = null, $price = null, $stopPrice = null, $newClientOrderId = null, $icebergQty = null, $newOrderRespType = null, $sideEffectType = "NO_SIDE_EFFECT", $timeInForce = "GTC") { // 类型 | 强制要求的参数 // LIMIT | timeInForce, quantity, price @@ -67,31 +67,41 @@ public function marginOrder(string $symbol, string $side = "BUY", string $type = switch($type) { case "LIMIT": + $opt['newOrderRespType'] = 'FULL'; $opt = array_merge($opt, compact('timeInForce', 'quantity', 'price')); break; case "MARKET": + $opt['newOrderRespType'] = 'FULL'; if(!is_null($quoteOrderQty)) $opt['quoteOrderQty'] = $quoteOrderQty; else $opt['quantity'] = $quantity; break; case "STOP_LOSS": + $opt['newOrderRespType'] = 'ACK'; $opt = array_merge($opt, compact('quantity', 'stopPrice')); break; case "STOP_LOSS_LIMIT": + $opt['newOrderRespType'] = 'ACK'; $opt = array_merge($opt, compact('timeInForce', 'quantity', 'price', 'stopPrice')); break; case "TAKE_PROFIT": + $opt['newOrderRespType'] = 'ACK'; $opt = array_merge($opt, compact('quantity', 'stopPrice')); break; case "TAKE_PROFIT_LIMIT": + $opt['newOrderRespType'] = 'ACK'; $opt = array_merge($opt, compact('timeInForce', 'quantity', 'price', 'stopPrice')); break; case "LIMIT_MAKER": + $opt['newOrderRespType'] = 'ACK'; $opt = array_merge($opt, compact('quantity', 'price')); break; } + if(!is_null($newOrderRespType)) + $opt['newOrderRespType'] = $newOrderRespType; + $qstring = "v1/margin/order"; return $this->httpRequest($qstring, "POST", $opt, true); } From 56d0643462b7f2f94a78b1d7338b86d125e1641d Mon Sep 17 00:00:00 2001 From: rc1021 Date: Mon, 1 Mar 2021 12:42:24 +0800 Subject: [PATCH 06/39] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=80=90=E5=80=89?= =?UTF-8?q?=E6=A7=93=E6=A1=BF=E5=88=AA=E5=96=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/margin-api.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/extensions/margin-api.php b/extensions/margin-api.php index 6da28fc5..d72a21b6 100644 --- a/extensions/margin-api.php +++ b/extensions/margin-api.php @@ -106,6 +106,21 @@ public function marginOrder(string $symbol, string $side = "BUY", string $type = return $this->httpRequest($qstring, "POST", $opt, true); } + /** + * 逐倉杠杆账户撤销订单 (TRADE) + * + * @param $symbol string BTC + * @param $orderId LONG + * @param $origClientOrderId string + * @param $newClientOrderId string + * @return array containing the response + * @throws \Exception + */ + public function marginDeleteIsolatedOrder(string $symbol, $orderId = null, string $origClientOrderId = null, string $newClientOrderId = null) + { + return $this->marginDeleteOrder($symbol, "TRUE", $orderId, $origClientOrderId, $newClientOrderId); + } + /** * 杠杆账户撤销订单 (TRADE) * From 979fd46e88980a5f1608220eba17717c96f02952 Mon Sep 17 00:00:00 2001 From: rc1021 Date: Mon, 1 Mar 2021 20:04:01 +0800 Subject: [PATCH 07/39] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9F=A5=E8=A9=A2?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E6=9D=A0=E6=9D=86=E9=80=90=E4=BB=93=E8=B4=A6?= =?UTF-8?q?=E6=88=B7=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/margin-api.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/extensions/margin-api.php b/extensions/margin-api.php index d72a21b6..7dbc9bed 100644 --- a/extensions/margin-api.php +++ b/extensions/margin-api.php @@ -169,6 +169,26 @@ public function marginDeleteOpenOrders(string $symbol, $isIsolated = false) return $this->httpRequest($qstring, "DELETE", $opt, true); } + /** + * 查询杠杆逐仓账户信息 + * + * @param $symbols string 最多可以传5个symbol; 由","分隔的字符串表示. e.g. "BTCUSDT,BNBUSDT,ADAUSDT" + * @return array containing the response + * @throws \Exception + */ + public function marginIsolatedAccount(string $symbols = null) + { + $opt = [ + "sapi" => true, + ]; + + if(!empty($symbols)) + $opt['symbols'] = $symbols; + + $qstring = "v1/margin/isolated/account"; + return $this->httpRequest($qstring, "GET", $opt, true); + } + /** * 杠杆逐仓账户划转 (MARGIN) * From a772e2f9eba6e2cc731c9b927783c9f4eb31b3c8 Mon Sep 17 00:00:00 2001 From: rc1021 Date: Tue, 2 Mar 2021 23:17:26 +0800 Subject: [PATCH 08/39] =?UTF-8?q?=E6=9F=A5=E8=A9=A2=E6=A7=93=E6=A1=BF?= =?UTF-8?q?=E5=B8=B3=E6=88=B6=E8=A8=82=E5=96=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/margin-api.php | 45 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/extensions/margin-api.php b/extensions/margin-api.php index 7dbc9bed..e1ef4027 100644 --- a/extensions/margin-api.php +++ b/extensions/margin-api.php @@ -132,7 +132,7 @@ public function marginDeleteIsolatedOrder(string $symbol, $orderId = null, strin * @return array containing the response * @throws \Exception */ - public function marginDeleteOrder(string $symbol, $isIsolated = false, $orderId = null, string $origClientOrderId = null, string $newClientOrderId = null) + public function marginDeleteOrder(string $symbol, $isIsolated = "FALSE", $orderId = null, string $origClientOrderId = null, string $newClientOrderId = null) { $opt = [ "sapi" => true, @@ -149,6 +149,47 @@ public function marginDeleteOrder(string $symbol, $isIsolated = false, $orderId return $this->httpRequest($qstring, "DELETE", $opt, true); } + /** + * 查询杠杆账户订单 (USER_DATA) 一些历史订单的 cummulativeQuoteQty < 0, 是指当前数据不存在。 + * + * @param $symbol string BTC + * @param $orderId LONG + * @param $origClientOrderId string + * @return array containing the response + * @throws \Exception + */ + public function marginGetIsIsolatedOrder(string $symbol, $orderId = null, string $origClientOrderId = null) + { + return $this->marginGetOrder($symbol, "TRUE", $orderId, $origClientOrderId); + } + + /** + * 查询杠杆账户订单 (USER_DATA) 一些历史订单的 cummulativeQuoteQty < 0, 是指当前数据不存在。 + * + * @param $symbol string BTC + * @param $isIsolated bool 是否為逐倉交易 + * @param $orderId LONG + * @param $origClientOrderId string + * @return array containing the response + * @throws \Exception + */ + public function marginGetOrder(string $symbol, $isIsolated = "FALSE", $orderId = null, string $origClientOrderId = null) + { + $opt = [ + "sapi" => true, + "symbol" => $symbol, + "isIsolated" => $isIsolated, + ]; + + if(!is_null($origClientOrderId)) + $opt['origClientOrderId'] = $origClientOrderId; + else + $opt['orderId'] = $orderId; + + $qstring = "v1/margin/order"; + return $this->httpRequest($qstring, "GET", $opt, true); + } + /** * 杠杆账户撤销单一交易对的所有挂单 (TRADE) * @@ -157,7 +198,7 @@ public function marginDeleteOrder(string $symbol, $isIsolated = false, $orderId * @return array containing the response * @throws \Exception */ - public function marginDeleteOpenOrders(string $symbol, $isIsolated = false) + public function marginDeleteOpenOrders(string $symbol, $isIsolated = "FALSE") { $opt = [ "sapi" => true, From 1dac13da1d5bd00159bb695bd942f2d924009dbb Mon Sep 17 00:00:00 2001 From: rc1021 Date: Fri, 12 Mar 2021 20:43:20 +0800 Subject: [PATCH 09/39] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E6=9F=A5=E8=A9=A2?= =?UTF-8?q?=E6=9D=A0=E6=9D=86=E8=B5=84=E4=BA=A7=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/margin-api.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/extensions/margin-api.php b/extensions/margin-api.php index e1ef4027..bfbe67d0 100644 --- a/extensions/margin-api.php +++ b/extensions/margin-api.php @@ -267,4 +267,38 @@ public function marginIsolatedTransfer(string $asset, string $symbol, string $tr $qstring = "v1/margin/isolated/transfer"; return $this->httpRequest($qstring, "POST", $opt, true); } + + /** + * 查询杠杆资产 (MARKET_DATA) + * + * @param $asset string 被划转的资产, 比如, BTC + * @return json containing the response + * @throws \Exception + */ + public function marginAsset(string $asset) + { + $opt = [ + "sapi" => true, + "asset" => $asset, + ]; + + $qstring = "v1/margin/asset"; + return $this->httpRequest($qstring, "GET", $opt, true); + } + + /** + * 获取所有杠杆资产信息 (MARKET_DATA) + * + * @return array containing the response + * @throws \Exception + */ + public function marginAllAsset() + { + $opt = [ + "sapi" => true, + ]; + + $qstring = "v1/margin/allAssets"; + return $this->httpRequest($qstring, "GET", $opt, true); + } } \ No newline at end of file From 8dea79a7af7c8ebf77f8e640cef67216aa31030d Mon Sep 17 00:00:00 2001 From: rc1021 Date: Fri, 12 Mar 2021 20:54:27 +0800 Subject: [PATCH 10/39] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E6=A7=93=E6=A1=BF?= =?UTF-8?q?=E8=A8=8A=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/margin-api.php | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/extensions/margin-api.php b/extensions/margin-api.php index bfbe67d0..0c4aa9c1 100644 --- a/extensions/margin-api.php +++ b/extensions/margin-api.php @@ -158,7 +158,7 @@ public function marginDeleteOrder(string $symbol, $isIsolated = "FALSE", $orderI * @return array containing the response * @throws \Exception */ - public function marginGetIsIsolatedOrder(string $symbol, $orderId = null, string $origClientOrderId = null) + public function marginGetIsolatedOrder(string $symbol, $orderId = null, string $origClientOrderId = null) { return $this->marginGetOrder($symbol, "TRUE", $orderId, $origClientOrderId); } @@ -210,6 +210,39 @@ public function marginDeleteOpenOrders(string $symbol, $isIsolated = "FALSE") return $this->httpRequest($qstring, "DELETE", $opt, true); } + /** + * 查询逐倉杠杆账户挂单记录 (USER_DATA) + * + * @param $symbol string BTC + * @param $isIsolated bool 是否為逐倉交易 + * @return array containing the response + * @throws \Exception + */ + public function marginIsolatedOpenOrders(string $symbol) + { + return $this->marginOpenOrders($symbol, "TRUE"); + } + + /** + * 查询杠杆账户挂单记录 (USER_DATA) + * + * @param $symbol string BTC + * @param $isIsolated bool 是否為逐倉交易 + * @return array containing the response + * @throws \Exception + */ + public function marginOpenOrders(string $symbol, $isIsolated = "FALSE") + { + $opt = [ + "sapi" => true, + "symbol" => $symbol, + "isIsolated" => $isIsolated, + ]; + + $qstring = "v1/margin/openOrders"; + return $this->httpRequest($qstring, "GET", $opt, true); + } + /** * 查询杠杆逐仓账户信息 * From 5ad32534df285f8279a6ad3c262fcf953c2a5ad8 Mon Sep 17 00:00:00 2001 From: rc1021 Date: Fri, 12 Mar 2021 21:10:54 +0800 Subject: [PATCH 11/39] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E6=9D=A0=E6=9D=86=E9=80=90=E4=BB=93=E8=B4=A6=E6=88=B7=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=20by=20key?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/margin-api.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/extensions/margin-api.php b/extensions/margin-api.php index 0c4aa9c1..71d2a00d 100644 --- a/extensions/margin-api.php +++ b/extensions/margin-api.php @@ -263,6 +263,28 @@ public function marginIsolatedAccount(string $symbols = null) return $this->httpRequest($qstring, "GET", $opt, true); } + /** + * 查询杠杆逐仓账户信息 + * + * @param $symbols string 最多可以传5个symbol; 由","分隔的字符串表示. e.g. "BTCUSDT,BNBUSDT,ADAUSDT" + * @return array containing the response + * @throws \Exception + */ + public function marginIsolatedAccountByKey(string $symbols = null) + { + $account = $this->marginIsolatedAccount($symbols); + if(array_key_exists('assets', $account)) {; + $tmp = []; + $keys = []; + // 只記錄在 SymbolType 裡存在的資訊 + foreach($account['assets'] as $key => $value){ + $tmp[$value['symbol']] = $value; + } + $account['assets'] = $tmp; + } + return $account; + } + /** * 杠杆逐仓账户划转 (MARGIN) * From 4dd7a46f40ac6a6e91f9aea16e4691241f1b3d83 Mon Sep 17 00:00:00 2001 From: rc1021 Date: Sat, 13 Mar 2021 22:16:18 +0800 Subject: [PATCH 12/39] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=9D=A0=E6=9D=86=E5=88=A9=E7=8E=87=E5=8E=86=E5=8F=B2=20(USER?= =?UTF-8?q?=5FDATA)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/margin-api.php | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/extensions/margin-api.php b/extensions/margin-api.php index 71d2a00d..95f3e9db 100644 --- a/extensions/margin-api.php +++ b/extensions/margin-api.php @@ -264,7 +264,7 @@ public function marginIsolatedAccount(string $symbols = null) } /** - * 查询杠杆逐仓账户信息 + * 查询杠杆逐仓账户信息 by key * * @param $symbols string 最多可以传5个symbol; 由","分隔的字符串表示. e.g. "BTCUSDT,BNBUSDT,ADAUSDT" * @return array containing the response @@ -275,7 +275,6 @@ public function marginIsolatedAccountByKey(string $symbols = null) $account = $this->marginIsolatedAccount($symbols); if(array_key_exists('assets', $account)) {; $tmp = []; - $keys = []; // 只記錄在 SymbolType 裡存在的資訊 foreach($account['assets'] as $key => $value){ $tmp[$value['symbol']] = $value; @@ -356,4 +355,38 @@ public function marginAllAsset() $qstring = "v1/margin/allAssets"; return $this->httpRequest($qstring, "GET", $opt, true); } + + /** + * 获取杠杆利率历史 (USER_DATA) + * + * @param $asset string 被划转的资产, 比如, BTC + * @param $vipLevel string 默认用户当前等级 + * @param $startTime LONG 默认7天前 + * @param $endTime LONG 默认当天,时间间隔最大为3个月 + * @param $limit int 默认20,最大100 + * @return array containing the response + * @throws \Exception + */ + public function marginInterestRateHistory(string $asset, string $vipLevel = null, $startTime = 0, $endTime = 0, $limit = 20) + { + $opt = [ + "sapi" => true, + "asset" => $asset, + ]; + + if($vipLevel) + $opt['vipLevel'] = $vipLevel; + + if($startTime) + $opt['startTime'] = $startTime; + + if($endTime) + $opt['endTime'] = $endTime; + + if($limit) + $opt['limit'] = $limit; + + $qstring = "v1/margin/interestRateHistory"; + return $this->httpRequest($qstring, "GET", $opt, true); + } } \ No newline at end of file From 6d17a9f0f15acc5bc69cd80ec65a2a0e1919c14e Mon Sep 17 00:00:00 2001 From: rc1021 Date: Fri, 19 Mar 2021 22:59:38 +0800 Subject: [PATCH 13/39] upgrade php 8.0 --- README.md | 4 ++-- examples/0.example.php | 4 ++-- examples/web_socket_chart.php | 4 ++-- php-binance-api.php | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 2a7b6b0f..cd62214c 100755 --- a/README.md +++ b/README.md @@ -1065,10 +1065,10 @@ $api->miniTicker(function($api, $ticker) { #### Realtime Complete Chart Updates via WebSockets ```php -$api->chart(["BNBBTC"], "15m", function($api, $symbol, $chart) { +$api->chart(["BNBBTC"], function($api, $symbol, $chart) { echo "{$symbol} chart update\n"; print_r($chart); -}); +}, "15m"); ```
View Response diff --git a/examples/0.example.php b/examples/0.example.php index bdcb4bc2..5c359b79 100755 --- a/examples/0.example.php +++ b/examples/0.example.php @@ -84,10 +84,10 @@ // Get complete realtime chart data via WebSockets -//$api->chart(["BNBBTC"], "15m", function($api, $symbol, $chart) { +//$api->chart(["BNBBTC"], function($api, $symbol, $chart) { // echo "{$symbol} chart update\n"; // print_r($chart); -//}); +//}, "15m"); // Grab realtime updated depth cache via WebSockets diff --git a/examples/web_socket_chart.php b/examples/web_socket_chart.php index 5b97f2a9..038c8a80 100755 --- a/examples/web_socket_chart.php +++ b/examples/web_socket_chart.php @@ -8,9 +8,9 @@ $api = new Binance\API(); // Get complete realtime chart data via WebSockets -$api->chart(["BNBBTC"], "15m", function($api, $symbol, $chart) { +$api->chart(["BNBBTC"], function($api, $symbol, $chart) { echo "{$symbol} chart update\n"; print_r($chart); $endpoint = strtolower( $symbol ) . '@kline_' . "15m"; $api->terminate( $endpoint ); -}); +}, "15m"); diff --git a/php-binance-api.php b/php-binance-api.php index 910bb2a4..3448f51b 100755 --- a/php-binance-api.php +++ b/php-binance-api.php @@ -2078,10 +2078,10 @@ public function ticker($symbol, callable $callback) /** * chart Pulls /kline data and subscribes to @klines WebSocket endpoint * - * $api->chart(["BNBBTC"], "15m", function($api, $symbol, $chart) { + * $api->chart(["BNBBTC"], function($api, $symbol, $chart) { * echo "{$symbol} chart update\n"; * print_r($chart); - * }); + * }, "15m"); * * @param $symbols string required symbols * @param $interval string time inteval @@ -2090,7 +2090,7 @@ public function ticker($symbol, callable $callback) * @return null * @throws \Exception */ - public function chart($symbols, string $interval = "30m", callable $callback, $limit = 500) + public function chart($symbols, callable $callback, string $interval = "30m", $limit = 500) { if (!is_array($symbols)) { $symbols = [ From 085fcaf6b79e93eea06d917a0091022c8f57ccc1 Mon Sep 17 00:00:00 2001 From: rc1021 Date: Sat, 20 Mar 2021 19:08:52 +0800 Subject: [PATCH 14/39] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9D=A0=E6=9D=86?= =?UTF-8?q?=E8=B4=A6=E6=88=B7=E6=92=A4=E9=94=80=E5=8D=95=E4=B8=80=E4=BA=A4?= =?UTF-8?q?=E6=98=93=E5=AF=B9=E7=9A=84=E6=89=80=E6=9C=89=E9=80=90=E5=80=89?= =?UTF-8?q?=E6=8C=82=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/margin-api.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/extensions/margin-api.php b/extensions/margin-api.php index 95f3e9db..8fd1bdd8 100644 --- a/extensions/margin-api.php +++ b/extensions/margin-api.php @@ -190,6 +190,18 @@ public function marginGetOrder(string $symbol, $isIsolated = "FALSE", $orderId = return $this->httpRequest($qstring, "GET", $opt, true); } + /** + * 杠杆账户撤销单一交易对的所有逐倉挂单 (TRADE) + * + * @param $symbol string BTC + * @return array containing the response + * @throws \Exception + */ + public function marginDeleteIsolatedOpenOrders(string $symbol) + { + return $this->marginDeleteOpenOrders($symbol, "TRUE"); + } + /** * 杠杆账户撤销单一交易对的所有挂单 (TRADE) * From e8bcd030dc3d09b540a7e5921f2a734ea995d1ff Mon Sep 17 00:00:00 2001 From: rc1021 Date: Fri, 26 Mar 2021 23:13:35 +0800 Subject: [PATCH 15/39] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E6=9D=A0=E6=9D=86=E8=B4=A6=E6=88=B7=E4=BA=A4=E6=98=93=E5=8E=86?= =?UTF-8?q?=E5=8F=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/margin-api.php | 48 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/extensions/margin-api.php b/extensions/margin-api.php index 8fd1bdd8..e5c11d14 100644 --- a/extensions/margin-api.php +++ b/extensions/margin-api.php @@ -149,6 +149,54 @@ public function marginDeleteOrder(string $symbol, $isIsolated = "FALSE", $orderI return $this->httpRequest($qstring, "DELETE", $opt, true); } + /** + * 查询逐倉杠杆账户交易历史 + * + * @param $symbol string BTCUSDT + * @param $orderId LONG + * @param $origClientOrderId string + * @return array containing the response + * @throws \Exception + */ + public function marginGetIsolatedMyTrades(string $symbol, $startTime = null, string $endTime = null, string $fromId, $limit = 500) + { + return $this->marginGetMyTrades($symbol, "TRUE", $startTime, $endTime, $fromId, $limit); + } + + /** + * 查询杠杆账户交易历史 + * + * @param $symbol string BTCUSDT + * @param $isIsolated bool 是否為逐倉交易 + * @param $orderId LONG + * @param $origClientOrderId string + * @return array containing the response + * @throws \Exception + */ + public function marginGetMyTrades(string $symbol, $isIsolated = "FALSE", $startTime = null, string $endTime = null, string $fromId, $limit = 500) + { + $opt = [ + "sapi" => true, + "symbol" => $symbol, + "isIsolated" => $isIsolated, + ]; + + if(!is_null($startTime)) + $opt['startTime'] = $startTime; + + if(!is_null($endTime)) + $opt['endTime'] = $endTime; + + if(!is_null($fromId)) + $opt['fromId'] = $fromId; + + if(!is_null($limit)) + $opt['limit'] = $limit; + + $qstring = "v1/margin/myTrades"; + return $this->httpRequest($qstring, "GET", $opt, true); + } + /** * 查询杠杆账户订单 (USER_DATA) 一些历史订单的 cummulativeQuoteQty < 0, 是指当前数据不存在。 * From fef43354c0cddd15652a6fe1b18473ea699e0fb9 Mon Sep 17 00:00:00 2001 From: rc1021 Date: Fri, 26 Mar 2021 23:17:06 +0800 Subject: [PATCH 16/39] fixup --- extensions/margin-api.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extensions/margin-api.php b/extensions/margin-api.php index e5c11d14..0644c87a 100644 --- a/extensions/margin-api.php +++ b/extensions/margin-api.php @@ -158,7 +158,7 @@ public function marginDeleteOrder(string $symbol, $isIsolated = "FALSE", $orderI * @return array containing the response * @throws \Exception */ - public function marginGetIsolatedMyTrades(string $symbol, $startTime = null, string $endTime = null, string $fromId, $limit = 500) + public function marginGetIsolatedMyTrades(string $symbol, $startTime = null, $endTime = null, $fromId = null, $limit = 500) { return $this->marginGetMyTrades($symbol, "TRUE", $startTime, $endTime, $fromId, $limit); } @@ -173,7 +173,7 @@ public function marginGetIsolatedMyTrades(string $symbol, $startTime = null, str * @return array containing the response * @throws \Exception */ - public function marginGetMyTrades(string $symbol, $isIsolated = "FALSE", $startTime = null, string $endTime = null, string $fromId, $limit = 500) + public function marginGetMyTrades(string $symbol, $isIsolated = "FALSE", $startTime = null, $endTime = null, $fromId = null, $limit = 500) { $opt = [ "sapi" => true, From ae935fbb4d5fa8fca8e92e5d342ec524754023b1 Mon Sep 17 00:00:00 2001 From: rc1021 Date: Fri, 26 Mar 2021 23:23:13 +0800 Subject: [PATCH 17/39] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E6=9D=A0=E6=9D=86=E8=B4=A6=E6=88=B7=E7=9A=84=E6=89=80=E6=9C=89?= =?UTF-8?q?=E8=AE=A2=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/margin-api.php | 64 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/extensions/margin-api.php b/extensions/margin-api.php index 0644c87a..6f210d7d 100644 --- a/extensions/margin-api.php +++ b/extensions/margin-api.php @@ -153,8 +153,10 @@ public function marginDeleteOrder(string $symbol, $isIsolated = "FALSE", $orderI * 查询逐倉杠杆账户交易历史 * * @param $symbol string BTCUSDT - * @param $orderId LONG - * @param $origClientOrderId string + * @param $startTime LONG + * @param $endTime LONG + * @param $fromId LONG + * @param $limit int * @return array containing the response * @throws \Exception */ @@ -168,8 +170,10 @@ public function marginGetIsolatedMyTrades(string $symbol, $startTime = null, $en * * @param $symbol string BTCUSDT * @param $isIsolated bool 是否為逐倉交易 - * @param $orderId LONG - * @param $origClientOrderId string + * @param $startTime LONG + * @param $endTime LONG + * @param $fromId LONG + * @param $limit int * @return array containing the response * @throws \Exception */ @@ -197,6 +201,58 @@ public function marginGetMyTrades(string $symbol, $isIsolated = "FALSE", $startT return $this->httpRequest($qstring, "GET", $opt, true); } + /** + * 查询逐倉杠杆账户的所有订单 + * + * @param $symbol string BTCUSDT + * @param $orderId LONG + * @param $startTime LONG + * @param $endTime LONG + * @param $limit int + * @return array containing the response + * @throws \Exception + */ + public function marginGetIsolatedAllOrders(string $symbol, $orderId = null, $startTime = null, $endTime = null, $fromId = null, $limit = 500) + { + return $this->marginGetAllOrders($symbol, "TRUE", $orderId, $startTime, $endTime, $limit); + } + + /** + * 查询杠杆账户的所有订单 + * + * @param $symbol string BTCUSDT + * @param $isIsolated bool 是否為逐倉交易 + * @param $orderId LONG + * @param $startTime LONG + * @param $endTime LONG + * @param $limit int + * @return array containing the response + * @throws \Exception + */ + public function marginGetAllOrders(string $symbol, $isIsolated = "FALSE", $orderId = null, $startTime = null, $endTime = null, $fromId = null, $limit = 500) + { + $opt = [ + "sapi" => true, + "symbol" => $symbol, + "isIsolated" => $isIsolated, + ]; + + if(!is_null($startTime)) + $opt['startTime'] = $startTime; + + if(!is_null($endTime)) + $opt['endTime'] = $endTime; + + if(!is_null($orderId)) + $opt['orderId'] = $orderId; + + if(!is_null($limit)) + $opt['limit'] = $limit; + + $qstring = "v1/margin/allOrders"; + return $this->httpRequest($qstring, "GET", $opt, true); + } + /** * 查询杠杆账户订单 (USER_DATA) 一些历史订单的 cummulativeQuoteQty < 0, 是指当前数据不存在。 * From 0d27a58767ef25d601cc4503e94de52de791a178 Mon Sep 17 00:00:00 2001 From: rc1021 Date: Fri, 26 Mar 2021 23:25:22 +0800 Subject: [PATCH 18/39] fixup --- extensions/margin-api.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extensions/margin-api.php b/extensions/margin-api.php index 6f210d7d..541241ea 100644 --- a/extensions/margin-api.php +++ b/extensions/margin-api.php @@ -212,7 +212,7 @@ public function marginGetMyTrades(string $symbol, $isIsolated = "FALSE", $startT * @return array containing the response * @throws \Exception */ - public function marginGetIsolatedAllOrders(string $symbol, $orderId = null, $startTime = null, $endTime = null, $fromId = null, $limit = 500) + public function marginGetIsolatedAllOrders(string $symbol, $orderId = null, $startTime = null, $endTime = null, $limit = 500) { return $this->marginGetAllOrders($symbol, "TRUE", $orderId, $startTime, $endTime, $limit); } @@ -229,7 +229,7 @@ public function marginGetIsolatedAllOrders(string $symbol, $orderId = null, $sta * @return array containing the response * @throws \Exception */ - public function marginGetAllOrders(string $symbol, $isIsolated = "FALSE", $orderId = null, $startTime = null, $endTime = null, $fromId = null, $limit = 500) + public function marginGetAllOrders(string $symbol, $isIsolated = "FALSE", $orderId = null, $startTime = null, $endTime = null, $limit = 500) { $opt = [ "sapi" => true, From 398811eb947b21356ea094e184d96db8ca9935f2 Mon Sep 17 00:00:00 2001 From: rc1021 Date: Fri, 26 Mar 2021 23:50:38 +0800 Subject: [PATCH 19/39] =?UTF-8?q?=E6=9D=A0=E6=9D=86=E8=B4=A6=E6=88=B7?= =?UTF-8?q?=E5=BD=92=E8=BF=98=E5=80=9F=E8=B4=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/margin-api.php | 40 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/extensions/margin-api.php b/extensions/margin-api.php index 541241ea..1a0c3971 100644 --- a/extensions/margin-api.php +++ b/extensions/margin-api.php @@ -253,6 +253,46 @@ public function marginGetAllOrders(string $symbol, $isIsolated = "FALSE", $order return $this->httpRequest($qstring, "GET", $opt, true); } + /** + * 逐倉杠杆账户归还借贷 + * + * @param $asset string + * @param $amount int + * @param $symbol string BTCUSDT + * @return array containing the response + * @throws \Exception + */ + public function marginIsolatedRepay($asset, $amount, string $symbol) + { + return $this->marginRepay($asset, $amount, "TRUE", $symbol); + } + + /** + * 杠杆账户归还借贷 + * + * @param $asset string + * @param $amount int + * @param $symbol string BTCUSDT + * @param $isIsolated bool 是否為逐倉交易 + * @return array containing the response + * @throws \Exception + */ + public function marginRepay($asset, $amount, $isIsolated = "FALSE", string $symbol = null) + { + $opt = [ + "sapi" => true, + "asset" => $asset, + "amount" => $amount, + "isIsolated" => $isIsolated, + ]; + + if($isIsolated != "FALSE") + $opt['symbol'] = $symbol; + + $qstring = "v1/margin/repay"; + return $this->httpRequest($qstring, "POST", $opt, true); + } + /** * 查询杠杆账户订单 (USER_DATA) 一些历史订单的 cummulativeQuoteQty < 0, 是指当前数据不存在。 * From 1586ab6f587a5f4a0c7ef4cdc7234c1cf61b5294 Mon Sep 17 00:00:00 2001 From: rc1021 Date: Sat, 27 Mar 2021 14:48:37 +0800 Subject: [PATCH 20/39] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E6=9D=A0=E6=9D=86=E4=BB=B7=E6=A0=BC=E6=8C=87=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/margin-api.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/extensions/margin-api.php b/extensions/margin-api.php index 1a0c3971..78452e90 100644 --- a/extensions/margin-api.php +++ b/extensions/margin-api.php @@ -149,6 +149,24 @@ public function marginDeleteOrder(string $symbol, $isIsolated = "FALSE", $orderI return $this->httpRequest($qstring, "DELETE", $opt, true); } + /** + * 查询杠杆价格指数 + * + * @param $symbol string BTC + * @return array containing the response + * @throws \Exception + */ + public function marginPriceIndex(string $symbol) + { + $opt = [ + "sapi" => true, + "symbol" => $symbol, + ]; + + $qstring = "v1/margin/priceIndex"; + return $this->httpRequest($qstring, "DELETE", $opt, true); + } + /** * 查询逐倉杠杆账户交易历史 * From 10abca10c833fd81a2932eb149369a6a351f05b7 Mon Sep 17 00:00:00 2001 From: rc1021 Date: Sat, 27 Mar 2021 14:50:56 +0800 Subject: [PATCH 21/39] fixup --- extensions/margin-api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/margin-api.php b/extensions/margin-api.php index 78452e90..6b168839 100644 --- a/extensions/margin-api.php +++ b/extensions/margin-api.php @@ -164,7 +164,7 @@ public function marginPriceIndex(string $symbol) ]; $qstring = "v1/margin/priceIndex"; - return $this->httpRequest($qstring, "DELETE", $opt, true); + return $this->httpRequest($qstring, "GET", $opt, true); } /** From 89bb10dda311afa9e294ec2ea6f8252cc33315d8 Mon Sep 17 00:00:00 2001 From: rc1021 Date: Sun, 28 Mar 2021 23:57:44 +0800 Subject: [PATCH 22/39] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20LastRequest=20?= =?UTF-8?q?=E7=B4=B0=E7=AF=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- php-binance-api.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/php-binance-api.php b/php-binance-api.php index 3448f51b..47802beb 100755 --- a/php-binance-api.php +++ b/php-binance-api.php @@ -1138,7 +1138,7 @@ protected function httpRequest(string $url, string $method = "GET", array $param if(isset($json['msg']) && !empty($json['msg'])){ // should always output error, not only on httpdebug // not outputing errors, hides it from users and ends up with tickets on github - throw new \Exception('signedRequest error: '.print_r($output, true)); + throw new \Exception('signedRequest error: '.print_r($output, true) . "\nrequest data: ".print_r($this->lastRequest, true)); } $this->transfered += strlen($output); $this->requestCount++; @@ -2488,5 +2488,10 @@ public function getXMbxUsedWeight () : int { public function getXMbxUsedWeight1m () : int { $this->xMbxUsedWeight1m; } + + public function getLastRequest() : array + { + return $this->lastRequest; + } } From 5a0efc097bee236aaf5f4df6151e6b57de3a95fb Mon Sep 17 00:00:00 2001 From: rc1021 Date: Thu, 1 Apr 2021 23:43:40 +0800 Subject: [PATCH 23/39] add output into last request if exception --- php-binance-api.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/php-binance-api.php b/php-binance-api.php index 47802beb..ad21c14b 100755 --- a/php-binance-api.php +++ b/php-binance-api.php @@ -1138,7 +1138,8 @@ protected function httpRequest(string $url, string $method = "GET", array $param if(isset($json['msg']) && !empty($json['msg'])){ // should always output error, not only on httpdebug // not outputing errors, hides it from users and ends up with tickets on github - throw new \Exception('signedRequest error: '.print_r($output, true) . "\nrequest data: ".print_r($this->lastRequest, true)); + $this->lastRequest['output'] = $output; + throw new \Exception('signedRequest error: '.print_r($output, true)); } $this->transfered += strlen($output); $this->requestCount++; From f177773dd70e87c661394d3ba6165d58a9f8c441 Mon Sep 17 00:00:00 2001 From: rc1021 Date: Tue, 6 Apr 2021 13:22:23 +0800 Subject: [PATCH 24/39] remove output key --- php-binance-api.php | 1 - 1 file changed, 1 deletion(-) diff --git a/php-binance-api.php b/php-binance-api.php index ad21c14b..4ffa2d17 100755 --- a/php-binance-api.php +++ b/php-binance-api.php @@ -1138,7 +1138,6 @@ protected function httpRequest(string $url, string $method = "GET", array $param if(isset($json['msg']) && !empty($json['msg'])){ // should always output error, not only on httpdebug // not outputing errors, hides it from users and ends up with tickets on github - $this->lastRequest['output'] = $output; throw new \Exception('signedRequest error: '.print_r($output, true)); } $this->transfered += strlen($output); From 4c83c131d98f1f81ca93c68e33c2dcfa1f94a7fb Mon Sep 17 00:00:00 2001 From: rc1021 Date: Sat, 17 Apr 2021 22:53:25 +0800 Subject: [PATCH 25/39] =?UTF-8?q?=E5=8A=A0=E5=85=A5U=E6=9C=AC=E4=BD=8D?= =?UTF-8?q?=E5=90=88=E7=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/futures-api.php | 23 +++++++++++++++++++++++ php-binance-api.php | 8 +++++++- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 extensions/futures-api.php diff --git a/extensions/futures-api.php b/extensions/futures-api.php new file mode 100644 index 00000000..2e542ff2 --- /dev/null +++ b/extensions/futures-api.php @@ -0,0 +1,23 @@ + true, + "symbol" => $symbol, + ]; + + $qstring = "v2/positionRisk"; + return $this->httpRequest($qstring, "GET", $opt, true); + } +} \ No newline at end of file diff --git a/php-binance-api.php b/php-binance-api.php index 4ffa2d17..f1ba084f 100755 --- a/php-binance-api.php +++ b/php-binance-api.php @@ -27,11 +27,12 @@ */ class API { - use Margin; + use Margin, Futures; protected $base = 'https://api.binance.com/api/'; // /< REST endpoint for the currency exchange protected $wapi = 'https://api.binance.com/wapi/'; // /< REST endpoint for the withdrawals protected $sapi = 'https://api.binance.com/sapi/'; // /< REST endpoint for the supporting network API + protected $fapi = 'https://api.binance.com/fapi/'; // /< REST endpoint for the features API protected $stream = 'wss://stream.binance.com:9443/ws/'; // /< Endpoint for establishing websocket connections protected $api_key; // /< API key that you created in the binance website member area protected $api_secret; // /< API secret that was given to you when you created the api key @@ -1038,6 +1039,11 @@ protected function httpRequest(string $url, string $method = "GET", array $param $base = $this->sapi; } + if (isset($params['fapi'])) { + unset($params['fapi']); + $base = $this->fapi; + } + $query = http_build_query($params, '', '&'); $signature = hash_hmac('sha256', $query, $this->api_secret); if ($method === "POST") { From 34dbf8a5b8aff199ebcd8ce908785ba6039ea129 Mon Sep 17 00:00:00 2001 From: rc1021 Date: Sat, 17 Apr 2021 23:13:12 +0800 Subject: [PATCH 26/39] =?UTF-8?q?=E6=9B=B4=E6=96=B0U=E6=9C=AC=E4=BD=8D?= =?UTF-8?q?=E5=90=88=E7=BA=A6endpoint=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- php-binance-api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php-binance-api.php b/php-binance-api.php index f1ba084f..8b32fc5e 100755 --- a/php-binance-api.php +++ b/php-binance-api.php @@ -32,7 +32,7 @@ class API protected $base = 'https://api.binance.com/api/'; // /< REST endpoint for the currency exchange protected $wapi = 'https://api.binance.com/wapi/'; // /< REST endpoint for the withdrawals protected $sapi = 'https://api.binance.com/sapi/'; // /< REST endpoint for the supporting network API - protected $fapi = 'https://api.binance.com/fapi/'; // /< REST endpoint for the features API + protected $fapi = 'https://fapi.binance.com/fapi/'; // /< REST endpoint for the features API protected $stream = 'wss://stream.binance.com:9443/ws/'; // /< Endpoint for establishing websocket connections protected $api_key; // /< API key that you created in the binance website member area protected $api_secret; // /< API secret that was given to you when you created the api key From e7e1a598ba59d0819159273a761513f58d0c2f57 Mon Sep 17 00:00:00 2001 From: rc1021 Date: Sun, 18 Apr 2021 16:43:42 +0800 Subject: [PATCH 27/39] =?UTF-8?q?=E8=A1=8C=E6=83=85=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/futures-api.php | 635 ++++++++++++++++++++++++++++++++++++- php-binance-api.php | 2 +- 2 files changed, 634 insertions(+), 3 deletions(-) diff --git a/extensions/futures-api.php b/extensions/futures-api.php index 2e542ff2..5be05ba3 100644 --- a/extensions/futures-api.php +++ b/extensions/futures-api.php @@ -3,10 +3,641 @@ trait Futures { + /** + * 获取交易规则和交易对 + * + * @return json containing the response + * @throws \Exception + */ + public function futuresExchangeInfo() + { + $opt = [ + "fapi" => true, + ]; + + $qstring = "fapi/v1/exchangeInfo"; + return $this->httpRequest($qstring, "GET", $opt, false); + } + + /** + * 深度信息 + * + * @param $symbol string 交易对 + * @param $limit int 默认 500; 可选值:[5, 10, 20, 50, 100, 500, 1000] + * @return json containing the response + * @throws \Exception + */ + public function futuresDepth(string $symbol, $limit = null) + { + $opt = [ + "fapi" => true, + "symbol" => $symbol, + ]; + + if(!is_null($limit)) + $opt['limit'] = $limit; + + $qstring = "fapi/v1/depth"; + return $this->httpRequest($qstring, "GET", $opt, false); + } + + /** + * 近期成交 + * + * @param $symbol string 交易对 + * @param $limit int 默认:500,最大1000 + * @return array containing the response + * @throws \Exception + */ + public function futuresTrades(string $symbol, $limit = null) + { + $opt = [ + "fapi" => true, + "symbol" => $symbol, + ]; + + if(!is_null($limit)) + $opt['limit'] = $limit; + + $qstring = "fapi/v1/trades"; + return $this->httpRequest($qstring, "GET", $opt, false); + } + + /** + * 查询历史成交(MARKET_DATA) + * + * @param $symbol string 交易对 + * @param $limit INT 默认值:500 最大值:1000. + * @param $fromId LONG 从哪一条成交id开始返回. 缺省返回最近的成交记录 + * @return array containing the response + * @throws \Exception + */ + public function futuresHistoricalTrades(string $symbol, $limit = null, $fromId = null) + { + $opt = [ + "fapi" => true, + "symbol" => $symbol, + ]; + + if(!is_null($limit)) + $opt['limit'] = $limit; + + if(!is_null($fromId)) + $opt['fromId'] = $fromId; + + $qstring = "fapi/v1/historicalTrades"; + return $this->httpRequest($qstring, "GET", $opt, true); + } + + /** + * 近期成交(归集) + * + * @param $symbol string 交易对 + * @param $fromId LONG 从哪一条成交id开始返回. 缺省返回最近的成交记录 + * @param $startTime LONG 从该时刻之后的成交记录开始返回结果 + * @param $endTime LONG 返回该时刻为止的成交记录 + * @param $limit INT 默认值:500 最大值:1000. + * @return array containing the response + * @throws \Exception + */ + public function futuresAggTrades(string $symbol, $fromId = null, $startTime = null, $endTime = null, $limit = null) + { + $opt = [ + "fapi" => true, + "symbol" => $symbol, + ]; + + if(!is_null($limit)) + $opt['limit'] = $limit; + + if(!is_null($fromId)) + $opt['fromId'] = $fromId; + + if(!is_null($startTime)) + $opt['startTime'] = $startTime; + + if(!is_null($endTime)) + $opt['endTime'] = $endTime; + + $qstring = "fapi/v1/aggTrades"; + return $this->httpRequest($qstring, "GET", $opt, false); + } + + /** + * K线数据 + * + * @param $symbol string 交易对 + * @param $interval ENUM 时间间隔 + * @param $startTime LONG 从该时刻之后的成交记录开始返回结果 + * @param $endTime LONG 返回该时刻为止的成交记录 + * @param $limit INT 默认值:500 最大值:1000. + * @return array containing the response + * @throws \Exception + */ + public function futuresKlines(string $symbol, $interval, $startTime = null, $endTime = null, $limit = null) + { + $opt = [ + "fapi" => true, + "symbol" => $symbol, + "interval" => $interval, + ]; + + if(!is_null($limit)) + $opt['limit'] = $limit; + + if(!is_null($startTime)) + $opt['startTime'] = $startTime; + + if(!is_null($endTime)) + $opt['endTime'] = $endTime; + + $qstring = "fapi/v1/klines"; + return $this->httpRequest($qstring, "GET", $opt, false); + } + + /** + * 连续合约K线数据 + * + * @param $pair string 标的交易对 + * @param $contractType ENUM 合约类型 + * @param $interval ENUM 时间间隔 + * @param $startTime LONG 起始时间 + * @param $endTime LONG 结束时间 + * @param $limit INT 默认值:500 最大值:1000. + * @return array containing the response + * @throws \Exception + */ + public function futuresContinuousKlines(string $symbol, $contractType, $interval, $startTime = null, $endTime = null, $limit = null) + { + // 合约类型: + // PERPETUAL 永续合约 + // CURRENT_MONTH 当月交割合约 + // NEXT_MONTH 次月交割合约 + // CURRENT_QUARTER 当季交割合约 + // NEXT_QUARTER 次季交割合约 + + $opt = [ + "fapi" => true, + "symbol" => $symbol, + "contractType" => $contractType, + "interval" => $interval, + ]; + + if(!is_null($limit)) + $opt['limit'] = $limit; + + if(!is_null($startTime)) + $opt['startTime'] = $startTime; + + if(!is_null($endTime)) + $opt['endTime'] = $endTime; + + $qstring = "fapi/v1/continuousKlines"; + return $this->httpRequest($qstring, "GET", $opt, false); + } + + /** + * 价格指数K线数据: 每根K线的开盘时间可视为唯一ID + * + * @param $pair string 标的交易对 + * @param $interval ENUM 时间间隔 + * @param $startTime LONG 从该时刻之后的成交记录开始返回结果 + * @param $endTime LONG 返回该时刻为止的成交记录 + * @param $limit INT 默认值:500 最大值:1000. + * @return array containing the response + * @throws \Exception + */ + public function futuresIndexPriceKlines(string $pair, $interval, $startTime = null, $endTime = null, $limit = null) + { + $opt = [ + "fapi" => true, + "pair" => $pair, + "interval" => $interval, + ]; + + if(!is_null($limit)) + $opt['limit'] = $limit; + + if(!is_null($startTime)) + $opt['startTime'] = $startTime; + + if(!is_null($endTime)) + $opt['endTime'] = $endTime; + + $qstring = "fapi/v1/indexPriceKlines"; + return $this->httpRequest($qstring, "GET", $opt, false); + } + + /** + * 标记价格K线数据: 每根K线的开盘时间可视为唯一ID + * + * @param $symbol string 交易对 + * @param $interval ENUM 时间间隔 + * @param $startTime LONG 从该时刻之后的成交记录开始返回结果 + * @param $endTime LONG 返回该时刻为止的成交记录 + * @param $limit INT 默认值:500 最大值:1000. + * @return array containing the response + * @throws \Exception + */ + public function futuresMarkPriceKlines(string $symbol, $interval, $startTime = null, $endTime = null, $limit = null) + { + $opt = [ + "fapi" => true, + "symbol" => $symbol, + "interval" => $interval, + ]; + + if(!is_null($limit)) + $opt['limit'] = $limit; + + if(!is_null($startTime)) + $opt['startTime'] = $startTime; + + if(!is_null($endTime)) + $opt['endTime'] = $endTime; + + $qstring = "fapi/v1/markPriceKlines"; + return $this->httpRequest($qstring, "GET", $opt, false); + } + + /** + * 最新标记价格和资金费率:采集各大交易所数据加权平均 + * + * @param $symbol string 交易对 + * @return array|json containing the response + * @throws \Exception + */ + public function futuresPremiumIndex(string $symbol = null) + { + $opt = [ + "fapi" => true, + ]; + + if(!is_null($symbol)) + $opt['symbol'] = $symbol; + + $qstring = "fapi/v1/premiumIndex"; + return $this->httpRequest($qstring, "GET", $opt, false); + } + + /** + * 查询资金费率历史 + * + * @param $symbol string 交易对 + * @param $startTime LONG 起始时间 + * @param $endTime LONG 结束时间 + * @param $limit INT 默认值:100 最大值:1000 + * @return array containing the response + * @throws \Exception + */ + public function futuresFundingRate(string $symbol = null, $startTime = null, $endTime = null, $limit = null) + { + $opt = [ + "fapi" => true, + ]; + + if(!is_null($symbol)) + $opt['symbol'] = $symbol; + + if(!is_null($limit)) + $opt['limit'] = $limit; + + if(!is_null($startTime)) + $opt['startTime'] = $startTime; + + if(!is_null($endTime)) + $opt['endTime'] = $endTime; + + $qstring = "fapi/v1/fundingRate"; + return $this->httpRequest($qstring, "GET", $opt, false); + } + + /** + * 24hr价格变动情况: 请注意,不携带symbol参数会返回全部交易对数据,不仅数据庞大,而且权重极高 + * + * @param $symbol string 交易对 + * @return array|json containing the response + * @throws \Exception + */ + public function futuresTicker24hr(string $symbol = null) + { + $opt = [ + "fapi" => true, + ]; + + if(!is_null($symbol)) + $opt['symbol'] = $symbol; + + $qstring = "fapi/v1/ticker/24hr"; + return $this->httpRequest($qstring, "GET", $opt, false); + } + + /** + * 最新价格 + * + * @param $symbol string 交易对 + * @return array|json containing the response + * @throws \Exception + */ + public function futuresTickerPrice(string $symbol = null) + { + $opt = [ + "fapi" => true, + ]; + + if(!is_null($symbol)) + $opt['symbol'] = $symbol; + + $qstring = "fapi/v1/ticker/price"; + return $this->httpRequest($qstring, "GET", $opt, false); + } + + /** + * 当前最优挂单 + * + * @param $symbol string 交易对 + * @return array|json containing the response + * @throws \Exception + */ + public function futuresTickerBookTicker(string $symbol = null) + { + $opt = [ + "fapi" => true, + ]; + + if(!is_null($symbol)) + $opt['symbol'] = $symbol; + + $qstring = "fapi/v1/ticker/bookTicker"; + return $this->httpRequest($qstring, "GET", $opt, false); + } + + /** + * 获取市场强平订单: 仅可查询最近7天数据 + * + * @param $symbol string 交易对 + * @param $startTime LONG 起始时间 + * @param $endTime LONG 结束时间,默认当前时间 + * @param $limit INT 从endTime倒推算起的数据条数,默认值:100 最大值:1000 + * @return array containing the response + * @throws \Exception + */ + public function futuresAllForceOrders(string $symbol = null, $startTime = null, $endTime = null, $limit = null) + { + $opt = [ + "fapi" => true, + ]; + + if(!is_null($symbol)) + $opt['symbol'] = $symbol; + + if(!is_null($limit)) + $opt['limit'] = $limit; + + if(!is_null($startTime)) + $opt['startTime'] = $startTime; + + if(!is_null($endTime)) + $opt['endTime'] = $endTime; + + $qstring = "fapi/v1/allForceOrders"; + return $this->httpRequest($qstring, "GET", $opt, false); + } + + /** + * 获取未平仓合约数 + * + * @param $symbol string 交易对 + * @return array|json containing the response + * @throws \Exception + */ + public function futuresOpenInterest(string $symbol) + { + $opt = [ + "fapi" => true, + "symbol" => $symbol, + ]; + + $qstring = "fapi/v1/openInterest"; + return $this->httpRequest($qstring, "GET", $opt, false); + } + + /** + * 合约持仓量 + * + * @param $symbol string 交易对 + * @param $period ENUM "5m","15m","30m","1h","2h","4h","6h","12h","1d" + * @param $limit INT default 30, max 500 + * @param $startTime LONG 起始时间 + * @param $endTime LONG 结束时间 + * @return array containing the response + * @throws \Exception + */ + public function futuresDataOpenInterestHist(string $symbol, $period, $limit = null, $startTime = null, $endTime = null) + { + $opt = [ + "fapi" => true, + "symbol" => $symbol, + "period" => $period, + ]; + + if(!is_null($limit)) + $opt['limit'] = $limit; + + if(!is_null($startTime)) + $opt['startTime'] = $startTime; + + if(!is_null($endTime)) + $opt['endTime'] = $endTime; + + $qstring = "futures/data/openInterestHist"; + return $this->httpRequest($qstring, "GET", $opt, false); + } + + /** + * 大户账户数多空比 + * + * @param $symbol string 交易对 + * @param $period ENUM "5m","15m","30m","1h","2h","4h","6h","12h","1d" + * @param $limit INT default 30, max 500 + * @param $startTime LONG 起始时间 + * @param $endTime LONG 结束时间 + * @return array containing the response + * @throws \Exception + */ + public function futuresDataTopLongShortAccountRatio(string $symbol, $period, $limit = null, $startTime = null, $endTime = null) + { + $opt = [ + "fapi" => true, + "symbol" => $symbol, + "period" => $period, + ]; + + if(!is_null($limit)) + $opt['limit'] = $limit; + + if(!is_null($startTime)) + $opt['startTime'] = $startTime; + + if(!is_null($endTime)) + $opt['endTime'] = $endTime; + + $qstring = "futures/data/topLongShortAccountRatio"; + return $this->httpRequest($qstring, "GET", $opt, false); + } + + /** + * 大户持仓量多空比 + * + * @param $symbol string 交易对 + * @param $period ENUM "5m","15m","30m","1h","2h","4h","6h","12h","1d" + * @param $limit INT default 30, max 500 + * @param $startTime LONG 起始时间 + * @param $endTime LONG 结束时间 + * @return array containing the response + * @throws \Exception + */ + public function futuresDataTopLongShortPositionRatio(string $symbol, $period, $limit = null, $startTime = null, $endTime = null) + { + $opt = [ + "fapi" => true, + "symbol" => $symbol, + "period" => $period, + ]; + + if(!is_null($limit)) + $opt['limit'] = $limit; + + if(!is_null($startTime)) + $opt['startTime'] = $startTime; + + if(!is_null($endTime)) + $opt['endTime'] = $endTime; + + $qstring = "futures/data/topLongShortPositionRatio"; + return $this->httpRequest($qstring, "GET", $opt, false); + } + + /** + * 多空持仓人数比 + * + * @param $symbol string 交易对 + * @param $period ENUM "5m","15m","30m","1h","2h","4h","6h","12h","1d" + * @param $limit INT default 30, max 500 + * @param $startTime LONG 起始时间 + * @param $endTime LONG 结束时间 + * @return array containing the response + * @throws \Exception + */ + public function futuresDataGlobalLongShortAccountRatio(string $symbol, $period, $limit = null, $startTime = null, $endTime = null) + { + $opt = [ + "fapi" => true, + "symbol" => $symbol, + "period" => $period, + ]; + + if(!is_null($limit)) + $opt['limit'] = $limit; + + if(!is_null($startTime)) + $opt['startTime'] = $startTime; + + if(!is_null($endTime)) + $opt['endTime'] = $endTime; + + $qstring = "futures/data/globalLongShortAccountRatio"; + return $this->httpRequest($qstring, "GET", $opt, false); + } + + /** + * 合约主动买卖量 + * + * @param $symbol string 交易对 + * @param $period ENUM "5m","15m","30m","1h","2h","4h","6h","12h","1d" + * @param $limit INT default 30, max 500 + * @param $startTime LONG 起始时间 + * @param $endTime LONG 结束时间 + * @return array containing the response + * @throws \Exception + */ + public function futuresDataTakerlongshortRatio(string $symbol, $period, $limit = null, $startTime = null, $endTime = null) + { + $opt = [ + "fapi" => true, + "symbol" => $symbol, + "period" => $period, + ]; + + if(!is_null($limit)) + $opt['limit'] = $limit; + + if(!is_null($startTime)) + $opt['startTime'] = $startTime; + + if(!is_null($endTime)) + $opt['endTime'] = $endTime; + + $qstring = "futures/data/takerlongshortRatio"; + return $this->httpRequest($qstring, "GET", $opt, false); + } + + /** + * 杠杆代币历史净值K线 + * + * @param $symbol string 交易对 + * @param $interval ENUM + * @param $startTime LONG 起始时间 + * @param $endTime LONG 结束时间 + * @param $limit INT 默认 500, 最大 1000 + * @return array containing the response + * @throws \Exception + */ + public function futuresLvtKlines(string $symbol, $interval, $limit = null, $startTime = null, $endTime = null) + { + $opt = [ + "fapi" => true, + "symbol" => $symbol, + "interval" => $interval, + ]; + + if(!is_null($limit)) + $opt['limit'] = $limit; + + if(!is_null($startTime)) + $opt['startTime'] = $startTime; + + if(!is_null($endTime)) + $opt['endTime'] = $endTime; + + $qstring = "fapi/v1/lvtKlines"; + return $this->httpRequest($qstring, "GET", $opt, false); + } + + /** + * 综合指数交易对信息 + * + * @param $symbol string 交易对 + * @return array containing the response + * @throws \Exception + */ + public function futuresIndexInfo(string $symbol = null) + { + $opt = [ + "fapi" => true, + ]; + + if(!is_null($symbol)) + $opt['symbol'] = $symbol; + + $qstring = "fapi/v1/indexInfo"; + return $this->httpRequest($qstring, "GET", $opt, false); + } + /** * 用户持仓风险V2 (USER_DATA) * - * @param $symbol string BTCUSDT + * @param $symbol string 交易对 * @return array containing the response * @throws \Exception */ @@ -17,7 +648,7 @@ public function futuresPositionRisk(string $symbol) "symbol" => $symbol, ]; - $qstring = "v2/positionRisk"; + $qstring = "fapi/v2/positionRisk"; return $this->httpRequest($qstring, "GET", $opt, true); } } \ No newline at end of file diff --git a/php-binance-api.php b/php-binance-api.php index 8b32fc5e..85e59c2e 100755 --- a/php-binance-api.php +++ b/php-binance-api.php @@ -32,7 +32,7 @@ class API protected $base = 'https://api.binance.com/api/'; // /< REST endpoint for the currency exchange protected $wapi = 'https://api.binance.com/wapi/'; // /< REST endpoint for the withdrawals protected $sapi = 'https://api.binance.com/sapi/'; // /< REST endpoint for the supporting network API - protected $fapi = 'https://fapi.binance.com/fapi/'; // /< REST endpoint for the features API + protected $fapi = 'https://fapi.binance.com/'; // /< REST endpoint for the features API protected $stream = 'wss://stream.binance.com:9443/ws/'; // /< Endpoint for establishing websocket connections protected $api_key; // /< API key that you created in the binance website member area protected $api_secret; // /< API secret that was given to you when you created the api key From 1d63eab92b0a5606f14e3f8425263cc0b65fc647 Mon Sep 17 00:00:00 2001 From: rc1021 Date: Wed, 21 Apr 2021 08:42:17 +0800 Subject: [PATCH 28/39] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=90=88=E7=B4=84api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/futures-api.php | 368 +++++++++++++++++++++++++++++++++++++ 1 file changed, 368 insertions(+) diff --git a/extensions/futures-api.php b/extensions/futures-api.php index 5be05ba3..a9c96159 100644 --- a/extensions/futures-api.php +++ b/extensions/futures-api.php @@ -1,6 +1,8 @@ httpRequest($qstring, "GET", $opt, false); } + /** + * 更改持仓模式(TRADE) + * + * @param $dualSidePosition string "true": 双向持仓模式;"false": 单向持仓模式 + * @return json containing the response + * @throws \Exception + */ + public function futuresPositionSideDual(string $dualSidePosition = 'false') + { + $opt = [ + "fapi" => true, + "dualSidePosition" => $dualSidePosition, + ]; + + $qstring = "fapi/v1/positionSide/dual"; + return $this->httpRequest($qstring, "POST", $opt, true); + } + + /** + * 查询持仓模式(USER_DATA) + * + * @param $symbol string 交易对 + * @return array containing the response + * @throws \Exception + */ + public function futuresGetPositionSideDual() + { + $opt = [ + "fapi" => true, + ]; + + $qstring = "fapi/v1/positionSide/dual"; + return $this->httpRequest($qstring, "GET", $opt, true); + } + + /** + * 下单 (TRADE) + * + * @param $symbol STRING 交易对 + * @param $side ENUM 买卖方向 SELL, BUY + * @param $positionSide ENUM 持仓方向,单向持仓模式下非必填,默认且仅可填BOTH;在双向持仓模式下必填,且仅可选择 LONG 或 SHORT + * @param $type ENUM 订单类型 LIMIT, MARKET, STOP, TAKE_PROFIT, STOP_MARKET, TAKE_PROFIT_MARKET, TRAILING_STOP_MARKET + * @param $reduceOnly STRING true, false; 非双开模式下默认false;双开模式下不接受此参数; 使用closePosition不支持此参数。 + * @param $quantity DECIMAL 下单数量,使用closePosition不支持此参数。 + * @param $price DECIMAL 委托价格 + * @param $newClientOrderId STRING 用户自定义的订单号,不可以重复出现在挂单中。如空缺系统会自动赋值。必须满足正则规则 ^[\.A-Z\:/a-z0-9_-]{1,36}$ + * @param $stopPrice DECIMAL 触发价, 仅 STOP, STOP_MARKET, TAKE_PROFIT, TAKE_PROFIT_MARKET 需要此参数 + * @param $closePosition STRING true, false;触发后全部平仓,仅支持STOP_MARKET和TAKE_PROFIT_MARKET;不与quantity合用;自带只平仓效果,不与reduceOnly 合用 + * @param $activationPrice DECIMAL 追踪止损激活价格,仅TRAILING_STOP_MARKET 需要此参数, 默认为下单当前市场价格(支持不同workingType) + * @param $callbackRate DECIMAL 追踪止损回调比例,可取值范围[0.1, 5],其中 1代表1% ,仅TRAILING_STOP_MARKET 需要此参数 + * @param $timeInForce ENUM 有效方法 + * @param $workingType ENUM stopPrice 触发类型: MARK_PRICE(标记价格), CONTRACT_PRICE(合约最新价). 默认 CONTRACT_PRICE + * @param $priceProtect STRING 条件单触发保护:"TRUE","FALSE", 默认"FALSE". 仅 STOP, STOP_MARKET, TAKE_PROFIT, TAKE_PROFIT_MARKET 需要此参数 + * @param $newOrderRespType ENUM "ACK", "RESULT", 默认 "ACK" + * @param $test boolean 是否測試購買? + * @return json containing the response + * @throws \Exception + */ + public function futuresOrder(string $symbol, string $side, string $type, $positionSide = null, $reduceOnly = null, $quantity = null, $price = null, $newClientOrderId = null, $stopPrice = null, $closePosition = null, $activationPrice = null, $callbackRate = null, $timeInForce = null, $workingType = null, $priceProtect = null, $newOrderRespType = "RESULT", bool $test = false) + { + $opt = [ + "fapi" => true, + "symbol" => $symbol, + "side" => $side, + "type" => $type, + "newOrderRespType" => $newOrderRespType, + ]; + + // 驗證必要參數 + switch($type) + { + case 'LIMIT': + if(is_null($timeInForce) or is_null($quantity) or is_null($price)) + throw new Exception('根據 order type的不同,強制要求參數:timeInForce, quantity, price'); + + break; + case 'MARKET': + if(is_null($quantity)) + throw new Exception('根據 order type的不同,強制要求參數:quantity'); + break; + case 'STOP': + case 'TAKE_PROFIT': + if(is_null($quantity) or is_null($price) or is_null($stopPrice)) + throw new Exception('根據 order type的不同,強制要求參數:quantity, price, stopPrice'); + break; + case 'STOP_MARKET': + case 'TAKE_PROFIT_MARKET': + if(is_null($stopPrice)) + throw new Exception('根據 order type的不同,強制要求參數:stopPrice'); + break; + case 'TRAILING_STOP_MARKET': + if(is_null($callbackRate)) + throw new Exception('根據 order type的不同,強制要求參數:callbackRate'); + break; + } + + $qstring = ($test) ? "fapi/v1/order/test" : "fapi/v1/order"; + return $this->httpRequest($qstring, "POST", $opt, true); + } + + /** + * 查询订单 (USER_DATA) + * + * @param $symbol string 交易对 + * @param $orderId LONG 系统订单号 + * @param $origClientOrderId string 用户自定义的订单号 + * @return array containing the response + * @throws \Exception + */ + public function futuresGetOrder(string $symbol, $orderId = null, $origClientOrderId = null) + { + $opt = [ + "fapi" => true, + "symbol" => $symbol, + ]; + + if(!is_null($orderId)) + $opt['orderId'] = $orderId; + + if(!is_null($origClientOrderId)) + $opt['origClientOrderId'] = $origClientOrderId; + + $qstring = "fapi/v1/order"; + return $this->httpRequest($qstring, "GET", $opt, true); + } + + /** + * 查询订单 (USER_DATA) + * + * @param $symbol string 交易对 + * @param $orderId LONG 系统订单号 + * @param $origClientOrderId string 用户自定义的订单号 + * @return array containing the response + * @throws \Exception + */ + public function futuresDeleteOrder(string $symbol, $orderId = null, $origClientOrderId = null) + { + $opt = [ + "fapi" => true, + "symbol" => $symbol, + ]; + + if(!is_null($orderId)) + $opt['orderId'] = $orderId; + + if(!is_null($origClientOrderId)) + $opt['origClientOrderId'] = $origClientOrderId; + + $qstring = "fapi/v1/order"; + return $this->httpRequest($qstring, "DELETE", $opt, true); + } + + /** + * 撤销全部订单 (TRADE) + * + * @param $symbol string 交易对 + * @return array containing the response + * @throws \Exception + */ + public function futuresAllOpenOrders(string $symbol) + { + $opt = [ + "fapi" => true, + "symbol" => $symbol, + ]; + + $qstring = "fapi/v1/allOpenOrders"; + return $this->httpRequest($qstring, "DELETE", $opt, true); + } + + /** + * 倒计时撤销所有订单 (TRADE) + * + * @param $symbol string 交易对 + * @param $countdownTime LONG 倒计时。 1000 表示 1 秒; 0 表示取消倒计时撤单功能。 + * @return array containing the response + * @throws \Exception + */ + public function futuresCountdownCancelAll(string $symbol, $countdownTime = 1000) + { + $opt = [ + "fapi" => true, + "symbol" => $symbol, + "countdownTime" => $countdownTime, + ]; + + $qstring = "fapi/v1/countdownCancelAll"; + return $this->httpRequest($qstring, "POST", $opt, true); + } + + /** + * 查询当前挂单 (USER_DATA) + * + * @param $symbol string 交易对 + * @param $orderId LONG 系统订单号 + * @param $origClientOrderId string 用户自定义的订单号 + * @return array containing the response + * @throws \Exception + */ + public function futuresOpenOrder(string $symbol, $orderId = null, $origClientOrderId = null) + { + $opt = [ + "fapi" => true, + "symbol" => $symbol, + ]; + + if(!is_null($orderId)) + $opt['orderId'] = $orderId; + + if(!is_null($origClientOrderId)) + $opt['origClientOrderId'] = $origClientOrderId; + + $qstring = "fapi/v1/openOrder"; + return $this->httpRequest($qstring, "GET", $opt, true); + } + + /** + * 查看当前全部挂单 (USER_DATA) + * + * @return array containing the response + * @throws \Exception + */ + public function futuresOpenOrders(string $symbol = null) + { + $opt = [ + "fapi" => true, + ]; + + if(!is_null($symbol)) + $opt['symbol'] = $symbol; + + $qstring = "fapi/v1/openOrders"; + return $this->httpRequest($qstring, "GET", $opt, true); + } + + /** + * 账户信息V2 (USER_DATA) + * + * @return array containing the response + * @throws \Exception + */ + public function futuresAccount() + { + $opt = [ + "fapi" => true, + ]; + + $qstring = "fapi/v2/account"; + return $this->httpRequest($qstring, "GET", $opt, true); + } + + /** + * 调整开仓杠杆 (TRADE) + * + * @param $symbol string 交易对 + * @param $leverage int 目标杠杆倍数:1 到 125 整数 + * @return array containing the response + * @throws \Exception + */ + public function futuresLeverage(string $symbol, $leverage = 5) + { + $opt = [ + "fapi" => true, + "symbol" => $symbol, + "leverage" => $leverage, + ]; + + $qstring = "fapi/v1/leverage"; + return $this->httpRequest($qstring, "POST", $opt, true); + } + + /** + * 变换逐仓模式 (TRADE) + * + * @param $symbol string 交易对 + * @param $marginType ENUM 保证金模式 ISOLATED(逐仓), CROSSED(全仓) + * @return array containing the response + * @throws \Exception + */ + public function futuresIsolatedMarginType(string $symbol) + { + return $this->futuresMarginType($symbol, 'ISOLATED'); + } + + /** + * 变换逐全仓模式 (TRADE) + * + * @param $symbol string 交易对 + * @param $marginType ENUM 保证金模式 ISOLATED(逐仓), CROSSED(全仓) + * @return array containing the response + * @throws \Exception + */ + public function futuresMarginType(string $symbol, $marginType = 'CROSSED') + { + $opt = [ + "fapi" => true, + "symbol" => $symbol, + "marginType" => $marginType, + ]; + + $qstring = "fapi/v1/marginType"; + return $this->httpRequest($qstring, "POST", $opt, true); + } + + /** + * 调整逐仓保证金 (TRADE) + * + * @param $symbol string 交易对 + * @param $positionSide ENUM 持仓方向,单向持仓模式下非必填,默认且仅可填BOTH;在双向持仓模式下必填,且仅可选择 LONG 或 SHORT + * @param $amount DECIMAL 保证金资金 + * @param $type int 调整方向 1: 增加逐仓保证金,2: 减少逐仓保证金 + * @return array containing the response + * @throws \Exception + */ + public function futuresPositionMargin(string $symbol, $amount, $type = 1, $positionSide = null) + { + $opt = [ + "fapi" => true, + "symbol" => $symbol, + "amount" => $amount, + "type" => $type, + ]; + + if(!is_null($positionSide)) + $opt['positionSide'] = $positionSide; + + $qstring = "fapi/v1/positionMargin"; + return $this->httpRequest($qstring, "POST", $opt, true); + } + /** * 用户持仓风险V2 (USER_DATA) * @@ -651,4 +983,40 @@ public function futuresPositionRisk(string $symbol) $qstring = "fapi/v2/positionRisk"; return $this->httpRequest($qstring, "GET", $opt, true); } + + /** + * 获取账户损益资金流水(USER_DATA) + * + * @param $symbol string 交易对 + * @param $incomeType STRING 收益类型 "TRANSFER","WELCOME_BONUS", "REALIZED_PNL","FUNDING_FEE", "COMMISSION", and "INSURANCE_CLEAR" + * @param $startTime LONG 起始时间 + * @param $endTime LONG 结束时间 + * @param $limit INT 返回的结果集数量 默认值:100 最大值:1000 + * @return array containing the response + * @throws \Exception + */ + public function futuresIncome(string $symbol = null, $incomeType = null, $startTime = null, $endTime = null, $limit = null) + { + $opt = [ + "fapi" => true, + ]; + + if(!is_null($symbol)) + $opt['symbol'] = $symbol; + + if(!is_null($incomeType)) + $opt['incomeType'] = $incomeType; + + if(!is_null($startTime)) + $opt['startTime'] = $startTime; + + if(!is_null($endTime)) + $opt['endTime'] = $endTime; + + if(!is_null($limit)) + $opt['limit'] = $limit; + + $qstring = "fapi/v1/income"; + return $this->httpRequest($qstring, "GET", $opt, true); + } } \ No newline at end of file From 6b15d89a716fa6639d275d0804c9d362da88817a Mon Sep 17 00:00:00 2001 From: rc1021 Date: Thu, 22 Apr 2021 22:46:35 +0800 Subject: [PATCH 29/39] =?UTF-8?q?=E5=90=88=E7=B4=84=E5=8A=A0=E5=85=A5?= =?UTF-8?q?=E6=96=B0=E7=AB=B9=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/futures-api.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/extensions/futures-api.php b/extensions/futures-api.php index a9c96159..8733941d 100644 --- a/extensions/futures-api.php +++ b/extensions/futures-api.php @@ -1019,4 +1019,27 @@ public function futuresIncome(string $symbol = null, $incomeType = null, $startT $qstring = "fapi/v1/income"; return $this->httpRequest($qstring, "GET", $opt, true); } + + /** + * 用户手续费率 (USER_DATA) + * + * @param $symbol string 交易对 + * @return array containing the response + * @throws \Exception + */ + public function futuresCommissionRate(string $symbol) + { + $opt = [ + "fapi" => true, + "symbol" => $symbol, + ]; + + $qstring = "fapi/v1/commissionRate"; + return $this->httpRequest($qstring, "GET", $opt, true); + // return: { + // "symbol": "BTCUSDT", + // "makerCommissionRate": "0.0002", // 0.02% + // "takerCommissionRate": "0.0004" // 0.04% + // } + } } \ No newline at end of file From fa499c470fe2f5416c7602ebfdea08eba944d699 Mon Sep 17 00:00:00 2001 From: rc1021 Date: Sun, 25 Apr 2021 18:09:48 +0800 Subject: [PATCH 30/39] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=90=88=E7=B4=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/futures-api.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/extensions/futures-api.php b/extensions/futures-api.php index 8733941d..8f855ecd 100644 --- a/extensions/futures-api.php +++ b/extensions/futures-api.php @@ -871,6 +871,35 @@ public function futuresOpenOrders(string $symbol = null) return $this->httpRequest($qstring, "GET", $opt, true); } + /** + * 账户信息V2 by key + * + * @param $symbols string 最多可以传5个symbol; 由","分隔的字符串表示. e.g. "BTCUSDT,BNBUSDT,ADAUSDT" + * @return array containing the response + * @throws \Exception + */ + public function futuresAccountByKey() + { + $account = $this->futuresAccount(); + if(array_key_exists('assets', $account)) {; + $tmp = []; + // 只記錄在 SymbolType 裡存在的資訊 + foreach($account['assets'] as $key => $value){ + $tmp[$value['asset']] = $value; + } + $account['assets'] = $tmp; + } + if(array_key_exists('positions', $account)) {; + $tmp = []; + // 只記錄在 SymbolType 裡存在的資訊 + foreach($account['positions'] as $key => $value){ + $tmp[$value['symbol']] = $value; + } + $account['positions'] = $tmp; + } + return $account; + } + /** * 账户信息V2 (USER_DATA) * From c3cf7bb8bc2c258a28ab0c102ca6d8708847f31f Mon Sep 17 00:00:00 2001 From: rc1021 Date: Wed, 28 Apr 2021 13:19:14 +0800 Subject: [PATCH 31/39] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=90=88=E7=B4=84?= =?UTF-8?q?=E5=85=A7=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/futures-api.php | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/extensions/futures-api.php b/extensions/futures-api.php index 8f855ecd..9c70dd13 100644 --- a/extensions/futures-api.php +++ b/extensions/futures-api.php @@ -701,41 +701,67 @@ public function futuresOrder(string $symbol, string $side, string $type, $positi "symbol" => $symbol, "side" => $side, "type" => $type, - "newOrderRespType" => $newOrderRespType, ]; + if(!is_null($newOrderRespType)) + $opt['newOrderRespType'] = $newOrderRespType; + // 驗證必要參數 switch($type) { case 'LIMIT': if(is_null($timeInForce) or is_null($quantity) or is_null($price)) throw new Exception('根據 order type的不同,強制要求參數:timeInForce, quantity, price'); - + $opt['timeInForce'] = $timeInForce; + $opt['quantity'] = $quantity; + $opt['price'] = $price; break; case 'MARKET': if(is_null($quantity)) throw new Exception('根據 order type的不同,強制要求參數:quantity'); + $opt['quantity'] = $quantity; break; case 'STOP': case 'TAKE_PROFIT': if(is_null($quantity) or is_null($price) or is_null($stopPrice)) throw new Exception('根據 order type的不同,強制要求參數:quantity, price, stopPrice'); + $opt['quantity'] = $quantity; + $opt['price'] = $price; + $opt['stopPrice'] = $stopPrice; break; case 'STOP_MARKET': case 'TAKE_PROFIT_MARKET': if(is_null($stopPrice)) throw new Exception('根據 order type的不同,強制要求參數:stopPrice'); + $opt['stopPrice'] = $stopPrice; break; case 'TRAILING_STOP_MARKET': if(is_null($callbackRate)) throw new Exception('根據 order type的不同,強制要求參數:callbackRate'); + $opt['callbackRate'] = $callbackRate; break; } + if(!is_null($closePosition) and $closePosition != 'false') + $opt['closePosition'] = $closePosition; + $qstring = ($test) ? "fapi/v1/order/test" : "fapi/v1/order"; return $this->httpRequest($qstring, "POST", $opt, true); } + /** + * 触发后全部平仓, + * + * @param $symbol STRING 交易对 + * @param $side ENUM 买卖方向 SELL, BUY + * @param $type ENUM 订单类型 LIMIT, MARKET, STOP, TAKE_PROFIT, STOP_MARKET, TAKE_PROFIT_MARKET, TRAILING_STOP_MARKET + * @throws \Exception + */ + public function featuresClosePositionOrder(string $symbol, string $side, string $type = 'STOP_MARKET', bool $test = false) + { + return $this->futuresOrder($symbol, $side, $type, null, null, null, null, null, null, 'true', null, null, null, null, null, "RESULT", $test); + } + /** * 查询订单 (USER_DATA) * @@ -795,7 +821,7 @@ public function futuresDeleteOrder(string $symbol, $orderId = null, $origClientO * @return array containing the response * @throws \Exception */ - public function futuresAllOpenOrders(string $symbol) + public function futuresDeleteAllOpenOrders(string $symbol) { $opt = [ "fapi" => true, From 64d51e3bc07b6965022ed91c37332370bf043d7c Mon Sep 17 00:00:00 2001 From: rc1021 Date: Sat, 1 May 2021 15:24:07 +0800 Subject: [PATCH 32/39] fixup features --- extensions/futures-api.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/extensions/futures-api.php b/extensions/futures-api.php index 9c70dd13..02e00c31 100644 --- a/extensions/futures-api.php +++ b/extensions/futures-api.php @@ -745,6 +745,27 @@ public function futuresOrder(string $symbol, string $side, string $type, $positi if(!is_null($closePosition) and $closePosition != 'false') $opt['closePosition'] = $closePosition; + if(!is_null($positionSide)) + $opt['positionSide'] = $positionSide; + + if(!is_null($reduceOnly)) + $opt['reduceOnly'] = $reduceOnly; + + if(!is_null($newClientOrderId)) + $opt['newClientOrderId'] = $newClientOrderId; + + if(!is_null($activationPrice)) + $opt['activationPrice'] = $activationPrice; + + if(!is_null($workingType)) + $opt['workingType'] = $workingType; + + if(!is_null($priceProtect)) + $opt['priceProtect'] = $priceProtect; + + if(!is_null($newOrderRespType)) + $opt['newOrderRespType'] = $newOrderRespType; + $qstring = ($test) ? "fapi/v1/order/test" : "fapi/v1/order"; return $this->httpRequest($qstring, "POST", $opt, true); } From 5c620c07385510448bfe9b10dc41f1d83639bf73 Mon Sep 17 00:00:00 2001 From: rc1021 Date: Sat, 15 May 2021 16:36:40 +0800 Subject: [PATCH 33/39] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=89=80=E6=9C=89?= =?UTF-8?q?=E9=80=90=E4=BB=93=E6=9D=A0=E6=9D=86=E4=BA=A4=E6=98=93=E5=AF=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/futures-api.php | 118 +++++++++++++++++++++++++------------ extensions/margin-api.php | 16 +++++ 2 files changed, 95 insertions(+), 39 deletions(-) diff --git a/extensions/futures-api.php b/extensions/futures-api.php index 02e00c31..343f534c 100644 --- a/extensions/futures-api.php +++ b/extensions/futures-api.php @@ -706,44 +706,49 @@ public function futuresOrder(string $symbol, string $side, string $type, $positi if(!is_null($newOrderRespType)) $opt['newOrderRespType'] = $newOrderRespType; - // 驗證必要參數 - switch($type) - { - case 'LIMIT': - if(is_null($timeInForce) or is_null($quantity) or is_null($price)) - throw new Exception('根據 order type的不同,強制要求參數:timeInForce, quantity, price'); - $opt['timeInForce'] = $timeInForce; - $opt['quantity'] = $quantity; - $opt['price'] = $price; - break; - case 'MARKET': - if(is_null($quantity)) - throw new Exception('根據 order type的不同,強制要求參數:quantity'); - $opt['quantity'] = $quantity; - break; - case 'STOP': - case 'TAKE_PROFIT': - if(is_null($quantity) or is_null($price) or is_null($stopPrice)) - throw new Exception('根據 order type的不同,強制要求參數:quantity, price, stopPrice'); - $opt['quantity'] = $quantity; - $opt['price'] = $price; - $opt['stopPrice'] = $stopPrice; - break; - case 'STOP_MARKET': - case 'TAKE_PROFIT_MARKET': - if(is_null($stopPrice)) - throw new Exception('根據 order type的不同,強制要求參數:stopPrice'); - $opt['stopPrice'] = $stopPrice; - break; - case 'TRAILING_STOP_MARKET': - if(is_null($callbackRate)) - throw new Exception('根據 order type的不同,強制要求參數:callbackRate'); - $opt['callbackRate'] = $callbackRate; - break; - } - - if(!is_null($closePosition) and $closePosition != 'false') + if(!is_null($closePosition) and $closePosition != 'false') { + if(is_null($stopPrice)) + throw new Exception('當 closePosition 為 true 強制要求參數:stopPrice'); + $opt['stopPrice'] = $stopPrice; $opt['closePosition'] = $closePosition; + } + else { + // 驗證必要參數 + switch($type) + { + case 'LIMIT': + if(is_null($timeInForce) or is_null($quantity) or is_null($price)) + throw new Exception('根據 order type的不同,強制要求參數:timeInForce, quantity, price'); + $opt['timeInForce'] = $timeInForce; + $opt['quantity'] = $quantity; + $opt['price'] = $price; + break; + case 'MARKET': + if(is_null($quantity)) + throw new Exception('根據 order type的不同,強制要求參數:quantity'); + $opt['quantity'] = $quantity; + break; + case 'STOP': + case 'TAKE_PROFIT': + if(is_null($quantity) or is_null($price) or is_null($stopPrice)) + throw new Exception('根據 order type的不同,強制要求參數:quantity, price, stopPrice'); + $opt['quantity'] = $quantity; + $opt['price'] = $price; + $opt['stopPrice'] = $stopPrice; + break; + case 'STOP_MARKET': + case 'TAKE_PROFIT_MARKET': + if(is_null($stopPrice)) + throw new Exception('根據 order type的不同,強制要求參數:stopPrice'); + $opt['stopPrice'] = $stopPrice; + break; + case 'TRAILING_STOP_MARKET': + if(is_null($callbackRate)) + throw new Exception('根據 order type的不同,強制要求參數:callbackRate'); + $opt['callbackRate'] = $callbackRate; + break; + } + } if(!is_null($positionSide)) $opt['positionSide'] = $positionSide; @@ -776,11 +781,12 @@ public function futuresOrder(string $symbol, string $side, string $type, $positi * @param $symbol STRING 交易对 * @param $side ENUM 买卖方向 SELL, BUY * @param $type ENUM 订单类型 LIMIT, MARKET, STOP, TAKE_PROFIT, STOP_MARKET, TAKE_PROFIT_MARKET, TRAILING_STOP_MARKET + * @param $stop_price int 限價 * @throws \Exception */ - public function featuresClosePositionOrder(string $symbol, string $side, string $type = 'STOP_MARKET', bool $test = false) + public function featuresClosePositionOrder(string $symbol, string $side, string $type = 'STOP_MARKET', $stop_price, bool $test = false) { - return $this->futuresOrder($symbol, $side, $type, null, null, null, null, null, null, 'true', null, null, null, null, null, "RESULT", $test); + return $this->futuresOrder($symbol, $side, $type, null, null, null, null, null, $stop_price, 'true', null, null, null, null, null, "RESULT", $test); } /** @@ -809,6 +815,40 @@ public function futuresGetOrder(string $symbol, $orderId = null, $origClientOrde return $this->httpRequest($qstring, "GET", $opt, true); } + /** + * 查询订单 (USER_DATA) + * + * @param $symbol string 交易对 + * @param $orderId LONG 只返回此orderID及之后的订单,缺省返回最近的订单 + * @param $startTime string 起始时间 + * @param $endTime string 结束时间 + * @param $limit int 返回的结果集数量 默认值:500 最大值:1000 + * @return array containing the response + * @throws \Exception + */ + public function futuresAllOrders(string $symbol, $orderId = null, $startTime = null, $endTime = null, $limit = null) + { + $opt = [ + "fapi" => true, + "symbol" => $symbol, + ]; + + if(!is_null($orderId)) + $opt['orderId'] = $orderId; + + if(!is_null($startTime)) + $opt['startTime'] = $startTime; + + if(!is_null($endTime)) + $opt['endTime'] = $endTime; + + if(!is_null($limit)) + $opt['limit'] = $limit; + + $qstring = "fapi/v1/allOrders"; + return $this->httpRequest($qstring, "GET", $opt, true); + } + /** * 查询订单 (USER_DATA) * diff --git a/extensions/margin-api.php b/extensions/margin-api.php index 6b168839..e9a047b4 100644 --- a/extensions/margin-api.php +++ b/extensions/margin-api.php @@ -563,4 +563,20 @@ public function marginInterestRateHistory(string $asset, string $vipLevel = null $qstring = "v1/margin/interestRateHistory"; return $this->httpRequest($qstring, "GET", $opt, true); } + + /** + * 获取所有逐仓杠杆交易对 + * + * @return array containing the response + * @throws \Exception + */ + public function marginIsolatedAllPairs() + { + $opt = [ + "sapi" => true, + ]; + + $qstring = "v1/margin/isolated/allPairs"; + return $this->httpRequest($qstring, "GET", $opt, true); + } } \ No newline at end of file From 8000cc3f60a57aac64474a63454a9fcdf6da0884 Mon Sep 17 00:00:00 2001 From: rc1021 Date: Sun, 23 May 2021 16:20:31 +0800 Subject: [PATCH 34/39] modify feature api --- extensions/futures-api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/futures-api.php b/extensions/futures-api.php index 343f534c..28105e69 100644 --- a/extensions/futures-api.php +++ b/extensions/futures-api.php @@ -784,7 +784,7 @@ public function futuresOrder(string $symbol, string $side, string $type, $positi * @param $stop_price int 限價 * @throws \Exception */ - public function featuresClosePositionOrder(string $symbol, string $side, string $type = 'STOP_MARKET', $stop_price, bool $test = false) + public function featuresClosePositionOrder(string $symbol, string $side, string $type = 'STOP_MARKET', $stop_price = null, bool $test = false) { return $this->futuresOrder($symbol, $side, $type, null, null, null, null, null, $stop_price, 'true', null, null, null, null, null, "RESULT", $test); } From 769977d09b41aca0f5183f6353e30b14137f4760 Mon Sep 17 00:00:00 2001 From: rc1021 Date: Sun, 30 May 2021 01:01:23 +0800 Subject: [PATCH 35/39] =?UTF-8?q?=E5=8A=A0=E5=85=A5=20margin=20=E5=83=B9?= =?UTF-8?q?=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/margin-api.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/extensions/margin-api.php b/extensions/margin-api.php index e9a047b4..050124a3 100644 --- a/extensions/margin-api.php +++ b/extensions/margin-api.php @@ -167,6 +167,19 @@ public function marginPriceIndex(string $symbol) return $this->httpRequest($qstring, "GET", $opt, true); } + /** + * 查询杠杆价格指数 + * + * @param $symbol string BTC + * @return array containing the response + * @throws \Exception + */ + public function marginPrice(string $symbol) + { + $ticker = $this->marginPriceIndex($symbol); + return $ticker['price']; + } + /** * 查询逐倉杠杆账户交易历史 * From 468724bedc79c8d82d3248631d60b4d3a2a3ad1f Mon Sep 17 00:00:00 2001 From: rc1021 Date: Sun, 30 May 2021 15:43:10 +0800 Subject: [PATCH 36/39] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E6=89=8B=E7=BB=AD?= =?UTF-8?q?=E8=B4=B9=E8=A1=A8=E6=9D=A0=E6=9D=86=E5=80=9F=E8=B4=B7=E5=88=A9?= =?UTF-8?q?=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/margin-api.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/extensions/margin-api.php b/extensions/margin-api.php index 050124a3..343a08cd 100644 --- a/extensions/margin-api.php +++ b/extensions/margin-api.php @@ -592,4 +592,37 @@ public function marginIsolatedAllPairs() $qstring = "v1/margin/isolated/allPairs"; return $this->httpRequest($qstring, "GET", $opt, true); } + + /** + * 获取所有 VIP LEVEL 交易對手續費 + * + * @return array containing the response + * @throws \Exception + */ + public function marginVipSpecList() + { + $curl = curl_init(); + curl_setopt_array($curl, [ + CURLOPT_URL => 'https://www.binance.com/gateway-api/v1/friendly/margin/vip/spec/list-all', + CURLOPT_RETURNTRANSFER => true, + CURLOPT_ENCODING => '', + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => 'GET', + CURLOPT_HTTPHEADER => ['Cookie: cid=kIck7YK5'], + ]); + $response = curl_exec($curl); + curl_close($curl); + $resp = json_decode($response); + if(array_key_exists('data', $resp)) {; + $tmp = []; + foreach($resp['data'] as $key => $value){ + $tmp[$value['assetName']] = $value; + } + $resp['data'] = $tmp; + } + return $resp; + } } \ No newline at end of file From 06e59acf8f9cdcde9beb9ec1d0eb0ba25b2e25f3 Mon Sep 17 00:00:00 2001 From: rc1021 Date: Sun, 30 May 2021 16:13:29 +0800 Subject: [PATCH 37/39] =?UTF-8?q?Revert=20"=E5=8A=A0=E5=85=A5=E6=89=8B?= =?UTF-8?q?=E7=BB=AD=E8=B4=B9=E8=A1=A8=E6=9D=A0=E6=9D=86=E5=80=9F=E8=B4=B7?= =?UTF-8?q?=E5=88=A9=E7=8E=87"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 468724bedc79c8d82d3248631d60b4d3a2a3ad1f. --- extensions/margin-api.php | 33 --------------------------------- 1 file changed, 33 deletions(-) diff --git a/extensions/margin-api.php b/extensions/margin-api.php index 343a08cd..050124a3 100644 --- a/extensions/margin-api.php +++ b/extensions/margin-api.php @@ -592,37 +592,4 @@ public function marginIsolatedAllPairs() $qstring = "v1/margin/isolated/allPairs"; return $this->httpRequest($qstring, "GET", $opt, true); } - - /** - * 获取所有 VIP LEVEL 交易對手續費 - * - * @return array containing the response - * @throws \Exception - */ - public function marginVipSpecList() - { - $curl = curl_init(); - curl_setopt_array($curl, [ - CURLOPT_URL => 'https://www.binance.com/gateway-api/v1/friendly/margin/vip/spec/list-all', - CURLOPT_RETURNTRANSFER => true, - CURLOPT_ENCODING => '', - CURLOPT_MAXREDIRS => 10, - CURLOPT_TIMEOUT => 0, - CURLOPT_FOLLOWLOCATION => true, - CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, - CURLOPT_CUSTOMREQUEST => 'GET', - CURLOPT_HTTPHEADER => ['Cookie: cid=kIck7YK5'], - ]); - $response = curl_exec($curl); - curl_close($curl); - $resp = json_decode($response); - if(array_key_exists('data', $resp)) {; - $tmp = []; - foreach($resp['data'] as $key => $value){ - $tmp[$value['assetName']] = $value; - } - $resp['data'] = $tmp; - } - return $resp; - } } \ No newline at end of file From f499f63f24280a8ce4fc9c37642f202b6e5282cb Mon Sep 17 00:00:00 2001 From: rc1021 Date: Sun, 4 Jul 2021 22:54:31 +0800 Subject: [PATCH 38/39] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E6=A7=93=E6=A1=BFpair?= =?UTF-8?q?=E6=9F=A5=E8=A9=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/margin-api.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/extensions/margin-api.php b/extensions/margin-api.php index 050124a3..8277db74 100644 --- a/extensions/margin-api.php +++ b/extensions/margin-api.php @@ -592,4 +592,21 @@ public function marginIsolatedAllPairs() $qstring = "v1/margin/isolated/allPairs"; return $this->httpRequest($qstring, "GET", $opt, true); } + + /** + * 获取所有逐仓杠杆交易对 + * + * @return array containing the response + * @throws \Exception + */ + public function marginIsolatedPairs($symbol) + { + $opt = [ + "sapi" => true, + "symbol" => $symbol, + ]; + + $qstring = "v1/margin/isolated/pair"; + return $this->httpRequest($qstring, "GET", $opt, true); + } } \ No newline at end of file From 5c0703c30d41a1f2e1c158c1ab617493f5feea1d Mon Sep 17 00:00:00 2001 From: rc1021 Date: Thu, 19 Aug 2021 22:42:15 +0800 Subject: [PATCH 39/39] Add Margin Isolated Ladder --- extensions/margin-api.php | 18 +++++++++++++++++- php-binance-api.php | 6 ++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/extensions/margin-api.php b/extensions/margin-api.php index 8277db74..76e6536e 100644 --- a/extensions/margin-api.php +++ b/extensions/margin-api.php @@ -2,7 +2,23 @@ namespace Binance; trait Margin -{ +{ + /** + * 查詢槓桿借貸階梯 + * + * @param mixed $symbol + * @return void + */ + public function marginIsolatedLadder(string $symbol) + { + $opt = [ + "bapi" => true, + ]; + + $qstring = "margin/v1/friendly/isolated-margin/ladder/{$symbol}"; + return $this->httpRequest($qstring, "GET", $opt); + } + /** * 杠杆账户逐倉下单 (TRADE) * diff --git a/php-binance-api.php b/php-binance-api.php index bc561230..6c8aea01 100755 --- a/php-binance-api.php +++ b/php-binance-api.php @@ -36,6 +36,7 @@ class API protected $wapi = 'https://api.binance.com/wapi/'; // /< REST endpoint for the withdrawals protected $sapi = 'https://api.binance.com/sapi/'; // /< REST endpoint for the supporting network API protected $fapi = 'https://fapi.binance.com/'; // /< REST endpoint for the features API + protected $bapi = 'https://www.binance.com/bapi/'; // /< REST endpoint for the base API protected $stream = 'wss://stream.binance.com:9443/ws/'; // /< Endpoint for establishing websocket connections protected $streamTestnet = 'wss://testnet.binance.vision/ws/'; // /< Testnet endpoint for establishing websocket connections protected $api_key; // /< API key that you created in the binance website member area @@ -1280,6 +1281,11 @@ protected function httpRequest(string $url, string $method = "GET", array $param unset($params['fapi']); $base = $this->fapi; } + + if (isset($params['bapi'])) { + unset($params['bapi']); + $base = $this->bapi; + } $query = $this->binance_build_query($params); $query = str_replace([ '%40' ], [ '@' ], $query);//if send data type "e-mail" then binance return: [Signature for this request is not valid.]