Created
August 23, 2020 14:35
-
-
Save udomsak/866fd08191cb6464f818371605976425 to your computer and use it in GitHub Desktop.
การเชื่อมต่อ กับ binance โดยใช้ WS ( WebSocket ) connect เพื่อการเชื่อมต่อแบบ Realtime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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