This repository was archived by the owner on Jun 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathdownload_portfolio.py
executable file
·71 lines (61 loc) · 2.37 KB
/
download_portfolio.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
#!/usr/bin/env python3
import argparse
import csv
#import logging
#logging.basicConfig(level=logging.DEBUG)
from robinhood.RobinhoodCachedClient import RobinhoodCachedClient, CACHE_FIRST, FORCE_LIVE
from robinhood.RobinhoodPortfolio import RobinhoodPortfolio
# Set up the client
client = RobinhoodCachedClient()
client.login()
def download_portfolio(cache_mode):
with open('portfolio.csv', 'w', newline='') as csv_file:
fieldnames = [
'symbol',
'name',
'quantity',
'average_buy_price',
'equity_cost',
'last_price',
'day_price_change',
'day_percentage_change',
'total_price_change',
'total_percentage_change',
'equity_worth',
'equity_percentage',
'equity_idx',
'robinhood_holders',
'buy_rating',
'sell_rating',
]
csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
csv_writer.writeheader()
portfolio = RobinhoodPortfolio(client, {'cache_mode': cache_mode})
for idx, position in enumerate(portfolio.positions_by_equity_worth):
csv_writer.writerow({
'symbol': position['symbol'],
'name': position['shortest_name'],
'quantity': position['quantity'],
'average_buy_price': round(position['average_buy_price'], 2),
'equity_cost': round(position['equity_cost'], 2),
'last_price': round(position['last_price'], 2),
'day_price_change': round(position['day_price_change'], 2),
'day_percentage_change': round(position['day_percentage_change'], 2),
'total_price_change': round(position['total_price_change'], 2),
'total_percentage_change': round(position['total_percentage_change'], 2),
'equity_worth': round(position['equity_worth'], 2),
'equity_percentage': round(position['equity_percentage'], 2),
'equity_idx': idx + 1,
'buy_rating': position['buy_rating'],
'sell_rating': position['sell_rating'],
'robinhood_holders': position['robinhood_holders'],
})
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Download a snapshot of your portfolio')
parser.add_argument(
'--live',
action='store_true',
help='Force to not use cache for APIs where values change'
)
args = parser.parse_args()
download_portfolio(FORCE_LIVE if args.live else CACHE_FIRST)