-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstonks.py
More file actions
60 lines (47 loc) · 2.1 KB
/
stonks.py
File metadata and controls
60 lines (47 loc) · 2.1 KB
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
#3rd part RH api: http://www.robin-stocks.com/en/latest/functions.html
#!/usr/bin/bash python3
#import requests
import json, pyotp
import robin_stocks
import config
import numpy as np
import plotly.graph_objects as go
def main():
#2FA step:
totp = pyotp.TOTP("My2factorAppHere").now()
l = robin_stocks.authentication.login(username=config.username, password=config.password, store_session=True, mfa_code=totp) #l = log-in info
r = robin_stocks.build_holdings() #builds dictionary regard stocks+positions the owner has
#initializing variable
new_access = True
print_holdings = False #Output holdings to console. Increases pie chart load time.
#visual header for holdings data
if(print_holdings == True):
dash='='*73
print(dash)
print("Stonk: Curr_price:\t Shares Owned:")
print(dash)
for key,val in r.items(): #key = ticker symbol, val = ticker data
if(new_access == True):
total = np.array([])
ticker = np.array([])
portfolio_sum = 0
new_access = False
if(print_holdings == True):
print("{:<5s}\t {:>6.2f}\t\t{:>3.4f}".format(key, float(val['price']), float(val['quantity'])))
#calculate stock price value and add it to total portfolio value
tot = float(val['price'])*float(val['quantity'])
portfolio_sum = portfolio_sum + tot
ticker = np.append(ticker, str(key))
total = np.append(total, tot)
#create pie chart with stock data + tickers as labels
layout = go.Layout(title='<span style="font-size:30px">Current Robinhood Portfolio:</span>'
+'<br>'
+ 'Value: ~$'
+ '{:.2f}'.format(portfolio_sum))
fig = go.Figure(data = [go.Pie(labels = ticker, values = total, textinfo ='label', hoverinfo='label+percent')],
layout = layout)
fig.update_layout(autosize=True)
# fig.show()
robin_stocks.authentication.logout()
if __name__ == "__main__":
main()