Overall Statistics |
Total Trades 484 Average Win 0.67% Average Loss -0.71% Compounding Annual Return 8.719% Drawdown 15.400% Expectancy 0.286 Net Profit 161.743% Sharpe Ratio 0.863 Loss Rate 34% Win Rate 66% Profit-Loss Ratio 0.94 Alpha 0.057 Beta 0.308 Annual Standard Deviation 0.103 Annual Variance 0.011 Information Ratio -0.087 Tracking Error 0.158 Treynor Ratio 0.289 Total Fees $1022.33 |
namespace QuantConnect { /* * QuantConnect University: Futures Example * * QuantConnect allows importing generic data sources! This example demonstrates importing a futures * data from the popular open data source Quandl. * * QuantConnect has a special deal with Quandl giving you access to Stevens Continuous Futurs (SCF) for free. * If you'd like to download SCF for local backtesting, you can download it through Quandl.com. */ public class DualMomentumSectorRotation : QCAlgorithm { // we'll use this to tell us when the month has ended DateTime LastRotationTime = DateTime.MinValue; TimeSpan RotationInterval = TimeSpan.FromDays(30); List<string> GEMSymbols = new List<string> { "SPY", "BIL", "AGG" }; // these are the growth symbols we'll rotate through List<string> SectorSymbols = new List<string> { "XLV", //healthcare "XLK", //technology "XLI", //industrial "XLU", //utilities "XLF", //financials "XLY", //consumerdisc "XLP", //consumerstap "XLB", //basic materials "XLE", // energy "PSR", //real estate "IYZ" // communications }; // we'll hold some computed data in these guys List<SymbolData> SectorSymbolData = new List<SymbolData>(); Dictionary<string, SymbolData> GEMSymbolData = new Dictionary<string, SymbolData>(); List<string> strongSectors = new List<string>(); // Indicators //Momentum _momSPY; //Momentum _momACWI; //Momentum _momTbill; //DateTime sampledToday = DateTime.Now; public override void Initialize() { SetStartDate(2003, 7, 1); SetEndDate(2015, 1, 1); SetCash(25000); // define our daily trade bar consolidator. we can access the daily bar // from the DataConsolidated events var dailyConsolidator = new TradeBarConsolidator(TimeSpan.FromDays(1)); // attach our event handler. the event handler is a function that will be called each time we produce // a new consolidated piece of data. dailyConsolidator.DataConsolidated += OnDataDaily; foreach (var symbol in SectorSymbols) { AddSecurity(SecurityType.Equity, symbol, Resolution.Minute); Securities[symbol].SetDataNormalizationMode(DataNormalizationMode.TotalReturn); Securities[symbol].SetLeverage(1); var momentum = MOM(symbol, 252, Resolution.Daily); SubscriptionManager.AddConsolidator(symbol, dailyConsolidator); SectorSymbolData.Add(new SymbolData { Symbol = symbol, MomScore = momentum }); } foreach (var symbol in GEMSymbols) { AddSecurity(SecurityType.Equity, symbol, Resolution.Minute); Securities[symbol].SetDataNormalizationMode(DataNormalizationMode.TotalReturn); Securities[symbol].SetLeverage(1); var momentum = MOM(symbol, 252, Resolution.Daily); SubscriptionManager.AddConsolidator(symbol, dailyConsolidator); GEMSymbolData.Add(symbol, new SymbolData { Symbol = symbol, MomScore = momentum }); } } private bool first = true; //Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol. private void OnDataDaily(object sender, TradeBar consolidated) { var _momSPY = GEMSymbolData["SPY"].MomScore; var _momTbill = GEMSymbolData["BIL"].MomScore; var Bonds = GEMSymbolData["AGG"].Symbol; decimal holdingPercent = 1m; if (!_momSPY.IsReady) { if (Portfolio["SPY"].Quantity == 0) { SetHoldings("SPY", holdingPercent); } } if (first) { first = false; LastRotationTime = consolidated.Time; return; } var delta = consolidated.Time.Subtract(LastRotationTime); if (delta > RotationInterval) { // Easy rebalance for backtest foreach(string symbol in Securities.Keys) { Log("Liquidating " + Portfolio[symbol].Quantity + "of " + symbol); Liquidate(symbol); } LastRotationTime = consolidated.Time; var orderedMomScores = SectorSymbolData.OrderByDescending(x => x.MomScore.Current.Value).ToList(); int numberOfSectors = 4; for (int i = 0; i < numberOfSectors; i++) { strongSectors.Add(orderedMomScores[i].Symbol); Log("Strong Symbol #" + i + "is " + orderedMomScores[i].Symbol); } foreach (var x in orderedMomScores) { Log(">>SCORE>>" + x.Symbol + ">>" + x.MomScore); } if (_momSPY < 0) { SetHoldings(Bonds, holdingPercent); Log("Holding Percent is " + holdingPercent); Log("Set Holdings to " + Portfolio[Bonds].Quantity + "of " + Bonds); } else { foreach (var etf in strongSectors) { SetHoldings(etf, holdingPercent * (1m / numberOfSectors)); Log("Count of strongSectors is " + numberOfSectors); } } strongSectors.Clear(); } } } class SymbolData { public string Symbol; public Momentum MomScore { get; set; } } }