-
Notifications
You must be signed in to change notification settings - Fork 0
/
TDAccount.py
204 lines (170 loc) · 6.21 KB
/
TDAccount.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
199
200
201
202
203
204
"""A package containing the Account, Trade, and Position classes"""
import pickle
import os
class Account:
"""An Account is just a holder of a list of positions and trades"""
def __init__(self, username, positions=[], trades=[]):
self.username = username
self.positions = positions
self.trades = trades
def __str__(self):
return 'Account Holder: ' + self.username + '\nPositions: ' + str(self.positions) + '\nTrades: ' + str(self.trades)
def get_username(self):
return self.username
def set_username(self, user):
self.username = user
def add_position(self, position):
self.positions.append(position)
def get_positions(self):
return self.positions
def get_trades(self):
return self.trades
def add_trade(self, trade):
self.trades.append(trade)
def remove_position(self, position):
self.positions.remove(position)
def remove_trade(self, trade):
self.trades.remove(trade)
def get_position(self, ticker):
for position in self.positions:
if position.ticker == ticker:
return position
def save_self(self, directory):
path = directory + '/' + self.username
try:
pickle.dump(self, open(path +'.act', 'wb'))
except:
os.mkdir(path.split('/')[0])
pickle.dump(self, open(path +'.act', 'wb'))
def view_feilds(self):
return [self.username, self.positions, self.trades]
@staticmethod
def load_account(directory, username):
path = directory + '/' + username + '.act'
try:
print('Successfull loading of account')
return 200, pickle.load(open(path, 'rb'))
except Exception as e:
print(e)
return e, None
@staticmethod
def delete_account(directory, username):
path = directory + '/' + username +'.act'
os.remove(path)
@staticmethod
def get_accounts(directory):
path = directory + '/'
accounts = os.listdir(path)
true_accounts = []
for account in accounts:
if '.act' in account:
true_accounts.append(account)
return true_accounts
class Trade():
def __init__(self, opening_position, closing_position):
self.opening_position = opening_position
self.closing_position = closing_position
def __str__(self):
return str(self.opening_position) + '-' + str(self.closing_position)
@staticmethod
def load_position(directory, opening_position, closing_position):
path = directory + '/'+ str(opening_position) + '-' + str(closing_position) + '.pos'
try:
return pickle.load(open(path, 'rb'))
except:
return '404: Error, Account not found.'
def save_self(self, directory):
path = directory + '/' + str(self)
try:
pickle.dump(self, open(path+'.pos', 'wb'))
except:
os.mkdir(path.split('/')[0])
pickle.dump(self, open(path +'.pos', 'wb'))
class Position():
def __init__(self, ticker, date, side, strike, price, symbol, closing_trade, short_trade, executed, trader, channel, time):
self.ticker = ticker
self.date = date
self.side = side
self.strike = strike
self.price = price
self.symbol = symbol
self.closing_trade = closing_trade
self.short_trade = short_trade
self.executed = executed
self.trader = trader
self.channel = channel
self.time = time
def __str__(self):
return str(self.ticker) + '_' + str(self.date) + '_' + str(self.trader) + '_' + self.time.strftime("%m/%d/%Y-%H:%M:%S")
def human_string(self):
return str(self.ticker) + ' ' + str(self.strike) + ' ' + str(self.side) + ' ' + str(self.date)
@staticmethod
def load_position(directory, ticker, date, trader):
path = directory + '/'+ str(ticker) + '_' + str(date) + '_' + str(trader) + '.pos'
try:
return pickle.load(open(path, 'rb'))
except:
return '404: Error, Account not found.'
def save_self(self, directory):
path = directory + '/' + str(self)
try:
pickle.dump(self, open(path+'.pos', 'wb'))
except:
os.mkdir(path.split('/')[0])
pickle.dump(self, open(path +'.pos', 'wb'))
def get_symbol(self):
return self.symbol
def get_short(self):
return self.short_trade
def get_executed(self):
return self.executed
def get_ticker(self):
return self.ticker
def set_ticker(self, ticker):
self.ticker = ticker
def get_date(self):
return self.date
def set_date(self, date):
self.date = date
def get_time(self):
return self.time
def set_time(self, time):
self.time = time
def get_side(self):
return self.side
def set_side(self, side):
self.side = side
def get_strike(self):
return self.strike
def set_strike(self, strike):
self.strike = strike
def get_price(self):
return self.price
def set_price(self, price):
self.price = price
def copy(self, ticker=None, date=None, side=None, strike=None, price=None, symbol=None, closing_trade=None, short_trade=None, executed=None, trader=None, channel=None, time=None):
if ticker == None:
ticker = self.ticker
if date == None:
date = self.date
if side == None:
side = self.side
if strike == None:
strike = self.strike
if type(price) == None:
price = self.price
if symbol == None:
symbol = self.symbol
if closing_trade == None:
closing_trade = self.closing_trade
if short_trade == None:
short_trade = self.short_trade
if executed == None:
executed = self.executed
if trader == None:
trader = self.trader
if channel == None:
channel = self.channel
if time == None:
time = self.time
return Position(ticker, date, side, strike, price, symbol, closing_trade, short_trade, executed, trader, channel, time)