-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: more tool like eps, current ratio (#2)
- Loading branch information
1 parent
dd34b3c
commit c0bb02c
Showing
4 changed files
with
182 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
from typing import Any | ||
import yfinance as yf | ||
import pandas as pd | ||
def get_parameter(symbol, parameter: str) -> Any: | ||
"""Retrieve a value for a specific parameter from the data dictionary. | ||
Args: | ||
symbol (str): stock symbol | ||
parameter (str): The key representing the parameter. | ||
Returns: | ||
Any: The value of the parameter, or a message if not found. | ||
""" | ||
suffix = '.NS' | ||
stock = yf.Ticker(symbol+suffix) | ||
return stock.info.get(parameter, None) | ||
|
||
def get_address1(symbol:str) -> str: | ||
"""Retrieve address1. | ||
Args: | ||
symbol (str): Stock symbol | ||
Return: | ||
str: address | ||
""" | ||
return get_parameter(symbol, 'address1') | ||
|
||
|
||
def get_current_price(symbol:str) -> float: | ||
"""Retrieve the current stock price. | ||
Args: | ||
symbol (str): Stock symbol | ||
Return: | ||
float: price | ||
""" | ||
return get_parameter(symbol, 'currentPrice') | ||
|
||
|
||
def get_marketcap(symbol:str) -> float: | ||
"""Retrieve the marketcap | ||
Args: | ||
symbol (str): Stock symbol | ||
Return: | ||
float: price | ||
""" | ||
return get_parameter(symbol, 'marketCap') | ||
|
||
def get_beta(symbol:str) -> float: | ||
"""Retrieve the beta | ||
Args: | ||
symbol (str): Stock symbol | ||
Return: | ||
float: price | ||
""" | ||
return get_parameter(symbol, 'beta') | ||
|
||
def get_stock_prices(symbol:str, duration:str) -> pd.DataFrame: | ||
"""Get stock prices from yahoo finance api | ||
Args: | ||
symbol (str): stock symbol | ||
duration (str): time duration | ||
Returns: | ||
pd.DataFrame: stock prices | ||
""" | ||
suffix='.NS' | ||
durations = {'1 year': '1y', '1month': '1mo', '1 month': '1mo', '1M': '1mo', '1year': '1y'} | ||
if duration not in durations: | ||
durations[duration] = duration | ||
try: | ||
stock = yf.Ticker(symbol+suffix) | ||
data = stock.history(period=durations[duration]) | ||
return data | ||
except Exception as e: | ||
return None | ||
|
||
|
||
def current_pe_ratio(symbol:str) -> int: | ||
"""Get current pe ratio for a stock | ||
Args: | ||
symbol (str): stock symbol | ||
Returns: | ||
int: current pe ratio | ||
""" | ||
return get_parameter(symbol, 'trailingPE') | ||
|
||
import yfinance as yf | ||
|
||
def get_52_week_low(symbol: str) -> float: | ||
"""Retrieve the 52 week low price | ||
Args: | ||
symbol (str): Stock symbol | ||
Return: | ||
float: 52week low price | ||
""" | ||
return get_parameter(symbol, 'fiftyTwoWeekLow') | ||
|
||
def get_52_week_high(symbol: str): | ||
"""Retrieve the debt-to-equity | ||
Args: | ||
symbol (str): Stock symbol | ||
Return: | ||
float: 52 week high prices | ||
""" | ||
return get_parameter(symbol, 'fiftyTwoWeekHigh') | ||
|
||
def get_price_to_book(symbol: str): | ||
"""Retrieve the price to book value | ||
Args: | ||
symbol (str): Stock symbol | ||
Return: | ||
float: price to book value | ||
""" | ||
return get_parameter(symbol, 'priceToBook') | ||
|
||
def get_eps(symbol: str) -> float: | ||
"""Retrieve the get EPS | ||
Args: | ||
symbol (str): Stock symbol | ||
Return: | ||
float: EPS | ||
""" | ||
return get_parameter(symbol, 'trailingEps') | ||
|
||
def get_current_ratio(symbol: str)-> float: | ||
"""Retrieve the current ratio | ||
Args: | ||
symbol (str): Stock symbol | ||
Return: | ||
float: current ratio | ||
""" | ||
return get_parameter(symbol, 'currentRatio') | ||
|
||
def get_debt_to_equity(symbol: str)-> float: | ||
"""Retrieve the debt-to-equity | ||
Args: | ||
symbol (str): Stock symbol | ||
Return: | ||
float: debt to equity | ||
""" | ||
return get_parameter(symbol, 'debtToEquity') | ||
|
||
def get_free_cash_flow(symbol: str): | ||
"""Retrieve the free cash flow | ||
Args: | ||
symbol (str): Stock symbol | ||
Return: | ||
float: cashflow | ||
""" | ||
return get_parameter(symbol, 'freeCashflow') |