php-binance-api icon indicating copy to clipboard operation
php-binance-api copied to clipboard

stop loss after the order

Open graz68a opened this issue 4 years ago • 8 comments

Hello

suppose I open a buy order using this

$order = $api->marketBuy("CHRUSDT", 300);

can I send a stop loss limit after the order ? If yes how can I send it ?

Thank you

graz68a avatar Apr 21 '21 12:04 graz68a

Yes you can,

Place a STOP LOSS order

// When the stop is reached, a stop order becomes a market order
$type = "STOP_LOSS"; // Set the type STOP_LOSS (market) or STOP_LOSS_LIMIT, and TAKE_PROFIT (market) or TAKE_PROFIT_LIMIT
$quantity = 1;
$price = 0.5; // Try to sell it for 0.5 btc
$stopPrice = 0.4; // Sell immediately if price goes below 0.4 btc
$order = $api->sell("BNBBTC", $quantity, $price, $type, ["stopPrice"=>$stopPrice]);
print_r($order);

sitenzo avatar Apr 22 '21 18:04 sitenzo

Thank you , as it seems it does not work for all crypto right ? I received error that I cannot set stop loss for CHRUSDT.

graz68a avatar Apr 23 '21 08:04 graz68a

True, same with ADAUSDT. I have changed the type to "STOP_LOSS_LIMIT"

sitenzo avatar Apr 23 '21 09:04 sitenzo

Thank you , could you show me what php code you use exactly with STOP_LOSS_LIMIT ?

graz68a avatar Apr 24 '21 14:04 graz68a

This is how my function look like.

    public function CreateStopLossOrder($Symbol,$Quantity,$SellPrice,$LimitPrice)
    {
        $PricePresission = $this->GetPricePression($Symbol);
        $LotPresission = $this->GetLotPression($Symbol);
        $Type = "STOP_LOSS_LIMIT"; 
        $SellPrice = $this->numberFormatPrecision($SellPrice, $PricePresission, '.');
        $LimitPrice = $this->numberFormatPrecision($LimitPrice, $PricePresission, '.');
        $Quantity  = $this->numberFormatPrecision($Quantity, $LotPresission, '.');
        try{
            $order = $this->API->sell($Symbol, $Quantity, $SellPrice, $Type,["stopPrice" => $LimitPrice,'timeInForce' => 'GTC']);
            $json = json_encode($order);
            $bytes = file_put_contents($this->ResourceDir.$Symbol.".json", $json);
            return true;
        }catch (\Exception $e){
            $this->HandleException($e,__LINE__,__FUNCTION__,$Symbol);
            return false;
        }
    }

sitenzo avatar Apr 25 '21 10:04 sitenzo

using this way you can put stop loss when you buy , right ? (not after the buy order)

graz68a avatar Apr 26 '21 13:04 graz68a

Depends. What do you want to do.

If you want to buy at 1 dollar and sell at 1.1 dollar you can buy with a take profit.

I do not buy with the api, i manually buy with a limit price on binance, My script handles the stoploss, and sell orders so i dont need to look when to go out of a trade is have.

sitenzo avatar May 01 '21 09:05 sitenzo

Hi @sitenzo

Is this similar to the OCO function in Binance? Im looking to place a order with take-profit and SL trigger

CodeWithDennis avatar May 01 '21 15:05 CodeWithDennis