-
Notifications
You must be signed in to change notification settings - Fork 3
/
connect.py
executable file
·98 lines (85 loc) · 3.95 KB
/
connect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os, json, websocket, time, ssl
import _thread as thread
from RingBuffer import *
from AggregatedBook import AggregatedBookRequest, AggregatedBookType
from AggregatedBookAnalytics import *
from Book import BookRequest, BookType
from MarketRanking import MarketRankingRequest, MarketRankingType
from Quote import QuoteRequest, QuoteType
from QuoteTrade import QuoteTradeRequest, QuoteTradeType
from ResumeMarket import ResumeMarketListRequest, ResumeMarketType
###### reading configuration
HOME = os.path.dirname(os.path.abspath(__file__))
with open('config.json', "r") as config_json_file:
cfg = json.load(config_json_file)
###### buffers
#BUFFER_SIZE = 10*60*10 # ~10 min
#analytic_buffer=RingBuffer(BUFFER_SIZE)
market = "XBSP" # XBSP := Bovespa, XBMF := BM&F || https://www.onixs.biz/fix-dictionary/4.4/app_6_c.html
def on_message(ws, raw_message):
data = json.loads(raw_message)
if 'type' in data:
if data['type'] == 'QuoteType':
quote = QuoteType(data)
#quote.print()
print(quote.__dict__)
elif data['type'] == 'AggregatedBookType':
book = AggregatedBookType(data)
#book.print()
print(book.__dict__)
analytics = AggregatedBookAnalytics(book)
#analytic_buffer.append(analytics)
#if analytic_buffer.len() > BUFFER_SIZE/2:
# print("process ...")
#analytics.print()
print(analytics.__dict__)
elif data['type'] == 'BookSnapshotType':
book = BookType(data)
#book.print()
elif data['type'] == 'MarketRankingType':
mrt = MarketRankingType(data)
#mrt.print()
elif data['type'] == 'ResumeMarketType':
rmt = ResumeMarketType(data)
#rmt.print()
elif data['type'] == 'BusinessBookType':
qtt = QuoteTradeType(data)
print(qtt.__dict__)
#qtt.print()
#else:
#print(data)
def on_error(ws, error):
print(error)
def on_close(ws):
print("### closed ###")
req = []
for symbol in cfg["SYMBOLS"]:
req.append(QuoteTradeRequest(cfg["TKNWF"], symbol).to_json())
#req.append(MarketRankingRequest(cfg["TKNWF"], "bovespa").to_json())
#req.append(ResumeMarketListRequest(cfg["TKNWF"], "highList").to_json())
#req.append(BookRequest(cfg["TKNWF"], symbol).to_json())
#req.append(QuoteRequest(cfg["TKNWF"], symbol).to_json())
req.append(AggregatedBookRequest(cfg["TKNWF"], symbol).to_json())
#req.append({"token":cfg["TKNWF"],"module":"negotiation","service":"financialAccountInformationCompl","parameters":{"account":cfg["ACCOUNT"],"market":market,"dispatch":False,"history":True,"omsFilter":False}})
#req.append({"token":cfg["TKNWF"],"module":"negotiation","service":"position","parameters":{"account":cfg["ACCOUNT"],"market":market,"history":False,"dispatch":False,"openQtyFilter":0}})
#req.append({"token":cfg["TKNWF"],"module":"negotiation","service":"dailyOrder","parameters":{"account":cfg["ACCOUNT"],"market":market,"dispatch":False,"history":True}})
#req.append({"token":cfg["TKNWF"],"module":"negotiation","service":"cancelOrderReject","parameters":{"account":cfg["ACCOUNT"],"dispatch":False,"market":market}})
#print("requests sent to symbol:",symbol)
def on_open(ws):
def run(*args):
for i in range(len(req)):
time.sleep(1)
ws.send(json.dumps(req[i]))
#print("subscription request sent")
thread.start_new_thread(run, ())
if __name__ == "__main__":
#print("*** STARTING BTG HB DAYTRADE SUPPORT SYSTEM ***")
websocket.enableTrace(False)
ws = websocket.WebSocketApp(cfg["ENDPOINT"]+cfg["TKNWF"],
on_open = on_open,
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})