forked from Chrisischris/trade-notifier-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsers.py
95 lines (66 loc) · 2.83 KB
/
parsers.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
from datetime import datetime
from dateutil.parser import parse
def orderEntryRequestFormatter(msgDict: dict):
msgDict = msgDict.get('OrderEntryRequestMessage').get('Order')
msg = '**'
sym = msgDict.get('Security').get('Symbol')
optionTDAString = None
if '_' in sym:
symSplit = sym.split('_')
msg += '$' + symSplit[0] + ' '
msg += symSplit[1][0:2] + '/' + symSplit[1][2:4] + '/' + symSplit[1][4:6] + ' ' + symSplit[1][6:]
optionTDAString = '.' + symSplit[0] + symSplit[1][4:6] + symSplit[1][0:2] + symSplit[1][2:4] + symSplit[1][6:]
else:
msg += '$' + sym
msg += ' (' + msgDict.get('Security').get('SecurityType')
if msgDict.get('Security').get('SymbolUnderlying'):
msg += ' ' + msgDict.get('Security').get('SymbolUnderlying')
msg += ') {Order Requested}** - '
if optionTDAString:
msg += '(' + optionTDAString + ') '
msg += datetime.fromisoformat(parse(msgDict.get('OrderEnteredDateTime')).isoformat()).astimezone().strftime("%m-%d-%Y %H:%M:%S %Z")
msg += '\n```diff\n'
quantity = msgDict.get('OriginalQuantity')
if msgDict.get('OrderInstructions') == 'Buy':
msg += '+' + quantity + ' Buy'
if msgDict.get('OrderInstructions') == 'Sell':
msg += '-' + quantity + ' Sell'
if optionTDAString:
openClose = msgDict.get('OpenClose')
msg += ' to ' + openClose
if msgDict.get('OrderType') == 'Limit':
price = msgDict.get('OrderPricing').get('Limit')
msg += ' at ' + price
msg += '\n```'
return msg
def orderFillFormatter(msgDict: dict):
order = msgDict.get('OrderFillMessage').get('Order')
execInfo = msgDict.get('OrderFillMessage').get('ExecutionInformation')
msg = '**'
sym = order.get('Security').get('Symbol')
optionTDAString = None
if '_' in sym:
symSplit = sym.split('_')
msg += '$' + symSplit[0] + ' '
msg += symSplit[1][0:2] + '/' + symSplit[1][2:4] + '/' + symSplit[1][4:6] + ' ' + symSplit[1][6:]
optionTDAString = '.' + symSplit[0] + symSplit[1][4:6] + symSplit[1][0:2] + symSplit[1][2:4] + symSplit[1][6:]
else:
msg += '$' + sym
msg += ' (' + order.get('Security').get('SecurityType')
if order.get('Security').get('SymbolUnderlying'):
msg += ' ' + order.get('Security').get('SymbolUnderlying')
msg += ') <<<Order Executed>>> ** - '
if optionTDAString:
msg += '(' + optionTDAString + ') '
msg += datetime.fromisoformat(parse(execInfo.get('Timestamp')).isoformat()).astimezone().strftime("%m/%d/%Y %H:%M:%S %Z")
msg += '\n```diff\n'
quantity = execInfo.get('Quantity')
if order.get('OrderInstructions') == 'Buy':
msg += '+' + quantity + ' BOT - ' + execInfo.get('ExecutionPrice')
if order.get('OrderInstructions') == 'Sell':
msg += '-' + quantity + ' SOLD - ' + execInfo.get('ExecutionPrice')
if optionTDAString:
openClose = order.get('OpenClose')
msg += ' to ' + openClose
msg += '\n```'
return msg