tiagosiebler / binance

Node.js & JavaScript SDK for Binance REST APIs & WebSockets, with TypeScript & browser support, integration tests, beautification & more.
MIT License
756 stars 266 forks source link

STOP_LOSS_LIMIT & TAKE_PROFIT #147

Closed AlphaCentauriNinja closed 2 years ago

AlphaCentauriNinja commented 3 years ago

Hi tiagosiebler,

I have wrote an Algo, I am struggling to implement to following. I want to create a STOP_LOSS_LIMIT order and when that one is succesful, it should create a TAKE_PROFIT inmediately.

I have the following params;

    let type = "STOP_LOSS_LIMIT";
    let side = "BUY"
    let quantity = dataObject.amountCoin;
    let price = dataObject.entryPrice;
    let stopPrice = dataObject.stopPrice;

How do I set a STOP_LOSS_LIMIT order on Futures market.. I set my leverage and margin type (isolated) already. That works like a charm. But I can't create the above order.

And how do I set a the following order TAKE_PROFIT ?

// let typeProfit = 'TAKE_PROFIT';
// let sideProfit = 'SELL';
// let takeProfitPrice = dataObject.exitPrice;
// // -------------------------------------------
// params2 = { 'stopPrice': takeProfitPrice }

I can't seem to figure out howto. Could you explain please?

Regards, The Alpha Centauri Ninja

tiagosiebler commented 3 years ago

I've admittedly not used this mechanism as I just use limit reduce orders for TPs. Did you figure this out? Sounds like a useful example to add. If you're still stuck you could try asking how the request should look in the binance API telegram: https://t.me/binance_api_english

AlphaCentauriNinja commented 2 years ago

Hi TiagoSiebler,

I managed the following:

/api/v3/order?symbol=VETUSDT&side=BUY&type=STOP_LOSS_LIMIT&timeInForce=GTC&quantity=825&price=0.1131&stopPrice=0.1120&newOrderRespType=RESULT&timestamp=1633422601488&signature=83d725fb12ac4e1c37778e45013c766951f58c95e40a99866f0523aeb01df1fe&recvWindow=60000

However I get the following error back : data: { code: -1022, msg: 'Signature for this request is not valid.' }

I manage to create the signature the following way:

serverTime = await binance.futuresTime();

let query_string = `timestamp=${serverTime}`
let secretKey = crypto.signature(query_string);

------------ FUNCTION IMPORT --------------------- const apiSecret = process.env.EXCHANGE_SECRET_KEY

function signature(query_string) { return crypto .createHmac('sha256', apiSecret) .update(query_string) .digest('hex'); }


Do you have an idea what I am doing wrong?

Regards, Ninja

tiagosiebler commented 2 years ago

Difficult to say with a fresh implementation. Why not use the library for this? If it's just api/v3/order for spot markets, then this should be the method:

const binanceApi = new MainClient({
  ...,
  recvWindow: 60000,
})

try {
  const result = await binanceApi.submitNewOrder({
    symbol: 'VETUSDT',
    side: 'BUY',
    type: 'STOP_LOSS_LIMIT',
    timeInForce: 'GTC',
    quantity: 825,
    price: 0.1131,
    stopPrice: 0.1120,
    newOrderRespType: 'RESULT',
  });
} catch (e) {
  // log/handle error
} 
AlphaCentauriNinja commented 2 years ago

Thanks, I will try that :D

tiagosiebler commented 2 years ago

Is this resolved?

ChenYCL commented 2 years ago

Can USDMClient use STOP_LOSS_LIMIT config ? if new orrder request activated with stop_price & profit_price ?

tiagosiebler commented 2 years ago
STOP_LOSS_LIMIT

@ChenYCL the futures docs does not mention STOP_LOSS_LIMIT as a type for new orders, so maybe not. You can try asking in the binance api support channel on telegram: https://t.me/binance_api_english

Screenshot 2022-08-06 at 21 31 36
ChenYCL commented 2 years ago
STOP_LOSS_LIMIT

@ChenYCL the futures docs does not mention STOP_LOSS_LIMIT as a type for new orders, so maybe not. You can try asking in the binance api support channel on telegram: https://t.me/binance_api_english

Screenshot 2022-08-06 at 21 31 36

Thank you. Is there a way to add take profit & stop loss in orders? eg. new order use market type and get orderId reponse.

tiagosiebler commented 2 years ago

Perhaps using batch orders (treating them as separate orders): https://binance-docs.github.io/apidocs/futures/en/#place-multiple-orders-trade

In my case I manually maintain/cancel/replace a TP limit order anytime I see a position update from the user data websocket.

For API/workflow questions not specific to only this connector though I really recommend asking in the api telegram community I've linked to above. You're more likely to get that kind of guidance there. Once you know which parameters to use with which endpoint, you can make those API calls using this connector.

ChenYCL commented 2 years ago

Perhaps using batch orders (treating them as separate orders): https://binance-docs.github.io/apidocs/futures/en/#place-multiple-orders-trade

In my case I manually maintain/cancel/replace a TP limit order anytime I see a position update from the user data websocket.

For API/workflow questions not specific to only this connector though I really recommend asking in the api telegram community I've linked to above. You're more likely to get that kind of guidance there. Once you know which parameters to use with which endpoint, you can make those API calls using this connector.

👍 ok,i think it can achieve my goal, make new order then websocket get message .