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.58k stars 768 forks source link

Question of response times #264

Closed pedrocarvajal closed 6 years ago

pedrocarvajal commented 6 years ago

I'm working with the "websockets" connection. I have a question and I need to know what solutions they recommend me.

There are moments that I want to do an operation "Buy or Sale" at the time I send the operation the price for example is 100, which is not consistent is when the order "Buy or sell" is executed the price is not 100. I suppose it is by internet connection? a delay? Can this be improved? as? I need you to open at the correct price.

jaggedsoft commented 6 years ago

There are always two different prices. The bid and ask. You should factor buys using the 'ask' price and sells using the 'bid' price.

pedrocarvajal commented 6 years ago

I understand, but when I receive the information from the socket I have "high, low, close, open" and keep in mind that I use "market" operations, which I only use symbol and "lot / quantity".

or do you tell me that bit and ask is calculated differently? starting from "close"?

jaggedsoft commented 6 years ago

You are talking about chart information, but the orderbook (bid/asks) control the true price when placing orders. E.g, the "close" price says 8162 for BTC/USDT and you want to buy some. The cheapest ask is $8165 so you end up paying slightly more.

binance.websockets.depthCache(["BTCUSDT"], function(symbol, depth) {
    let asks = binance.sortAsks(depth.asks);
    let bids = binance.sortBids(depth.bids);
    const ask_sum = binance.sum(Object.values(asks)).toFixed(2);
    const bid_sum = binance.sum(Object.values(bids)).toFixed(2);
    const best_ask = binance.first(asks);
    const best_bid = binance.first(bids);
    console.log("bid: $"+best_bid+" ask: $"+best_ask+" bids: "+bid_sum+" BTC. asks: "+ask_sum+" BTC.");
});

bid: $8158.88000000 ask: $8158.89000000 bids: 647.34 BTC. asks: 411.87 BTC.

pedrocarvajal commented 6 years ago

That is, suppose I want to make an opening when the "tick" or closing price is at: 100 .. I do not have to be guided by that 100, if not by the "ask", which would be a close value in the example?

jaggedsoft commented 6 years ago

This might be better for you. It has the ask price built in, and updates once per second or more

binance.websockets.prevDay("BTCUSDT", (error, price) => {
  //console.log(price); // view all data
  console.log("BTCUSDT close: "+price.close+" ask: "+price.bestAsk+" volume:"+price.volume+" change: "+price.percentChange+"%");
});

// This works for all symbols
/*binance.websockets.prevDay(false, (error, prevDay) => {
  //console.log(prevDay); // view all data
  for ( let obj of prevDay ) {
    let symbol = obj.symbol;
    console.log(symbol+" volume:"+obj.volume+" change: "+obj.priceChangePercent+"%");
  }
});*/

BTCUSDT close: 8115.00000000 ask: 8115.00000000 volume:27262.78084300 change: -1.421%

pedrocarvajal commented 6 years ago

No, I think it does not work for me, because I'm working a strategy that works for the price on each "tick.

For example, I paste my code that I am using to request the request.

https://paste.ofcode.org/3jHcCUJyfwekG4BnXNEmqD

For each "tick" I would be opting for the last ask.

pedrocarvajal commented 6 years ago

Taking as a reference the "bid / ask" solves the problem that when I enter an operation I enter with another price?

pedrocarvajal commented 6 years ago

I show you an image of what you intend to explain, the first date indicates the price at which an opening condition was fulfilled, but if you look below (second arrow) it is the price at which the order was executed! They have more than 2 USD difference.

Why do you think that problem is? A. Delay in the execution of the order? (lag)? B. I'm taking "t.close" (http://prntscr.com/kcviq9) as the reference price, and should I take "bid"?

jaggedsoft commented 6 years ago

It's because you need to use 'ask' not 'close' if you want the price to be the same Chart data does not have this available, which is why it's easier to read it directly from the order book (depthCache)

pedrocarvajal commented 6 years ago

Ok, Thank you very much for your answers!