From 08d93182cddb6ed1573877fe13cf9b4b81e58aa5 Mon Sep 17 00:00:00 2001 From: ePascalC Date: Mon, 19 Jul 2021 17:06:47 +0200 Subject: [PATCH] OCO Order function ocoOrder - first implementation --- php-binance-api.php | 66 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/php-binance-api.php b/php-binance-api.php index 45644b8..bf37274 100755 --- a/php-binance-api.php +++ b/php-binance-api.php @@ -2846,5 +2846,71 @@ public function apiTradingStatus() $arr = array(); $arr['sapi'] = $this->httpRequest("v1/account/apiTradingStatus", 'GET', [ 'sapi' => true ], true); return $arr; + } + + /** + * ocoOrder - Create a new OCO order + * + * @link https://binance-docs.github.io/apidocs/spot/en/#new-oco-trade + * + * @property int $weight 1 + * + * @param string $side (mandatory) Should be SELL or BUY + * @param string $symbol (mandatory) The symbol, e.g. BTCBUSD + * @param float $quantity (mandatory) Quantity to buy/sell + * @param int $price (mandatory) Price + * @param int $stopprice (mandatory) Stop Price + * @param int $stoplimitprice (optional) Stop Limit Price + * @param int $stoplimittimeinforce (optional) GTC, FOK or IOC + * @param array $flags (optional) Extra flags/parameters + * + * @return array containing the response + * @throws \Exception + */ + public function ocoOrder(string $side, string $symbol, $quantity, $price, $stopprice, $stoplimitprice = null, $stoplimittimeinforce = 'GTC', array $flags = []) + { + $opt = [ + "symbol" => $symbol, + "side" => $side, + "recvWindow" => 60000, + ]; + + if (is_numeric($quantity) === false) { + $error = "Parameter quantity expected numeric for ' $side . ' ' . $symbol .', got " . gettype($quantity); + trigger_error($error, E_USER_ERROR); + } else { + $opt['quantity'] = $quantity; + } + + if (is_numeric($price) === false) { + $error = "Parameter price expected numeric for ' $side . ' ' . $symbol .', got " . gettype($price); + trigger_error($error, E_USER_ERROR); + } else { + $opt['price'] = $price; + } + + if (is_numeric($stopprice) === false) { + $error = "Parameter stopprice expected numeric for ' $side . ' ' . $symbol .', got " . gettype($stopprice); + trigger_error($error, E_USER_ERROR); + } else { + $opt['stopprice'] = $stopprice; + } + + if (is_null($stoplimitprice) === false && empty($stoplimitprice) === false) { + $opt['stopLimitPrice'] = $stoplimitprice; + if ( ($stoplimittimeinforce == 'FOK') || ($stoplimittimeinforce == 'IOC') ) { + $opt['stopLimitTimeInForce'] = $stoplimittimeinforce; + } else { + $opt['stopLimitTimeInForce'] = 'GTC'; // `Good 'till cancel`. Needed if flag `stopLimitPrice` used. + } + } + + // Check other flags + foreach (array('icebergQty','stopIcebergQty','listClientOrderId','limitClientOrderId','stopClientOrderId','newOrderRespType') as $flag) { + if ( isset($flags[$flag]) && !empty($flags[$flag]) ) + $opt[$flag] = $flags[$flag]; + } + + return $this->httpRequest("v3/order/oco", "POST", $opt, true); } }