financial data analysis by brice.
Project description
Python Financial Data Analysis (FinanceDA)
Project Overview
A project that integrates functions such as stock data downloading, analysis, visualization, backtesting, and optimization.
Author: Ye Junjie Creation Date: May 9, 2021 Last Updated: December 18, 2025
Core Functional Modules
from financeDA.class_price_list import PriceList # Price list data (stocks, futures, options, etc.), e.g., stock closing prices, stock returns, etc.
from financeDA.class_stock_data import StockData # Stock data including [Close, High, Low, Open, Volume] and key extended indicators [Diff, Signal, Close_Open, Returns, Log_Returns, 42d, 252d, Mov_Vol, etc.]
from financeDA.class_stock_po import StockPO # Stock portfolio optimization
from financeDA import stat_describe, stat_norm_tests, stat_gen_paths # Statistical description, normality tests, random path generation
from financeDA import stock_diff, stock_tsa, stock_tests # Visualization: line charts, candlestick charts, histograms, QQ plots, stock return analysis, stock time series analysis, stock statistical tests
from financeDA import ff_reg # Factor data regression analysis
from financeDA import gbm_mcs_stat, gbm_mcs_dyna, gbm_mcs_amer, option_premium # Option pricing related: GBM model static simulation, dynamic simulation, American option simulation, option premium calculation
from financeDA import bsm_call_imp_vol, bsm_call_value, bsm_vega # Option pricing related: BSM model implied volatility, option value, option Vega
from financeDA import var_gbm, var_jd, var_diff, var_cva, var_cva_eu # Value at Risk (VaR) related: GBM model VaR, JD model VaR, incremental VaR, CVA VaR, CVA VaR (European options)
from financeDA import hello_financeda # Print hello_financeda greeting
Acknowledgments
Hilpisch: Python for Financial Data Analysis (Primary reference source for most of the code) Duan Xiaoshou: Python Quantitative Trading in Depth (Referenced for two components: candlestick charts and simple KNN-based trading strategies)
Quick Start
Install the financeDA Package
First, install Python version 3.13.5 or higher (Python 3.14.0 is recommended). Then download and install the financeDA package:
pip install financeDA
Single Stock Data Download, Analysis, and Visualization
from financeDA.class_stock_data import StockData # Stock data including [Close, High, Low, Open, Volume] and key extended indicators [Diff, Signal, Close_Open, Returns, Log_Returns, 42d, 252d, Mov_Vol, etc.]
from financeDA import stock_diff, stock_tsa, stock_tests # stock return analysis, stock time series analysis, stock statistical tests
df_stock = StockData("BABA", start="2020-01-01", end="2025-12-20", source="yfinance").DF
print(df_stock.head())
stock_diff(df_stock)
stock_tsa(df_stock)
stock_tests(df_stock)
Portfolio Optimization (Multiple Stocks)
import numpy as np
import pandas as pd
from financeDA.class_stock_data import StockData
from financeDA.class_stock_po import StockPO
stocks = {
'600031.SH': 'Sany Heavy Industry',
'601138.SH': 'Foxconn Industrial Internet',
'000768.SZ': 'AVIC Xi\'an Aircraft Industry',
'600519.SH': 'Kweichow Moutai'
}
sdpo = StockPO(stocks, source="tushare", token="your tushare token")
print(sdpo.data.tail())
weights, results = sdpo.po_random()
print(weights, results)
sdpo.po_mc(200000) # Defaults to 2000 simulations; increase the number for higher accuracy
opts = sdpo.po_max_sharpe()
optv = sdpo.po_min_vol()
print(opts, optv) # Manually calculate portfolio weights for maximum Sharpe ratio and minimum volatility
sdpo.po_ef(trets=np.linspace(0.06, 0.15, 30)) # Manually compute the efficient frontier; adjust trets values for optimal results
sdpo.po_cml() # Manually calculate the maximum Sharpe ratio portfolio under the efficient frontier (Capital Market Line)
Simple Investment Strategy Using Factor Regression
from financeDA import ff_reg
stocks = {
"600100.SH": "Tongfang Co., Ltd.",
"600626.SH": "Shengda Co., Ltd.",
"000630.SZ": "Tongling Nonferrous Metals",
"000850.SZ": "Huamao Co., Ltd.",
"600368.SH": "Wuzhou Transportation",
"603766.SH": "Loncin General Machinery",
"600105.SH": "Yongding Co., Ltd.",
"600603.SH": "Guanghui Logistics",
"002344.SZ": "Haining Leather City",
"000407.SZ": "Shengli Co., Ltd.",
"000883.SZ": "Hubei Energy"
}
df = ff_reg(stocks=stocks, start_date='2024-10-01', end_date='2025-10-31', mode=5)
print(df)
outfile = "ff_reg.csv"
df.to_csv(outfile)
Project Dependencies
The Python code requires version 3.13.5 or higher (Python 3.14.0 is recommended).
Dependency Management with uv
Project configuration file: [project.toml]
uv python install 3.14
uv python pin 3.14
uv sync
uv lock
Dependency Management with Pip
Install required packages (Project dependency file: [requirements.txt])
pip install numpy pandas matplotlib mplfinance scipy
pip install pandas_datareader yfinance tushare jqdatasdk
pip install scikit-learn statsmodels factor-analyzer
# To speed up installation, append the -i parameter to each command to use domestic pip mirrors. For example:
pip install pandas -i https://pypi.tuna.tsinghua.edu.cn/simple
# You can also install dependencies directly using the requirements.txt file
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
# For certain Python versions (e.g., Python 3.9.5), if errors occur when running yfinance, you may need to adjust the urllib3 version
pip install urllib3==1.25.11
Mind Map
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file financeda-0.3.1.tar.gz.
File metadata
- Download URL: financeda-0.3.1.tar.gz
- Upload date:
- Size: 793.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
699bd7c5543a2262d5a8c914f87bb21395bdb6c668b2a13035e39e8d86200298
|
|
| MD5 |
c9a0fc6562d9c58191bbbffffce09d8c
|
|
| BLAKE2b-256 |
03a81ab1458203a696261c012bf68600266dce1bf02874c6a4622d7d007b6578
|
File details
Details for the file financeda-0.3.1-py3-none-any.whl.
File metadata
- Download URL: financeda-0.3.1-py3-none-any.whl
- Upload date:
- Size: 802.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
59047a544c4190ec30eb747deabd9e620925ca31066bc92f0b7a7c3d8aec8562
|
|
| MD5 |
4badaeb736bd0c99fc5d7e3226fbf675
|
|
| BLAKE2b-256 |
fa94f982945490fa02a994d567da73cbbc9fa9d1d235914fad1e047e59c742d7
|