Skip to content

Instantly share code, notes, and snippets.

@igotit-anything
Last active July 5, 2022 14:10
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 igotit-anything/d49faba4357607fc1d729cb378efd650 to your computer and use it in GitHub Desktop.
Save igotit-anything/d49faba4357607fc1d729cb378efd650 to your computer and use it in GitHub Desktop.
bybit_websocket_execution_example.py
def processing_execution(data_dic_one):
leaves_qty = dic_json.get('leaves_qty')
if leaves_qty > 0 :
return
## 여기 이하 전량체결 이벤트일 때 수행됨.
## 봇에서 주문 낸 지정가 주문(지정가 의도했으나 시장가 체결된것 포함) 이 전량체결 시점임.
_symbol = dic_json.get('symbol')
_order_qty = dic_json.get('order_qty') # 주문수량. = (총체결수량). 인버스 종목은 주문수량 최소단위 1이므로 정수로 처리해도 됨.
_price_str = dic_json.get('price') # 체결가격. 문자열로 기록되어있음.
_side = dic_json.get('side') # Buy 혹은 Sell
is_maker = dic_json.get('is_maker') # true 이면 지정가 주문, false 이면 시장가 주문.
if is_maker == False: # 시장가 주문으로 체결된 이벤트임. 봇에서 지정가 주문의도하였으나, 시장가 주문 체결된것임.
print('!!!Bot try order by limit but filled market order. symbol = ' + _symbol )
print('order all qty were filled symbol = ' + _symbol + '. qty = ' + str(_order_qty) + '. price = ' + _price_str + '. side = ' + _side)
#여기서 체결완료된것 대응처리 수행.
if _side == 'Buy': # 매수 주문한것이 체결완료되었음.
# 신규 매도 주문 송신.
elif _side == 'Sell' : # 매도 주문한것이 체결완료되었음.
# 신규 매수 주문 송신.
# 웹소켓 수신데이터 분지 처리기. 메인.
def processing_all_ws_received(str_json):
#print('received data from bybit: ' + str_json)
if '"ret_msg":"pong"' in str_json: # ping 응답 수신.
processing_response_ping(str_json)
elif '"success":true' in str_json : # subscribe 한것의 응답이 이 단계에서 수신되는 경우임. 아래 topic 오는 구문에는 "success":true 없음.
print('response success for request : ' + str_json)
elif '"topic":"' in str_json: # subscribe 한것들의 이벤트 수신. 모두 공통적으로 리스트형식의 data 키가 있음.
dic_json = json.loads(str_json);
topic = dic_json.get('topic',0)
if topic == 0 :
print('!!! the key topic not found:processing_all_ws_received. full string received= '+ str_json)
return
data_list = dic_json.get('data',0) # topic 키가 있는 문자열에는 공통적으로 리스트 형식의 data 있음.
if data_list == 0 :
print('!!! the key data not found:processing_all_ws_received. full string received= '+ str_json)
return
### 요청한 종목에 대해서만 데이터 수신되는것 2개 . trade, klineV2
if '"topic":"trade.' in str_json: # 시장 체결틱 수신. subscribe trade 에 의하여 수신되는것. topic 에 종목이름이 부착되어있음. 예 . trade.BTCUSD
#print('received trade. string received = ' + str_json )
processing_trade(data_list)
elif '"topic":"klineV2.1.' in str_json: # 캔들 1분봉. topic 에 캔들 타임프레임과 종목이름 부착되어있음. 예 . klineV2.1.BTCUSD
#print('received klineV2.1. string received = ' + str_json )
processing_klineV2_1minute(data_list)
## 모든 인버스 종목에 대하여 데이터 수신되는것. 심볼 지정하여 요청못함. 심볼 정보는 list_data 내의 symbol 키값 확인해야 알 수 있음.
else :
## data 리스트 룹 돌리면서 처리대상 심볼에 대해서만 처리하도록 하며, 각 세부 처리기는 리스트요소 1개 단위로 처리 호출함.
for data_dic_one in data_list:
symbol = data_dic_one.get('symbol') # 심볼 받았다.
if SYMBOL_DIC.get(symbol,0) == 0: # 본 코드에서 처리하기로 지정된 심볼이 아닌 경우. 스킵처리임
continue # 아래 부분 실행되지 않고 스킵하는것임.
if topic == 'order': #if '"topic":"order' in str_json:
#print('received order. string received = ' + str_json )
processing_order(data_dic_one)
elif topic == 'execution': #elif '"topic":"execution' in str_json:
#print('received execution. string received = ' + str_json )
processing_execution(data_dic_one)
elif topic == 'position': #elif '"topic":"position' in str_json:
#print('received position. string received = ' + str_json )
processing_position(data_dic_one)
#end of for data_dic_one in data_list:
else:
print('!!! not supporting type in processing_all_ws_received. full string received = ' + str_json)
async def my_loop_WebSocket_usdt_private_bybit():
중략...
# 필요한 실시간 정보들 요청.
await ws_usdt_private.send('{"op": "subscribe", "args": ["execution"]}');
중략...
while True:
data_rcv_strjson = await ws_usdt_private.recv()
#print('received data from bybit: ' + data_rcv_strjson)
processing_all_ws_received(data_rcv_strjson)
@igotit-anything
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment