P A
P A

Reputation: 11

No output for 13 f scraping, how do I get the program to scrape a 13 f filing in colab research

Complete starter so sorry in advance, I'm getting "Request successful!" and "Number of filings found: 0"

Goal to scrape a 13 F and output the text, but instead I get results successful but no aactul output

I tried this code, It's possible that the code is not working because there are no filings available for Bridgewater Associates, LP - I treid multiple other compaanies even chnaged the date time frame

!pip install pexpect
import pexpect

import json
import requests

# Set the API endpoint URL , using placeholder token! 
endpoint = "https://api.sec-api.io?token=010101"


# Set the search parameters
params = {
    "query": {
        "query_string": {
            "query": "name:\"Bridgewater Associates, LP\""
        }
    },
    "from": "0",
    "size": "10",
    "sort": [
        {
            "filedAt": {
                "order": "desc"
            }
        }
    ],
    "range": {
        "filedAt": {
            "gte": "2021-01-01",
            "lte": "2021-03-15"
        }
    }
}


# Send the search request to the API endpoint
response = requests.post(endpoint, json=params)

if response.status_code == 200:
    print("Request successful!")
else:
    print("Request failed with status code:", response.status_code)


# Parse the response JSON data
data = json.loads(response.content)

# Get the filing documents from the response
filings = []
if 'results' in data:
    data = data['results']
    if data:
        data = data[0]
        if 'filings' in data:
            filings = data['filings']['docs']

# Loop through each filing document and get the holdings data
for filing in filings:
    filing_url = filing['linkToHtml']
    filing_date = filing['filedAt']
    holdings = filing['holdings']
    
    # Loop through each holding and extract the required information
    for holding in holdings:
        name_of_issuer = holding['nameOfIssuer']
        title_of_class = holding['titleOfClass']
        cusip = holding['cusip']
        ticker = holding['ticker']
        cik = holding['cik']
        value = holding['value']
        shares = holding['shrsOrPrnAmt']['sshPrnamt']
        share_type = holding['shrsOrPrnAmt']['sshPrnamtType']
        investment_discretion = holding['investmentDiscretion']
        
        # Print the extracted information
        print(f"Name of Issuer: {name_of_issuer}")
        print(f"Title of Class: {title_of_class}")
        print(f"CUSIP: {cusip}")
        print(f"Ticker: {ticker}")
        print(f"CIK: {cik}")
        print(f"Value: {value}")
        print(f"Shares: {shares}")
        print(f"Share Type: {share_type}")
        print(f"Investment Discretion: {investment_discretion}")
        print(f"Filing Date: {filing_date}")
        print(f"Filing URL: {filing_url}")
        print("-------------")

        print(f"Number of filings found: {len(filings)}")

I'm getting "Request successful!" and "Number of filings found: 0"

Upvotes: 1

Views: 84

Answers (1)

Jay
Jay

Reputation: 2069

There are two issues with your code. First, try to change your query to the following:

params = {
    "query": {
        "query_string": {
            "query": "companyName:\"Bridgewater Associates, LP\" AND filedAt:[2021-01-01 TO 2021-03-15]",
            "time_zone": "America/New_York"
        }
    },
    "from": "0",
    "size": "20",
    "sort": [
        {
            "filedAt": {
                "order": "desc"
            }
        }
    ]
}
  • Use companyName instead of name
  • Move the date range query into the query using filedAt:[2021-01-01 TO 2021-03-15] and add the timezone parameter "time_zone": "America/New_York"

Second, handle edge cases where the ticker and cik aren't disclosed by the filer by adding the following two lines:

ticker = holding['ticker'] if 'ticker' in holding else '' 
cik = holding['cik'] if 'cik' in holding else '' 

You can also replace the if 'filings' in data: clause with a simple filings = data['filings'].

The complete code looks like this:

!pip install pexpect
import pexpect
import json
import requests

# Set the API endpoint URL , using placeholder token! 
endpoint = "https://api.sec-api.io?token=YOUR_API_KEY"

# Set the search parameters
params = {
    "query": {
        "query_string": {
            "query": "companyName:\"Bridgewater Associates, LP\" AND filedAt:[2021-01-01 TO 2021-03-15]",
            "time_zone": "America/New_York"
        }
    },
    "from": "0",
    "size": "20",
    "sort": [
        {
            "filedAt": {
                "order": "desc"
            }
        }
    ]
    # "range": {
    #     "filedAt": {
    #         "gte": "2021-01-01",
    #         "lte": "2021-03-15"
    #     }
    # }
}

# Send the search request to the API endpoint
response = requests.post(endpoint, json=params)

if response.status_code == 200:
    print("Request successful!")
else:
    print("Request failed with status code:", response.status_code)


# Parse the response JSON data
data = json.loads(response.content)

# Get the filing documents from the response
filings = data['filings']
# filings = []
# if 'filings' in data:
#     data = data['filings']
#     if data:
#         data = data[0]
#         if 'filings' in data:
#             filings = data['filings']['docs']

# Loop through each filing document and get the holdings data
for filing in filings:
    filing_url = filing['linkToHtml']
    filing_date = filing['filedAt']
    holdings = filing['holdings']
    
    # Loop through each holding and extract the required information
    for holding in holdings:
        name_of_issuer = holding['nameOfIssuer']
        title_of_class = holding['titleOfClass']
        cusip = holding['cusip']
        ticker = holding['ticker'] if 'ticker' in holding else '' 
        cik = holding['cik'] if 'cik' in holding else '' 
        value = holding['value']
        shares = holding['shrsOrPrnAmt']['sshPrnamt']
        share_type = holding['shrsOrPrnAmt']['sshPrnamtType']
        investment_discretion = holding['investmentDiscretion']
        
        # Print the extracted information
        print(f"Name of Issuer: {name_of_issuer}")
        print(f"Title of Class: {title_of_class}")
        print(f"CUSIP: {cusip}")
        print(f"Ticker: {ticker}")
        print(f"CIK: {cik}")
        print(f"Value: {value}")
        print(f"Shares: {shares}")
        print(f"Share Type: {share_type}")
        print(f"Investment Discretion: {investment_discretion}")
        print(f"Filing Date: {filing_date}")
        print(f"Filing URL: {filing_url}")
        print("-------------")

        print(f"Number of filings found: {len(filings)}")

Upvotes: 0

Related Questions