-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack_utils.py
150 lines (135 loc) · 5.37 KB
/
slack_utils.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
'''
Interface which can be called directly by Slack users
Here ideally we should be getting response from functions in utils.py
format them and then return.
'''
import logging
import datetime
from tabulate import tabulate
from logging.config import fileConfig
from utils import check_vr_reco, min_max_reco_date, get_live_price, alert_below_percentage, get_vr_stocks_live_util
#TODO: use decorator to catch exceptions
def get_quotes(symbol):
'''
:param symbol:
:return: info about stock symbol
'''
#check VR stock or not
try:
stock = check_vr_reco(symbol)
logging.debug(f'{symbol}: stock obj {stock}')
info = get_live_price(symbol)
data = [[info.name],
["LastPrice:", info.lastPrice, info.pChange],
["Day Low:", info.dayLow],
["Day High:", info.dayHigh],
["Day Open:", info.open],
]
logging.debug(str(data))
if stock:
# stock recommended in VR, get reco price and other details
result = min_max_reco_date(symbol)
print(result[1])
_, vr_reco_date, vr_reco_price, vr_min, vr_max = result[1].split(';')
data.append(["VR Reco Date:", vr_reco_date]),
data.append(["VR Reco Price:", vr_reco_price])
data.append(["VR Min:", vr_min])
data.append( ["VR Max:", vr_max])
ret = tabulate(data, tablefmt="simple", headers="firstrow")# numalign="left")
logging.debug(ret)
return ret
except IndexError as err:
logging.debug(str(err))
return 'IndexError'
def get_performance(symbol):
'''
:param symbol:
:return: stock parameters from the reco-date
1. High it attained
2. Low it attained
3. Performance as per current price
'''
#TODO should be done by slack.py
symbol = symbol.upper()
try:
if(check_vr_reco(symbol) is None):
return 'Not a VR recommendation'
result = min_max_reco_date(symbol)
info = get_live_price(symbol)
_, vr_reco_date, vr_reco_price, vr_min, vr_max = result[1].split(';')
response = [[info.name],
["CurrentPrice:", info.lastPrice, info.pChange],
["Vr Reco Date:", vr_reco_date],
["VR Reco Price:", vr_reco_price],
["VR Max:", vr_max],
["VR Min:", vr_min]
]
perf = ((info.lastPrice - float(vr_reco_price)) / float(vr_reco_price)) * 100
response.append(["Performance:",round(perf,2) ])
ret = tabulate(response, tablefmt="simple", headers="firstrow")
return ret
except IndexError:
return 'TBA'
def get_vr_stocks_below(percentage, all_time=True):
'''
:param percentange:
:return:
All stocks making loss or profits above percentage provided
'''
response = []
if not all_time:
response.append(f'Alert for {datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S")}')
for symbol in alert_below_percentage(percentage = int(percentage), all_time=all_time):
slist = symbol.split(';')
logging.debug(f'{symbol}')
response.append(f'{"Symbol:":<15}{slist[0]:<25}')
if not all_time:
response.append(f'{"Live price:":<15}{slist[2]:<5}{"( ":<2}{slist[3].strip():<1}{"%)":<2}')
response.append(f'{"Reco price:":<15}{slist[1]:<25}')
else:
response.append(f'{"Live price:":<15}{slist[2]:<25}')
response.append(f'{"Reco price:":<15}{slist[1]:<25}')
percent = ((float(slist[2]) - float(slist[1])) / float(slist[1])) * 100
response.append(f'{"% change:":<15}{round(percent, 2):<5}%')
response.append(f'{""}')
return '\n'.join(response)
def get_vr_stocks_live():
'''
:param percentange:
:return:
All stocks making loss or profits above percentage provided
'''
data = []
logging.debug(f'Calling test')
for symbol in get_vr_stocks_live_util():
slist = symbol.split(';')
logging.debug(f'{symbol}')
data.append([slist[0], "Current:", "{0}({1}%)".format(slist[2].rstrip(), slist[3].rstrip()), "RecoP:", slist[1]])
#response.append(f'{slist[0]:<25}CurP: {slist[2]:<10}({slist[3]}%) RecoP: {slist[1]:<25}')
ret = tabulate(data, tablefmt="simple", numalign="left")# numalign="left")
logging.debug(ret)
return ret
def get_vr_stocks_live_below(percentage):
'''
:param percentange:
:return:
All stocks making loss or profits above percentage provided
'''
data = []
logging.debug(f'Calling test')
for symbol in get_vr_stocks_live_util():
slist = symbol.split(';')
logging.debug(f'GT {symbol}')
if float(slist[3].rstrip()) < (-1 * float(percentage)):
logging.debug(f'{symbol}')
data.append([slist[0], "Current:", "{0}({1}%)".format(slist[2].rstrip(), slist[3].rstrip()), "RecoP:", slist[1]])
#response.append(f'{slist[0]:<25}CurP: {slist[2]:<10}({slist[3]}%) RecoP: {slist[1]:<25}')
data.append(f'{""}')
ret = tabulate(data, tablefmt="simple", numalign="left")# numalign="left")
logging.debug(ret)
return ret
if __name__ == '__main__':
fileConfig('logging.ini', disable_existing_loggers=True)
logger = logging.getLogger()
res = get_quotes("INDIGO")
logging.debug("get quotes %s" % str(res))