ViewBlock / binance-api-node

:chart: A complete and heavily tested wrapper with typings for the Binance API.
654 stars 495 forks source link

Exemple of batchOrders #616

Closed MathewsMarra closed 1 year ago

MathewsMarra commented 1 year ago

I'm trying to call the futuresBatchOrders function, but I'm probably doing something wrong.

When I try to call this function passing an array, I receive "Data sent for parameter 'batchOrders:[object Object]' is not valid." and the URL called is https://testnet.binancefuture.com/fapi/v1/batchOrders?batchOrders=%5Bobject%20Object%5D&timestamp=1675524983285&signature= [Omitted]

My code for this is the following:

    const batchOrders = []
    batchOrders.push({
        symbol: req.query.symbol,
        side: req.query.side,
        type: req.query.type,
        quantity: qtt,
        reduceOnly: false,
    })

    if(req.query.tpsl){
        batchOrders.push({
            symbol: req.query.symbol,
            side: req.query.side === 'BUY' ? 'SELL' : 'BUY',
            type: 'TAKE_PROFIT_MARKET',
            quantity: qtt,
            timeInForce: 'GTE_GTC',
            reduceOnly: true,
            stopPrice: req.query.oco_take_profit,
            workingType: 'MARK_PRICE',
        })
        batchOrders.push({
            symbol: req.query.symbol,
            side: req.query.side === 'BUY' ? 'SELL' : 'BUY',
            type: 'STOP_MARKET',
            quantity: qtt,
            timeInForce: 'GTE_GTC',
            reduceOnly: true,
            stopPrice: req.query.oco_stop,
            workingType: 'MARK_PRICE',
        })
    }
    else if (req.query.stop_limit){
        batchOrders.push({
            symbol: req.query.symbol,
            side: req.query.side === 'BUY' ? 'SELL' : 'BUY',
            type: 'STOP_MARKET',
            quantity: qtt,
            timeInForce: 'GTE_GTC',
            reduceOnly: true,
            stopPrice: req.query.oco_stop,
            workingType: 'MARK_PRICE',
        })
    }

    const response = await client.futuresBatchOrders({batchOrders: batchOrders})

I've logged my batchOrders variable and it is:

[ { symbol: 'BTCUSDT', side: 'BUY', type: 'MARKET', quantity: '0.013', reduceOnly: false } ]

I've also tried creating an order directly on a string, but also without success:

const response = await client.futuresBatchOrders({batchOrders:'[{symbol:"BTCUSDT",side:"BUY",type:"MARKET",quantity:"0.013"}]'})

responseText: {"code":-1130,"msg":"Data sent for parameter 'batchOrders:[{symbol:"BTCUSDT",side:"BUY",type:"MARKET",quantity:"0.013"}]' is not valid."}

Can someone please help me find what the issue is? Or maybe provide a working exemple of how should I call the futuresBatchOrders method

MathewsMarra commented 1 year ago

I've managed do do it. In my case, the problem was that my Json didn't had quotation marks on the label of the values. For exemple, mine was: { symbol: '0.013', ... }

when it should've been:

{ "symbol": '0.013', ... }

I've ended up creating an array called batchOrders, pushing the Json of all the orders into the array and calling the method as it follows:

const response = await client.futuresBatchOrders({batchOrders: JSON.stringify(batchOrders)})

I hope this helps anyone who's having trouble in the future. Another tip is to download Binance API Postman and try to create an order from there, checking the URL that is being called. That was what helped me the most.

hlongvu commented 9 months ago

this fix can not apply for typescript, please check?