Quant.Strategy (quant v0.1.0-alpha.1)

Main API module for quantitative trading strategies.

This module provides a unified interface for creating, composing, and executing trading strategies using technical indicators from Quant.Math.

Strategy Types

  • Signal-based strategies: Generate buy/sell signals based on technical indicators
  • Momentum strategies: Follow trends using moving averages and momentum indicators
  • Mean reversion strategies: Trade on price reversals using oscillators
  • Composite strategies: Combine multiple indicators for robust signal generation

Basic Usage

# Simple moving average crossover strategy
strategy = Quant.Strategy.sma_crossover(fast_period: 12, slow_period: 26)
signals = Quant.Strategy.generate_signals(df, strategy)

# RSI oversold/overbought strategy
strategy = Quant.Strategy.rsi_threshold(oversold: 30, overbought: 70)
signals = Quant.Strategy.generate_signals(df, strategy)

# Composite strategy combining multiple indicators
strategy = Quant.Strategy.composite([
  Quant.Strategy.sma_crossover(fast_period: 12, slow_period: 26),
  Quant.Strategy.rsi_threshold(oversold: 30, overbought: 70)
], logic: :all)

Signal Format

All strategies generate signals as DataFrame columns:

  • signal: Integer values (-1: sell, 0: hold, 1: buy)
  • signal_strength: Float values (0.0-1.0) indicating confidence
  • signal_reason: String describing the signal trigger

Strategy Composition

Strategies can be combined using logical operators:

  • :all - All component strategies must agree (AND logic)
  • :any - Any component strategy can trigger (OR logic)
  • :majority - Majority of strategies must agree
  • :weighted - Weighted combination based on strategy confidence

Summary

Types

signal_strength()

@type signal_strength() :: float()

signal_value()

@type signal_value() :: -1 | 0 | 1

strategy()

@type strategy() :: map()

strategy_result()

@type strategy_result() :: {:ok, Explorer.DataFrame.t()} | {:error, term()}

Functions

analyze_performance(backtest_results, opts \\ [])

@spec analyze_performance(
  Explorer.DataFrame.t(),
  keyword()
) :: {:ok, map()} | {:error, term()}

Analyze strategy performance and generate metrics.

Returns comprehensive performance analysis including:

  • Total return, annualized return
  • Sharpe ratio, Sortino ratio
  • Maximum drawdown
  • Win rate, profit factor
  • Risk metrics

backtest(dataframe, strategy, opts \\ [])

@spec backtest(Explorer.DataFrame.t(), strategy(), keyword()) :: strategy_result()

Backtest a strategy against historical data.

Parameters

  • dataframe - Historical OHLCV data
  • strategy - Strategy to test
  • opts - Backtesting options

Options

  • :initial_capital - Starting capital (default: 10000.0)
  • :position_size - Position sizing method (default: :fixed)
  • :commission - Trading commission rate (default: 0.001)
  • :slippage - Market slippage rate (default: 0.0005)

Returns

DataFrame with backtest results including:

  • Portfolio value over time
  • Positions and trades
  • Performance metrics

bollinger_bands(opts \\ [])

See Quant.Strategy.Volatility.bollinger_bands/1.

combine_signals(signals_list, opts \\ [])

See Quant.Strategy.Composite.combine/2.

composite(strategies, opts \\ [])

See Quant.Strategy.Composite.create/2.

ema_crossover(opts \\ [])

See Quant.Strategy.MovingAverage.ema_crossover/1.

generate_signals(dataframe, strategy, opts \\ [])

@spec generate_signals(Explorer.DataFrame.t(), strategy(), keyword()) ::
  strategy_result()

Generate trading signals for a given DataFrame using the specified strategy.

Parameters

  • dataframe - Explorer DataFrame with OHLCV data
  • strategy - Strategy configuration map
  • opts - Optional parameters for signal generation

Options

  • :column - Base price column to use (default: :close)
  • :validate - Whether to validate required columns (default: true)
  • :cleanup - Whether to remove intermediate indicator columns (default: false)

Examples

# Simple SMA crossover
iex> strategy = Quant.Strategy.sma_crossover(fast_period: 5, slow_period: 10)
iex> {:ok, df_with_signals} = Quant.Strategy.generate_signals(df, strategy)
iex> DataFrame.names(df_with_signals) |> Enum.member?("signal")
true

# RSI with custom thresholds
iex> strategy = Quant.Strategy.rsi_threshold(oversold: 25, overbought: 75)
iex> {:ok, df_with_signals} = Quant.Strategy.generate_signals(df, strategy, column: :close)

macd_crossover(opts \\ [])

See Quant.Strategy.Momentum.macd_crossover/1.

rsi_threshold(opts \\ [])

See Quant.Strategy.Momentum.rsi_threshold/1.

sma_crossover(opts \\ [])

See Quant.Strategy.MovingAverage.sma_crossover/1.