This repository has been archived by the owner on Jun 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
analyser
executable file
·224 lines (169 loc) · 6.08 KB
/
analyser
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
216
217
218
219
220
221
222
223
224
#!/usr/bin/env python3
from stockanalyser.stock import Stock, unpickle_stock
import stockanalyser.analysis
import importlib
import logging
from stockanalyser.stock import Stock, stock_pickle_path
from stockanalyser.analysis import levermann
from stockanalyser.analysis.levermann import (Levermann, unpickle_levermann,
unpickle_levermann_sym)
import datetime
import os
import sys
import pickle
from stockanalyser import config, input
import argparse
logger = logging.getLogger(__name__)
def configure_argparse():
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--debug", action='store_true')
subparsers = parser.add_subparsers()
show_parser = subparsers.add_parser("list")
show_parser.add_argument("-v", "--verbose", action='store_true')
show_parser.add_argument("-o", "--outdated",
help="show list of stocks with outdated"
" figure release date", action='store_true')
show_parser.set_defaults(func=list)
add_parser = subparsers.add_parser("add")
add_parser.add_argument("ISIN", help="Stock ISIN")
add_parser.set_defaults(func=add)
update_parser = subparsers.add_parser("update")
update_parser.add_argument("STOCK_SYMBOL", help="Stock Symbol", nargs="?")
update_parser.add_argument("-f", "--force", help="enforce update",
action="store_true")
update_parser.set_defaults(func=update)
set_parser = subparsers.add_parser("set")
set_parser.add_argument("stock_symbol", help="Stock Symbol with"
" country ending")
set_parser.set_defaults(func=set)
args = parser.parse_args()
configure_logger(args.debug)
if not "func" in args:
parser.print_help()
sys.exit(0)
args.func(args)
#logger.debug("Argparse arguments: %s" % args)
#if args.add:
# add_stock()
#elif args.show:
# show(args.verbose)
#else:
def configure_logger(debug):
if debug:
level = logging.DEBUG
else:
level = logging.INFO
logging.getLogger().setLevel(level)
def unpickle_levermann_objs():
levermann_objs = []
for f in os.listdir(config.DATA_PATH):
path = os.path.join(config.DATA_PATH, f)
if not path:
continue
if "levermann" in f:
l = unpickle_levermann(path)
levermann_objs.append(l)
return levermann_objs
def set(args):
sym = args.stock_symbol
path = levermann.levermann_pickle_path(sym)
if not os.path.isfile(path):
print("Stock %s data file doesn't exist.\n" % (sym, path))
sys.exit(1)
l = unpickle_levermann_sym(sym)
if args.quarterly_figures_release_date in l.stock.quarterly_figure_dates:
print("Date was already stored, stored quarterly figure release dates:"
" %s" % l.stock.quarterly_figure_dates)
return
l.stock.quarterly_figure_dates.append(args.quarterly_figures_release_date)
l.save()
print("Last Quarterly figures release stored, stored dates: %s" %
l.stock.quarterly_figure_dates)
def update(args):
levermann_objs = []
updated_stocks = []
if args.STOCK_SYMBOL:
levermann_objs.append(unpickle_levermann_sym(args.STOCK_SYMBOL))
else:
levermann_objs = unpickle_levermann_objs()
for l in levermann_objs:
if args.force or l.outdated():
print("Creating Levermann Analysis for %s" % l.stock.symbol)
l.stock.update_stock_info()
try:
if l.evaluate():
updated_stocks.append(l.stock.symbol)
except Exception as e:
print("creating levermann analyis failed: %s" % e)
else:
print("Analysis for %s is already uptodate" % l.stock.symbol)
l.save()
print("-" * 80)
print("-" * 80)
print("The following Stocks were updated:", "\n".join(updated_stocks))
def show_outdated(levermann_objs):
outdated = []
for l in levermann_objs:
if l.stock.is_quarterly_figures_release_date_outdated():
outdated.append("%s (%s): %s" %
(l.stock.name, l.stock.symbol,
l.stock.last_quarterly_figures_release_date()))
print("For the following stocks the last figure release date is outdated:\n",
"\n".join(outdated))
print("-" * 80)
def list(args):
levermann_objs = unpickle_levermann_objs()
if args.outdated:
return show_outdated(levermann_objs)
levermann_objs = sorted(levermann_objs,
key=lambda x: x.evaluation_results[-1].score,
reverse=True)
print(levermann_objs[0].short_summary_header())
for l in levermann_objs:
if args.verbose:
print(l)
else:
print(l.short_summary())
print(levermann_objs[0].short_summary_footer())
print("Total: %s" % len(levermann_objs))
def add(args):
create_data_dir()
s = Stock(isin=args.ISIN)
path = levermann.levermann_pickle_path(s.symbol)
if os.path.isfile(path):
logger.info("Stock already exists (%s)" % path)
sys.exit(1)
s.update_stock_info()
logger.info("Adding Stock %s" % s.symbol)
l = Levermann(s)
l.evaluate()
l.save()
print(l)
def load_levermann():
l = unpickle_levermann_sym("VOW.DE")
print("%s" % l)
def eval_levermann_from_pickled_stock():
stock = unpickle_stock("VOW.DE")
l = Levermann(stock)
l.evaluate()
l.save()
print(l)
def create_data_dir():
if not os.path.exists(config.DATA_PATH):
os.makedirs(config.DATA_PATH)
logger.debug("Created directory %s" % config.DATA_PATH)
def main():
create_data_dir()
sym = input.query_input("Stock Symbol", input.QueryType.STOCK_SYMBOL)
try:
l = unpickle_levermann_sym(sym)
l.evaluate()
except FileNotFoundError:
s = Stock(sym)
l = Levermann(s)
set_leverman_values(s)
l.evaluate()
l.save()
print(l)
if __name__ == "__main__":
configure_argparse()