Quant.Strategy.MovingAverage (quant v0.1.0-alpha.1)
Moving average based trading strategies.
This module implements various moving average crossover strategies that generate buy/sell signals when faster moving averages cross above or below slower moving averages.
Supported Strategies
- SMA Crossover: Simple Moving Average crossover
- EMA Crossover: Exponential Moving Average crossover
- Dual MA: Combination of different MA types
Strategy Examples
# Golden Cross: 50-day SMA crosses above 200-day SMA
strategy = Quant.Strategy.MovingAverage.sma_crossover(
fast_period: 50,
slow_period: 200
)
# Fast EMA crossover for short-term trading
strategy = Quant.Strategy.MovingAverage.ema_crossover(
fast_period: 12,
slow_period: 26
)
Summary
Functions
Apply the required technical indicators for moving average strategies.
Create an Exponential Moving Average crossover strategy.
Get the column names that will be created by this strategy.
Create a Simple Moving Average crossover strategy.
Validate that a DataFrame has the required columns for moving average strategies.
Functions
@spec apply_indicators(Explorer.DataFrame.t(), map(), keyword()) :: {:ok, Explorer.DataFrame.t()} | {:error, term()}
Apply the required technical indicators for moving average strategies.
This function adds the necessary moving averages to the DataFrame before signal generation.
Parameters
dataframe- Input DataFrame with OHLCV datastrategy- Strategy configurationopts- Additional options
Returns
DataFrame with moving averages added as new columns.
Create an Exponential Moving Average crossover strategy.
Parameters
:fast_period- Period for fast EMA (default: 12):slow_period- Period for slow EMA (default: 26):column- Price column to use (default: :close)
Examples
iex> strategy = Quant.Strategy.MovingAverage.ema_crossover(fast_period: 8, slow_period: 21)
iex> strategy.type
:ema_crossover
iex> strategy.indicator
:ema
Get the column names that will be created by this strategy.
Useful for understanding what columns will be added to the DataFrame.
Examples
iex> strategy = Quant.Strategy.MovingAverage.sma_crossover(fast_period: 5, slow_period: 10)
iex> Quant.Strategy.MovingAverage.get_indicator_columns(strategy)
["close_sma_5", "close_sma_10"]
Create a Simple Moving Average crossover strategy.
Parameters
:fast_period- Period for fast SMA (default: 12):slow_period- Period for slow SMA (default: 26):column- Price column to use (default: :close)
Returns
Strategy configuration map for use with Quant.Strategy.generate_signals/2.
Examples
iex> strategy = Quant.Strategy.MovingAverage.sma_crossover(fast_period: 5, slow_period: 10)
iex> strategy.type
:sma_crossover
iex> strategy.fast_period
5
iex> strategy.slow_period
10
@spec validate_dataframe(Explorer.DataFrame.t(), map()) :: :ok | {:error, term()}
Validate that a DataFrame has the required columns for moving average strategies.
Parameters
dataframe- DataFrame to validatestrategy- Strategy configuration
Returns
:ok if valid, {:error, reason} if invalid.