Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions php-binance-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}