https://github.com/jialuechen/pytca
Python Library for Transaction Cost Analysis and Market Simulation
https://github.com/jialuechen/pytca
algorithmic-trading best-execution market-microstructure order-flow price-impact scaling-laws what-if-analysis
Last synced: 19 days ago
JSON representation
Python Library for Transaction Cost Analysis and Market Simulation
- Host: GitHub
- URL: https://github.com/jialuechen/pytca
- Owner: jialuechen
- License: bsd-2-clause
- Created: 2024-06-28T20:14:25.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-02-04T04:37:35.000Z (3 months ago)
- Last Synced: 2025-03-28T00:06:11.513Z (26 days ago)
- Topics: algorithmic-trading, best-execution, market-microstructure, order-flow, price-impact, scaling-laws, what-if-analysis
- Language: Python
- Homepage: https://jialuechen.github.io/pytca/
- Size: 26.9 MB
- Stars: 137
- Watchers: 4
- Forks: 6
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
![]()
# PyTCA: Python Library for Transaction Cost Analysis (TCA)
[](https://pypi.org/project/pytca/)
[](https://pypi.org/project/pytca/)

[](https://pytca.readthedocs.io/en/latest/?badge=latest)
[](https://opensource.org/licenses/BSD-2-Clause)
[](https://coveralls.io/github/jialuechen/pytca?branch=main)**PyTCA** is a Python package for transaction cost analysis in financial markets, supporting both stock and forex data at the tick level.
## Features
- Support tick-level data processing and analytics for stocks and forex
- Perform various analyses including slippage, market impact, and timing cost and Calculate key metrics such as VWAP and implementation shortfall
- Generate visualizations and reports
- Enable RESTful API for integration with other systems
- Support for Excel and KDB+ as well as well as other RDBMS data sources## Installation
```bash
pip install -U pytca
```## Quick Start
```python
import pytca# Load tick data
tick_data = pytca.load_tick_data('path/to/tick_data.csv', data_type='stock')# Analyze tick data
analysis_results = pytca.analyze_tick_data(tick_data)
print("Tick Data Analysis Results:", analysis_results)# Visualize tick data
fig = pytca.plot_tick_data(tick_data, plot_type='summary')
fig.write_html('tick_data_summary.html')
```## More Examples
### Loading Data from Different Sources
```python
import pytca# Load data from CSV
csv_data = pytca.load_tick_data('path/to/tick_data.csv', data_type='stock')# Load data from Excel
excel_data = pytca.read_excel('path/to/tick_data.xlsx', sheet_name='Tick Data')# Load data from KDB
kdb_handler = pytca.KDBHandler(host='localhost', port=5000)
kdb_data = kdb_handler.load_tick_data('tickdata', '2023.07.15T09:30:00.000', '2023.07.15T16:00:00.000')
```### Performing Analysis
```python
import pytca# Load data
stock_data = pytca.load_tick_data('path/to/stock_data.csv', data_type='stock')
forex_data = pytca.load_tick_data('path/to/forex_data.csv', data_type='forex')# Analyze stock data
stock_analysis = pytca.analyze_stock_trade(stock_data, benchmark_data)
print("Stock Analysis Results:", stock_analysis)# Analyze forex data
forex_analysis = pytca.analyze_forex_trade(forex_data, benchmark_data)
print("Forex Analysis Results:", forex_analysis)# Calculate slippage
slippage = pytca.calculate_slippage(executed_price=100.05, benchmark_price=100.00)
print("Slippage:", slippage)# Calculate VWAP
vwap = pytca.calculate_vwap(prices=[100.00, 100.05, 100.10], volumes=[1000, 2000, 1500])
print("VWAP:", vwap)
```### Generating Visualizations
```python
import pytca# Load data
tick_data = pytca.load_tick_data('path/to/tick_data.csv', data_type='stock')# Create basic plot
basic_fig = pytca.plot_tick_data(tick_data, plot_type='basic')
basic_fig.savefig('basic_plot.png')# Create candlestick chart
candlestick_fig = pytca.plot_tick_data(tick_data, plot_type='candlestick', interval='5min')
candlestick_fig.write_html('candlestick.html')# Create order book depth chart
depth_fig = pytca.plot_tick_data(tick_data, plot_type='depth')
depth_fig.write_html('depth_chart.html')# Create trade flow chart
trade_flow_fig = pytca.plot_tick_data(tick_data, plot_type='trade_flow', window='5min')
trade_flow_fig.write_html('trade_flow.html')# Create summary dashboard
summary_fig = pytca.plot_tick_data(tick_data, plot_type='summary')
summary_fig.write_html('summary_dashboard.html')
```### Using the RESTful API
```python
import pytca# Start the API server
pytca.run_api(host='localhost', port=5000)# Now you can make HTTP requests to the API endpoints, for example:
# POST http://localhost:5000/analyze_tick_data
# with JSON body: {"table_name": "tickdata", "start_time": "2023.07.15T09:30:00.000", "end_time": "2023.07.15T16:00:00.000", "symbols": ["AAPL", "GOOGL"]}
```## Roadmap
Q4 2024: Implement an order flow simulator which can generate large-scale alpha-less orders,i.e., unbiased trades from randomized interventional experiments.## Contributing
We welcome contributions! Please see our [Contributing Guide](CONTRIBUTING.md) for more details.
## License
This project is licensed under the BSD-2-Clause License - see the [LICENSE](LICENSE) file for details.