-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount.py
198 lines (162 loc) · 5.49 KB
/
account.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
from orders import Equity, Option
class Account:
"""
Class to represent tda account with current balances, status
and opened positions. Class designed to be nested inside
of client class.
"""
def __init__(self, account_id=''):
self._account_id = account_id
self.type = None
self.roundTrips = None
self.isDayTrader = None
self.isClosingOnlyRestricted = None
self.initialBalances = {}
self.currentBalances = {}
self.projectedBalances = {}
self.positions = Positions()
self.orderStategies = []
@property
def account_id(self):
return self._account_id
@account_id.setter
def account_id(self, account_id):
if self._account_id == '':
self._account_id = account_id
else:
raise Exception("Account id already is already assigned")
def update(self, response):
"""
Take raw response from api and update objects
"""
response = response['securitiesAccount']
if response.get('positions', None) is not None:
positions = response.pop('positions', None)
self.positions.parse(positions)
self.__dict__.update(response)
class Positions(dict):
"""
subclass of dict as a container to store the accounts currently opened
positions. Each item in dict in another object storing data for
the given position.
"""
def __init__(self):
pass
def __repr__(self):
return f"<Class: '{self.__class__.__name__}', Number Of Positions: '{len(self)}'>"
def parse(self, positions):
"""
Parse list object recieved by client into corresponding objects stored
in this container class
"""
symbols = [position['instrument']['symbol'] for position in positions]
for symbol in self.keys():
if symbol not in symbols:
self.pop(symbol)
for i, symbol in enumerate(symbols):
assetType = positions[i]['instrument']['assetType']
if assetType == 'EQUITY':
self[symbol] = EquityPosition(positions[i])
elif assetType == 'OPTION':
self[symbol] = OptionsPosition(positions[i])
class Position:
"""
Stores data for opened position on account
"""
def __init__(self, position):
instrument = position.pop('instrument')
self.__dict__.update({kw : position[kw] for kw in position.keys()})
self.__dict__.update({kw : instrument[kw] for kw in instrument.keys()})
def __repr__(self):
return f"<Class: '{self.__class__.__name__}', "\
f"Asset: '{self.assetType}', "\
f"Ticket: '{self.symbol}', "\
f"Average: '${self.averagePrice}', "\
f"Quantity: '{max(self.longQuantity,self.shortQuantity)}', "\
f"Market Value: '${self.marketValue}'>"
def sub(self):
if self.longQuantity:
return -self.longQuantity
elif self.shortQuantity:
return self.shortQuantity
def add(self):
if self.longQuantity:
return self.longQuantity
elif self.shortQuantity:
return -self.shortQuantity
class EquityPosition(Position):
"""
Subclass of position, stores data for opened equity position on account
contains methods to modify existing position
"""
def __init__(self, position):
super().__init__(position)
def form_close_order(self, **kwargs):
"""
Form order object that will close this position
"""
order = Equity(**kwargs)
order.add_leg(
symbol=self.symbol,
quantity=self.sub(),
closing=True
)
return order
def form_add_order(self, percent, **kwargs):
"""
Form order object that will add to this position
"""
order = Equity(**kwargs)
order.add_leg(
symbol=self.symbol,
quantity=int(self.add()*percent/100)
)
return order
def form_sub_order(self, percent, **kwargs):
"""
Form order object that will reduce this position
"""
order = Equity(**kwargs)
order.add_leg(
symbol=self.symbol,
quantity=int(self.sub()*percent/100)
)
return order
class OptionsPosition(Position):
"""
Subclass of position, stores data for opened options position on account
contains methods to modify existing position
"""
def __init__(self, position):
super().__init__(position)
def form_close_order(self, **kwargs):
"""
Form order object that will close this position
"""
order = Option(**kwargs)
order.add_leg(
symbol=self.symbol,
quantity=self.sub(),
closing=True
)
return order
def form_add_order(self, percent, **kwargs):
"""
Form order object that will add to this position
"""
order = Option(**kwargs)
order.add_leg(
symbol=self.symbol,
quantity=int(self.add()*percent/100)
)
return order
def form_sub_order(self, percent, **kwargs):
"""
Form order object that will reduce this position
"""
order = Option(**kwargs)
order.add_leg(
symbol=self.symbol,
quantity=int(self.sub()*percent/100)
)
return order