Fix url issue and change logic for edge cases

This commit is contained in:
nectariosouzo
2023-09-27 19:34:56 -04:00
parent b932f1cceb
commit 83d46a55bf
4 changed files with 28 additions and 28 deletions

View File

@@ -74,9 +74,10 @@ def get_stock_price_change(
Percent change of closing price over time period
"""
current_user = get_current_user()
url = f"""https://financialmodelingprep.com/api/v3/historical-price-full/{ticker}?
from={start_date}&to={end_date}&serietype=line
&apikey={current_user.credentials.API_KEY_FINANCIALMODELINGPREP}"""
url = f"""https://financialmodelingprep.com/api/v3/historical-price-full/{ticker}?\
from={start_date}&to={end_date}&serietype=line\
&apikey={current_user.credentials.API_KEY_FINANCIALMODELINGPREP}"""
response = request(url)
if response.status_code != 200 or "Error Message" in response.json():
message = f"Error, Status Code: {response.status_code}."
@@ -113,8 +114,8 @@ def get_etf_holdings(ticker: str) -> List[Dict[str, Any]]:
"""
current_user = get_current_user()
url = f"""https://financialmodelingprep.com/api/v3/etf-holder/{ticker}?
apikey={current_user.credentials.API_KEY_FINANCIALMODELINGPREP}"""
url = f"""https://financialmodelingprep.com/api/v3/etf-holder/{ticker}\
?apikey={current_user.credentials.API_KEY_FINANCIALMODELINGPREP}"""
response = request(url)
if response.status_code != 200 or "Error Message" in response.json():
message = f"Error, Status Code: {response.status_code}."
@@ -146,26 +147,25 @@ def get_holdings_pct_change(
pd.DataFrame
Calculated percentage change for each stock in the ETF, in descending order.
"""
holdings = get_etf_holdings(ticker=ticker)
if len(holdings) == 0:
return pd.DataFrame()
data_list = []
df = pd.DataFrame(columns=['Ticker', 'Name', 'Percent Change'], data=[])
holdings = get_etf_holdings(ticker)
for stock in holdings:
if stock["asset"]:
pct_change = get_stock_price_change(
ticker=stock["asset"], start_date=start_date, end_date=end_date
)
data_list.append(
{
"Ticker": stock["asset"],
"Name": stock["name"],
"Percent Change": pct_change,
}
)
else:
data_list.append({"Ticker": "", "Name": stock["name"], "Percent Change": 0})
pct_change = get_stock_price_change(stock["asset"], start_date, end_date)
if pct_change is None:
pct_change = 0
df = pd.DataFrame(data_list)
df.sort_values(by="Percent Change", ascending=False, inplace=True)
new_df = pd.DataFrame(
{
"Ticker": stock["asset"],
"Name": stock["name"],
"Percent Change": pct_change,
},
index=[0],
)
return df
df = pd.concat([df, new_df], ignore_index=True)
sorted_df = df.sort_values(by="Percent Change", ascending=False, inplace=False)
return sorted_df

View File

@@ -148,7 +148,7 @@ def view_etf_holdings_performance(
export: str
Type of format to export data
"""
data = fmp_model.get_holdings_pct_change(ticker, start_date, end_date)[0:limit]
data = fmp_model.get_holdings_pct_change(ticker, start_date, end_date).head(limit)
if raw:
print_rich_table(

View File

@@ -1 +1 @@
""
,Ticker,Name,Percent Change
1 Ticker Name Percent Change

View File

@@ -1 +1 @@
""
,Ticker,Name,Percent Change
1 Ticker Name Percent Change