Skip to content

Instantly share code, notes, and snippets.

@icehongssii
Created January 26, 2019 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icehongssii/66f0292c09b1a0da872fcc6251ebc11c to your computer and use it in GitHub Desktop.
Save icehongssii/66f0292c09b1a0da872fcc6251ebc11c to your computer and use it in GitHub Desktop.
import websocket
import requests
import json
# 중요한 값은 상수사용합니다.
SYMBOL_LIST_ENDPOINT = "https://www.bitmex.com/api/v1/instrument/active"
ENDPOINT = 'wss://www.bitmex.com/realtime'
def sendRequest(url):
res = requests.get(url)
contents = res.content.decode('utf-8')
# python2에서는 contents = res.content만 적어도 됩니다.
# python2에서는 res.content가 string으로 오지만
# python3에서는 byte로 오기 때문에 디코딩이 필요합니다.
# 대부분의 api는 json으로 반환되기 때문에 곧바로 json 라이브러리를 이용해 로드합니다
json_contents = json.loads(contents)
return json_contents
def getBitmexSymbolList(contents):
symbol_list = [
pair['symbol']
for pair in contents
]
# contents안에 들어있는 각각의 아이템들 중에서
# 키가 symbol인 것들만 리스트에 넣겠다는 뜻
formatted_list = [ 'tradeBin1m:{}'.format(symbol) for symbol in symbol_list]
# {"op":"subscribe", "args" : []}의 형식이었죠? args에 formatted_list를
# 웹서버로 보내주면 bitmex에서 제공하는 모든 symbol에 대한 OHLCV를 얻을 수 있습니다.
return {"op":"subscribe", "args" : formatted_list}
def on_message(ws, message):
print(message)
def on_error(ws, error):
print(error)
ws.on_close(ws)
def on_close(ws):
print("### closed ###")
ws.close()
def on_open(ws):
print("### open ###")
contents = sendRequest(SYMBOL_LIST_ENDPOINT)
symbols = getBitmexSymbolList(contents)
ws.send(json.dumps(symbols))
def run(endpoint):
websocket.enableTrace(True)
ws = websocket.WebSocketApp(endpoint,
on_open = on_open,
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.run_forever()
if __name__ == "__main__":
run(ENDPOINT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment