File size: 8,942 Bytes
38171fa |
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
"""
"""
import logging
from django.conf import settings
import requests
from bs4 import BeautifulSoup
from core.models import MutualFund
from core.constants import MONEYCONTROL_TOPFUNDS_URL
from data_pipeline.interfaces.api_client import DataClient
logger = logging.getLogger(__name__)
settings.MORNINGSTAR_API_HEADERS = {
"X-RapidAPI-Key": settings.MORNINGSTAR_KEY,
"X-RapidAPI-Host": settings.MORNINGSTAR_HOST,
}
class MFList(DataClient):
model = MutualFund
# Monring Star List MF url
api_url = "https://lt.morningstar.com/api/rest.svc/g9vi2nsqjb/security/screener?page=1&pageSize=15000&sortOrder=name%20asc&outputType=json&version=1&languageId=en¤cyId=INR&universeIds=FOIND%24%24ALL%7CFCIND%24%24ALL&securityDataPoints=secId%2ClegalName%2CclosePrice%2CclosePriceDate%2Cyield_M12%2CongoingCharge%2CcategoryName%2CMedalist_RatingNumber%2CstarRatingM255%2CreturnD1%2CreturnW1%2CreturnM1%2CreturnM3%2CreturnM6%2CreturnM0%2CreturnM12%2CreturnM36%2CreturnM60%2CreturnM120%2CmaxFrontEndLoad%2CmanagerTenure%2CmaxDeferredLoad%2CexpenseRatio%2Cisin%2CinitialPurchase%2CfundTnav%2CequityStyleBox%2CbondStyleBox%2CaverageMarketCapital%2CaverageCreditQualityCode%2CeffectiveDuration%2CmorningstarRiskM255%2CalphaM36%2CbetaM36%2Cr2M36%2CstandardDeviationM36%2CsharpeM36%2CtrackRecordExtension&filters=&term="
def __init__(self) -> None:
self.api_response = None
self.transformed_data = None
def extract(self):
super().extract()
logger.info("Calling Morningstar API")
response = requests.get(self.api_url)
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Parse JSON response
self.api_response = response.json()
logger.info(
f'Morningstar API response received {len(self.api_response["rows"])} funds'
)
else:
logger.info("Received status code: {response.status_code}")
logger.info(response.json())
def transform(self):
"""
Transform the data to the format required by the model
"""
super().transform()
self.transformed_data = [
{
"fund_name": fund["legalName"],
"isin_number": fund.get("isin"),
"security_id": fund["secId"],
"data": {"list_info": fund},
}
for fund in self.api_response["rows"]
]
def load(self):
"""
Load the data into the database
"""
create_count = 0
update_count = 0
for data_dict in self.transformed_data:
try:
mf = self.model.objects.get(isin_number=data_dict["isin_number"])
mf.data.update(data_dict["data"])
mf.save()
update_count += 1
except self.model.DoesNotExist:
mf = self.model(**data_dict)
mf.save()
create_count += 1
logger.info(
"Created %s records; Updated %s records", create_count, update_count
)
class MFQuote(DataClient):
model = MutualFund
# Monring Star get quote url
api_url = f"https://{settings.MORNINGSTAR_HOST}/etf/get-quote"
def __init__(self, isin) -> None:
self.api_response = None # {"quotes": None, "holdings": None}
self.transformed_data = None
self.isin = isin
self.mf = self.model.objects.get(isin_number=self.isin)
def extract(self):
logger.info(f"Calling Morningstar Quote API for quotes with isin {self.isin}")
querystring = {"securityId": self.mf.security_id}
response = requests.get(
self.api_url, headers=settings.MORNINGSTAR_API_HEADERS, params=querystring
)
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Parse JSON response
self.api_response = response.json()
else:
logger.info(f"API response: %s", response.status_code)
response.raise_for_status()
def load(self):
self.mf.data.update({"quotes": self.transformed_data})
self.mf.save()
logger.info(f"Successfully stored data of quotes for {self.mf.fund_name}")
class MFHoldings(DataClient):
model = MutualFund
api_url = f"https://{settings.MORNINGSTAR_HOST}/etf/portfolio/get-holdings"
def __init__(self, isin) -> None:
self.api_response = None
self.transformed_data = None
self.isin = isin
self.mf = self.model.objects.get(isin_number=self.isin)
def extract(self):
querystring = {"securityId": self.mf.security_id}
response = requests.get(
self.api_url, headers=settings.MORNINGSTAR_API_HEADERS, params=querystring
)
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Parse JSON response
self.api_response = response.json()
else:
logger.info(f"received status code {response.status_code} for {self.isin}")
logger.debug(response.content)
response.raise_for_status()
def load(self):
self.mf.data.update({"holdings": self.transformed_data})
self.mf.save()
logger.info(f"Successfully stored data of holdings for {self.mf.fund_name}")
class MFRiskMeasures(DataClient):
model = MutualFund
api_url = (
f"https://{settings.MORNINGSTAR_HOST}/etf/risk/get-risk-volatility-measures"
)
def __init__(self, isin) -> None:
self.api_response = None
self.isin = isin
self.mf = self.model.objects.get(isin_number=self.isin)
def extract(self):
querystring = {"securityId": self.mf.security_id}
response = requests.get(
self.api_url, headers=settings.MORNINGSTAR_API_HEADERS, params=querystring
)
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Parse JSON response
self.api_response = response.json()
else:
logger.info(response.json())
response.raise_for_status()
def load(self):
self.mf.data.update({"risk_measures": self.transformed_data})
self.mf.save()
logger.info(
f"Successfully stored data of risk measures for {self.mf.fund_name}"
)
class MFRanking(DataClient):
api_url = MONEYCONTROL_TOPFUNDS_URL
model = MutualFund
def __init__(self) -> None:
self.api_response = None
self.transformed_data = None
def extract(self) -> None:
"""
Fetches the top mutual funds from MoneyControl website based on their returns and
returns a tuple containing lists of fund names, fund types, CRISIL ranks,
INF numbers, and AUM data of top mutual funds.
"""
super().extract()
logger.info("Fetching top mutual funds from MoneyControl website")
response = requests.get(self.api_url)
# Check if the request was successful (status code 200)
response.raise_for_status()
soup = BeautifulSoup(response.text, "html.parser")
# Find all rows containing fund information
fund_rows = soup.find_all("tr", class_=lambda x: x and "INF" in x)
logger.info("Found %s rows", len(fund_rows))
fund_details = []
# Extract fund name from each row of sponsored funds
for row in fund_rows:
columns = row.find_all("td")
fund_name = columns[0].text.strip()
fund_type = columns[2].text.strip()
crisil_rank = columns[3].text.strip()
aum = columns[4].text.strip()
isin_number = row["class"][0]
fund_details.append(
{
"fund_name": fund_name,
"fund_type": fund_type,
"crisil_rank": crisil_rank,
"isin_number": isin_number,
"aum": aum,
}
)
self.api_response = fund_details
def load(self) -> None:
"""
Load the data into the database
"""
# clear the rank field
MutualFund.objects.exclude(rank=None).update(rank=None)
for rank, fund_details in enumerate(self.transformed_data, 1):
mf = MutualFund.objects.get(isin_number=fund_details["isin_number"])
mf.crisil_rank = (
fund_details["crisil_rank"] if fund_details["crisil_rank"] != "-" else 0
)
mf.rank = rank
mf.aum = float(fund_details["aum"].replace(",", ""))
mf.save()
logger.info(
f"Updated {rank=} {mf.fund_name} | {fund_details=} {fund_details['crisil_rank']=} {fund_details['aum']=}"
)
|