From 1d4290655619d3daf52160e0f52224469969dfb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl?= Date: Sun, 11 Feb 2024 21:37:06 +0100 Subject: [PATCH] trading information --- mstarpy/security.py | 49 +++++++++++++++++++++++++++++++++++++++------ mstarpy/stock.py | 49 ++++++++++++++++++++++++++++----------------- test.py | 20 +++++++++--------- 3 files changed, 85 insertions(+), 33 deletions(-) diff --git a/mstarpy/security.py b/mstarpy/security.py index 05d32e6..3368311 100644 --- a/mstarpy/security.py +++ b/mstarpy/security.py @@ -155,7 +155,7 @@ def __init__( else: raise ValueError(f"0 {self.asset_type} found with the term {term}") - def GetData(self, field, params={}, headers={}, url_suffixe="data"): + def GetData(self, field, params={}, headers={}, url_suffix="data"): """ Generic function to use MorningStar global api. @@ -163,7 +163,7 @@ def GetData(self, field, params={}, headers={}, url_suffixe="data"): field (str) : endpoint of the request params (dict) : parameter for the request headers (dict) : headers of the request - url_suffixe (str) : suffie of the url + url_suffix (str) : suffixe of the url Raises: TypeError raised whenever type of paramater are invalid @@ -182,14 +182,14 @@ def GetData(self, field, params={}, headers={}, url_suffixe="data"): if not isinstance(params, dict): raise TypeError("params parameter should be a dict") - if not isinstance(url_suffixe, str): - raise TypeError("url_suffixe parameter should be a string") + if not isinstance(url_suffix, str): + raise TypeError("url_suffix parameter should be a string") # url of API url = f"""https://api-global.morningstar.com/sal-service/v1/{self.asset_type}/{field}/{self.code}""" - if url_suffixe: - url += f"""/{url_suffixe}""" + if url_suffix: + url += f"""/{url_suffix}""" # headers default_headers = { @@ -248,6 +248,43 @@ def ltData(self, field, currency="EUR"): else: return {} + def RealtimeData(self, url_suffix: str) -> dict: + """ + This function retrieves historical data of the specified fields + + Args: + url_suffix (str) : suffixe of the url + + Returns: + dict of realtime data + + Examples: + >>> Stock("visa", "us").RealtimeData("quotes") + + Raises: + TypeError: raised whenever the parameter type + is not the type expected + ConnectionError : raised whenever the response is not 200 OK + + """ + # error raised if url_suffix is not a string + if not isinstance(url_suffix, str): + raise TypeError("url_suffix parameter should be a string or a list") + # url for realtime data + url = f"""https://www.morningstar.com/api/v2/stores/realtime/{url_suffix}?securities={self.code}""" + + # header with user agent + headers = { + 'user-agent': random_user_agent(), + } + # response + response = requests.get(url, headers=headers, proxies=self.proxies, + timeout=60) + # manage response + not_200_response(url, response) + # result + return response.json() + def TimeSeries(self, field, start_date, end_date, frequency="daily"): """ This function retrieves historical data of the specified fields diff --git a/mstarpy/stock.py b/mstarpy/stock.py index afaa3a7..7722808 100644 --- a/mstarpy/stock.py +++ b/mstarpy/stock.py @@ -51,7 +51,7 @@ def analysisData(self): >>> Stock("visa", exchange="nyse").analysisData() """ - return self.GetData("morningstarTake/v3", url_suffixe="analysisData") + return self.GetData("morningstarTake/v3", url_suffix="analysisData") def analysisReport(self): """ @@ -64,7 +64,7 @@ def analysisReport(self): >>> Stock("visa", exchange="nyse").analysisReport() """ - return self.GetData("morningstarTake/v4", url_suffixe="analysisReport") + return self.GetData("morningstarTake/v4", url_suffix="analysisReport") def balanceSheet(self, period="annual", reportType="original"): """ @@ -156,7 +156,7 @@ def financialHealth(self): >>> Stock("visa", exchange="nyse").financialHealth() """ - return self.GetData("keyStats/financialHealth", url_suffixe="") + return self.GetData("keyStats/financialHealth", url_suffix="") def financialStatement( self, statement="summary", period="annual", reportType="original" @@ -218,7 +218,7 @@ def financialStatement( params = {"reportType": reportType_choice[reportType]} if statement == "summary": return self.GetData( - "newfinancials", params=params, url_suffixe=f"{period}/summary" + "newfinancials", params=params, url_suffix=f"{period}/summary" ) params["dataType"] = period_choice[period] @@ -226,7 +226,7 @@ def financialStatement( return self.GetData( "newfinancials", params=params, - url_suffixe=f"{statement_choice[statement]}/detail", + url_suffix=f"{statement_choice[statement]}/detail", ) def financialSummary(self, period="annual", reportType="original"): @@ -259,7 +259,7 @@ def freeCashFlow(self): >>> Stock("visa", exchange="nyse").freeCashFlow() """ - return self.GetData("keyStats/cashFlow", url_suffixe="") + return self.GetData("keyStats/cashFlow", url_suffix="") def historical(self, start_date, end_date, frequency="daily"): """ @@ -323,7 +323,7 @@ def institutionBuyers(self, top=20): raise TypeError("top parameter should be an integer") return self.GetData( - "ownership/v1", url_suffixe=f"Buyers/institution/{top}/data" + "ownership/v1", url_suffix=f"Buyers/institution/{top}/data" ) def institutionConcentratedOwners(self, top=20): @@ -344,7 +344,7 @@ def institutionConcentratedOwners(self, top=20): raise TypeError("top parameter should be an integer") return self.GetData( - "ownership/v1", url_suffixe=f"ConcentratedOwners/institution/{top}/data" + "ownership/v1", url_suffix=f"ConcentratedOwners/institution/{top}/data" ) def institutionOwnership(self, top=20): @@ -365,7 +365,7 @@ def institutionOwnership(self, top=20): raise TypeError("top parameter should be an integer") return self.GetData( - "ownership/v1", url_suffixe=f"OwnershipData/institution/{top}/data" + "ownership/v1", url_suffix=f"OwnershipData/institution/{top}/data" ) def institutionSellers(self, top=20): @@ -385,7 +385,7 @@ def institutionSellers(self, top=20): raise TypeError("top parameter should be an integer") return self.GetData( - "ownership/v1", url_suffixe=f"Sellers/institution/{top}/data" + "ownership/v1", url_suffix=f"Sellers/institution/{top}/data" ) def keyExecutives(self): @@ -431,7 +431,7 @@ def mutualFundBuyers(self, top=20): if not isinstance(top, int): raise TypeError("top parameter should be an integer") - return self.GetData("ownership/v1", url_suffixe=f"Buyers/mutualfund/{top}/data") + return self.GetData("ownership/v1", url_suffix=f"Buyers/mutualfund/{top}/data") def mutualFundConcentratedOwners(self, top=20): """ @@ -451,7 +451,7 @@ def mutualFundConcentratedOwners(self, top=20): raise TypeError("top parameter should be an integer") return self.GetData( - "ownership/v1", url_suffixe=f"ConcentratedOwners/mutualfund/{top}/data" + "ownership/v1", url_suffix=f"ConcentratedOwners/mutualfund/{top}/data" ) def mutualFundOwnership(self, top=20): @@ -472,7 +472,7 @@ def mutualFundOwnership(self, top=20): raise TypeError("top parameter should be an integer") return self.GetData( - "ownership/v1", url_suffixe=f"OwnershipData/mutualfund/{top}/data" + "ownership/v1", url_suffix=f"OwnershipData/mutualfund/{top}/data" ) def mutualFundSellers(self, top=20): @@ -493,7 +493,7 @@ def mutualFundSellers(self, top=20): raise TypeError("top parameter should be an integer") return self.GetData( - "ownership/v1", url_suffixe=f"Sellers/mutualfund/{top}/data" + "ownership/v1", url_suffix=f"Sellers/mutualfund/{top}/data" ) def operatingGrowth(self): @@ -507,7 +507,7 @@ def operatingGrowth(self): >>> Stock("visa", exchange="nyse").operatingGrowth() """ - return self.GetData("keyStats/growthTable", url_suffixe="") + return self.GetData("keyStats/growthTable", url_suffix="") def operatingMargin(self): """ @@ -520,7 +520,7 @@ def operatingMargin(self): >>> Stock("visa", exchange="nyse").operatingMargin() """ - return self.GetData("keyStats/OperatingAndEfficiency", url_suffixe="") + return self.GetData("keyStats/OperatingAndEfficiency", url_suffix="") def operatingPerformance(self): """ @@ -533,7 +533,7 @@ def operatingPerformance(self): >>> Stock("visa", exchange="nyse").operatingPerformance() """ - return self.GetData("operatingPerformance/v2", url_suffixe="") + return self.GetData("operatingPerformance/v2", url_suffix="") def split(self): """ @@ -548,6 +548,19 @@ def split(self): """ return self.GetData("split") + def tradingInformation(self) -> dict: + """ + This function retrieves the trading information of the stock + such as Previous Close Price, Day Range, 52-Week Range, Bid/Ask, + Market Cap, Volume/Avg + Returns: + dict with performance + Examples: + >>> Stock("visa", exchange="nyse").tradingInformation() + + """ + return self.RealtimeData("quotes") + def trailingTotalReturn(self): """ This function retrieves the performance of the stock and its index. @@ -598,4 +611,4 @@ def valuation(self): >>> Stock("visa", exchange="nyse").valuation() """ - return self.GetData("valuation", url_suffixe="") + return self.GetData("valuation", url_suffix="") diff --git a/test.py b/test.py index 442dfd1..0040f14 100644 --- a/test.py +++ b/test.py @@ -8,15 +8,17 @@ -code = "FOUSA00LIX" -fund = Funds(code,country="us") -print(fund.name) - -print(fund.carbonMetrics()) -start_date = datetime.date(2018,1,1) -end_date = datetime.date.today() -history = fund.nav(start_date,end_date) -print(history) +# code = "FOUSA00LIX" +# fund = Funds(code,country="us") +# print(fund.name) + +# print(fund.carbonMetrics()) +# start_date = datetime.date(2018,1,1) +# end_date = datetime.date.today() +# history = fund.nav(start_date,end_date) +# print(history) + +print(Stock("visa", exchange="nyse").tradingInformation()) #print(Funds("myria").investmentLookup()) # print(list(EXCHANGE))