/// <summary> /// Initialize the model /// </summary> /// <param name="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</param> /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param> /// <param name="lookback">Historical return lookback period</param> /// <param name="period">The time interval of history price to calculate the weight</param> /// <param name="resolution">The resolution of the history price</param> /// <param name="targetReturn">The target portfolio return</param> /// <param name="optimizer">The portfolio optimization algorithm. If the algorithm is not provided then the default will be mean-variance optimization.</param> public 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) : this(rebalancingFunc != null ? (Func <DateTime, DateTime?>)(timeUtc => rebalancingFunc(timeUtc)) : null, portfolioBias, lookback, period, resolution, targetReturn, optimizer) { }
/// <summary> /// Initialize the model /// </summary> /// <param name="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</param> /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param> /// <param name="lookback">Historical return lookback period</param> /// <param name="period">The time interval of history price to calculate the weight</param> /// <param name="resolution">The resolution of the history price</param> /// <param name="riskFreeRate">The risk free rate</param> /// <param name="delta">The risk aversion coeffficient of the market portfolio</param> /// <param name="tau">The model parameter indicating the uncertainty of the CAPM prior</param> /// <param name="optimizer">The portfolio optimization algorithm. If no algorithm is explicitly provided then the default will be max Sharpe ratio optimization.</param> public BlackLittermanOptimizationPortfolioConstructionModel(Func <DateTime, DateTime> rebalancingFunc, PortfolioBias portfolioBias = PortfolioBias.LongShort, int lookback = 1, int period = 63, Resolution resolution = Resolution.Daily, double riskFreeRate = 0.0, double delta = 2.5, double tau = 0.05, IPortfolioOptimizer optimizer = null) : this(rebalancingFunc != null ? (Func <DateTime, DateTime?>)(timeUtc => rebalancingFunc(timeUtc)) : null, portfolioBias, lookback, period, resolution, riskFreeRate, delta, tau, optimizer) { }
/// <summary> /// Initialize the model /// </summary> /// <param name="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.</param> /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param> /// <param name="lookback">Historical return lookback period</param> /// <param name="period">The time interval of history price to calculate the weight</param> /// <param name="resolution">The resolution of the history price</param> /// <param name="targetReturn">The target portfolio return</param> /// <param name="optimizer">The portfolio optimization algorithm. If the algorithm is not provided then the default will be mean-variance optimization.</param> public 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) : base(rebalancingFunc) { _lookback = lookback; _period = period; _resolution = resolution; _portfolioBias = portfolioBias; var lower = portfolioBias == PortfolioBias.Long ? 0 : -1; var upper = portfolioBias == PortfolioBias.Short ? 0 : 1; _optimizer = optimizer ?? new MinimumVariancePortfolioOptimizer(lower, upper, targetReturn); _symbolDataDict = new Dictionary <Symbol, ReturnsSymbolData>(); }
public void PortfolioBiasIsRespected(Language language, PortfolioBias bias) { SetPortfolioConstruction(language, bias); var appl = _algorithm.AddEquity("AAPL"); var bac = _algorithm.AddEquity("BAC"); var spy = _algorithm.AddEquity("SPY"); var ibm = _algorithm.AddEquity("IBM"); var aig = _algorithm.AddEquity("AIG"); var qqq = _algorithm.AddEquity("QQQ"); foreach (var equity in new[] { qqq, aig, ibm, spy, bac, appl }) { equity.SetMarketPrice(new Tick(_nowUtc, equity.Symbol, 30, 30)); } var insights = new[] { new Insight(_nowUtc, appl.Symbol, TimeSpan.FromDays(1), InsightType.Price, InsightDirection.Up, -0.1d, null), new Insight(_nowUtc, spy.Symbol, TimeSpan.FromDays(1), InsightType.Price, InsightDirection.Down, -0.1d, null), new Insight(_nowUtc, ibm.Symbol, TimeSpan.FromDays(1), InsightType.Price, InsightDirection.Down, 0d, null), new Insight(_nowUtc, aig.Symbol, TimeSpan.FromDays(1), InsightType.Price, InsightDirection.Down, -0.1d, null), new Insight(_nowUtc, qqq.Symbol, TimeSpan.FromDays(1), InsightType.Price, InsightDirection.Up, -0.1d, null), new Insight(_nowUtc, bac.Symbol, TimeSpan.FromDays(1), InsightType.Price, InsightDirection.Down, -0.1d, null) }; _algorithm.PortfolioConstruction.OnSecuritiesChanged(_algorithm, SecurityChangesTests.AddedNonInternal(appl, spy, ibm, aig, qqq, bac)); foreach (var target in _algorithm.PortfolioConstruction.CreateTargets(_algorithm, insights)) { QuantConnect.Logging.Log.Trace($"{target.Symbol}: {target.Quantity}"); if (target.Quantity == 0) { continue; } Assert.AreEqual(Math.Sign((int)bias), Math.Sign(target.Quantity)); } }
private void SetPortfolioConstruction(Language language, QCAlgorithm algorithm, PortfolioBias bias = PortfolioBias.LongShort) { algorithm.SetPortfolioConstruction(new AccumulativeInsightPortfolioConstructionModel((Func <DateTime, DateTime>)null, bias)); if (language == Language.Python) { using (Py.GIL()) { var name = nameof(AccumulativeInsightPortfolioConstructionModel); var instance = Py.Import(name).GetAttr(name).Invoke(((object)null).ToPython(), ((int)bias).ToPython()); var model = new PortfolioConstructionModelPythonWrapper(instance); algorithm.SetPortfolioConstruction(model); } } foreach (var kvp in _algorithm.Portfolio) { kvp.Value.SetHoldings(kvp.Value.Price, 0); } _algorithm.Portfolio.SetCash(_startingCash); SetUtcTime(new DateTime(2018, 7, 31)); var changes = SecurityChanges.Added(_algorithm.Securities.Values.ToArray()); algorithm.PortfolioConstruction.OnSecuritiesChanged(_algorithm, changes); }
/// <summary> /// Initialize a new instance of <see cref="EqualWeightingPortfolioConstructionModel"/> /// </summary> /// <param name="timeSpan">Rebalancing frequency</param> /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param> public EqualWeightingPortfolioConstructionModel(TimeSpan timeSpan, PortfolioBias portfolioBias = PortfolioBias.LongShort) : this(dt => dt.Add(timeSpan), portfolioBias) { }
/// <summary> /// Initialize a new instance of <see cref="ConfidenceWeightedPortfolioConstructionModel"/> /// </summary> /// <param name="rebalancingDateRules">The date rules used to define the next expected rebalance time /// in UTC</param> /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param> public ConfidenceWeightedPortfolioConstructionModel(IDateRule rebalancingDateRules, PortfolioBias portfolioBias = PortfolioBias.LongShort) : base(rebalancingDateRules, portfolioBias) { }
/// <summary> /// Initialize a new instance of <see cref="InsightWeightingPortfolioConstructionModel"/> /// </summary> /// <param name="timeSpan">Rebalancing frequency</param> /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param> public InsightWeightingPortfolioConstructionModel(TimeSpan timeSpan, PortfolioBias portfolioBias = PortfolioBias.LongShort) : base(timeSpan, portfolioBias) { }
/// <summary> /// Initialize a new instance of <see cref="InsightWeightingPortfolioConstructionModel"/> /// </summary> /// <param name="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</param> /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param> /// <remarks>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</remarks> public InsightWeightingPortfolioConstructionModel(PyObject rebalance, PortfolioBias portfolioBias = PortfolioBias.LongShort) : base(rebalance, portfolioBias) { }
/// <summary> /// Initialize a new instance of <see cref="InsightWeightingPortfolioConstructionModel"/> /// </summary> /// <param name="resolution">Rebalancing frequency</param> /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param> public InsightWeightingPortfolioConstructionModel(Resolution resolution = Resolution.Daily, PortfolioBias portfolioBias = PortfolioBias.LongShort) : base(resolution, portfolioBias) { }
/// <summary> /// Initialize a new instance of <see cref="AccumulativeInsightPortfolioConstructionModel"/> /// </summary> /// <param name="rebalancingDateRules">The date rules used to define the next expected rebalance time /// in UTC</param> /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param> /// <param name="percent">The percentage amount of the portfolio value to allocate /// to a single insight. The value of percent should be in the range [0,1]. /// The default value is 0.03.</param> public AccumulativeInsightPortfolioConstructionModel(IDateRule rebalancingDateRules, PortfolioBias portfolioBias = PortfolioBias.LongShort, double percent = 0.03) : this(rebalancingDateRules.ToFunc(), portfolioBias, percent) { }
/// <summary> /// Initialize a new instance of <see cref="AccumulativeInsightPortfolioConstructionModel"/> /// </summary> /// <param name="resolution">Rebalancing frequency</param> /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param> /// <param name="percent">The percentage amount of the portfolio value to allocate /// to a single insight. The value of percent should be in the range [0,1]. /// The default value is 0.03.</param> public AccumulativeInsightPortfolioConstructionModel(Resolution resolution, PortfolioBias portfolioBias = PortfolioBias.LongShort, double percent = 0.03) : this(resolution.ToTimeSpan(), portfolioBias, percent) { }
/// <summary> /// Initialize a new instance of <see cref="AccumulativeInsightPortfolioConstructionModel"/> /// </summary> /// <param name="timeSpan">Rebalancing frequency</param> /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param> /// <param name="percent">The percentage amount of the portfolio value to allocate /// to a single insight. The value of percent should be in the range [0,1]. /// The default value is 0.03.</param> public AccumulativeInsightPortfolioConstructionModel(TimeSpan timeSpan, PortfolioBias portfolioBias = PortfolioBias.LongShort, double percent = 0.03) : this(dt => dt.Add(timeSpan), portfolioBias, percent) { }
/// <summary> /// Initialize a new instance of <see cref="ConfidenceWeightedPortfolioConstructionModel"/> /// </summary> /// <param name="timeSpan">Rebalancing frequency</param> /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param> public ConfidenceWeightedPortfolioConstructionModel(TimeSpan timeSpan, PortfolioBias portfolioBias = PortfolioBias.LongShort) : base(timeSpan, portfolioBias) { }
/// <summary> /// Initialize a new instance of <see cref="ConfidenceWeightedPortfolioConstructionModel"/> /// </summary> /// <param name="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</param> /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param> public ConfidenceWeightedPortfolioConstructionModel(Func <DateTime, DateTime> rebalancingFunc, PortfolioBias portfolioBias = PortfolioBias.LongShort) : base(rebalancingFunc, portfolioBias) { }
/// <summary> /// Initialize a new instance of <see cref="ConfidenceWeightedPortfolioConstructionModel"/> /// </summary> /// <param name="rebalancingParam">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</param> /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param> /// <remarks>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</remarks> public ConfidenceWeightedPortfolioConstructionModel(PyObject rebalancingParam, PortfolioBias portfolioBias = PortfolioBias.LongShort) : base(rebalancingParam, portfolioBias) { }
/// <summary> /// Initialize a new instance of <see cref="EqualWeightingPortfolioConstructionModel"/> /// </summary> /// <param name="resolution">Rebalancing frequency</param> /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param> public EqualWeightingPortfolioConstructionModel(Resolution resolution = Resolution.Daily, PortfolioBias portfolioBias = PortfolioBias.LongShort) : this(resolution.ToTimeSpan(), portfolioBias) { }
/// <summary> /// Initialize a new instance of <see cref="EqualWeightingPortfolioConstructionModel"/> /// </summary> /// <param name="rebalancingDateRules">The date rules used to define the next expected rebalance time /// in UTC</param> /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param> public EqualWeightingPortfolioConstructionModel(IDateRule rebalancingDateRules, PortfolioBias portfolioBias = PortfolioBias.LongShort) : this(rebalancingDateRules.ToFunc(), portfolioBias) { }
/// <summary> /// Initialize a new instance of <see cref="InsightWeightingPortfolioConstructionModel"/> /// </summary> /// <param name="rebalancingDateRules">The date rules used to define the next expected rebalance time /// in UTC</param> /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param> public InsightWeightingPortfolioConstructionModel(IDateRule rebalancingDateRules, PortfolioBias portfolioBias = PortfolioBias.LongShort) : base(rebalancingDateRules, portfolioBias) { }
/// <summary> /// Initialize a new instance of <see cref="EqualWeightingPortfolioConstructionModel"/> /// </summary> /// <param name="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. If null will be ignored</param> /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param> public EqualWeightingPortfolioConstructionModel(Func <DateTime, DateTime?> rebalancingFunc, PortfolioBias portfolioBias = PortfolioBias.LongShort) : base(rebalancingFunc) { _portfolioBias = portfolioBias; }
/// <summary> /// Initialize a new instance of <see cref="InsightWeightingPortfolioConstructionModel"/> /// </summary> /// <param name="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</param> /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param> public InsightWeightingPortfolioConstructionModel(Func <DateTime, DateTime> rebalancingFunc, PortfolioBias portfolioBias = PortfolioBias.LongShort) : base(rebalancingFunc, portfolioBias) { }
/// <summary> /// Initialize a new instance of <see cref="EqualWeightingPortfolioConstructionModel"/> /// </summary> /// <param name="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</param> /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param> public EqualWeightingPortfolioConstructionModel(Func <DateTime, DateTime> rebalancingFunc, PortfolioBias portfolioBias = PortfolioBias.LongShort) : this(rebalancingFunc != null ? (Func <DateTime, DateTime?>)(timeUtc => rebalancingFunc(timeUtc)) : null, portfolioBias) { }
public IPortfolioConstructionModel GetPortfolioConstructionModel(Language language, Resolution resolution, PortfolioBias bias) { if (language == Language.CSharp) { return(new MeanVarianceOptimizationPortfolioConstructionModel(resolution, bias, 1, 63, Resolution.Daily, 0.0001)); } using (Py.GIL()) { const string name = nameof(MeanVarianceOptimizationPortfolioConstructionModel); var instance = Py.Import(name).GetAttr(name) .Invoke(((int)resolution).ToPython(), ((int)bias).ToPython(), 1.ToPython(), 63.ToPython(), ((int)Resolution.Daily).ToPython(), 0.0001.ToPython()); return(new PortfolioConstructionModelPythonWrapper(instance)); } }
/// <summary> /// Initialize a new instance of <see cref="EqualWeightingPortfolioConstructionModel"/> /// </summary> /// <param name="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</param> /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param> /// <remarks>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</remarks> public EqualWeightingPortfolioConstructionModel(PyObject rebalance, PortfolioBias portfolioBias = PortfolioBias.LongShort) : this((Func <DateTime, DateTime?>)null, portfolioBias) { SetRebalancingFunc(rebalance); }
public BLOPCM(IPortfolioOptimizer optimizer, PortfolioBias portfolioBias) : base(optimizer: optimizer, portfolioBias: portfolioBias) { }
/// <summary> /// Initialize a new instance of <see cref="ConfidenceWeightedPortfolioConstructionModel"/> /// </summary> /// <param name="resolution">Rebalancing frequency</param> /// <param name="portfolioBias">Specifies the bias of the portfolio (Short, Long/Short, Long)</param> public ConfidenceWeightedPortfolioConstructionModel(Resolution resolution = Resolution.Daily, PortfolioBias portfolioBias = PortfolioBias.LongShort) : base(resolution, portfolioBias) { }