-
Notifications
You must be signed in to change notification settings - Fork 0
/
robinhood.py
215 lines (195 loc) · 9.51 KB
/
robinhood.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
"""/**
* Author: Vignesh Sivanandha Rao
* Created: 05.08.2020
*
**/"""
from datetime import datetime, timedelta, date
from json import loads
from math import fsum
from os import environ, mkdir, path
from shutil import rmtree
from sys import exit
from time import perf_counter
import matplotlib.pyplot as plt
from pandas import read_html as reader
from pyrh import Robinhood
from requests import get
from twilio.rest import Client
from lib.emailer import Emailer
def market_status():
url = 'https://www.nasdaqtrader.com/trader.aspx?id=Calendar'
holidays_list = list(reader(url)[0][0])
today = date.today().strftime("%B %d, %Y")
if today in holidays_list:
print(f'{today}: The markets are closed today.')
else:
return True
def watcher():
global graph_msg, graph_min, graph_max
rh = Robinhood()
rh.login(username=rh_user, password=rh_pass, qr_code=rh_qr)
raw_result = rh.positions()
result = raw_result['results']
shares_total = []
port_msg = f"Your portfolio ({rh.get_account()['account_number']}):\n"
loss_output = 'Loss:'
profit_output = 'Profit:'
loss_total = []
profit_total = []
graph_msg = None # initiates a variable graph_msg as None for looped condition below
n = 0
n_ = 0
for data in result:
share_id = str(data['instrument'].split('/')[-2])
buy = round(float(data['average_buy_price']), 2)
shares_count = int(data['quantity'].split('.')[0])
if shares_count != 0:
n = n + 1
n_ = n_ + shares_count
else:
continue
raw_details = rh.get_quote(share_id)
share_name = raw_details['symbol']
call = raw_details['instrument']
share_full_name = loads(get(call).text)['simple_name']
total = round(shares_count * float(buy), 2)
shares_total.append(total)
current = round(float(raw_details['last_trade_price']), 2)
current_total = round(shares_count * current, 2)
difference = round(float(current_total - total), 2)
if difference < 0:
loss_output += (
f'\n{share_full_name}:\n{shares_count} shares of {share_name} at ${buy} Currently: ${current}\n'
f'Total bought: ${total} Current Total: ${current_total}'
f'\nLOST ${-difference}\n')
loss_total.append(-difference)
else:
profit_output += (
f'\n{share_full_name}:\n{shares_count} shares of {share_name} at ${buy} Currently: ${current}\n'
f'Total bought: ${total} Current Total: ${current_total}'
f'\nGained ${difference}\n')
profit_total.append(difference)
if graph_min and graph_max:
graph_min = float(graph_min)
graph_max = float(graph_max)
if difference > graph_max or difference < -graph_min:
time_now = datetime.now()
metrics = time_now - timedelta(days=7)
numbers = []
historic_data = (rh.get_historical_quotes(share_name, '10minute', 'week'))
historical_values = historic_data['results'][0]['historicals']
for close_price in historical_values:
numbers.append(round(float(close_price['close_price']), 2))
fig, ax = plt.subplots()
if difference > graph_max:
plt.title(f"Stock Price Trend for {share_full_name}\nShares: {shares_count} Profit: ${difference}")
elif difference < graph_min:
plt.title(f"Stock Price Trend for {share_full_name}\nShares: {shares_count} LOSS: ${-difference}")
plt.xlabel(f"1 Week trend with 10 minutes interval from {metrics.strftime('%m-%d %H:%M')} to "
f"{time_now.strftime('%m-%d %H:%M')}")
plt.ylabel('Price in USD')
ax.plot(numbers, linewidth=1.5)
if not path.isdir('img'):
mkdir('img')
fig.savefig(f"img/{share_full_name}.png", format="png")
plt.close() # close plt to avoid memory exception when more than 20 graphs are generated
# stores graph_msg only if a graph is generated else graph_msg remains None
if not graph_msg: # used if not to avoid storing the message repeatedly
graph_msg = f"Attached are the graphs for stocks which exceeded a profit of " \
f"${graph_max} or deceeded a loss of ${graph_min}"
elif not graph_msg: # used elif not to avoid storing the message repeatedly
graph_msg = "Add the env variables for <graph_min> and <graph_max> to include a graph of previous " \
"week's trend."
lost = round(fsum(loss_total), 2)
gained = round(fsum(profit_total), 2)
port_msg += f'The below values will differ from overall profit/loss if shares were purchased ' \
f'with different price values.\nTotal Profit: ${gained}\nTotal Loss: ${lost}\n'
net_worth = round(float(rh.equity()), 2)
output = f'Total number of stocks purchased: {n}\n'
output += f'Total number of shares owned: {n_}\n\n'
output += f'Current value of your total investment is: ${net_worth}\n'
total_buy = round(fsum(shares_total), 2)
output += f'Value of your total investment while purchase is: ${total_buy}\n'
total_diff = round(float(net_worth - total_buy), 2)
if total_diff < 0:
output += f'Overall Loss: ${total_diff}'
else:
output += f'Overall Profit: ${total_diff}'
yesterday_close = round(float(rh.equity_previous_close()), 2)
two_day_diff = round(float(net_worth - yesterday_close), 2)
output += f"\n\nYesterday's closing value: ${yesterday_close}"
if two_day_diff < 0:
output += f"\nCurrent Dip: ${two_day_diff}"
else:
output += f"\nCurrent Spike: ${two_day_diff}"
if not graph_msg: # if graph_msg was not set above
graph_msg = f"You have not lost more than ${graph_min} or gained more than " \
f"${graph_max} to generate a graph."
return port_msg, profit_output, loss_output, output, graph_msg
def send_email(attachment):
print("Sending email...")
footer_text = "\n----------------------------------------------------------------" \
"----------------------------------------\n" \
"A report on the list shares you have purchased.\n" \
"The data is being collected using http://api.robinhood.com/," \
f"\nFor more information check README.md in https://github.com/thevickypedia/robinhood_monitor"
Emailer(access_key=access_key, secret_key=secret_key,
sender=f"Robinhood Monitor <{sender}>", recipients=[recipient],
title=f'Investment Summary as of {dt_string}',
text=f'{overall_result}\n\n{port_head}\n{profit}\n{loss}\n\n{graph_msg}\n\n{footer_text}',
attachment=attachment)
if 'Attached' in graph_msg: # only tries to delete if graphs have been generated
rmtree('img')
def send_whatsapp():
print('Sending whats app notification...')
Client(sid, token).messages.create(
body=f'{dt_string}\nRobinhood Report\n{overall_result}\n\nCheck your email for summary',
from_=f"whatsapp:{send}",
to=f"whatsapp:{receive}"
)
if __name__ == '__main__':
if market_status():
if environ.get('DOCKER'):
print('Using json file for secrets')
from json import load
json_file = load(open('params.json'))
rh_user = json_file.get('user')
rh_pass = json_file.get('pass')
rh_qr = json_file.get('qr')
sid = json_file.get('SID')
token = json_file.get('TOKEN')
send = json_file.get('SEND')
receive = json_file.get('RECEIVE')
sender = json_file.get('SENDER')
access_key = json_file.get('ACCESS_KEY')
secret_key = json_file.get('SECRET_KEY')
current_time = datetime.now() - timedelta(hours=5)
else:
rh_user = environ.get('user')
rh_pass = environ.get('pass')
rh_qr = environ.get('qr')
sid = environ.get('SID')
token = environ.get('TOKEN')
send = environ.get('SEND')
receive = environ.get('RECEIVE')
sender = environ.get('SENDER')
access_key = environ.get('ACCESS_KEY')
secret_key = environ.get('SECRET_KEY')
current_time = datetime.now()
recipient = rh_user
# below are optional to get graphs for stocks that have profited more than graph_max and loss below graph_min
graph_min = environ.get('graph_min')
graph_max = environ.get('graph_max')
env_vars = [rh_user, rh_pass, rh_qr, sid, token, send, receive, sender, recipient]
if any(env_var is None for env_var in env_vars):
exit("Check your environment variables. It should be set as:\n"
"'user=<login_email>'\n'pass=<password>'\n'qr=<qr_code>'\n"
"'SID=<twilio_sid>'\n'TOKEN=<twilio_token>'\n'SEND=<twilio_sender>'\n"
"'RECEIVE=<phone_number>'\n'SENDER=<emailID>'")
dt_string = current_time.strftime("%A, %B %d, %Y %I:%M %p")
print(f'{dt_string}')
print('Gathering your investment details...')
port_head, profit, loss, overall_result, graph_msg = watcher()
send_email(attachment=True)
send_whatsapp()
print(f"Process Completed in {round(float(perf_counter()), 2)} seconds")