-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaccount_data_retriever.py
172 lines (144 loc) · 5.14 KB
/
account_data_retriever.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
from config import *
from models.stock import Stock
import operator
import random
import csv
class AccountDataRetriever:
'''
This class allows for easy access to relevent account information for debugging,
updating the webpage, etc.
NOTE: initializing an instance connects us to the api and sets class up with account
and positions info
'''
def __init__(self) -> None:
self.api = API
self.account = self.api.get_account()
self.positions = self.__get_account_positions()
self.plpc_sorted_holdings = sorted(self.positions, key=operator.attrgetter('abs_intraday_plpc'), reverse=True)
self.formatted_positions = self.format_positions()
def __get_account_positions(self) -> list:
'''
Goes through positions received from Alpaca, initializes Stock objects with
relevant info, adds them to an array which is eventually returned.
'''
positions = []
for position in self.api.list_positions():
positions.append(
Stock(
position.symbol, int(position.qty), float(position.current_price),
float(position.lastday_price), float(position.market_value),
float(position.unrealized_intraday_pl), float(position.unrealized_intraday_plpc),
float(position.change_today), float(position.unrealized_pl),
float(position.unrealized_plpc), float(position.avg_entry_price),
abs(float(position.market_value))
)
)
return positions
def format_positions(self):
''' FOR USE IN holdings.py TABLE'''
positions = self.__get_account_positions()
formatted_positions = []
total = self.get_stock_equity()
i = 1
for pos in positions[::-1]:
if i % 2 == 0:
back_color = "#0f0f0f"
else:
back_color = "#151515"
daily_plpc = "{:,.2f}".format(pos.intraday_plpc)
if pos.intraday_plpc < 0:
daily_color = '''#ad0017'''
daily_plpc = '-' + str(daily_plpc) + '%'
else:
daily_color = '''#008523'''
daily_plpc = str(daily_plpc) + '%'
pl = "${:,.2f}".format(pos.pl)
if pl[1] == '-':
pl = pl[1] + pl[0] + pl[2:]
pl_color = '''#ad0017'''
else:
pl_color = '''#008523'''
i += 1
pie_color = random.choices(range(256), k=3)
formatted_positions.append(
[
pos.symbol,
daily_plpc,
'$' + "{:,.2f}".format(pos.cost),
'$' + "{:,.2f}".format(pos.current_price),
pos.qty,
'$' + "{:,.2f}".format(pos.market_value),
pl,
daily_color,
pl_color,
back_color,
round(100 * (pos.market_value / total), 2),
pie_color
]
)
return formatted_positions
def get_stock_equity(self) -> float:
'''
Returns a tuple containing the float value for account equity and
a formatted string of this value.
'''
equity = 0
for position in self.positions:
equity += position.market_value
return equity
def get_buying_power(self) -> float:
'''
Returns a tuple containing the float value for buying power and a
formatted string of this value.
'''
buying_power = float(self.account.cash)
return buying_power
def get_account_daily_change(self) -> float:
'''
Returns a tuple containing the float value for the account's daily change
and a formatted string of this value.
'''
daily_change = 0
for position in self.positions:
daily_change += position.current_price - position.lastday_price
return daily_change
def get_account_percent_change(self) -> float:
'''
Returns a tuple containing the float value for the account's daily percent
change and a formatted string of this value.
'''
yesterday_equity = self.get_stock_equity() - self.get_account_daily_change()
return ( ( self.get_stock_equity() - yesterday_equity ) / yesterday_equity ) * 100
def get_position_colors(self) -> dict:
'''
Returns a dictionary mapping strings of stock symbols to a string, either
'green' or 'red' based on if the stock is up / down on the day.
'''
colors = {}
for position in self.positions:
if position.intraday_pl >= 0:
colors[position.symbol] = "green"
else:
colors[position.symbol] = "red"
return colors
def print_stock_price_alphabetical(self):
''' for debugging, so you can see what you're handling '''
for position in sorted(self.positions):
print(position)
def get_cats(self):
company_industry = {}
with open('symbols.csv', 'r') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
company_industry[row[1]] = row[2]
percents = {}
for pos in self.positions:
if company_industry[pos.symbol] in percents.keys():
percents[company_industry[pos.symbol]] += pos.market_value
else:
percents[company_industry[pos.symbol]] = pos.market_value
# add all the markets values together for each category in a dictionary
total = self.get_stock_equity()
for cat in percents.keys():
percents[cat] = round(100 * percents[cat] / total,2)
return percents