jaggedsoft / node-binance-api

Node Binance API is an asynchronous node.js library for the Binance API designed to be easy to use.
MIT License
1.57k stars 767 forks source link

Futures Accounts Support #367

Open vitaly-t opened 4 years ago

vitaly-t commented 4 years ago

Any plans to add support for operations around the new Futures Accounts?

vitaly-t commented 4 years ago

@jaggedsoft Is it somehow possible to work-around in the current version of the library?

vitaly-t commented 4 years ago

I've got it all working just fine, with the existing library, but I had to go a little lower-level for that.

Here's a simplified example of getting all futures orders in my TypeScript adapter:

const nodeBinance = require('node-binance-api');
const config = require('./config.json');

const binance = nodeBinance().options({
    APIKEY: config.api.key,
    APISECRET: config.api.secret,
});

async getAllFuturesOrders(): Promise<any> {
    return new Promise((resolve, reject) => {
        const url = 'https://fapi.binance.com/fapi/v1/allOrders';
        binance.signedRequest(url, {symbol: 'BTCUSDT'}, (error, data) => {
            if (error) {
                reject(error);
            } else {
                resolve(data);
            }
        });
    });
}

Also, I had to re-generate my API key, because it seems that for the old key I had, futures just wouldn't work. And after creating it, you have to switch on the Futures tick box.

jaggedsoft commented 4 years ago

complete futures api should be done soon, just need to test placing and cancelling orders and finish the websocket

vitaly-t commented 4 years ago

@jaggedsoft It would make sense to close this issue after the support has been added, not before.

As it stands, the library offers only very limited futures support, according to the comments:

https://github.com/jaggedsoft/node-binance-api/blob/0448e17ed65dc1df02ece0b5943f405e267b5124/node-binance-api.js#L2369

vitaly-t commented 4 years ago

@jaggedsoft Thank you for re-opening the issue.

I tried to build some futures API on top of what's already there, but couldn't because the library does not export the essential promiseRequest method it uses:

https://github.com/jaggedsoft/node-binance-api/blob/0448e17ed65dc1df02ece0b5943f405e267b5124/node-binance-api.js#L328

What are your plans for finishing this up? For now I seem to have to continue using a fully custom extension, to get the futures working.

jaggedsoft commented 4 years ago

It was my goal to have everything wrapped up by monday, at least everything but websockets, but unfortunately this week has been extremely busy for me, and I can't give any ETA. please note there is another library that has some futures support which you might find useful https://github.com/Ashlar/binance-api-node

vitaly-t commented 4 years ago

@jaggedsoft I have been bashing my head at implementing futuresSubscribe, but no luck so far. Can you at least provide any hint? Or is it even possible to implement within the current version of the library?

Subscription is kind of the most important piece of them all, because the rest of the API can be easily patched from the client-side, which is what I did here. It's just the futures trade subscription, I cannot figure out how to do it, because of all the inner stream architecture.

Please help!!!

vitaly-t commented 4 years ago

I was only able to get the basic trades subscription, using the WebSocket type directly, from the ws module, like this:

import WebSocket from 'ws';

subscribeForTrades(cb: (trade: IFuturesTrade) => void) {
    const url = `wss://fstream.binance.com/ws/${this.coin.toLowerCase()}@trade`;
    const ws = new WebSocket(url);
    ws.on('message', (data: string) => {
        if (data) {
            const a = JSON.parse(data);
            cb({
                symbol: a.s,
                time: new Date(a.T),
                price: parseFloat(a.p),
                quantity: parseFloat(a.q),
                maker: a.m
            });
        }
    });
}