-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue.py
118 lines (86 loc) · 4.31 KB
/
value.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
import sys
import argparse
import pandas as pd
def clean_data(df):
# Replace missing values with "0,0" in column: 'Direktav. - Senaste'
df = df.fillna({'Direktav. - Senaste': "0,0"})
# Drop rows with missing data in column: 'P/S - Senaste'
df = df.dropna(subset=['P/S - Senaste'])
# Drop rows with missing data in column: 'P/B - Senaste'
df = df.dropna(subset=['P/B - Senaste'])
# Drop rows with missing data in column: 'Kursutveck. - Utveck. 1år'
df = df.dropna(subset=['Kursutveck. - Utveck. 1år'])
# Convert to float: Kursutveck. - Utveck. 6m
Column = 'Kursutveck. - Utveck. 6m'
# Step 1: Remove the percentage symbol and convert to float
df[Column] = df[Column].str.replace(',', '.').str.rstrip('%').astype(float)
# Step 2: Divide by 100 to get the decimal representation of the percentages
df[Column] = df[Column] / 100
# Convert to float: Kursutveck. - Utveck. 3m
Column = 'Kursutveck. - Utveck. 3m'
# Step 1: Remove the percentage symbol and convert to float
df[Column] = df[Column].str.replace(',', '.').str.rstrip('%').astype(float)
# Step 2: Divide by 100 to get the decimal representation of the percentages
df[Column] = df[Column] / 100
# Convert to float: Kursutveck. - Utveck. 1år
Column = 'Kursutveck. - Utveck. 1år'
# Step 1: Remove the percentage symbol and convert to float
df[Column] = df[Column].str.replace(',', '.').str.rstrip('%').astype(float)
# Step 2: Divide by 100 to get the decimal representation of the percentages
df[Column] = df[Column] / 100
# Convert to float: Direktav. - Senaste
Column = 'Direktav. - Senaste'
# Step 1: Remove the percentage symbol and convert to float
df[Column] = df[Column].str.replace(',', '.').str.rstrip('%').astype(float)
# Step 2: Divide by 100 to get the decimal representation of the percentages
df[Column] = df[Column] / 100
# Convert to float: P/E Senaste
Column = 'P/E - Senaste'
# Step 1: Remove the percentage symbol and convert to float
df[Column] = df[Column].str.replace(',', '.').str.rstrip('%').astype(float)
# Step 2: Divide by 100 to get the decimal representation of the percentages
df[Column] = df[Column]
# Convert to float: P/S Senaste
Column = 'P/S - Senaste'
# Step 1: Remove the percentage symbol and convert to float
df[Column] = df[Column].str.replace(',', '.').str.rstrip('%').astype(float)
# Step 2: Divide by 100 to get the decimal representation of the percentages
df[Column] = df[Column]
# Convert to float: P/B Senaste
Column = 'P/B - Senaste'
# Step 1: Remove the percentage symbol and convert to float
df[Column] = df[Column].str.replace(',', '.').str.rstrip('%').astype(float)
# Step 2: Divide by 100 to get the decimal representation of the percentages
df[Column] = df[Column]
return df
"""
Index(['Börsdata ID', 'Bolagsnamn', 'Kursutveck. - Utveck. 6m',
'Kursutveck. - Utveck. 1år', 'Kursutveck. - Utveck. 3m',
'EV/EBITDA - Senaste', 'P/FCF - Senaste', 'Utdelning - Senaste',
'Info - Ticker', 'Direktav. - Senaste', 'P/E - Senaste',
'P/S - Senaste', 'P/B - Senaste', 'Info - Tid', 'Info - Aktiekurs',
'Info - Rapport', 'Info - Sektor'],
dtype='object')
"""
def create_ranking(df):
rankings = { 'P/E - Senaste': -1, 'P/FCF - Senaste': -1, 'EV/EBITDA - Senaste': -1, 'P/S - Senaste': -1, 'Direktav. - Senaste': 1 }
ratio_scores = pd.DataFrame()
for column, rank in rankings.items():
ratio_scores[column] = df[column].rank(ascending=rank)
return ratio_scores
def calc_momentum(df):
mom = df[['Kursutveck. - Utveck. 3m', 'Kursutveck. - Utveck. 6m', 'Kursutveck. - Utveck. 1år']].mean(axis=1)
return mom
def calc_composite_value(df):
comp_value = df.mean(axis=1)
return comp_value
if __name__ == "__main__":
df = pd.read_csv(sys.argv[1])
df_clean = clean_data(df.copy())
df_clean.head()
ranking = create_ranking(df_clean)
df_clean['comp_value'] = calc_composite_value(ranking)
df_clean['momentum'] = calc_momentum(df_clean)
a = df_clean.sort_values('comp_value', ascending=False)
result = a.head(80).sort_values('momentum', ascending=False)
print(result.head(15))