So it would be nice if LEAN supported multiple algorithms out of the box with a sort of portfolio manager but it doesn’t. So I made my own which works to my purpose.
The idea is to make a SubAlgorithm class:
public SubAlgorithm(PortfolioAlgorithm mainalgorithm,decimal portfolioPct)
{
_mainAlgorithm = mainalgorithm;
PortfolioPct = portfolioPct;
}
public virtual void Initialize()
{
}
public virtual void OnData(TradeBars data)
{
}
You can then make a new sub algorithm deriving from this class, say a ETF rotation algorithm:
public class ETFRotation_SubAlgorithm: SubAlgorithm
{
public ETFRotation_SubAlgorithm(PortfolioAlgorithm mainalgorithm,decimal portfolioPct): base(mainalgorithm,portfolioPct)
{
}
public override void Initialize ()
{
foreach (var symbol in GrowthSymbols.Union(SafetySymbols))
{
var res = _mainAlgorithm.LiveMode ? Resolution.Minute : Resolution.Daily;
if (!_mainAlgorithm.Securities.ContainsKey(symbol))
_mainAlgorithm.AddSecurity(SecurityType.Equity, symbol, res);
var oneMonthPerformance = _mainAlgorithm.MOM(symbol, 30, Resolution.Daily);
var threeMonthPerformance = _mainAlgorithm.MOM(symbol, 90, Resolution.Daily);
var sd = new MeanRevertSymbolData {
Symbol = symbol,
OneMonthPerformance = oneMonthPerformance,
ThreeMonthPerformance = threeMonthPerformance,
_tradeReturns = AlgoHelper.LoadSymbolReturns (symbol),
_lastBuyTime = AlgoHelper.LoadSymbolBuyTime (symbol),
};
SymbolData.Add(sd);
var history = _mainAlgorithm.History(symbol, 90,Resolution.Daily);
foreach (var bar in history) {
oneMonthPerformance.Update (bar.Time,bar.Close);
threeMonthPerformance.Update (bar.Time,bar.Close);
}
}
}
Our PortfolioAlgorithm is a normal algorithm but contains a list of our sub algorithms
public class PortfolioAlgorithm: QCAlgorithm
{
private readonly List<SubAlgorithm> _subAlgorithms = new List<SubAlgorithm> ();
public PortfolioAlgorithm ()
{
Test1_SubAlgorithm sub = new Test1_SubAlgorithm (this, 0.15m);
Test2_SubAlgorithm sub2 = new Test2_SubAlgorithm (this, 0.10m);
Test3_SubAlgorithm sub3 = new Test3_SubAlgorithm (this, 0.30m);
Test4_SubAlgorithm sub4 = new Test4_SubAlgorithm (this, 0.15m);
Test5_SubAlgorithm sub5 = new Test5_SubAlgorithm (this, 0.15m);
Test6_SubAlgorithm sub6 = new Test6_SubAlgorithm (this, 0.10m);
_subAlgorithms.Add (sub);
_subAlgorithms.Add (sub2);
_subAlgorithms.Add (sub3);
_subAlgorithms.Add (sub4);
_subAlgorithms.Add (sub5);
_subAlgorithms.Add (sub6);
}
public override void Initialize ()
{
SetStartDate(2015, 01, 15); //Set Start Date
SetEndDate(2015,06, 01); //Set End Date
SetCash(100000); //Set Strategy Cash
foreach (var sub in _subAlgorithms) {
sub.Initialize ();
}
}
public void OnData(TradeBars data)
{
foreach (var sub in _subAlgorithms) {
sub.OnData (data);
}
}
}
Voila! Now we can add all the sub algorithms we want and it does work both in backtesting and live.
How to optimize the portfolio percentage of each sub algorithm is a good question. Making it dynamic is probably the best.
Currently I am testing this with a mean reversion algorithm, three ETF rotation algorithms, a xiv/vxx algorithm and a buy-and-hold hedge algorithm.