diff --git a/README.md b/README.md index 7397eabd..0c49c684 100755 --- a/README.md +++ b/README.md @@ -1066,10 +1066,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/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/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/extensions/futures-api.php b/extensions/futures-api.php new file mode 100644 index 00000000..28105e69 --- /dev/null +++ b/extensions/futures-api.php @@ -0,0 +1,1161 @@ + 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); + } + + /** + * 更改持仓模式(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, + ]; + + if(!is_null($newOrderRespType)) + $opt['newOrderRespType'] = $newOrderRespType; + + 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; + + 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); + } + + /** + * 触发后全部平仓, + * + * @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', $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); + } + + /** + * 查询订单 (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 只返回此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) + * + * @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 futuresDeleteAllOpenOrders(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 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) + * + * @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) + * + * @param $symbol string 交易对 + * @return array containing the response + * @throws \Exception + */ + public function futuresPositionRisk(string $symbol) + { + $opt = [ + "fapi" => true, + "symbol" => $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); + } + + /** + * 用户手续费率 (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 diff --git a/extensions/margin-api.php b/extensions/margin-api.php new file mode 100644 index 00000000..76e6536e --- /dev/null +++ b/extensions/margin-api.php @@ -0,0 +1,628 @@ + true, + ]; + + $qstring = "margin/v1/friendly/isolated-margin/ladder/{$symbol}"; + return $this->httpRequest($qstring, "GET", $opt); + } + + /** + * 杠杆账户逐倉下单 (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 = "NO_SIDE_EFFECT", $timeInForce = "GTC") + { + 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 = "NO_SIDE_EFFECT", $timeInForce = "GTC") + { + // 类型 | 强制要求的参数 + // 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, + "sideEffectType" => $sideEffectType + ]; + + 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); + } + + /** + * 逐倉杠杆账户撤销订单 (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) + * + * @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); + } + + /** + * 查询杠杆价格指数 + * + * @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, "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']; + } + + /** + * 查询逐倉杠杆账户交易历史 + * + * @param $symbol string BTCUSDT + * @param $startTime LONG + * @param $endTime LONG + * @param $fromId LONG + * @param $limit int + * @return array containing the response + * @throws \Exception + */ + public function marginGetIsolatedMyTrades(string $symbol, $startTime = null, $endTime = null, $fromId = null, $limit = 500) + { + return $this->marginGetMyTrades($symbol, "TRUE", $startTime, $endTime, $fromId, $limit); + } + + /** + * 查询杠杆账户交易历史 + * + * @param $symbol string BTCUSDT + * @param $isIsolated bool 是否為逐倉交易 + * @param $startTime LONG + * @param $endTime LONG + * @param $fromId LONG + * @param $limit int + * @return array containing the response + * @throws \Exception + */ + public function marginGetMyTrades(string $symbol, $isIsolated = "FALSE", $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($fromId)) + $opt['fromId'] = $fromId; + + if(!is_null($limit)) + $opt['limit'] = $limit; + + $qstring = "v1/margin/myTrades"; + 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, $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, $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); + } + + /** + * 逐倉杠杆账户归还借贷 + * + * @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, 是指当前数据不存在。 + * + * @param $symbol string BTC + * @param $orderId LONG + * @param $origClientOrderId string + * @return array containing the response + * @throws \Exception + */ + public function marginGetIsolatedOrder(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) + * + * @param $symbol string BTC + * @return array containing the response + * @throws \Exception + */ + public function marginDeleteIsolatedOpenOrders(string $symbol) + { + return $this->marginDeleteOpenOrders($symbol, "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); + } + + /** + * 查询逐倉杠杆账户挂单记录 (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); + } + + /** + * 查询杠杆逐仓账户信息 + * + * @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); + } + + /** + * 查询杠杆逐仓账户信息 by key + * + * @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 = []; + // 只記錄在 SymbolType 裡存在的資訊 + foreach($account['assets'] as $key => $value){ + $tmp[$value['symbol']] = $value; + } + $account['assets'] = $tmp; + } + return $account; + } + + /** + * 杠杆逐仓账户划转 (MARGIN) + * + * @param $asset string 被划转的资产, 比如, BTC + * @param $symbol string 逐仓 symbol + * @param $transFrom string "SPOT", "ISOLATED_MARGIN" + * @param $transTo string "SPOT", "ISOLATED_MARGIN" + * @param $amount DECIMAL 划转数量 + * @return array containing the response + * @throws \Exception + */ + public function marginIsolatedTransfer(string $asset, string $symbol, string $transFrom, string $transTo, $amount) + { + $opt = [ + "sapi" => true, + "asset" => $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 = "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); + } + + /** + * 获取杠杆利率历史 (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); + } + + /** + * 获取所有逐仓杠杆交易对 + * + * @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); + } + + /** + * 获取所有逐仓杠杆交易对 + * + * @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 diff --git a/php-binance-api.php b/php-binance-api.php index 81ba80ae..6c8aea01 100755 --- a/php-binance-api.php +++ b/php-binance-api.php @@ -29,10 +29,14 @@ */ class API { + use Margin, Futures; + protected $base = 'https://api.binance.com/api/'; // /< REST endpoint for the currency exchange protected $baseTestnet = 'https://testnet.binance.vision/api/'; // /< Testnet 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/'; // /< 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 @@ -1272,6 +1276,16 @@ protected function httpRequest(string $url, string $method = "GET", array $param unset($params['sapi']); $base = $this->sapi; } + + if (isset($params['fapi'])) { + 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.] @@ -2367,10 +2381,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