Skip to content

Commit

Permalink
trading information
Browse files Browse the repository at this point in the history
  • Loading branch information
Mael-J committed Feb 11, 2024
1 parent dc9d6b0 commit 1d42906
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 33 deletions.
49 changes: 43 additions & 6 deletions mstarpy/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,15 @@ 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.
Args:
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
Expand All @@ -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 = {
Expand Down Expand Up @@ -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
Expand Down
49 changes: 31 additions & 18 deletions mstarpy/stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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"):
"""
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -218,15 +218,15 @@ 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]

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"):
Expand Down Expand Up @@ -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"):
"""
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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):
"""
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
"""
Expand All @@ -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):
"""
Expand All @@ -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):
"""
Expand All @@ -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.
Expand Down Expand Up @@ -598,4 +611,4 @@ def valuation(self):
>>> Stock("visa", exchange="nyse").valuation()
"""
return self.GetData("valuation", url_suffixe="")
return self.GetData("valuation", url_suffix="")
20 changes: 11 additions & 9 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 1d42906

Please sign in to comment.