I'm working on my first algorithm and am getting a timeout error when I try to run a backtest. Any insight on what I'm doing wrong would be much appreciated. Here's the code, it's pretty simple:
namespace QuantConnect
{
public class FaberMonthlySectorRotation : QCAlgorithm
{
DateTime LastRotationTime = new DateTime(1980, 1, 1);
DateTime GoLiveDate = new DateTime(2005, 1, 1);
TimeSpan RotationInterval = TimeSpan.FromDays(30);
private Symbol spy = QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA);
SimpleMovingAverage spySma = null;
List<string> SectorEtfSymbols = new List<string>{ "XLI", "XLB", "XLE", "XLV", "XLP", "XLU", "XLF", "XLY", "XLK" };
List<SymbolData> SectorEtfs = new List<SymbolData>();
public override void Initialize()
{
SetCash(10000);
SetStartDate(2004, 1, 1);
SetEndDate(2016, 12, 31);
AddEquity(spy.ID.Symbol, Resolution.Daily);
spySma = SMA(spy, 300, Resolution.Daily);
foreach (var sym in SectorEtfSymbols)
{
Symbol symbol = QuantConnect.Symbol.Create(sym, SecurityType.Equity, Market.USA);
AddSecurity(SecurityType.Equity, sym, Resolution.Minute);
var threeMonthPerformance = MOM(symbol, 90, Resolution.Daily);
SectorEtfs.Add(new SymbolData
{
Symbol = symbol,
ThreeMonthPerformance = threeMonthPerformance
});
}
#region Charting
Chart stockPlot = new Chart("SPY");
Series spyPriceSeries = new Series("Price", SeriesType.Line, 0);
stockPlot.AddSeries(spyPriceSeries);
Series spySmaSeries = new Series("SMA", SeriesType.Line, 0);
stockPlot.AddSeries(spySmaSeries);
Series buyingAllowedSeries = new Series("Buying Allowed", SeriesType.Line, 1);
stockPlot.AddSeries(buyingAllowedSeries);
AddChart(stockPlot);
#endregion
}
public void OnData(TradeBars data)
{
if (Time.Date >= GoLiveDate)
{
TradeBar spyBar = data[spy];
//Plot("SPY", "Price", data[spy].Close);
//Plot("SPY", "SMA", spySma);
//Plot("SPY", "Buying Allowed", data[spy].Close > spySma ? 1 : -1);
var delta = Time.Subtract(LastRotationTime);
if (delta > RotationInterval)
{
LastRotationTime = Time;
if (data[spy].Close > spySma)
{
List<Symbol> topPerformers = this.SectorEtfs.OrderByDescending(x => x.ThreeMonthPerformance).Select(x => x.Symbol).Take(3).ToList();
foreach(Symbol x in Portfolio.Keys)
{
if (Portfolio[x].Invested && !topPerformers.Contains(x))
{
Liquidate(x);
}
}
foreach(Symbol x in topPerformers)
{
SetHoldings(x, .333);
}
}
else
{
Liquidate();
}
}
}
}
}
class SymbolData
{
public Symbol Symbol;
public Momentum ThreeMonthPerformance { get; set; }
}
}
Caleb Sandfort
Here's the backtest for the specifics
Caleb Sandfort
In case anyone's interested the culprite was using Symbol type in my helper class. Changed to a string and it worked fine.
class SymbolData { public Symbol Symbol; public Momentum ThreeMonthPerformance { get; set; } }
to
public String Symbol;
Jared Broad
Thank you for posting the solution Caleb! We were looking into this - it looks like there's a bug there somewhere which we'll solve shortly.
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Caleb Sandfort
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!