AI-Powered Investing Assistant - Revolutionizing Stock Analysis Part 3
People & Blogs
Introduction
Welcome to Part 3 of our video series on building an AI investing assistant. In this series, we aim to design and develop an AI assistant that performs financial analysis and answers questions regarding any public company. In previous videos, we showcased the assistant's capabilities in handling complex questions and generating useful answers to inform investment decisions. We also discussed how transforming natural language into SQL database queries enables the assistant to access up-to-date information while minimizing the chances of inaccuracies or hallucinations.
System Architecture
The system architecture is divided into three segments:
- Data Loading
- Natural Language to SQL
- Answer Generation
In this article, we will delve into the Data Loading segment.
Data Loading Process
The data loading segment involves using a financial API to gather a company's data, which is then loaded into a SQL database. When a user submits a stock symbol to the investment assistant, a request is sent to the financial API. The API responds with a comprehensive financial dataset for that company, which is subsequently loaded into the database. Once this process is complete, the chat screen appears, indicating that the assistant is ready to answer questions and conduct analysis on that company.
The financial API utilized for this project is FM Cloud. Although it requires a paid subscription and an API key, it is reasonably priced, making it an ideal choice for proof of concept and prototype development. With a subscription, different types of data can be accessed, including:
- Financial statement data (income, balance sheet, and cash flow statements)
- Financial ratios
- Earnings call transcripts
- Insider trading data
- Company profile data
For our focus, we will concentrate on the financial statement data.
To retrieve the financial statement data, we will send requests to specific URLs along with the company symbol and API key as parameters. Separate requests will be made for each financial statement: the income statement, balance sheet, and cash flow statement, along with requests for both annual and quarterly financial results. In total, this amounts to six requests to acquire a company's complete historical financial dataset.
JSON Data Format
The data is returned in JSON format, represented as objects for different periods. For example, Apple's annual income statement data can be accessed, which spans as far back as 1985. Each JSON object contains typical income statement items, including:
- Revenue
- Gross profit
- EBITDA (Earnings Before Interest, Taxes, Depreciation, and Amortization)
- Earnings per share
Code Implementation
The code for handling the data loading consists of two main modules. The first module, financials
, is involved in requesting the data from the API and preparing it for loading into the database. The libraries employed include urllib
for making API requests and pandas
for data manipulation, as well as an environment variable library (os
) for managing the API key securely.
Functions in the Financials Module
get_single_financial_statement: Retrieves a single statement type for a single period based on the company symbol, statement type (income statement, balance sheet, or cash flow statement), and period type (quarterly or annual).
get_period_financial_statements: Uses the previous function to acquire all statements for a specific period type (annual or quarterly).
trailing_12_months: Calculates the trailing twelve-month data by aggregating financial data over the previous four quarters.
financial_adjustments: Creates modified data columns for specific financial items and calculates metrics like funds from operations and free cash flow per share.
single_company_full_financials: Combines the above functions to retrieve a comprehensive dataset of a company's financial information across multiple periods and prepares it for database entry.
When executed, for example, using Microsoft, this comprehensive data set reveals 344 rows and 119 columns of information, with data extending back to the 1980s.
Loading Data to the Database
The second module, load_database
, is straightforward, containing two functions: DB_connection
and load_data
. It employs the SQLAlchemy
library to connect to a MySQL database hosted on Google Cloud Platform; however, it can be adjusted to work with any database.
DB_connection: Establishes a connection to the database.
load_data: Takes the company symbol as input, retrieves the complete financial data set using the
single_company_full_financials
function, connects to the database, and loads the data frame directly into the database.
By executing these procedures, we provide our investment assistant with access to up-to-date information on any public company, laying the groundwork for accurately addressing users' inquiries. The next stage of our project involves enabling the assistant to access the database using natural language. Stay tuned for the following video in this series, and feel free to check out my channel for more content focused on AI in finance.
Keywords
- AI Investing Assistant
- Financial API
- Data Loading
- SQL Database
- Financial Statements
- JSON Format
- Data Integration
- Microsoft Example
FAQ
1. What is the purpose of the AI investing assistant? The AI investing assistant is designed to perform financial analysis and answer inquiries about any public company.
2. What financial data can the assistant access? The assistant can access various types of financial data, including income statements, balance sheets, cash flow statements, financial ratios, and company profiles.
3. How does the data loading process work? Data is retrieved from a financial API, sent through specific URLs, and then loaded into a SQL database for analysis.
4. What programming libraries are used in the project?
The main libraries used include urllib
for making API requests, pandas
for data manipulation, and SQLAlchemy
for database connectivity.
5. How does the assistant handle updates to financial data? The assistant gets up-to-date information by making requests to the financial API each time a user inputs a stock symbol.
6. What will be covered in the next video? The upcoming video will focus on how to enable the investment assistant to access the database using natural language queries.