forked from DamianPiuselli/indexing_arg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparsing.py
More file actions
145 lines (116 loc) · 4.28 KB
/
parsing.py
File metadata and controls
145 lines (116 loc) · 4.28 KB
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
import pandas as pd
import yfinance as yf
from urllib import request
from urllib.request import Request, urlopen
import ssl
def convertir_ratio(string):
"""convierte el ratio de una string a:b a un float a/b"""
[a, b] = string.split(":")
return float(a) / float(b)
## Parseo de wikipedia. Tickers, Empresas, Sectores, reports.
## Parseo de slickcharts. Componentes.
def parsing_wikipedia():
# leer contenido https sin contexto.
url = "https://en.wikipedia.org/wiki/List_of_S%26P_500_companies"
context = ssl._create_unverified_context()
response = request.urlopen(url, context=context)
html = response.read()
return pd.read_html(html)[0]
def parsing_slickcharts():
# leer contenido https, con crawler bloqueado. Emular browser.
req = Request(
"https://www.slickcharts.com/sp500", headers={"User-Agent": "Mozilla/5.0"},
)
webpage = urlopen(req).read()
return pd.read_html(webpage)[0]
def parsing_cedears():
# leer contenido https sin contexto.
url = "https://www.comafi.com.ar/2254-CEDEAR-SHARES.note.aspx"
context = ssl._create_unverified_context()
response = request.urlopen(url, context=context)
html = response.read()
data = pd.read_html(html)[0]
# dropear datos innecesarios
data = data.drop(
columns=[
"Programa de CEDEAR",
"Alcance Público Inversor",
"Código Caja de Valores",
"CUSIP No.",
"Mercado de Origen",
"Frecuencia de Pago de Dividendo",
]
)
# renombrar campos
data = data.rename(
columns={
"Símbolo BYMA": "Symbol BYMA",
"Ticker en Mercado de Origen": "Symbol",
"Ratio CEDEARs/valor subyacente": "Ratio",
}
)
# setear index a ticker
data = data.set_index("Symbol")
##correcciones a los datos
# ticker BRK/B --> BRK-B
data = data.rename(index={"BRK/B": "BRK-B"})
# convertir Ratio de una string a un float
data["Ratio"] = data["Ratio"].apply(convertir_ratio)
return data
def main_data():
# datos
data = parsing_wikipedia()
componentes = parsing_slickcharts()
# mergear datos por ticker
new = pd.merge(data, componentes, on="Symbol")
# dropear datos innecesarios
new = new.drop(
columns=[
"Company",
"SEC filings",
"Headquarters Location",
"Date first added",
"Founded",
"#",
"Price",
"Chg",
"% Chg",
]
)
# setear index a ticker
new = new.set_index("Symbol")
##correcciones a los datos
# Ignorar sector real estate (no hay cedears disponibles para REITS)
# Ignorar utilities (no hay cedears disponibles)
new = new[new["GICS Sector"] != "Real Estate"]
new = new[new["GICS Sector"] != "Utilities"]
# ticker de berkshire (para yfinance) BRK/B --> BRK-B
new = new.rename(index={"BRK.B": "BRK-B"})
# mergear 2 tipos de acciones de google GOOGL -> GOOG+GOOGL
new.loc["GOOGL", "Weight"] = new.loc["GOOG", "Weight"] + new.loc["GOOGL", "Weight"]
new = new.drop("GOOG")
return new
def add_current_price(dataframe):
"""agrega columna al dataframe con el ultimo precio de cierre"""
tickers = dataframe.index.to_list()
price_data = yf.Tickers(tickers).download(period="1d", threads=True)
price_data.index = ["Price"] # cambio fecha actual a precio
# agrego la columna de precios (transponiendo el dataframe)
return pd.merge(dataframe, price_data["Close"].T, left_index=True, right_index=True)
def ytd_prices(dataframe):
"""devuelve los precios de cierre del ultimo año para los activos en un dataframe"""
tickers = dataframe.index.to_list() + ["SPY"]
price_data = yf.Tickers(tickers).download(
period="ytd", threads=True, proxy="http://proxy-cac.cnea.gov.ar:1280"
)
return price_data["Close"]
if __name__ == "__main__":
# Parseo a disco para evitar demoras de multiples http requests
# datos
data = main_data()
data_c = parsing_cedears()
target_weight_by_sector = data.groupby(["GICS Sector"]).sum(["Weight"])["Weight"]
# guardar datos
data.to_pickle("data/data_US.pkl")
data_c.to_pickle("data/data_ARG.pkl")
target_weight_by_sector.to_pickle("data/target_by_sector.pkl")