Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@udomsak
Created August 23, 2020 14:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save udomsak/866fd08191cb6464f818371605976425 to your computer and use it in GitHub Desktop.
Save udomsak/866fd08191cb6464f818371605976425 to your computer and use it in GitHub Desktop.
การเชื่อมต่อ กับ binance โดยใช้ WS ( WebSocket ) connect เพื่อการเชื่อมต่อแบบ Realtime
"""
Quest: https://www.facebook.com/groups/admin.py.dev/permalink/1437718479746684/
"""
from binance.client import Client
from binance.websockets import BinanceSocketManager
"""
เวลาทำงานกับ WSS ( Web Socket ให้ทำงานกับ API ตัวนี้ )
https://github.com/binance-exchange/binance-official-api-docs/blob/master/web-socket-streams.md
เอกสารของ python library python-binance
https://python-binance.readthedocs.io/en/latest/websockets.html
"""
api_key = ''
api_secret = ''
def process_message(msg):
"""
ฟังกชั่นนี้ จะเป็นตัว callback ของ message. เราจะดักเอา ข้ดมูลจาก variable msg จะไปทำอะไรต่อ ก็
แล้วแต่เรา
ความหมายของ แต่ละฟิลด์ที return มา
{
"e": "trade", # Event type
"E": 123456789, # Event time
"s": "BNBBTC", # Symbol
"t": 12345, # Trade ID
"p": "0.001", # Price
"q": "100", # Quantity
"b": 88, # Buyer order Id
"a": 50, # Seller order Id
"T": 123456785, # Trade time
"m": true, # Is the buyer the market maker?
"M": true # Ignore.
}
เอามาจาก
https://github.com/sammchardy/python-binance/blob/master/binance/websockets.py
:param msg: Any()
:return:
"""
print(msg)
if __name__ == '__main__':
client_instance = Client(api_key, api_secret)
client_web_socket = BinanceSocketManager(client_instance)
conn_key = client_web_socket.start_trade_socket('BNBBTC', process_message)
print("Staring Realtime connect")
client_web_socket.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment