Lean
$LEAN_TAG$
|
Provides an implementation of Mean-Variance portfolio optimization based on modern portfolio theory. The interval of weights in optimization method can be changed based on the long-short algorithm. The default model uses the last three months daily price to calculate the optimal weight with the weight range from -1 to 1 and minimize the portfolio variance with a target return of 2% More...
Public Member Functions | |
MeanVarianceOptimizationPortfolioConstructionModel (IDateRule rebalancingDateRules, PortfolioBias portfolioBias=PortfolioBias.LongShort, int lookback=1, int period=63, Resolution resolution=Resolution.Daily, double targetReturn=0.02, IPortfolioOptimizer optimizer=null) | |
Initialize the model More... | |
MeanVarianceOptimizationPortfolioConstructionModel (Resolution rebalanceResolution=Resolution.Daily, PortfolioBias portfolioBias=PortfolioBias.LongShort, int lookback=1, int period=63, Resolution resolution=Resolution.Daily, double targetReturn=0.02, IPortfolioOptimizer optimizer=null) | |
Initialize the model More... | |
MeanVarianceOptimizationPortfolioConstructionModel (TimeSpan timeSpan, PortfolioBias portfolioBias=PortfolioBias.LongShort, int lookback=1, int period=63, Resolution resolution=Resolution.Daily, double targetReturn=0.02, IPortfolioOptimizer optimizer=null) | |
Initialize the model More... | |
MeanVarianceOptimizationPortfolioConstructionModel (PyObject rebalance, PortfolioBias portfolioBias=PortfolioBias.LongShort, int lookback=1, int period=63, Resolution resolution=Resolution.Daily, double targetReturn=0.02, PyObject optimizer=null) | |
Initialize the model More... | |
MeanVarianceOptimizationPortfolioConstructionModel (Func< DateTime, DateTime > rebalancingFunc, PortfolioBias portfolioBias=PortfolioBias.LongShort, int lookback=1, int period=63, Resolution resolution=Resolution.Daily, double targetReturn=0.02, IPortfolioOptimizer optimizer=null) | |
Initialize the model More... | |
MeanVarianceOptimizationPortfolioConstructionModel (Func< DateTime, DateTime?> rebalancingFunc, PortfolioBias portfolioBias=PortfolioBias.LongShort, int lookback=1, int period=63, Resolution resolution=Resolution.Daily, double targetReturn=0.02, IPortfolioOptimizer optimizer=null) | |
Initialize the model More... | |
override void | OnSecuritiesChanged (QCAlgorithm algorithm, SecurityChanges changes) |
Event fired each time the we add/remove securities from the data feed More... | |
![]() | |
PortfolioConstructionModel (Func< DateTime, DateTime?> rebalancingFunc) | |
Initialize a new instance of PortfolioConstructionModel More... | |
PortfolioConstructionModel (Func< DateTime, DateTime > rebalancingFunc=null) | |
Initialize a new instance of PortfolioConstructionModel More... | |
virtual IEnumerable< IPortfolioTarget > | CreateTargets (QCAlgorithm algorithm, Insight[] insights) |
Create portfolio targets from the specified insights More... | |
Protected Member Functions | |
override bool | ShouldCreateTargetForInsight (Insight insight) |
Method that will determine if the portfolio construction model should create a target for this insight More... | |
override Dictionary< Insight, double > | DetermineTargetPercent (List< Insight > activeInsights) |
Will determine the target percent for each insight More... | |
![]() | |
void | SetPythonWrapper (PortfolioConstructionModelPythonWrapper pythonWrapper) |
Used to set the PortfolioConstructionModelPythonWrapper instance if any More... | |
virtual List< Insight > | GetTargetInsights () |
Gets the target insights to calculate a portfolio target percent for More... | |
void | SetRebalancingFunc (PyObject rebalance) |
Python helper method to set the rebalancing function. This is required due to a python net limitation not being able to use the base type constructor, and also because when python algorithms use C# portfolio construction models, it can't convert python methods into func nor resolve the correct constructor for the date rules, timespan parameter. For performance we prefer python algorithms using the C# implementation More... | |
virtual bool | IsRebalanceDue (Insight[] insights, DateTime algorithmUtc) |
Determines if the portfolio should be rebalanced base on the provided rebalancing func, if any security change have been taken place or if an insight has expired or a new insight arrived If the rebalancing function has not been provided will return true. More... | |
void | RefreshRebalance (DateTime algorithmUtc) |
Refresh the next rebalance time and clears the security changes flag More... | |
Additional Inherited Members | |
![]() | |
static Insight[] | FilterInvalidInsightMagnitude (IAlgorithm algorithm, Insight[] insights) |
Helper class that can be used by the different IPortfolioConstructionModel implementations to filter Insight instances with an invalid Insight.Magnitude value based on the IAlgorithmSettings More... | |
![]() | |
virtual bool | RebalanceOnSecurityChanges = true [get, set] |
True if should rebalance portfolio on security changes. True by default More... | |
virtual bool | RebalanceOnInsightChanges = true [get, set] |
True if should rebalance portfolio on new insights or expiration of insights. True by default More... | |
IAlgorithm | Algorithm [get] |
The algorithm instance More... | |
PortfolioConstructionModelPythonWrapper | PythonWrapper [get, set] |
This is required due to a limitation in PythonNet to resolved overriden methods. When Python calls a C# method that calls a method that's overriden in python it won't run the python implementation unless the call is performed through python too. More... | |
Provides an implementation of Mean-Variance portfolio optimization based on modern portfolio theory. The interval of weights in optimization method can be changed based on the long-short algorithm. The default model uses the last three months daily price to calculate the optimal weight with the weight range from -1 to 1 and minimize the portfolio variance with a target return of 2%
Definition at line 35 of file MeanVarianceOptimizationPortfolioConstructionModel.cs.
QuantConnect.Algorithm.Framework.Portfolio.MeanVarianceOptimizationPortfolioConstructionModel.MeanVarianceOptimizationPortfolioConstructionModel | ( | IDateRule | rebalancingDateRules, |
PortfolioBias | portfolioBias = PortfolioBias.LongShort , |
||
int | lookback = 1 , |
||
int | period = 63 , |
||
Resolution | resolution = Resolution.Daily , |
||
double | targetReturn = 0.02 , |
||
IPortfolioOptimizer | optimizer = null |
||
) |
Initialize the model
rebalancingDateRules | The date rules used to define the next expected rebalance time in UTC |
portfolioBias | Specifies the bias of the portfolio (Short, Long/Short, Long) |
lookback | Historical return lookback period |
period | The time interval of history price to calculate the weight |
resolution | The resolution of the history price |
targetReturn | The target portfolio return |
optimizer | The portfolio optimization algorithm. If the algorithm is not provided then the default will be mean-variance optimization. |
Definition at line 55 of file MeanVarianceOptimizationPortfolioConstructionModel.cs.
QuantConnect.Algorithm.Framework.Portfolio.MeanVarianceOptimizationPortfolioConstructionModel.MeanVarianceOptimizationPortfolioConstructionModel | ( | Resolution | rebalanceResolution = Resolution.Daily , |
PortfolioBias | portfolioBias = PortfolioBias.LongShort , |
||
int | lookback = 1 , |
||
int | period = 63 , |
||
Resolution | resolution = Resolution.Daily , |
||
double | targetReturn = 0.02 , |
||
IPortfolioOptimizer | optimizer = null |
||
) |
Initialize the model
rebalanceResolution | Rebalancing frequency |
portfolioBias | Specifies the bias of the portfolio (Short, Long/Short, Long) |
lookback | Historical return lookback period |
period | The time interval of history price to calculate the weight |
resolution | The resolution of the history price |
targetReturn | The target portfolio return |
optimizer | The portfolio optimization algorithm. If the algorithm is not provided then the default will be mean-variance optimization. |
Definition at line 76 of file MeanVarianceOptimizationPortfolioConstructionModel.cs.
QuantConnect.Algorithm.Framework.Portfolio.MeanVarianceOptimizationPortfolioConstructionModel.MeanVarianceOptimizationPortfolioConstructionModel | ( | TimeSpan | timeSpan, |
PortfolioBias | portfolioBias = PortfolioBias.LongShort , |
||
int | lookback = 1 , |
||
int | period = 63 , |
||
Resolution | resolution = Resolution.Daily , |
||
double | targetReturn = 0.02 , |
||
IPortfolioOptimizer | optimizer = null |
||
) |
Initialize the model
timeSpan | Rebalancing frequency |
portfolioBias | Specifies the bias of the portfolio (Short, Long/Short, Long) |
lookback | Historical return lookback period |
period | The time interval of history price to calculate the weight |
resolution | The resolution of the history price |
targetReturn | The target portfolio return |
optimizer | The portfolio optimization algorithm. If the algorithm is not provided then the default will be mean-variance optimization. |
Definition at line 97 of file MeanVarianceOptimizationPortfolioConstructionModel.cs.
QuantConnect.Algorithm.Framework.Portfolio.MeanVarianceOptimizationPortfolioConstructionModel.MeanVarianceOptimizationPortfolioConstructionModel | ( | PyObject | rebalance, |
PortfolioBias | portfolioBias = PortfolioBias.LongShort , |
||
int | lookback = 1 , |
||
int | period = 63 , |
||
Resolution | resolution = Resolution.Daily , |
||
double | targetReturn = 0.02 , |
||
PyObject | optimizer = null |
||
) |
Initialize the model
rebalance | Rebalancing func or if a date rule, timedelta will be converted into func. For a given algorithm UTC DateTime the func returns the next expected rebalance time or null if unknown, in which case the function will be called again in the next loop. Returning current time will trigger rebalance. If null will be ignored |
portfolioBias | Specifies the bias of the portfolio (Short, Long/Short, Long) |
lookback | Historical return lookback period |
period | The time interval of history price to calculate the weight |
resolution | The resolution of the history price |
targetReturn | The target portfolio return |
optimizer | The portfolio optimization algorithm. If the algorithm is not provided then the default will be mean-variance optimization. |
This is required since python net can not convert python methods into func nor resolve the correct constructor for the date rules parameter. For performance we prefer python algorithms using the C# implementation
Definition at line 124 of file MeanVarianceOptimizationPortfolioConstructionModel.cs.
QuantConnect.Algorithm.Framework.Portfolio.MeanVarianceOptimizationPortfolioConstructionModel.MeanVarianceOptimizationPortfolioConstructionModel | ( | Func< DateTime, DateTime > | rebalancingFunc, |
PortfolioBias | portfolioBias = PortfolioBias.LongShort , |
||
int | lookback = 1 , |
||
int | period = 63 , |
||
Resolution | resolution = Resolution.Daily , |
||
double | targetReturn = 0.02 , |
||
IPortfolioOptimizer | optimizer = null |
||
) |
Initialize the model
rebalancingFunc | For a given algorithm UTC DateTime returns the next expected rebalance UTC time. Returning current time will trigger rebalance. If null will be ignored |
portfolioBias | Specifies the bias of the portfolio (Short, Long/Short, Long) |
lookback | Historical return lookback period |
period | The time interval of history price to calculate the weight |
resolution | The resolution of the history price |
targetReturn | The target portfolio return |
optimizer | The portfolio optimization algorithm. If the algorithm is not provided then the default will be mean-variance optimization. |
Definition at line 159 of file MeanVarianceOptimizationPortfolioConstructionModel.cs.
QuantConnect.Algorithm.Framework.Portfolio.MeanVarianceOptimizationPortfolioConstructionModel.MeanVarianceOptimizationPortfolioConstructionModel | ( | Func< DateTime, DateTime?> | rebalancingFunc, |
PortfolioBias | portfolioBias = PortfolioBias.LongShort , |
||
int | lookback = 1 , |
||
int | period = 63 , |
||
Resolution | resolution = Resolution.Daily , |
||
double | targetReturn = 0.02 , |
||
IPortfolioOptimizer | optimizer = null |
||
) |
Initialize the model
rebalancingFunc | For a given algorithm UTC DateTime returns the next expected rebalance time or null if unknown, in which case the function will be called again in the next loop. Returning current time will trigger rebalance. |
portfolioBias | Specifies the bias of the portfolio (Short, Long/Short, Long) |
lookback | Historical return lookback period |
period | The time interval of history price to calculate the weight |
resolution | The resolution of the history price |
targetReturn | The target portfolio return |
optimizer | The portfolio optimization algorithm. If the algorithm is not provided then the default will be mean-variance optimization. |
Definition at line 188 of file MeanVarianceOptimizationPortfolioConstructionModel.cs.
|
protectedvirtual |
Method that will determine if the portfolio construction model should create a target for this insight
insight | The insight to create a target for |
Reimplemented from QuantConnect.Algorithm.Framework.Portfolio.PortfolioConstructionModel.
Definition at line 215 of file MeanVarianceOptimizationPortfolioConstructionModel.cs.
|
protectedvirtual |
Will determine the target percent for each insight
activeInsights | The active insights to generate a target for |
Reimplemented from QuantConnect.Algorithm.Framework.Portfolio.PortfolioConstructionModel.
Definition at line 246 of file MeanVarianceOptimizationPortfolioConstructionModel.cs.
|
virtual |
Event fired each time the we add/remove securities from the data feed
algorithm | The algorithm instance that experienced the change in securities |
changes | The security additions and removals from the algorithm |
Reimplemented from QuantConnect.Algorithm.Framework.Portfolio.PortfolioConstructionModel.
Definition at line 295 of file MeanVarianceOptimizationPortfolioConstructionModel.cs.