Skip to content

Commit

Permalink
Updated base to properly check for available dataframes holding the…
Browse files Browse the repository at this point in the history
… last 100 13F filing addresses (without this change script will fail the pytest).
  • Loading branch information
git-shogg committed Jun 16, 2024
1 parent 5868237 commit b28521c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
29 changes: 17 additions & 12 deletions finsec/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def _validate_cik(self, cik:str):

def _get_last_100_13f_filings_url(self):
"""Searches the last 13F-HR and 13F-HR/A filings. Returns a 13f_filings variable and 13f_amendment_filings variable"""
if self._13f_filings or self._13f_amendment_filings:
if self._13f_filings is not None or self._13f_amendment_filings is not None:
return

webpage = requests.get(_13F_SEARCH_URL_.format(self.cik),headers=_REQ_HEADERS_)
Expand Down Expand Up @@ -197,26 +197,31 @@ def _parse_13f_url(self, url:str, date:str):
def _apply_amendments(self, qtr_year_str:str, original_cover_page:dict, original_holdings_table:pd.DataFrame, original_simplified_holdings_table: pd.DataFrame):
self._13f_amendment_filings_period_of_filings()
select_amendment_filings = self._13f_amendment_filings[self._13f_amendment_filings['Period of Report Quarter Year'] == qtr_year_str].iloc[::-1] # Check for matching amendments and reverse the order of the list so amendments can be made chronologically.

# Start by setting the output variables, this ensures that if no amendment filings are found, these can be returned as is, unaltered.
output_cover_page = original_cover_page # Set as original cover page to begin with
output_holdings_table = original_holdings_table
output_simplified_holdings_table = original_simplified_holdings_table

if len(select_amendment_filings) > 0:
for index, row in select_amendment_filings.iterrows():
a_cover_page, a_holdings_table, a_simplified_holdings_table = self._parse_13f_url(row['url'], row['Filing Date'])
amended_cover_page = original_cover_page # Set as original cover page to begin with
if a_cover_page["amendment_type"] == "NEW HOLDINGS":

amended_cover_page['portfolio_value'] = original_cover_page['portfolio_value'] + a_cover_page['portfolio_value']
amended_cover_page['count_holdings'] = original_cover_page['count_holdings'] + a_cover_page['count_holdings']
output_cover_page['portfolio_value'] = original_cover_page['portfolio_value'] + a_cover_page['portfolio_value']
output_cover_page['count_holdings'] = original_cover_page['count_holdings'] + a_cover_page['count_holdings']

# original_holdings_table = original_holdings_table.append(a_holdings_table, ignore_index=True)
amended_holdings_table = pd.concat([original_holdings_table,a_holdings_table],ignore_index=True)
output_holdings_table = pd.concat([original_holdings_table,a_holdings_table],ignore_index=True)
# original_simplified_holdings_table = original_simplified_holdings_table.append(a_simplified_holdings_table, ignore_index=True)
amended_simplified_holdings_table = pd.concat([original_simplified_holdings_table,a_simplified_holdings_table], ignore_index=True)
amended_cover_page['filing_amended'] = True
output_simplified_holdings_table = pd.concat([original_simplified_holdings_table,a_simplified_holdings_table], ignore_index=True)
output_cover_page['filing_amended'] = True
else: # If it is a not a "New Holdings" filing type, simply overwrite the entirety of the previous filing.
amended_cover_page = a_cover_page
amended_holdings_table = a_holdings_table
amended_simplified_holdings_table = a_simplified_holdings_table
amended_cover_page['filing_amended'] = True
return amended_cover_page, amended_holdings_table, amended_simplified_holdings_table
output_cover_page = a_cover_page
output_holdings_table = a_holdings_table
output_simplified_holdings_table = a_simplified_holdings_table
output_cover_page['filing_amended'] = True
return output_cover_page, output_holdings_table, output_simplified_holdings_table

def convert_filings_to_excel(self, simplified:bool = True, inc_cover_page_tabs:bool = False):
"""Outputs existing 'self.filings' dictionary to excel. Note that this will overwrite any existing files that may be present."""
Expand Down
3 changes: 1 addition & 2 deletions finsec/filing.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ def latest_13f_portfolio_value(self):
def latest_13f_count_holdings(self):
return self.get_latest_13f_num_holdings()

@property
def latest_13f_filing_detailed(self):
return self.get_latest_13f_filing(simplified=False, amend_filing=False)
return self.get_latest_13f_filing(simplified=False, amend_filing=True)

@property
def latest_13f_filing_cover_page(self):
Expand Down
5 changes: 2 additions & 3 deletions tests/test_13f.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""

import pandas as pd

import finsec


Expand All @@ -21,7 +20,7 @@ def test_latest_13f_filing(self):
assert len(self.filing.filings) > 0

def test_latest_13f_filing_detailed(self):
df = self.filing.latest_13f_filing_detailed
df = self.filing.latest_13f_filing_detailed()
assert isinstance(df, pd.DataFrame)

def test_latest_13f_filing_cover_page(self):
Expand All @@ -30,7 +29,7 @@ def test_latest_13f_filing_cover_page(self):

def test_latest_13f_portfolio_value(self):
portfolio_value = self.filing.latest_13f_portfolio_value
assert isinstance(portfolio_value, float)
assert isinstance(portfolio_value, int)

def test_latest_13f_count_holdings(self):
holdings_count = self.filing.latest_13f_count_holdings
Expand Down

0 comments on commit b28521c

Please sign in to comment.