Alpha

Supported Models

Introduction

This page describes the pre-built Alpha models in LEAN. The number of models grows over time. To add a model to LEAN, make a pull request to the GitHub repository. If none of these models perform exactly how you want, create a custom Alpha model.

Null Model

The NullAlphaModel doesn't emit any insights. It's the default Alpha model.

AddAlpha(new NullAlphaModel());
self.AddAlpha(NullAlphaModel())

To view the implementation of this model, see the LEAN GitHub repositoryLEAN GitHub repository.

Constant Model

The ConstantAlphaModel always returns the same insight for each security.

AddAlpha(new ConstantAlphaModel(type, direction, period));
self.AddAlpha(ConstantAlphaModel(type, direction, period))

The following table describes the arguments the model accepts:

ArgumentData TypeDescriptionDefault Value
typeInsightTypeThe type of insight
directionInsightDirectionThe direction of the insight
periodTimeSpantimedeltaThe period over which the insight will come to fruition
magnitudedouble?float/NoneTypeThe predicted change in magnitude as a +/- percentagenullNone
confidencedouble?float/NoneTypeThe confidence in the insightnullNone
weightdouble?The portfolio weight of the insightsnull

To view the implementation of this model, see the LEAN GitHub repositoryLEAN GitHub repository.

Historical Returns Model

The HistoricalReturnsAlphaModel buys securities that have a positive trailing return and sells securities that have a negative trailing return. It sets the magnitude of the Insight objects to the trailing rate of change.

AddAlpha(new HistoricalReturnsAlphaModel());
self.AddAlpha(HistoricalReturnsAlphaModel())

The following table describes the arguments the model accepts:

ArgumentData TypeDescriptionDefault Value
lookback intHistorical return lookback period1
resolutionResolutionThe resolution of historical dataResolution.Daily

To view the implementation of this model, see the LEAN GitHub repositoryLEAN GitHub repository.

EMA Cross Model

The EmaCrossAlphaModel uses an exponential moving average (EMA) cross to create insights. When the slow EMA crosses above the fast EMA, it emits up insights. When the slow EMA crosses below the fast EMA, it emits down insights. It sets the duration of Insight objects to be the product of the resolution and fastPeriod arguments.

AddAlpha(new EmaCrossAlphaModel());
self.AddAlpha(EmaCrossAlphaModel())

The following table describes the arguments the model accepts:

ArgumentData TypeDescriptionDefault Value
fastPeriodintThe fast EMA period12
slowPeriodintThe slow EMA period26
resolutionResolutionThe resolution of data sent into the EMA indicatorsResolution.Daily

To view the implementation of this model, see the LEAN GitHub repositoryLEAN GitHub repository.

MACD Model

The MacdAlphaModel emits insights based on moving average convergence divergence (MACD) crossovers. If the MACD signal line is 1% above the security price, the model emits an up insight. If the MACD signal line is 1% below the security price, the model emits a down insight. If the MACD signal line is within 1% of the security price, the model emits a flat insight.

AddAlpha(new MacdAlphaModel());
self.AddAlpha(MacdAlphaModel())

The following table describes the arguments the model accepts:

ArgumentData TypeDescriptionDefault Value
fastPeriodintThe MACD fast period12
slowPeriodintThe MACD slow period26
signalPeriodintThe smoothing period for the MACD signal9
movingAverageTypeMovingAverageTypeThe type of moving average to use in the MACDMovingAverageType.Exponential
resolutionResolutionThe resolution of data sent into the MACD indicatorResolution.Daily

To view the implementation of this model, see the LEAN GitHub repositoryLEAN GitHub repository.

RSI Model

The RsiAlphaModel generates insights based on the relative strength index (RSI) indicator values. When the RSI value passes above 70, the model emits a down insight. When the RSI value passes below 30, the model emits an up insight. The model uses the Wilder moving average type and sets the duration of Insight objects to be the product of the resolution and period arguments.

AddAlpha(new RsiAlphaModel());
self.AddAlpha(RsiAlphaModel())

The following table describes the arguments the model accepts:

ArgumentData TypeDescriptionDefault Value
periodintThe RSI indicator period14
resolutionResolutionThe resolution of data sent into the RSI indicatorResolution.Daily

To view the implementation of this model, see the LEAN GitHub repositoryLEAN GitHub repository.

The BasePairsTradingAlphaModel analyzes every possible pair combination from securities that the Universe Selection model selects. This model calculates a ratio between the two securities by dividing their historical prices over a lookback window. It then calculates the mean of this ratio by taking the 500-period EMA of the quotient. When the ratio diverges far enough from the mean ratio, this model emits generates alternating long ratio/short ratio insights emitted as a group to capture the reversion of the ratio.

AddAlpha(new BasePairsTradingAlphaModel());
self.AddAlpha(BasePairsTradingAlphaModel())

The following table describes the arguments the model accepts:

ArgumentData TypeDescriptionDefault Value
lookbackintLookback period of the analysis1
resolutionResolutionAnalysis resolutionResolution.Daily
thresholddecimalfloatThe percent [0, 100] deviation of the ratio from the mean before emitting an insight1

To view the implementation of this model, see the LEAN GitHub repositoryLEAN GitHub repository.

The PearsonCorrelationPairsTradingAlphaModel ranks every pair combination by its Pearson correlation coefficient and trades the pair with the highest correlation. This model follows the same insight logic as the BasePairsTradingModel.

AddAlpha(new PearsonCorrelationPairsTradingAlphaModel());
self.AddAlpha(PearsonCorrelationPairsTradingAlphaModel())

The following table describes the arguments the model accepts:

ArgumentData TypeDescriptionDefault Value
lookbackintLookback period of the analysis15
resolutionResolutionAnalysis resolutionResolution.Minute
thresholddecimalfloatThe percent [0, 100] deviation of the ratio from the mean before emitting an insight1
minimumCorrelationdoublefloatThe minimum correlation to consider a tradable pair0.5

To view the implementation of this model, see the LEAN GitHub repositoryLEAN GitHub repository.

You can also see our Videos. You can also get in touch with us via Discord.