This repository has been archived by the owner on Jun 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathshow_potentials.py
executable file
·71 lines (59 loc) · 2.41 KB
/
show_potentials.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
from decimal import Decimal
import argparse
import json
from robinhood.RobinhoodCachedClient import RobinhoodCachedClient, CACHE_FIRST, FORCE_LIVE
from robinhood.RobinhoodPortfolio import RobinhoodPortfolio
# Set up the client
client = RobinhoodCachedClient()
client.login()
def show_potentials(cache_mode):
# Load the portfolio
portfolio = RobinhoodPortfolio(client, {'cache_mode': cache_mode})
# Load the sentiment
with open('sentiment.json', 'r') as sentiment_file:
sentiment_json = json.load(sentiment_file)
# Augment the portfolio with sentiment
for symbol, symbol_sentiment in sentiment_json.items():
position = portfolio.get_position_for_symbol(symbol)
assert 'sentiment' not in position
position['sentiment'] = symbol_sentiment
position['category'] = symbol_sentiment.get('category', 'N/A')
position['priority'] = symbol_sentiment.get('priority', 99)
if 'equity_target' in symbol_sentiment:
equity_target = symbol_sentiment['equity_target']
position['equity_target'] = equity_target
equity_left = equity_target - position['equity_worth']
if equity_left <= 0:
position['equity_left'] = 0
position['shares_needed'] = 0
else:
position['equity_left'] = equity_left
position['shares_needed'] = equity_left / position['last_price']
position['paused'] = symbol_sentiment.get('paused', False)
positions_by_priority = sorted(portfolio.positions, key=lambda p: p.get('priority', 999))
print('pri\tsym\teq_tot\tlast\twrth\ttrgt\tlft\tsh\tcat')
for position in positions_by_priority:
if position.get('paused'):
continue
print('{}\t{}\t{:.2f}\t{:.2f}\t{:.2f}\t{}\t{:.2f}\t{:.2f}\t{}'.format(
position.get('priority', 'N/A'),
position['symbol'],
position['equity_cost'],
position['last_price'],
position['equity_worth'],
position.get('equity_target', 'N/A'),
position.get('equity_left', 0),
position.get('shares_needed', 0),
position.get('category')
))
# TODO: Discounted (below buy in price)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Show various position potentials to buy into')
parser.add_argument(
'--live',
action='store_true',
help='Force to not use cache for APIs where values change'
)
args = parser.parse_args()
show_potentials(FORCE_LIVE if args.live else CACHE_FIRST)