Skip to content

tradai-strategy

TradAI-only trading strategy framework extending Freqtrade IStrategy.

Status: 🚧 In Development | Coverage: TBD

Features

  • Extends Freqtrade - TradAIStrategy inherits from IStrategy
  • Type-Safe - Enums instead of strings
  • Standardized Metadata - Required metadata for all strategies
  • Validation - Server-side validation of strategy compliance
  • Quality Checks - Preflight, sanity, and CI gate validation
  • Testing Utilities - OHLCV dataframe factories and fixtures
  • No Legacy Support - TradAI-only, no backward compatibility with raw Freqtrade

Installation

uv add tradai-strategy

Usage

from tradai.strategy import TradAIStrategy, StrategyMetadata
from tradai.strategy.enums import StrategyCategory
import talib.abstract as ta

class MyStrategy(TradAIStrategy):
    """Custom trading strategy"""

    timeframe = '1h'
    can_short = True
    stoploss = -0.08

    def get_metadata(self) -> StrategyMetadata:
        return StrategyMetadata(
            name="MyStrategy",
            version="1.0.0",
            description="My trading strategy",
            timeframe=self.timeframe,
            can_short=self.can_short,
            category=StrategyCategory.MEAN_REVERSION,
            tags=["macd", "rsi"]
        )

    def populate_indicators(self, dataframe, metadata):
        # Use TA-Lib directly
        dataframe['rsi'] = ta.RSI(dataframe)
        return dataframe

    def populate_entry_trend(self, dataframe, metadata):
        # Entry logic
        return dataframe

    def populate_exit_trend(self, dataframe, metadata):
        return dataframe

Architecture

  • Core: Enums, metadata, base class, validation
  • Quality Checks: Preflight, sanity, and CI gate validation with unified entities
  • Testing: OHLCV dataframe factories and pytest fixtures
  • No Adapter: TradAIStrategy IS IStrategy (works directly with Freqtrade)
  • Reuses Libraries: TA-Lib, pandas-ta, Freqtrade utilities

Validation

# Validation entities share a common hierarchy
from tradai.strategy.validation_entities import CheckSeverity, ValidationIssue

# Sanity checks (post-backtest anomaly detection)
from tradai.strategy.sanity import SanityCheckService, SanityCheckResult

# CI gate (merge quality thresholds)
from tradai.strategy.ci import CIBacktestGate, CIValidationResult

# Preflight (pre-backtest checks)
from tradai.strategy.preflight import PreflightValidationService, PreflightResult

Testing Utilities

from tradai.strategy.testing import (
    create_ohlcv_dataframe,      # Realistic OHLCV data
    create_trending_dataframe,   # Trending price data
    create_oversold_dataframe,   # Oversold conditions
    sample_ohlcv_fixture,        # Pytest fixture
)

# Create reproducible test data
df = create_ohlcv_dataframe(n_candles=200, seed=42)

Development

# Run tests
uv run pytest

# Run with coverage
uv run pytest --cov

# Type checking
uv run mypy src

Documentation

  • DESIGN.md - Architecture decisions and patterns
  • Strategy examples are in the separate tradai-strategies repository