# MATLAB to Python Trading Algorithm Conversions

This directory contains Python implementations of various algorithmic trading strategies and utility functions originally written in MATLAB.

## Overview

The converted files include:

### Utility Functions
- `backshift.py` - Shift data backward in time
- `fwdshift.py` - Shift data forward in time  
- `movingAvg.py` - Calculate moving averages
- `movingStd.py` - Calculate moving standard deviations
- `smartmean.py` - Mean calculation ignoring NaN/Inf values
- `smartstd.py` - Standard deviation ignoring NaN/Inf values
- `smartsum.py` - Sum calculation ignoring NaN/Inf values
- `smartMovingStd.py` - Moving standard deviation ignoring NaN/Inf values
- `calculateReturns.py` - Calculate returns from price series
- `calculateMaxDD.py` - Calculate maximum drawdown and duration

### Data Loading
- `data_loader.py` - Load converted CSV/JSON data files
- `convert_mat_files.py` - Convert .mat files to CSV/JSON format

### Trading Strategies
- `TU_mom.py` - Treasury futures momentum strategy
- `TU_mom_hypothesisTest.py` - Hypothesis testing for TU momentum strategy
- `kentdaniel.py` - Long-short equity momentum strategy
- `gapFutures_FSTX.py` - Gap trading strategy for futures
- `pead.py` - Post-earnings announcement drift strategy

## Installation

1. Install required dependencies:
```bash
pip install -r requirements.txt
```

2. Add the parent directory to your Python path or install as a package:
```python
import sys
sys.path.append('/path/to/algo_trading_book')
```

## Usage

### Using Utility Functions
```python
import numpy as np
from converted_code import backshift, smartmean, calculateReturns

# Example: Calculate returns
prices = np.array([100, 102, 101, 103, 105])
returns = calculateReturns(prices, 1)
print(returns)

# Example: Backshift data
shifted_prices = backshift(1, prices)
print(shifted_prices)

# Example: Smart mean (ignoring NaN)
data_with_nan = np.array([1, 2, np.nan, 4, 5])
mean_val = smartmean(data_with_nan)
print(mean_val)
```

### Loading Real Market Data
```python
from converted_code.data_loader import load_futures_data, load_stock_data, load_earnings_data

# Load Treasury futures data
tu_data = load_futures_data('TU', '20120813')
print(f"Loaded {len(tu_data['tday'])} days of TU data")

# Load stock data
stock_data = load_stock_data('20120424')
print(f"Stock data shape: {stock_data['cl'].shape}")

# Load earnings announcements
earnings = load_earnings_data()
print(f"Earnings data shape: {earnings['earnann'].shape}")
```

### Running Trading Strategies
```python
# Run TU momentum strategy
from converted_code.TU_mom import main as tu_mom_main
tu_mom_main()

# Run Kent Daniel momentum strategy  
from converted_code.kentdaniel import main as kent_daniel_main
kent_daniel_main()

# Run PEAD strategy
from converted_code.pead import main as pead_main
pead_main()
```

## Important Notes

### Data Requirements
The implementations now support both **real market data** (from converted .mat files) and **synthetic data** for demonstration.

#### Real Data (Converted from .mat files)
- Located in `converted_code/data/` directory
- Includes futures, stocks, ETFs, and earnings data
- Automatically loaded by trading strategies
- CSV format for time series, JSON for metadata

#### Data Format
- **Prices**: 2D numpy arrays (time x assets)
- **Returns**: Same format as prices  
- **Dates**: 1D array of date integers (YYYYMMDD format)
- **OHLC Data**: Separate arrays for Open, High, Low, Close

#### Available Datasets
- **Futures**: TU (Treasury), CL (Crude Oil), VX (VIX), HG (Copper), etc.
- **Stocks**: OHLC data for 500+ stocks
- **ETFs**: Exchange-traded fund data
- **Earnings**: Earnings announcement calendar
- **Interest Rates**: AUD and CAD interest rate data

### Converting Additional .mat Files
```python
# Run the conversion script to convert new .mat files
python convert_mat_files.py
```

## Performance Considerations

1. **Vectorization**: The code uses numpy vectorization for efficiency
2. **Memory Usage**: Large datasets may require chunked processing
3. **NaN Handling**: Smart functions handle NaN/Inf values gracefully

## Differences from MATLAB

1. **Indexing**: Python uses 0-based indexing vs MATLAB's 1-based
2. **Array Operations**: Uses numpy instead of MATLAB's matrix operations
3. **Function Names**: Some functions renamed for Python conventions
4. **Error Handling**: Added proper error handling and type checking

## Testing

Each strategy file can be run independently:
```bash
python converted_code/TU_mom.py
python converted_code/kentdaniel.py
python converted_code/pead.py
```

## Contributing

When adding new conversions:
1. Follow the existing code structure
2. Add proper docstrings
3. Include error handling
4. Use synthetic data for examples
5. Update this README

## License

This code is converted from the original MATLAB implementations for educational and research purposes. 