Skip to content

mehdibadjian/PyMAC

Repository files navigation

PyMAC - Moomoo Python Algo-Core

Event-Driven Algorithmic Trading System

Overview

PyMAC is an extensible, production-ready algorithmic trading framework designed for the Moomoo platform. The architecture ensures that the same strategy code runs for both 10-year historical backtests and live trading.

Key Features

  • Unified Strategy Interface: Write once, run in backtest or live
  • Parquet-Based Storage: Efficient handling of 10+ years of 1-minute data
  • Dual Data Sources: Yahoo Finance (free) or Moomoo API (premium)
  • Full Backtesting Engine: Realistic order simulation with slippage and commissions
  • Event-Driven Architecture: Real-time market data processing
  • Risk Management: Built-in position limits and safeguards
  • Type-Safe: Full Python type hints for reliability
  • Performance Metrics: Sharpe, Sortino, Max Drawdown, Win Rate

Quick Start

1. Installation

# Install dependencies
pip install -r requirements.txt

2. Download Historical Data

# From Yahoo Finance (free, no setup)
python download_data_v2.py AAPL --source yahoo --period 5y

# Or from Moomoo (requires OpenD running)
python download_data_v2.py US.AAPL --source moomoo --start 2020-01-01

3. Run a Backtest

python main.py --mode backtest \
  --strategy SimplePriceStrategy \
  --symbol US.AAPL \
  --start 2020-01-01 \
  --end 2023-12-31 \
  --capital 10000

4. View Results

The engine displays:

  • 📊 Returns and P&L
  • 📈 Sharpe and Sortino ratios
  • 📉 Maximum drawdown
  • 💼 Trade statistics and detailed trade log

5. Run Live Trading

# Paper trading (safe - simulated money)
python main.py --mode live \
  --strategy SimplePriceStrategy \
  --symbol US.AAPL \
  --paper

# Live trading (⚠️ real money!)
python main.py --mode live \
  --strategy SimplePriceStrategy \
  --symbol US.AAPL

⚠️ Always start with --paper mode! See docs/live_trading_guide.md for safety information.

Project Structure

PyMAC/
├── src/
│   ├── core/           # Abstract interfaces (BaseStrategy, DataProvider)
│   ├── adapters/       # Concrete implementations (Backtester, MoomooLive)
│   ├── strategies/     # Your trading strategies
│   └── utils/          # Logging, metrics
├── configs/            # YAML configuration files
├── data/              # Parquet files (gitignored)
├── logs/              # Execution logs (gitignored)
└── main.py            # CLI entry point

Development

Adding a New Strategy

  1. Create a new file in src/strategies/
  2. Inherit from BaseStrategy
  3. Implement on_bar() method
  4. Add configuration to configs/strategies.yaml

Running Tests

pytest tests/

Safety Notice

⚠️ IMPORTANT: This system can execute real trades. Always:

  • Start with paper trading
  • Set strict max_position_size limits
  • Keep require_confirmation: true until fully tested
  • Never commit API keys to git

License

MIT License - See LICENSE file for details

About

Moomoo Python Algo-Core: Production-Ready Algorithmic Trading System

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages