BrelLibrary / brel

A Python library for reading XBRL reports
Other
16 stars 5 forks source link

Documentation Request: Could you show how to get an itemized income statement as an example? #6

Open ghost opened 6 months ago

ghost commented 6 months ago

I find this repo to be very interesting. I want to use it to get the balance sheet, income statement, and statement of cash flows. In particular, I would like to use this repo to be able to carry our some discounted cash flow analysis on companies. However, I am finding it difficult to get started. Could you post an example of how one would completely parse one of the data tables found in the 10-Q? Or are there any videos out there demonstrating some financial analysis? That would be super helpful to me and to any others who find this in depth repo.

Here is an image of TSLA's income statement for reference. : income  statement Eventually, I hope to code a way to get a company's revenues broken down by segment! TSLA revenue by segment say automotive sales is very important to my analysis.

One again thanks for this repo, I look forward to using it more and more.

ghost commented 6 months ago

As a simple example:


    def traverse_node(node):
        """
        Recursively traverse a single node and its children.
        """

        # Initialize a list to store the data for this node and its children
        node_data = []
        # -- if the node is a CalculationNetworkNode -- it holds a concept

        # --> Get the concept
        concept = node.get_concept()
        print(concept)
        # --> look up its value using filing.
        facts_list = filing_tsla.get_facts_by_concept(concept)
        pprint(facts_list)

        # Get our fact information
        # Facts look like: concept: us-gaap:Assets, entity: {http://www.sec.gov/CIK}0001318605, period: on 2023-09-30, unit: usd, value: 93941000000
        # concept: us-gaap:Assets, entity: {http://www.sec.gov/CIK}0001318605, period: on 2022-12-31, unit: usd, value: 82338000000
        # concept: us-gaap:Assets, entity: {http://www.sec.gov/CIK}0001318605, period: on 2023-09-30, srt:ConsolidatedEntitiesAxis: us-gaap:VariableInterestEntityPrimaryBeneficiaryMember, unit: usd, value: 4760000000
        for fact in facts_list:
            # if it does not have extra axis information
            # TODO: figure out how to handle extra axis information
            if len(fact.get_aspects()) <= 4:                         # get_aspects() returns concept, entity, period etc from above. len of normal stuff is 4
                node_data.append({
                    'Name': str(fact.get_concept().get_value()),
                    'Value': fact.get_value(),
                    'Date': fact.get_period().get_instant_period()
                })

        print(node_data)

        # --> Get the human readable labels.
        labels_lists = concept.get_labels()  # takes form of [['Current Assets', 'Assets, current', 'Assets, Current'], ... ] 

        text_labels = []
        for label_list in labels_lists:
            text_labels.append(label_list.get_content())

        # TODO : see if there is a way to standardize this across filings -- probably hard. 
        # Can use find the closest string such as Current Assets on the labels.
        text_label = text_labels[-1]  # Set the list to be the last label of the list.

        # Recursively traverse children and accumulate data
        for child_node in node.get_children():
            node_data.extend(traverse_node(child_node))

        return node_data

    # Initialize an empty list to store the data for all nodes
    all_node_data = []

    # Traverse each starting node
    for start_node in nodes:
        all_node_data.extend(traverse_node(start_node))

    def unnest_list(lst):
        result = []
        for item in lst:
            if isinstance(item, list):
                result.extend(unnest_list(item))
            else:
                result.append(item)
        return result

    all_node_data = unnest_list(all_node_data)

    # Convert the list of dictionaries to a DataFrame
    df = pd.DataFrame(all_node_data)

    return df

# Use this function on the selected_calculation_network
# Traverse the network and collect data into a list
balance_sheet_df = traverse_network(selected_calculation_network.get_roots())
print(balance_sheet_df)

is supposed to traverse the node list and create a Dataframe holding the values of the balance sheet. However, I think I need to edit: facts_list = filing_tsla.get_facts_by_concept(concept) so I am only getting the facts of the whole filing versus the facts of the balance sheet. I am unsure of how to fix this.