forked from Chrisischris/trade-notifier-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbotUser.py
77 lines (61 loc) · 2.27 KB
/
botUser.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
import json
import xmltodict
import discord
from tda.auth import easy_client
from tda.streaming import StreamClient
from const import TOKEN_PATH, REDIRECT_URI, BOT_NICK
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from parsers import *
class BotUser():
def __init__(self, webhook, accountID, apiKey):
self._webhook = webhook
self._loginDataPath = TOKEN_PATH + accountID
self._accountID = accountID
self._apiKey = apiKey
@property
def webhook(self):
return self._webhook
@property
def accountID(self):
return self._accountID
@property
def apiKey(self):
return self._apiKey
async def stream_account_activity(self):
client = easy_client(
api_key=self._apiKey,
redirect_uri=REDIRECT_URI,
token_path=self._loginDataPath,
webdriver_func=lambda: webdriver.Chrome(ChromeDriverManager().install()))
stream_client = StreamClient(client, account_id=int(self._accountID))
await self.read_stream(self.send_message, stream_client)
async def read_stream(self, handler, stream_client):
await stream_client.login()
await stream_client.quality_of_service(StreamClient.QOSLevel.EXPRESS)
await stream_client.account_activity_sub()
stream_client.add_account_activity_handler(handler)
while True:
await stream_client.handle_message()
def send_message(self, msg):
print('RAW TDA Response:\n', json.dumps(msg, indent=2))
for msg in msg['content']:
msgType = msg['MESSAGE_TYPE']
msgData = msg['MESSAGE_DATA']
if msgData:
parsedDict = xmltodict.parse(msgData)
rawJsonMSG = '```json\n' + json.dumps(parsedDict, indent=2) + '\n```'
msgToSend = ''
if msgType == 'OrderEntryRequest':
# Disabled this for now, needs more formatted info to be useful
# msgToSend = orderEntryRequestFormatter(parsedDict)
break
elif msgType == 'OrderFill':
msgToSend = orderFillFormatter(parsedDict)
else:
return
print('Parsed Response JSON:\n', rawJsonMSG)
try:
self._webhook.send(msgToSend, username=BOT_NICK)
except discord.errors.HTTPException as e:
print('Discord HTTPException: ', e, '\nMsg Attempt: ', msgToSend)