# Evolutionary Factor Searching

**Evolutionary Factor Searching for Sparse Portfolio Optimization Using Large Language Models**

A cutting-edge implementation that leverages Large Language Models (LLMs) to generate, evolve, and optimize trading factors for portfolio construction through an evolutionary search process.

## 🌟 Features

- **🧠 LLM-Powered Factor Generation**: Utilize GPT-4 and DeepSeek models to generate innovative trading factors
- **🧬 Evolutionary Optimization**: Evolve factors through natural selection, keeping top performers and mutating for improvement
- **📊 Advanced Backtesting**: Comprehensive backtesting with transaction costs, multiple strategies, and performance metrics
- **🛡️ Safe Execution**: Sandboxed factor execution with validation for reliable strategy implementation
- **🎨 Modern GUI**: Beautiful dark-themed interface with glassmorphism effects
- **📈 Real-time Visualization**: Interactive charts and performance monitoring
- **🔧 Flexible Configuration**: Support for multiple datasets, models, and optimization parameters

## 🚀 Quick Start

### Prerequisites

- Python 3.8+
- pip package manager
- OpenAI or DeepSeek API key (for LLM factor generation)

### Quick Usage

#### GUI Application
```bash
python efs_gui.py
```

#### Command Line
```bash
# Run factor search
python mining.py --data_path dataset/market/NASDAQ_50.csv --model deepseek-chat

# Run backtesting
python backtest.py --data_path dataset/market/NASDAQ_50.csv --search_ckp checkpoint.bin
```

## 📖 Documentation

### Core Components

#### 1. Factor Search (`mining.py`)
The evolutionary factor search engine that:
- Starts with seed factors from `factors/expression_lib.py`
- Uses LLMs to generate new factors based on performance feedback
- Evaluates factors using Rank IC and other metrics
- Evolves the best factors through mutation and crossover

```bash
python mining.py \
  --data_path dataset/market/NASDAQ_50.csv \
  --model gpt-4-1106-preview \
  --win_size 30 \
  --warmup_teps 15 \
  --search_steps 5 \
  --gen_num 10
```

Due to the `mining.py` is currently used for another project we are not able to provide it in this demo.

#### 2. Backtesting (`backtest.py`)
Portfolio backtesting with advanced features:
- Transaction cost modeling
- Multiple portfolio strategies (equal weight, weighted scores)
- Low change hands strategy
- Comprehensive performance metrics

```bash
python backtest.py \
  --data_path dataset/market/NASDAQ_50.csv \
  --search_ckp search_results.bin \
  --m 10 \
  --n_factors 5 \
  --trans_cost 0.001 \
  --use_lowch \
  --use_w2s
```

#### 3. Example
We provide two searching checkpoint files under `./search_ckp` folder.