Single-stock analysis using Python and local machine learning/ AI tools (Ollama, LSTM).
Single-stock analysis using Python and local machine learning/ AI tools (Ollama, LSTM). CC BY-NC-SA.
📘 Access the FinAPy Notebook:
Open Version 1.4
Open Version 1.2
Open Version 1.1
Check requirements:
requirements.txt
Download Ollama to have access to the local LLM features
Information: Ollama and the local LLM features require a discrete GPU to run properly, with at least 8GB of VRAM. Your computer should also have at least 16GB of RAM. If you are below these specifications, you can still run the script but without these AI features.
Table of contents
- What is FinAPy?
- How to run the project?
- Video demonstration
- Quick preview
- The project in detail
- Potential practical applications
- Limitations and planned developments
What is FinAPy?
FinAPY (FINancial Analysis with PYthon) is a research project using Python that aims to provide a synthetic and yet comprehensive overview of the main characteristics of a company and its stock from simply its ticker.The main challenge was to work on a way to process financial data using AI locally, without sending any data to servers on the other side of the globe. Using Ollama and certain Python libraries helped to reach that objective.
The project relies on several libraries, but gets its data using yfinance and pygooglenews mainly. The main features include:
- A basic synthesis about the company: name, location, sector, industry,...
- A market data analysis:
- A ratio-based analysis, computed from the financial statements of the last 3 years:
How to run the project?
- Step 1: Download the latest notebook.
- Step 2: install the libraries required (listed on the requirements.txt file at the top of this README)
- Step 3 (optional- for AI features only): Download Ollama (Link at the top of the Readme)
- Step 4 (optional- for AI features only): Install gpt-oss-20b on Ollama: Open Ollama, select as "model" "gpt-oss-20b" and send a random prompt to start downloading the model
- Step 5: Open the notebook in Python (keep Ollama open on the side) and run the script.
Video demonstration
V1.4. Demo:Quick preview
Example using the ticker of Netflix (NFLX): Example of the market data analysis component:
Example of the Operational ratios component: 
The same kind of analysis is provided with the different ratio components (Profitability/ assets/ liquidity/ solvency (level of debt + coverage)).
The project in detail
Here is how FinAPy works:- Prologue: Ticker input collection and essential functions and data:
- Step 1: Events and news fetching:
- Step 2: Forecast using Machine Learning LSTM:
- Step 3: Market data restitution:
- Step 4: Financial statement analysis:
* Profitability ratios: Profit margin, ROA, ROCE, ROE,... * Asset related ratios: Asset turnover, working capital. * Liquidity ratios: Current ratio, quick ratio, cash ratio. * Solvency ratios: debt to assets, debt to capital, financial leverage, coverage ratios,... * Operational ratios (cashflow related): CFI/ CFF/ CFO ratios, cash return on assets,... * Bankrupcy and financial health scores: Altman Z-score/ Ohlson O-score.
- Appendix: Financial statements:
Potential practical applications
As is: For educational and research purposes only: * Visualisation of ratios and metrics of any stock for students, researchers,... * CFA program candidates looking to visualise real-life cases ratios and data as learnt from the curriculum (skewness, kurtosis,... and other theoretical concepts professional models don't always explicitly show). * Financial ratio visualisation and analysis (informative only). * LSTM forecast preview * ...Once refined/ adapted/ modified, it might have the potential to implement: * In-house data analysis of financial information with a high level of confidentiality (the advantage of using Ollama locally). Main requirements: usage-adpated APIs for financial data and news/ hardware to run Ollama. * Qualitative analysis integration, with the graphical visualisation of news and their impact over time. * Weak signal additional detection layer with the AI analysis. It is not as reliable as a real human experienced analyst, but can sometimes highlight aspects to help deepen one's analysis. * ...
Limitations and planned developments
My work focuses at the moment on improving the following aspects:- Improving the sentiment analysis of the news with Finbert, as the accuracy is sometimes off when articles talk about several companies.
- Integrating sector/industry averages of the ratios, and potentially Q1/Q3 to help assess the financial situation of a company within its industry.
- Adding metrics to assess the relevancy of the LSTM forecast.
- Certain providers don't issue the financial statements in the same format as others on Yahoo Finance, which leads to the ratio not being calculated at all in certain rare cases.
- I'm planning the integration of elements of this script to a portfolio analysis project with multi-factor optimisation.
Code: © G.RUQUILLA - CC BY-NC-SA
This license enables reusers to distribute, remix, adapt, and build upon the material in any medium or format for noncommercial purposes only, and only so long as attribution is given to the creator. If you remix, adapt, or build upon the material, you must license the modified material under identical terms. Data is fetched from Yahoo Finance through Yfinance and Google RSS feeds through pygooglenews. The information provided is for general informational and educational purposes only and does not constitute investment advice, financial advice, trading advice, or any other form of advice.