[Bug] Fixes the stocks ca cashflow command (#2486)

Fixes issue #2478

Co-authored-by: James Maslek <jmaslek11@gmail.com>
Co-authored-by: Colin Delahunty <72827203+colin99d@users.noreply.github.com>
This commit is contained in:
northern-64bit
2022-09-05 11:34:04 +02:00
committed by GitHub
parent d0a869784c
commit 9c5ad13e2a
4 changed files with 67 additions and 61 deletions

View File

@@ -735,7 +735,8 @@ class ComparisonAnalysisController(BaseController):
dest="s_timeframe",
type=str,
default=None,
help="Specify yearly/quarterly timeframe. Default is last.",
help="Specify year/quarter of the cashflow statement to be retrieved. The format for year is YYYY and for "
"quarter is DD-MMM-YYY (for example, 30-Sep-2021). Default is last year/quarter.",
)
ns_parser = self.parse_known_args_and_warn(
parser, other_args, EXPORT_ONLY_RAW_DATA_ALLOWED

View File

@@ -33,7 +33,7 @@ def get_financial_comparisons(
data : str
Data to get. Can be income, balance or cashflow
timeframe : str
What year to look at
What year/quarter to look at
quarter : bool
Flag to use quarterly data.
@@ -50,7 +50,9 @@ def get_financial_comparisons(
l_timeframes, ddf_financials = prepare_comparison_financials(symbols, data, quarter)
if timeframe:
if timeframe not in l_timeframes:
if (timeframe == str(datetime.now().year - 1)) and quarter:
timeframe = l_timeframes[-1]
elif timeframe not in l_timeframes:
raise ValueError(
f"Timeframe selected should be one of {', '.join(l_timeframes)}"
)
@@ -195,64 +197,67 @@ def prepare_df_financials(
if statement not in financial_urls:
raise ValueError(f"type {statement} is not in {financial_urls.keys()}")
period = "quarter" if quarter else "annual"
text_soup_financials = BeautifulSoup(
requests.get(
financial_urls[statement][period].format(ticker),
headers={"User-Agent": get_user_agent()},
).text,
"lxml",
)
# Define financials columns
a_financials_header = [
financials_header.text.strip("\n").split("\n")[0]
for financials_header in text_soup_financials.findAll(
"th", {"class": "overflow__heading"}
)
]
s_header_end_trend = ("5-year trend", "5- qtr trend")[quarter]
if s_header_end_trend in a_financials_header:
df_financials = pd.DataFrame(
columns=a_financials_header[
0 : a_financials_header.index(s_header_end_trend)
]
)
else:
# We don't have the data we need for whatever reason, so return an empty DataFrame
return pd.DataFrame()
find_table = text_soup_financials.findAll(
"div", {"class": "element element--table table--fixed financials"}
)
if not find_table:
return df_financials
financials_rows = find_table[0].findAll(
"tr", {"class": ["table__row is-highlighted", "table__row"]}
)
for a_row in financials_rows:
constructed_row = []
financial_columns = a_row.findAll(
"td", {"class": ["overflow__cell", "overflow__cell fixed--column"]}
try:
period = "quarter" if quarter else "annual"
text_soup_financials = BeautifulSoup(
requests.get(
financial_urls[statement][period].format(ticker),
headers={"User-Agent": get_user_agent()},
).text,
"lxml",
)
if not financial_columns:
continue
# Define financials columns
a_financials_header = [
financials_header.text.strip("\n").split("\n")[0]
for financials_header in text_soup_financials.findAll(
"th", {"class": "overflow__heading"}
)
]
for a_column in financial_columns:
column_to_text = a_column.text.strip()
if "\n" in column_to_text:
column_to_text = column_to_text.split("\n")[0]
s_header_end_trend = ("5-year trend", "5- qtr trend")[quarter]
if s_header_end_trend in a_financials_header:
df_financials = pd.DataFrame(
columns=a_financials_header[
0 : a_financials_header.index(s_header_end_trend)
]
)
else:
# We don't have the data we need for whatever reason, so return an empty DataFrame
return pd.DataFrame()
if column_to_text == "":
find_table = text_soup_financials.findAll(
"div", {"class": "element element--table table--fixed financials"}
)
if not find_table:
return df_financials
financials_rows = find_table[0].findAll(
"tr", {"class": ["table__row is-highlighted", "table__row"]}
)
for a_row in financials_rows:
constructed_row = []
financial_columns = a_row.findAll(
"td", {"class": ["overflow__cell", "overflow__cell fixed--column"]}
)
if not financial_columns:
continue
constructed_row.append(column_to_text)
df_financials.loc[len(df_financials)] = constructed_row
for a_column in financial_columns:
column_to_text = a_column.text.strip()
if "\n" in column_to_text:
column_to_text = column_to_text.split("\n")[0]
if column_to_text == "":
continue
constructed_row.append(column_to_text)
df_financials.loc[len(df_financials)] = constructed_row
except Exception:
df_financials = pd.DataFrame()
return df_financials

View File

@@ -146,7 +146,7 @@ def display_cashflow_comparison(
You can also get a list of comparable peers with
finnhub_peers(), finviz_peers(), polygon_peers().
timeframe : str
What year to look at
What year/quarter to look at
quarter : bool, optional
Whether to use quarterly statements, by default False
export : str, optional

View File

@@ -1,17 +1,17 @@
```text
usage: cashflow [-q] [-t S_TIMEFRAME] [--export {csv,json,xlsx}] [-h]
usage: cashflow [-q] [-t S_TIMEFRAME] [-h] [--export EXPORT]
```
Compares the cashflow statements of the selected companies.
Prints either yearly or quarterly cashflow statement the company, and compares it against similar companies.
```
optional arguments:
-q, --quarter Quarter financial data flag. (default: False)
-t S_TIMEFRAME, --timeframe S_TIMEFRAME
Specify yearly/quarterly timeframe. Default is last. (default: None)
--export {csv,json,xlsx}
Export dataframe data to csv,json,xlsx file (default: )
Specify year/quarter of the cashflow statement to be retrieved. The format for year is YYYY and for quarter is DD-MMM-YYY (for example,
30-Sep-2021). Default is last year/quarter. (default: None)
-h, --help show this help message (default: False)
--export EXPORT Export raw data into csv, json, xlsx (default: )
```
Example: