This repository has been archived by the owner on Mar 24, 2023. It is now read-only.
forked from phoemur/fundamentus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagic_formula.py
68 lines (50 loc) · 1.72 KB
/
magic_formula.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
import re
from collections import OrderedDict
from fundamentus import get_data
def to_float(string):
formatted = re.sub(r'[.%]', '', string).replace(',', '.')
return float(formatted)
def transform(item_name, item_value):
roic = to_float(item_value['ROIC'])
ev_ebit = to_float(item_value['EV/EBIT'])
return { 'name': item_name, 'roic': roic, 'ev_ebit': ev_ebit }
def rank(lst):
eligible = []
for item_name,item_value in lst.items():
item = transform(item_name, item_value)
if item['roic'] > 0 and item['ev_ebit'] > 0:
eligible.append(item)
eligible = sorted(eligible, key=lambda x: x['roic'], reverse=True)
for position, item in enumerate(eligible):
item['roic_rank'] = position
eligible = sorted(eligible, key=lambda x: x['ev_ebit'])
for position, item in enumerate(eligible):
item['ev_ebit_rank'] = position
item['final_rank'] = item['roic_rank'] + item['ev_ebit_rank']
eligible = sorted(eligible, key=lambda x: x['final_rank'])
return eligible
def output(lst):
ranked = rank(lst)
print('{0:<7} {1:<10} {2:<7} {3:<10} {4:<10} {5:<10}'.format(
'Papel',
'EV/EBIT',
'ROIC',
'EV/EBIT Rank',
'ROIC Rank',
'Final Rank'
))
for item in ranked:
print('{0:<7} {1:<10} {2:<7} {3:<10} {4:<10} {5:<10}'.format(
item['name'],
item['ev_ebit'],
item['roic'],
item['ev_ebit_rank'],
item['roic_rank'],
item['final_rank'],
))
if __name__ == '__main__':
from waitingbar import WaitingBar
THE_BAR = WaitingBar('[*] Downloading...')
lst = get_data()
THE_BAR.stop()
output(lst)