-
Notifications
You must be signed in to change notification settings - Fork 0
/
holdings.py
521 lines (415 loc) · 22.2 KB
/
holdings.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#/usr/bin/env python
"""holdings.py:"""
from __future__ import annotations
__author__ = "Jacob T. Cassady"
__email__ = "[email protected]"
# Built-in Modules
from typing import Dict, Union, Callable, List, TextIO, Any
from abc import ABC, abstractclassmethod, abstractstaticmethod
from enum import Enum, unique
from threading import Lock
from time import time
# 3rd party modules
from pandas import DataFrame, Series
from numpy import array
from StatusLogger import Logger, Message
# Stocker Library Modules
from interfaces.stock import get_stock_price
from interfaces.crypto import get_crypto_price
from interfaces.mint import Mint
from accounts.binance_us_account import BinanceAccount
from accounts.coinbase_account import CoinbaseAccount
from utilities.time_util import Stocker_Event
from utilities.Cipher import VigenereCipher, load_json_resource
class Holdings(object):
"""[summary]"""
def __init__(self, cryptocoins: Dict[str, Holdings.Cryptocoin],
stocks: Dict[str, Holdings.Stock],
checking_accounts: Dict[str, Holdings.CheckingAccount],
floating_usd: Dict[str, float]):
"""[summary]
Args:
stocker (object): [description]
cryptocoins (Dict[str, Holdings.Cryptocoin]): [description]
stocks (Dict[str, Holdings.Stock]): [description]
checking_accounts (Dict[str, Holdings.CheckingAccount]): [description]
floating_usd (Dict[str, float]): [description]"""
self.lock: Lock = Lock()
self.initial_update = Stocker_Event()
self.cryptocoins: Dict[str, Holdings.Cryptocoin] = cryptocoins
self.crypto_df: DataFrame = DataFrame(columns=Holdings.Cryptocoin.get_columns())
self.crypto_df.set_index("datetime")
self.stocks: Dict[str, Holdings.Stock] = stocks
self.stocks_df: DataFrame = DataFrame(columns=Holdings.Stock.get_columns())
self.stocks_df.set_index("datetime")
self.checking_accounts: Dict[str, Holdings.CheckingAccount] = checking_accounts
self.checking_account_df: DataFrame = DataFrame(columns=Holdings.CheckingAccount.get_columns())
self.checking_account_df.set_index("datetime")
self.floating_usd: Dict[str, float] = floating_usd
self.floating_usd_df: DataFrame = DataFrame(columns=["datetime", "location", "equity"])
self.floating_usd_df.set_index("datetime")
def __str__(self) -> str:
return str({
"stocks": [{"symbol": stock_name, "equity": stock.calculate_equity()} for stock_name, stock in self.stocks.items()],
"crypto": [{"symbol": crypto_name, "equity": crypto.calculate_equity()} for crypto_name, crypto in self.cryptocoins.items()],
"checking": [{"account name": account_name, "equity": checking_account.equity} for account_name, checking_account in self.checking_accounts.items()],
"float": [{"location": location, "equity": equity} for location, equity in self.floating_usd.items()]
})
def update(self, binance_account: Union[BinanceAccount, None] = None, coinbase_account: Union[CoinbaseAccount, None] = None,
verbose: bool = False) -> None:
"""[summary]"""
update_time = time()
self.lock.acquire()
for coin_name, coin in self.cryptocoins.items():
coin.price = get_crypto_price(coin_name=coin_name, binance_account=binance_account,
coinbase_account=coinbase_account)
Logger.verbose_console_log(verbose=verbose,
message=f"[CRYPTO] {coin_name} is currently valued at ${coin.price} per coin.",
message_type=Message.MESSAGE_TYPE.STATUS)
self.crypto_df = self.crypto_df.append(coin.to_series(update_time=update_time))
self.crypto_df = self.crypto_df.sort_index()
for stock_symbol, stock in self.stocks.items():
stock.price = get_stock_price(symbol=stock_symbol)
Logger.verbose_console_log(verbose=verbose,
message=f"[STOCK] {stock.name} is currently valued at ${stock.price} per share.",
message_type=Message.MESSAGE_TYPE.STATUS)
self.stocks_df = self.stocks_df.append(stock.to_series(update_time=update_time))
self.stocks_df = self.stocks_df.sort_index()
for account_name, checking_account in self.checking_accounts.items():
#checking_account.update()
Logger.verbose_console_log(verbose=verbose,
message="[CHECKING] " + account_name + " is currently valued at $" + str(checking_account.equity),
message_type=Message.MESSAGE_TYPE.STATUS)
self.checking_account_df = self.checking_account_df.append(checking_account.to_series(update_time=update_time))
self.checking_account_df = self.checking_account_df.sort_index()
for location, usd_in_float in self.floating_usd.items():
self.floating_usd_df = self.floating_usd_df.append(Series({"datetime": update_time, "location": location, "equity": usd_in_float}, name=time()))
self.floating_usd_df = self.floating_usd_df.sort_index()
self.lock.release()
Logger.verbose_console_log(verbose=verbose,
message=str(type(self)) + " is finished updating.",
message_type=Message.MESSAGE_TYPE.SUCCESS)
if not self.initial_update.is_set():
self.initial_update.set()
def calculate_equity(self, holding_type: Union[str, Holdings.HOLDING_TYPE] = "all", verbose: bool = False) -> float:
"""[summary]
Args:
holding_type (Union[str, Holdings.HOLDING_TYPE], optional): [description]. Defaults to "all".
Returns:
float: [description]"""
assert type(holding_type) == str or type(holding_type) == Holdings.HOLDING_TYPE
Logger.verbose_console_log(verbose=verbose,
message=str(type(self)) + " is calculating the equity of holding_type: " + str(holding_type),
message_type=Message.MESSAGE_TYPE.STATUS)
equity: float = 0.
if type(holding_type) == str:
holding_type: Holdings.HOLDING_TYPE = Holdings.HOLDING_TYPE(holding_type)
if holding_type == Holdings.HOLDING_TYPE.STOCK or holding_type == Holdings.HOLDING_TYPE.ALL:
equity += Holdings.calculate_holding_equity(holdings=self.stocks, verbose=verbose)
if holding_type == Holdings.HOLDING_TYPE.CRYPTOCURRENCY or holding_type == Holdings.HOLDING_TYPE.ALL:
equity += Holdings.calculate_holding_equity(holdings=self.cryptocoins, verbose=verbose)
if holding_type == Holdings.HOLDING_TYPE.CHECKING or holding_type == Holdings.HOLDING_TYPE.ALL:
equity += Holdings.calculate_holding_equity(holdings=self.checking_accounts, verbose=verbose)
if (holding_type == Holdings.HOLDING_TYPE.FLOATING_USD or holding_type == Holdings.HOLDING_TYPE.ALL) and self.floating_usd is not None:
equity += Holdings.calculate_holding_equity(holdings=self.floating_usd, verbose=verbose)
Logger.verbose_console_log(verbose=verbose,
message=str(type(self)) + " equity of holding_type: " + str(holding_type) + " = " + "$%.2f" % equity,
message_type=Message.MESSAGE_TYPE.SUCCESS)
return equity
def sum_equities(self, holding_type: Holdings.HOLDING_TYPE, time: float) -> float:
total_equity = 0.
if holding_type == Holdings.HOLDING_TYPE.CRYPTOCURRENCY or holding_type == Holdings.HOLDING_TYPE.ALL:
crypto_at_time: DataFrame = self.crypto_df[self.crypto_df.datetime == time]
crypto_equity = crypto_at_time.quantity * crypto_at_time.price
total_equity += crypto_equity.sum()
if holding_type == Holdings.HOLDING_TYPE.STOCK or holding_type == Holdings.HOLDING_TYPE.ALL:
stocks_at_time: DataFrame = self.stocks_df[self.stocks_df.datetime == time]
stock_equity = stocks_at_time.quantity * stocks_at_time.price
total_equity += stock_equity.sum()
if holding_type == Holdings.HOLDING_TYPE.CHECKING or holding_type == Holdings.HOLDING_TYPE.ALL:
total_equity += self.checking_account_df[self.checking_account_df.datetime == time]['equity'].sum()
if holding_type == Holdings.HOLDING_TYPE.FLOATING_USD or holding_type == Holdings.HOLDING_TYPE.ALL:
total_equity += self.floating_usd_df[self.floating_usd_df.datetime == time]['equity'].sum()
return total_equity
def calculate_holding_equities(self, holding_type: Holdings.HOLDING_TYPE, times: array) -> array:
"""[summary]
Args:
holding_type (Holdings.HOLDING_TYPE): [description]
times (array): [description]
Returns:
array: [description]"""
return array([self.sum_equities(holding_type=holding_type, time=time) for time in times])
def get_times(self, holding_type: Holdings.HOLDING_TYPE) -> array:
"""[summary]
Args:
holding_type (Holdings.HOLDING_TYPE): [description]
Returns:
[type]: [description]"""
if holding_type == Holdings.HOLDING_TYPE.ALL:
stock_times = self.stocks_df['datetime'].unique().tolist()
crypto_times = self.crypto_df['datetime'].unique().tolist()
checking_times = self.checking_account_df['datetime'].unique().tolist()
floating_times = self.floating_usd_df['datetime'].unique().tolist()
return array(sorted(list(set(stock_times + crypto_times + checking_times + floating_times))))
elif holding_type == Holdings.HOLDING_TYPE.STOCK:
return array(self.stocks_df['datetime'].unique())
elif holding_type == Holdings.HOLDING_TYPE.CRYPTOCURRENCY:
return array(self.crypto_df['datetime'].unique())
elif holding_type == Holdings.HOLDING_TYPE.CHECKING:
return array(self.checking_account_df['datetime'].unique())
elif holding_type == Holdings.HOLDING_TYPE.FLOATING_USD:
return array(self.floating_usd_df['datetime'].unique())
@staticmethod
def calculate_holding_equity(holdings: Union[Dict[str, Holdings.Stock],
Dict[str, Holdings.Cryptocoin],
Dict[str, Holdings.CheckingAccount],
Dict[str, float]],
verbose: bool = False) -> float:
"""[summary]
Args:
holdings (Union[Dict[str, Holdings.Stock],
Dict[str, Holdings.Cryptocoin],
Dict[str, Holdings.CheckingAccount],
Dict[str, Holdings.FloatingUSD]]): [description]
verbose (bool, optional): [description]. Defaults to False.
Returns:
float: [description]"""
total_equity: float = 0.
if len(holdings.keys()) > 0:
equity_type: Callable = type(holdings[list(holdings.keys())[0]])
else:
return 0.
for holding_name, holding in holdings.items():
if equity_type == float or equity_type == int:
holding_equity = holding
else:
holding_equity = holding.calculate_equity()
total_equity += holding_equity
Logger.verbose_console_log(verbose=verbose,
message=str(holding_name) + " has a " + str(equity_type) + " value of " + "$%.2f" % holding_equity,
message_type=Message.MESSAGE_TYPE.STATUS)
Logger.verbose_console_log(verbose=verbose,
message=str(Holdings) + " has calculated a total " + str(equity_type) + " equity of " + "$%.2f" % total_equity,
message_type=Message.MESSAGE_TYPE.SUCCESS)
return total_equity
@staticmethod
def load(holding_file_name: str, file_name_cipher: VigenereCipher, data_cipher: VigenereCipher, mint: Union[Mint, None]) -> Holdings:
"""[summary]
Args:
holding_file_name (str): [description]
Returns:
Holdings: [description]"""
stocks: Dict[str, Holdings.Stock] = {}
crypto: Dict[str, Holdings.Cryptocoin] = {}
checking_accounts: Dict[str, Holdings.CheckingAccount] = {}
floating_usd: Dict[str, float] = {}
stock_and_crypto_holdings: Dict[str, Dict[str, Dict[str, Any]]] = load_json_resource(file_name_cipher=file_name_cipher,
data_cipher=data_cipher,
resource_file_name=holding_file_name)
# Load stocks
for stock_symbol, stock_data in stock_and_crypto_holdings['stock'].items():
stocks[stock_symbol] = Holdings.Stock(symbol=stock_symbol,
name=stock_data['name'],
quantity=stock_data['quantity'],
cost_basis_per_share=stock_data['cost basis per share'])
# Load crypto
for coin, coin_data in stock_and_crypto_holdings['crypto'].items():
crypto[coin] = Holdings.Cryptocoin(name=coin_data["name"],
coin=coin,
quantity=coin_data['quantity'],
investment=coin_data['investment'])
# Load float
for float_location, usd_in_float in stock_and_crypto_holdings['float'].items():
floating_usd[float_location] = usd_in_float
# Load checking accounts from mint
if mint is not None:
for account_data in mint.get_accounts(False):
checking_accounts[account_data['accountName']] = Holdings.CheckingAccount(name=account_data['accountName'],
equity=account_data['value'])
mint.close()
return Holdings(stocks=stocks,
cryptocoins=crypto,
checking_accounts=checking_accounts,
floating_usd=floating_usd)
class Holding(ABC, object):
"""[summary]"""
@abstractclassmethod
def __str__(self) -> str:
"""[summary]
Returns:
str: [description]"""
pass
@abstractclassmethod
def calculate_equity(self) -> float:
"""TODO"""
pass
@abstractstaticmethod
def to_series(self, update_time: float) -> Series:
"""[summary]
Args:
update_time (float): [description]
Returns:
Series: [description]"""
pass
@abstractclassmethod
def get_columns() -> List[str]:
"""[summary]
Returns:
List[str]: [description]"""
pass
class Cryptocoin(Holding):
"""[summary]"""
def __init__(self, name: str, coin: str, quantity: float, investment: float,
unrealized_rewards: float = 0.0):
"""Constructor.
Args:
name (str): [description]
quantity (float): [description]
investment (float): [description]
gain (float): [description]
unrealized_rewards (float, optional): [description]. Defaults to 0.0."""
self.name: str = name
self.coin: str = coin
self.quantity: float = quantity
self.unrealized_rewards: float = unrealized_rewards
self.investment: float = investment
self.price = None
def __str__(self) -> str:
"""[summary]
Returns:
str: [description]"""
return str({"name": self.name, "quantity": self.quantity,
"unrealized_rewards": self.unrealized_rewards, "cost basis per coin": self.investment / self.quantity,
"price": self.price})
def calculate_equity(self) -> float:
"""[summary]
Returns:
float: [description]"""
assert self.price is not None, "Equity cannot be calculated without initially updating price."
return self.quantity * self.price
def generate_database_table(self, database_script: TextIO):
"""[summary]
Args:
database_script (TextIO): [description]"""
database_script.write("CREATE TABLE {}(\n".format(self.coin))
database_script.write(" datetime DATETIME PRIMARY KEY,\n")
database_script.write(" quantity float,\n")
database_script.write(" stake float,\n")
database_script.write(" unrealized_rewards float,\n")
database_script.write(" investment float,\n")
database_script.write(" price float);\n\n")
def to_series(self, update_time: float) -> Series:
"""[summary]
Args:
update_time (datetime): [description]
Returns:
Series: [description]"""
return Series({"datetime": update_time, "name": self.name, "investment": self.investment,
"quantity": self.quantity, "price": self.price},
name=time())
@staticmethod
def get_columns() -> List[str]:
"""[summary]
Returns:
List[str]: [description]"""
return ["datetime", "name", "investment", "quantity", "price"]
class Stock(Holding):
"""[summary]"""
def __init__(self, symbol: str, name: str,
quantity: float, cost_basis_per_share: float):
"""Constructor.
Args:
symbol (str): [description]
name (str): [description]
quantity (float): [description]
cost_basis_per_share (float): [description]"""
self.symbol = symbol
self.name = name
self.quantity = quantity
self.cost_basis_per_share = cost_basis_per_share
self.price: Union[float, None] = None
def __str__(self) -> str:
"""[summary]
Returns:
str: [description]"""
return str({"symbol": self.symbol, "name": self.name, "cost_basis_per_share": self.cost_basis_per_share,
"quantity": self.quantity, "price": self.price})
def calculate_equity(self) -> float:
"""[summary]
Returns:
float: [description]"""
assert self.price is not None, "Equity cannot be calculated without initially updating price."
return self.quantity * self.price
def generate_database_table(self, database_script: TextIO):
"""[summary]
Args:
database_script (TextIO): [description]"""
database_script.write("CREATE TABLE {}(\n".format(self.symbol))
database_script.write(" datetime DATETIME PRIMARY KEY,\n")
database_script.write(" quantity float,\n")
database_script.write(" cost_basis_per_share float,\n")
database_script.write(" price float);\n\n")
def to_series(self, update_time: float) -> Series:
"""[summary]
Args:
update_time (datetime): [description]
Returns:
Series: [description]"""
return Series({"datetime": update_time, "stock": self.symbol, "cost_basis_per_share": self.cost_basis_per_share,
"quantity": self.quantity, "price": self.price},
name=time())
@staticmethod
def get_columns() -> List[str]:
"""[summary]
Returns:
List[str]: [description]"""
return ["datetime", "stock", "cost_basis_per_share", "quantity", "price"]
class CheckingAccount(Holding):
"""[summary]"""
def __init__(self, name: str, equity: float):
"""Constructor.
Args:
name (str): [description]
balance (float): [description]"""
self.name = name
self.equity = equity
def __str__(self) -> str:
"""[summary]
Returns:
str: [description]"""
return str({"name": self.name, "equity": self.equity})
def calculate_equity(self) -> float:
"""[summary]
Returns:
float: [description]"""
return self.equity
@staticmethod
def get_columns() -> List[str]:
"""[summary]
Returns:
List[str]: [description]"""
return ["datetime", "account_name", "equity"]
def to_series(self, update_time: float) -> Series:
"""[summary]
Args:
update_time (datetime): [description]
Returns:
Series: [description]"""
return Series({"datetime": update_time, "account_name": self.name, "equity": self.equity},
name=time())
def update(self, stocker: object) -> None:
"""[summary]
Args:
stocker (object): [description]"""
mint_accounts_data = stocker.mint.get_accounts()
matching_account_data = [account_data for account_data in mint_accounts_data if account_data['accountName'] == self.name]
assert len(matching_account_data) == 1, "An unexpected number of matching account data was found: " + str(len(matching_account_data))
self.equity = matching_account_data[0]['amount']
@unique
class HOLDING_TYPE(Enum):
"""[summary]"""
ALL = "all"
STOCK = "stock"
CRYPTOCURRENCY = "crypto"
CHECKING = "checking account"
FLOATING_USD = "float"