forked from kubadlugosz/algorithmic-trading
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magic_formula.py
164 lines (138 loc) · 7.6 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
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
# ============================================================================
# Greenblatt's Magic Formula Implementation
# Author - Mayank Rasu
# Please report bugs/issues in the Q&A section
# =============================================================================
import requests
from bs4 import BeautifulSoup
import pandas as pd
tickers = ["MMM","AXP","AAPL","BA","CAT","CVX","CSCO","KO","DIS","DWDP",
"XOM","GE","GS","HD","IBM","INTC","JNJ","JPM","MCD","MRK",
"MSFT","NKE","PFE","PG","TRV","UTX","UNH","VZ","V","WMT"]
#list of tickers whose financial data needs to be extracted
financial_dir = {}
for ticker in tickers:
#getting balance sheet data from yahoo finance for the given ticker
temp_dir = {}
url = 'https://in.finance.yahoo.com/quote/'+ticker+'/balance-sheet?p='+ticker
print(url)
page = requests.get(url)
page_content = page.content
soup = BeautifulSoup(page_content,'html.parser')
tabl = soup.find_all("table", {"class" : "Lh(1.7) W(100%) M(0)"})
for t in tabl:
rows = t.find_all("tr")
for row in rows:
if len(row.get_text(separator='|').split("|")[0:2])>1:
temp_dir[row.get_text(separator='|').split("|")[0]]=row.get_text(separator='|').split("|")[1]
#getting income statement data from yahoo finance for the given ticker
url = 'https://in.finance.yahoo.com/quote/'+ticker+'/financials?p='+ticker
page = requests.get(url)
page_content = page.content
soup = BeautifulSoup(page_content,'html.parser')
tabl = soup.find_all("table", {"class" : "Lh(1.7) W(100%) M(0)"})
for t in tabl:
rows = t.find_all("tr")
for row in rows:
if len(row.get_text(separator='|').split("|")[0:2])>1:
temp_dir[row.get_text(separator='|').split("|")[0]]=row.get_text(separator='|').split("|")[1]
#getting cashflow statement data from yahoo finance for the given ticker
url = 'https://in.finance.yahoo.com/quote/'+ticker+'/cash-flow?p='+ticker
page = requests.get(url)
page_content = page.content
soup = BeautifulSoup(page_content,'html.parser')
tabl = soup.find_all("table", {"class" : "Lh(1.7) W(100%) M(0)"})
for t in tabl:
rows = t.find_all("tr")
for row in rows:
if len(row.get_text(separator='|').split("|")[0:2])>1:
temp_dir[row.get_text(separator='|').split("|")[0]]=row.get_text(separator='|').split("|")[1]
#getting key statistics data from yahoo finance for the given ticker
url = 'https://in.finance.yahoo.com/quote/'+ticker+'/key-statistics?p='+ticker
page = requests.get(url)
page_content = page.content
soup = BeautifulSoup(page_content,'html.parser')
tabl = soup.findAll("table", {"class": "table-qsp-stats Mt(10px)"})
for t in tabl:
rows = t.find_all("tr")
for row in rows:
if len(row.get_text(separator='|').split("|")[0:2])>0:
temp_dir[row.get_text(separator='|').split("|")[0]]=row.get_text(separator='|').split("|")[-1]
#combining all extracted information with the corresponding ticker
financial_dir[ticker] = temp_dir
#storing information in pandas dataframe
combined_financials = pd.DataFrame(financial_dir)
combined_financials.dropna(how='all',axis=1,inplace=True) #dropping columns with all NaN values
tickers = combined_financials.columns #updating the tickers list based on only those tickers whose values were successfully extracted
# creating dataframe with relevant financial information for each stock using fundamental data
stats = ["Earnings before interest and taxes",
"Market cap (intra-day)",
"Net income applicable to common shares",
"Total cash flow from operating activities",
"Capital expenditure",
"Total current assets",
"Total current liabilities",
"Property plant and equipment",
"Total stockholder equity",
"Long-term debt",
"Preferred stock",
"Minority interest",
"Forward annual dividend yield"] # change as required
indx = ["EBIT","MarketCap","NetIncome","CashFlowOps","Capex","CurrAsset",
"CurrLiab","PPE","BookValue","TotDebt","PrefStock","MinInterest","DivYield"]
all_stats = {}
for ticker in tickers:
try:
temp = combined_financials[ticker]
ticker_stats = []
for stat in stats:
ticker_stats.append(temp.loc[stat])
all_stats['{}'.format(ticker)] = ticker_stats
except:
print("can't read data for ",ticker)
all_stats_df = pd.DataFrame(all_stats,index=indx)
# cleansing of fundamental data imported in dataframe
all_stats_df.iloc[1,:] = [x.replace("M","E+03") for x in all_stats_df.iloc[1,:].values]
all_stats_df.iloc[1,:] = [x.replace("B","E+06") for x in all_stats_df.iloc[1,:].values]
all_stats_df.iloc[1,:] = [x.replace("T","E+09") for x in all_stats_df.iloc[1,:].values]
all_stats_df.iloc[-1,:] = [str(x).replace("%","E-02") for x in all_stats_df.iloc[-1,:].values]
all_stats_df[tickers] = all_stats_df[tickers].replace({',': ''}, regex=True)
for ticker in all_stats_df.columns:
all_stats_df[ticker] = pd.to_numeric(all_stats_df[ticker].values,errors='coerce')
# calculating relevant financial metrics for each stock
transpose_df = all_stats_df.transpose()
final_stats_df = pd.DataFrame()
final_stats_df["EBIT"] = transpose_df["EBIT"]
final_stats_df["TEV"] = transpose_df["MarketCap"].fillna(0) \
+transpose_df["TotDebt"].fillna(0) \
+transpose_df["PrefStock"].fillna(0) \
+transpose_df["MinInterest"].fillna(0) \
-(transpose_df["CurrAsset"].fillna(0)-transpose_df["CurrLiab"].fillna(0))
final_stats_df["EarningYield"] = final_stats_df["EBIT"]/final_stats_df["TEV"]
final_stats_df["FCFYield"] = (transpose_df["CashFlowOps"]-transpose_df["Capex"])/transpose_df["MarketCap"]
final_stats_df["ROC"] = transpose_df["EBIT"]/(transpose_df["PPE"]+transpose_df["CurrAsset"]-transpose_df["CurrLiab"])
final_stats_df["BookToMkt"] = transpose_df["BookValue"]/transpose_df["MarketCap"]
final_stats_df["DivYield"] = transpose_df["DivYield"]
################################Output Dataframes##############################
# finding value stocks based on Magic Formula
final_stats_val_df = final_stats_df.loc[tickers,:]
final_stats_val_df["CombRank"] = final_stats_val_df["EarningYield"].rank(ascending=False,na_option='bottom')+final_stats_val_df["ROC"].rank(ascending=False,na_option='bottom')
final_stats_val_df["MagicFormulaRank"] = final_stats_val_df["CombRank"].rank(method='first')
value_stocks = final_stats_val_df.sort_values("MagicFormulaRank").iloc[:,[2,4,8]]
print("------------------------------------------------")
print("Value stocks based on Greenblatt's Magic Formula")
print(value_stocks)
# finding highest dividend yield stocks
high_dividend_stocks = final_stats_df.sort_values("DivYield",ascending=False).iloc[:,6]
print("------------------------------------------------")
print("Highest dividend paying stocks")
print(high_dividend_stocks)
# # Magic Formula & Dividend yield combined
final_stats_df["CombRank"] = final_stats_df["EarningYield"].rank(ascending=False,method='first') \
+final_stats_df["ROC"].rank(ascending=False,method='first') \
+final_stats_df["DivYield"].rank(ascending=False,method='first')
final_stats_df["CombinedRank"] = final_stats_df["CombRank"].rank(method='first')
value_high_div_stocks = final_stats_df.sort_values("CombinedRank").iloc[:,[2,4,6,8]]
print("------------------------------------------------")
print("Magic Formula and Dividend Yield combined")
print(value_high_div_stocks)