Hey experts.
I seem to get incorrect data from time to time. From the testnet websocket wss://stream.binancefuture.com I get data that isn’t even close to what I see on 28556.8 | BTCUSDT USDⓈ-Margined Perpetual | Binance Futures. So my first question is, am I using the correct endpoints? Since I’m receiving user stream updates on that stream when I make a trade on the testnet website, I must be using the correct enpoints, right?
Anyway, I decided to use the main net endpoint for data the main net websocket. Data is much more inline with the main net UI but still, from time to time I get incorrect "low"s through the websocket as I logged every message received from the websocket. For getting data from the rest api, these are the parameters:
/fapi/v1/continuousKlines?pair=btcusdt&contractType=PERPETUAL&interval=1m&limit=100&startTime=1680669180000&endTime=1680675120000
I think posting all of my code here is not relevant since I’m just giving you the raw data returned and a screenshot from what I see on the binance UI. In my system I get the first 100 klines from the rest api, and update this data set whenever the websocket returns the first new candle. Then I take the previous candle(= last candle received through websocket) and add it to the full data set.
I’m using the official binance python library:
self.client_ws_data.continuous_kline(
pair=‘btcusdt’,
id=1,
contractType=‘perpetual’,
interval=‘1m’,
callback=update_market,
)
This is the payload that is sent to the server: b’{“method”: “SUBSCRIBE”, “params”: [“btcusdt_perpetual@continuousKline_1m”], “id”: 3}’
Here is the raw data received from the websocket, as from kline 1680663120000. I’m receiving an updated candle every second but I’m only posting the last received kline udpate received from the websocket:
{“E”:1680665188181,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899588134,“Q”:“1851.26940”,“T”:1680665219999,“V”:“0.065”,“c”:“28475.50”,“f”:30899583211,“h”:“28482.50”,“i”:“1m”,“l”:“28471.50”,“n”:17,“o”:“28479.70”,“q”:“77586.44980”,“t”:1680665160000,“v”:“2.725”,“x”:false},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680665162052,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899582469,“Q”:“1713910.11440”,“T”:1680665159999,“V”:“60.164”,“c”:“28482.50”,“f”:30899571245,“h”:“28487.70”,“i”:“1m”,“l”:“28477.90”,“n”:27,“o”:“28483.70”,“q”:“2386524.20820”,“t”:1680665100000,“v”:“83.781”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680665102051,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899570343,“Q”:“4509715.36700”,“T”:1680665099999,“V”:“158.333”,“c”:“28483.70”,“f”:30899558486,“h”:“28491.00”,“i”:“1m”,“l”:“28477.90”,“n”:32,“o”:“28488.00”,“q”:“6187576.15770”,“t”:1680665040000,“v”:“217.243”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680665040475,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899557105,“Q”:“386665.06350”,“T”:1680665039999,“V”:“13.571”,“c”:“28488.00”,“f”:30899546896,“h”:“28496.50”,“i”:“1m”,“l”:“28485.40”,“n”:32,“o”:“28492.70”,“q”:“1465991.31290”,“t”:1680664980000,“v”:“51.456”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680664982043,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899546349,“Q”:“2773783.14540”,“T”:1680664979999,“V”:“97.340”,“c”:“28504.50”,“f”:30899534843,“h”:“28504.50”,“i”:“1m”,“l”:“28482.60”,“n”:36,“o”:“28486.10”,“q”:“4897853.31360”,“t”:1680664920000,“v”:“171.905”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680664921281,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899534344,“Q”:“1769582.54390”,“T”:1680664919999,“V”:“62.123”,“c”:“28483.50”,“f”:30899522488,“h”:“28504.50”,“i”:“1m”,“l”:“28480.50”,“n”:29,“o”:“28504.50”,“q”:“2663401.76920”,“t”:1680664860000,“v”:“93.505”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680664860051,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899522469,“Q”:“2126146.21220”,“T”:1680664859999,“V”:“74.679”,“c”:“28504.50”,“f”:30899510432,“h”:“28504.50”,“i”:“1m”,“l”:“28359.00”,“n”:68,“o”:“28470.50”,“q”:“3884053.04530”,“t”:1680664800000,“v”:“136.449”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680664802047,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899509377,“Q”:“2242000.94740”,“T”:1680664799999,“V”:“78.688”,“c”:“28470.50”,“f”:30899497810,“h”:“28504.50”,“i”:“1m”,“l”:“28466.50”,“n”:40,“o”:“28491.10”,“q”:“4224878.08160”,“t”:1680664740000,“v”:“148.334”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680664742046,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899496711,“Q”:“1917253.48880”,“T”:1680664739999,“V”:“67.305”,“c”:“28490.50”,“f”:30899484888,“h”:“28491.10”,“i”:“1m”,“l”:“28477.20”,“n”:30,“o”:“28482.00”,“q”:“2931183.61490”,“t”:1680664680000,“v”:“102.902”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680664680817,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899484351,“Q”:“1044400.80250”,“T”:1680664679999,“V”:“36.644”,“c”:“28485.90”,“f”:30899473184,“h”:“28504.50”,“i”:“1m”,“l”:“28485.00”,“n”:38,“o”:“28499.90”,“q”:“3022076.42730”,“t”:1680664620000,“v”:“106.056”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680664622033,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899470029,“Q”:“2013967.77450”,“T”:1680664619999,“V”:“70.678”,“c”:“28495.50”,“f”:30899460515,“h”:“28504.50”,“i”:“1m”,“l”:“28486.00”,“n”:26,“o”:“28490.00”,“q”:“3632823.30000”,“t”:1680664560000,“v”:“127.505”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680664560810,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899460181,“Q”:“36965.19950”,“T”:1680664559999,“V”:“1.297”,“c”:“28490.00”,“f”:30899448760,“h”:“28504.50”,“i”:“1m”,“l”:“28485.00”,“n”:16,“o”:“28486.00”,“q”:“1728675.09110”,“t”:1680664500000,“v”:“60.686”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680664501109,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899448655,“Q”:“256787.54640”,“T”:1680664499999,“V”:“9.008”,“c”:“28489.00”,“f”:30899436639,“h”:“28534.50”,“i”:“1m”,“l”:“28485.00”,“n”:38,“o”:“28520.20”,“q”:“3101333.73210”,“t”:1680664440000,“v”:“108.774”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680664440963,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899436295,“Q”:“457507.98790”,“T”:1680664439999,“V”:“16.039”,“c”:“28520.20”,“f”:30899424700,“h”:“28534.50”,“i”:“1m”,“l”:“28511.50”,“n”:24,“o”:“28514.60”,“q”:“2231332.76820”,“t”:1680664380000,“v”:“78.243”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680664382041,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899424434,“Q”:“1315677.13800”,“T”:1680664379999,“V”:“46.140”,“c”:“28514.70”,“f”:30899413611,“h”:“28517.30”,“i”:“1m”,“l”:“28501.50”,“n”:30,“o”:“28508.80”,“q”:“1619750.04700”,“t”:1680664320000,“v”:“56.806”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680664322045,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899412781,“Q”:“172174.77380”,“T”:1680664319999,“V”:“6.038”,“c”:“28512.80”,“f”:30899402748,“h”:“28520.20”,“i”:“1m”,“l”:“28508.80”,“n”:29,“o”:“28516.20”,“q”:“1407922.65910”,“t”:1680664260000,“v”:“49.374”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680664262039,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899401062,“Q”:“2082491.05990”,“T”:1680664259999,“V”:“73.056”,“c”:“28520.20”,“f”:30899389619,“h”:“28530.90”,“i”:“1m”,“l”:“28336.30”,“n”:70,“o”:“28510.00”,“q”:“2856101.89770”,“t”:1680664200000,“v”:“100.220”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680664201381,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899389269,“Q”:“1231787.44650”,“T”:1680664199999,“V”:“43.194”,“c”:“28514.00”,“f”:30899377891,“h”:“28519.60”,“i”:“1m”,“l”:“28500.00”,“n”:50,“o”:“28518.50”,“q”:“1824890.75660”,“t”:1680664140000,“v”:“63.996”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680664141137,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899376808,“Q”:“32586.20940”,“T”:1680664139999,“V”:“1.142”,“c”:“28522.50”,“f”:30899366060,“h”:“28534.50”,“i”:“1m”,“l”:“28518.50”,“n”:32,“o”:“28534.20”,“q”:“734303.15580”,“t”:1680664080000,“v”:“25.736”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680664081085,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899365179,“Q”:“1318474.24390”,“T”:1680664079999,“V”:“46.185”,“c”:“28530.50”,“f”:30899354351,“h”:“28559.20”,“i”:“1m”,“l”:“28528.10”,“n”:42,“o”:“28542.40”,“q”:“2712592.46790”,“t”:1680664020000,“v”:“95.048”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680664020859,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899353563,“Q”:“574126.11140”,“T”:1680664019999,“V”:“20.112”,“c”:“28542.80”,“f”:30899343215,“h”:“28548.70”,“i”:“1m”,“l”:“28534.10”,“n”:29,“o”:“28545.00”,“q”:“2747649.21120”,“t”:1680663960000,“v”:“96.264”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680663962051,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899341276,“Q”:“1273678.41760”,“T”:1680663959999,“V”:“44.620”,“c”:“28548.70”,“f”:30899332099,“h”:“28549.30”,“i”:“1m”,“l”:“28540.90”,“n”:30,“o”:“28541.30”,“q”:“1500867.42630”,“t”:1680663900000,“v”:“52.580”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680663902058,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899330902,“Q”:“436628.19300”,“T”:1680663899999,“V”:“15.296”,“c”:“28542.90”,“f”:30899320468,“h”:“28549.30”,“i”:“1m”,“l”:“28535.50”,“n”:18,“o”:“28539.50”,“q”:“2095028.24890”,“t”:1680663840000,“v”:“73.399”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680663841079,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899319490,“Q”:“2124694.67600”,“T”:1680663839999,“V”:“74.434”,“c”:“28535.50”,“f”:30899308825,“h”:“28549.30”,“i”:“1m”,“l”:“28535.50”,“n”:31,“o”:“28545.50”,“q”:“5207341.57090”,“t”:1680663780000,“v”:“182.434”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680663780661,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899307608,“Q”:“999376.18630”,“T”:1680663779999,“V”:“35.005”,“c”:“28550.90”,“f”:30899297031,“h”:“28559.20”,“i”:“1m”,“l”:“28528.10”,“n”:42,“o”:“28559.20”,“q”:“2586818.78070”,“t”:1680663720000,“v”:“90.598”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680663720701,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899295864,“Q”:“523224.71040”,“T”:1680663719999,“V”:“18.317”,“c”:“28559.20”,“f”:30899285339,“h”:“28577.60”,“i”:“1m”,“l”:“28557.70”,“n”:37,“o”:“28577.60”,“q”:“2173983.42000”,“t”:1680663660000,“v”:“76.116”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680663661119,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899284701,“Q”:“1189267.95800”,“T”:1680663659999,“V”:“41.622”,“c”:“28573.60”,“f”:30899272777,“h”:“28587.30”,“i”:“1m”,“l”:“28561.40”,“n”:40,“o”:“28569.30”,“q”:“1884810.40540”,“t”:1680663600000,“v”:“65.960”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680663600052,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899272658,“Q”:“1315760.35150”,“T”:1680663599999,“V”:“46.071”,“c”:“28569.30”,“f”:30899261010,“h”:“28569.30”,“i”:“1m”,“l”:“28553.70”,“n”:38,“o”:“28560.30”,“q”:“2694833.14410”,“t”:1680663540000,“v”:“94.362”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680663542043,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899259118,“Q”:“1235826.30310”,“T”:1680663539999,“V”:“43.273”,“c”:“28557.50”,“f”:30899248172,“h”:“28561.70”,“i”:“1m”,“l”:“28556.00”,“n”:26,“o”:“28560.00”,“q”:“2231896.40600”,“t”:1680663480000,“v”:“78.153”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680663480573,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899247526,“Q”:“10152308.68080”,“T”:1680663479999,“V”:“355.719”,“c”:“28556.00”,“f”:30899235244,“h”:“28557.70”,“i”:“1m”,“l”:“28528.10”,“n”:44,“o”:“28540.40”,“q”:“10771847.74010”,“t”:1680663420000,“v”:“377.424”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680663420632,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899234195,“Q”:“3194053.68980”,“T”:1680663419999,“V”:“111.906”,“c”:“28537.20”,“f”:30899222637,“h”:“28562.10”,“i”:“1m”,“l”:“28535.00”,“n”:40,“o”:“28539.00”,“q”:“3928538.32900”,“t”:1680663360000,“v”:“137.642”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680663360693,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899221159,“Q”:“3425459.11220”,“T”:1680663359999,“V”:“120.042”,“c”:“28535.00”,“f”:30899209836,“h”:“28554.80”,“i”:“1m”,“l”:“28347.00”,“n”:48,“o”:“28525.50”,“q”:“4152030.34380”,“t”:1680663300000,“v”:“145.504”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680663208294,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899190211,“Q”:“686860.17230”,“T”:1680663239999,“V”:“24.041”,“c”:“28546.10”,“f”:30899184663,“h”:“28571.10”,“i”:“1m”,“l”:“28336.30”,“n”:63,“o”:“28570.90”,“q”:“1848417.29070”,“t”:1680663180000,“v”:“64.857”,“x”:false},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680663138524,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899175721,“Q”:“620464.41020”,“T”:1680663179999,“V”:“21.715”,“c”:“28571.10”,“f”:30899171932,“h”:“28588.00”,“i”:“1m”,“l”:“28570.70”,“n”:20,“o”:“28588.00”,“q”:“623721.63800”,“t”:1680663120000,“v”:“21.829”,“x”:false},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
EDIT: As I just made this account, I can only upload 1 image per post, I added the plot of this data in an answer to this post.
Here is a screenshot from binance futures UI:
As you can see in the data, there are 3 abnormal lows going way down below 28400 where this clearly does not happen in the UI.
Here is the data that I received and is incorrect, check out the “l” values:
{“E”:1680664860051,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899522469,“Q”:“2126146.21220”,“T”:1680664859999,“V”:“74.679”,“c”:“28504.50”,“f”:30899510432,“h”:“28504.50”,“i”:“1m”,“l”:“28359.00”,“n”:68,“o”:“28470.50”,“q”:“3884053.04530”,“t”:1680664800000,“v”:“136.449”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680664262039,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899401062,“Q”:“2082491.05990”,“T”:1680664259999,“V”:“73.056”,“c”:“28520.20”,“f”:30899389619,“h”:“28530.90”,“i”:“1m”,“l”:“28336.30”,“n”:70,“o”:“28510.00”,“q”:“2856101.89770”,“t”:1680664200000,“v”:“100.220”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680663360693,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899221159,“Q”:“3425459.11220”,“T”:1680663359999,“V”:“120.042”,“c”:“28535.00”,“f”:30899209836,“h”:“28554.80”,“i”:“1m”,“l”:“28347.00”,“n”:48,“o”:“28525.50”,“q”:“4152030.34380”,“t”:1680663300000,“v”:“145.504”,“x”:true},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
{“E”:1680663208294,“e”:“continuous_kline”,“k”:{“B”:“0”,“L”:30899190211,“Q”:“686860.17230”,“T”:1680663239999,“V”:“24.041”,“c”:“28546.10”,“f”:30899184663,“h”:“28571.10”,“i”:“1m”,“l”:“28336.30”,“n”:63,“o”:“28570.90”,“q”:“1848417.29070”,“t”:1680663180000,“v”:“64.857”,“x”:false},“ct”:“PERPETUAL”,“ps”:“BTCUSDT”}
If anyone has any idea of what I’m doing wrong, please let me know. If I can’t really rely on the data coming from the websocket, I’m not very confident in going live with my bots…
Thanks in advance for your help!
cheers