Python-binance: web socket kline socket issue

Created on 23 Feb 2018  路  6Comments  路  Source: sammchardy/python-binance

Hi,
My intention is to get the last tick data/kline and feed it to my algorithm. I intended to do that through the websocket with the below code

bm = BinanceSocketManager(client)
conn_key = bm.start_kline_socket('BTCUSDT', process_message, interval=KLINE_INTERVAL_1MINUTE)
bm.start()

My expectation is/was to get one update per minute. However i get continuous updates for the same period (same t/T) with increasing c/h/n/V/Q values. Is there a work around for this? It feels like i am missing something obvious here.

bm.start()
message type: kline
{'e': 'kline', 'E': 1519422629533, 's': 'BTCUSDT', 'k': {'t': 1519422600000, 'T': 1519422659999, 's': 'BTCUSDT', 'i': '1m', 'f': 21751516, 'L': 21751994, 'o': '9901.04000000', 'c': '9895.81000000', 'h': '9908.57000000', 'l': '9876.00000000', 'v': '64.31203300', 'n': 479, 'x': False, 'q': '635875.32398396', 'V': '28.63972300', 'Q': '283097.49123652', 'B': '0'}}
message type: kline
{'e': 'kline', 'E': 1519422632093, 's': 'BTCUSDT', 'k': {'t': 1519422600000, 'T': 1519422659999, 's': 'BTCUSDT', 'i': '1m', 'f': 21751516, 'L': 21751995, 'o': '9901.04000000', 'c': '9895.71000000', 'h': '9908.57000000', 'l': '9876.00000000', 'v': '64.31607500', 'n': 480, 'x': False, 'q': '635915.32244378', 'V': '28.64376500', 'Q': '283137.48969634', 'B': '0'}}
message type: kline
{'e': 'kline', 'E': 1519422633333, 's': 'BTCUSDT', 'k': {'t': 1519422600000, 'T': 1519422659999, 's': 'BTCUSDT', 'i': '1m', 'f': 21751516, 'L': 21752001, 'o': '9901.04000000', 'c': '9886.21000000', 'h': '9908.57000000', 'l': '9876.00000000', 'v': '64.59356000', 'n': 486, 'x': False, 'q': '638658.67653625', 'V': '28.65253800', 'Q': '283224.30476017', 'B': '0'}}
message type: kline
{'e': 'kline', 'E': 1519422634634, 's': 'BTCUSDT', 'k': {'t': 1519422600000, 'T': 1519422659999, 's': 'BTCUSDT', 'i': '1m', 'f': 21751516, 'L': 21752002, 'o': '9901.04000000', 'c': '9886.10000000', 'h': '9908.57000000', 'l': '9876.00000000', 'v': '64.60560800', 'n': 487, 'x': False, 'q': '638777.78426905', 'V': '28.65253800', 'Q': '283224.30476017', 'B': '0'}}
message type: kline
{'e': 'kline', 'E': 1519422636953, 's': 'BTCUSDT', 'k': {'t': 1519422600000, 'T': 1519422659999, 's': 'BTCUSDT', 'i': '1m', 'f': 21751516, 'L': 21752019, 'o': '9901.04000000', 'c': '9888.04000000', 'h': '9908.57000000', 'l': '9876.00000000', 'v': '65.32125200', 'n': 504, 'x': False, 'q': '645853.26091582', 'V': '28.99860800', 'Q': '286646.11360300', 'B': '0'}}
message type: kline
{'e': 'kline', 'E': 1519422638976, 's': 'BTCUSDT', 'k': {'t': 1519422600000, 'T': 1519422659999, 's': 'BTCUSDT', 'i': '1m', 'f': 21751516, 'L': 21752044, 'o': '9901.04000000', 'c': '9889.27000000', 'h': '9908.57000000', 'l': '9876.00000000', 'v': '66.41713000', 'n': 529, 'x': False, 'q': '656694.36519283', 'V': '29.80284000', 'Q': '294603.97445759', 'B': '0'}}
message type: kline
{'e': 'kline', 'E': 1519422641212, 's': 'BTCUSDT', 'k': {'t': 1519422600000, 'T': 1519422659999, 's': 'BTCUSDT', 'i': '1m', 'f': 21751516, 'L': 21752053, 'o': '9901.04000000', 'c': '9889.32000000', 'h': '9908.57000000', 'l': '9876.00000000', 'v': '66.72798600', 'n': 538, 'x': False, 'q': '659768.97291406', 'V': '29.88193900', 'Q': '295386.67243759', 'B': '0'}}
message type: kline
{'e': 'kline', 'E': 1519422643720, 's': 'BTCUSDT', 'k': {'t': 1519422600000, 'T': 1519422659999, 's': 'BTCUSDT', 'i': '1m', 'f': 21751516, 'L': 21752068, 'o': '9901.04000000', 'c': '9890.00000000', 'h': '9908.57000000', 'l': '9876.00000000', 'v': '67.66981900', 'n': 553, 'x': False, 'q': '669083.25447617', 'V': '29.89740500', 'Q': '295539.77887789', 'B': '0'}}

Most helpful comment

Look for["k"]["x"] == True which is the close for the candle size you requested. Each.

All 6 comments

Im dealing with the exact same problem right now with issue #195

Let me know if you figure out the usage of klines websocket please. I'm still scratching my head on how to collect it for certain intervals like 5m or 1h

build them as you go. The data comes approx every second, but it should be for that time period, updated with new values until the 'candle' is closed. So for a 1 minute Kline just keep updating your values for the time period above ( 'k': {'t': 1519422600000, 'T': 1519422659999, ) and then when the klines start coming in for the next minute move on to the next, something like this

        k = data['k']
        t_stamp = int(k['t'])

        if self.ref_frame != t_stamp:
            self.save_frame()           # update data storeage object, list, database etc...
            self.ref_frame = t_stamp    # ref-frame is the current minute at the exchange

@bilgecetin - t is kline start time, T is kline end time, i is the interval, so I'm guessing if we set interval=KLINE_INTERVAL_1MINUTE that we are setting it for a 1 minute candle, so t & T will not change until the candle is over.
I am also guessing that because we are looking at a stream of data for that candle (through the websocket) that those values within the nested dictionary {'k':{These values may be updated every second, until the candle/kline interval has finished}}.

I am also looking at doing something with the data stream, and wondering is we need to store selected values in an array, so [t, o, c, l, h, v] in order to process/ do calculations and use indicators etc...

Any progress that yourself or @Technorocker make, I would really be interested in.

@sammchardy
It would be really cool to have some examples of how to use the live stream data to calculate / display indicators, maybe using it with other libraries like TA-Lib for example. I know that you mentioned this on your blog https://sammchardy.github.io/ that you intended to do Subsequent posts will look at how to use pandas and TA-Lib.

Being able to do something with the data from python-binance is I guess what most people want to do, to be able to display & alert different indictors RSI, MCAD, KDJ etc.. and compose those indicators into signals we can read.

One simple alert I would like to do is to create an alert for bearish engulfing & bullish engulfing candlestick patterns. This is the main reason I began looking at python-binance.

I'l like to be able to look for RSI signals above 80 & below 20 etc...

Look for["k"]["x"] == True which is the close for the candle size you requested. Each.

Look for["k"]["x"] == True which is the close for the candle size you requested. Each.

Best solution ever! Thanks

Was this page helpful?
0 / 5 - 0 ratings