Overall Statistics |
Total Trades 371 Average Win 1.76% Average Loss -1.71% Compounding Annual Return 14.506% Drawdown 36.400% Expectancy 0.264 Net Profit 120.524% Sharpe Ratio 0.59 Probabilistic Sharpe Ratio 10.327% Loss Rate 38% Win Rate 62% Profit-Loss Ratio 1.03 Alpha 0.043 Beta 0.843 Annual Standard Deviation 0.207 Annual Variance 0.043 Information Ratio 0.182 Tracking Error 0.156 Treynor Ratio 0.145 Total Fees $1364.43 Estimated Strategy Capacity $6300000.00 Lowest Capacity Asset IYE RVLEALAHHC2T |
#region imports using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Globalization; using System.Drawing; using QuantConnect; using QuantConnect.Algorithm.Framework; using QuantConnect.Algorithm.Framework.Selection; using QuantConnect.Algorithm.Framework.Alphas; using QuantConnect.Algorithm.Framework.Portfolio; using QuantConnect.Algorithm.Framework.Execution; using QuantConnect.Algorithm.Framework.Risk; using QuantConnect.Parameters; using QuantConnect.Benchmarks; using QuantConnect.Brokerages; using QuantConnect.Util; using QuantConnect.Interfaces; using QuantConnect.Algorithm; using QuantConnect.Indicators; using QuantConnect.Data; using QuantConnect.Data.Consolidators; using QuantConnect.Data.Custom; using QuantConnect.DataSource; using QuantConnect.Data.Fundamental; using QuantConnect.Data.Market; using QuantConnect.Data.UniverseSelection; using QuantConnect.Notifications; using QuantConnect.Orders; using QuantConnect.Orders.Fees; using QuantConnect.Orders.Fills; using QuantConnect.Orders.Slippage; using QuantConnect.Scheduling; using QuantConnect.Securities; using QuantConnect.Securities.Equity; using QuantConnect.Securities.Future; using QuantConnect.Securities.Option; using QuantConnect.Securities.Forex; using QuantConnect.Securities.Crypto; using QuantConnect.Securities.Interfaces; using QuantConnect.Storage; using QuantConnect.Data.Custom.AlphaStreams; using QCAlgorithmFramework = QuantConnect.Algorithm.QCAlgorithm; using QCAlgorithmFrameworkBridge = QuantConnect.Algorithm.QCAlgorithm; #endregion namespace QuantConnect.Algorithm.CSharp { public class WellDressedVioletChinchilla : QCAlgorithm { private List<BrainSentimentIndicatorUniverse> _brainUniverseData; private Dictionary<Symbol, List<Symbol>> _etfConstituentsBySymbol = new(); private Dictionary<Symbol, decimal?> _scoreBySector = new(); private int _numEtfs = 3; private Dictionary<DateTime, decimal> _portfolioValues = new(); private bool _rebalance = false; public override void Initialize() { SetStartDate(2017, 1, 1); SetEndDate(2022, 11, 1); SetCash(100000); AddUniverse<BrainSentimentIndicatorUniverse>( "BrainSentimentIndicatorUniverse", Resolution.Daily, altCoarse => { _brainUniverseData = altCoarse.ToList(); return Enumerable.Empty<Symbol>(); }); // Sector ETFs from https://www.cnbc.com/sector-etfs/ var tickers = new[] { "XLE", // Energy Select Sector SPDR Fund "XLF", // Financial Select Sector SPDR Fund "XLU", // Utilities Select Sector SPDR Fund "XLI", // Industrial Select Sector SPDR Fund "GDX", // VanEck Gold Miners ETF "XLK", // Technology Select Sector SPDR Fund "XLV", // Health Care Select Sector SPDR Fund "XLY", // Consumer Discretionary Select Sector SPDR Fund "XLP", // Consumer Staples Select Sector SPDR Fund "XLB", // Materials Select Sector SPDR Fund "XOP", // Spdr S&P Oil & Gas Exploration & Production Etf "IYR", // iShares U.S. Real Estate ETF "XHB", // Spdr S&P Homebuilders Etf "ITB", // iShares U.S. Home Construction ETF "VNQ", // Vanguard Real Estate Index Fund ETF Shares "GDXJ",// VanEck Junior Gold Miners ETF "IYE", // iShares U.S. Energy ETF "OIH", // VanEck Oil Services ETF "XME", // SPDR S&P Metals & Mining ETF "XRT", // Spdr S&P Retail Etf "SMH", // VanEck Semiconductor ETF "IBB", // iShares Biotechnology ETF "KBE", // SPDR S&P Bank ETF "KRE", // SPDR S&P Regional Banking ETF "XTL" // SPDR S&P Telecom ETF }; foreach (var ticker in tickers) { var etfSymbol = AddEquity(ticker, Resolution.Daily).Symbol; AddUniverse(Universe.ETF(etfSymbol, Market.USA, UniverseSettings, constituents => { _etfConstituentsBySymbol[etfSymbol] = constituents.Select(x => x.Symbol).ToHashSet().ToList(); return Enumerable.Empty<Symbol>(); }) ); } // Schedule the rebalance Schedule.On(DateRules.MonthStart(GetParameter("rebalance-day", 0)), TimeRules.Midnight, () => { _rebalance = true; }); } public override void OnData(Slice data) { // Record net portfolio value _portfolioValues[Time] = Portfolio.TotalPortfolioValue; // Rebalance? if (!_rebalance) { return; } _rebalance = false; // Calculate sector sentiment foreach (var kvp in _etfConstituentsBySymbol) { _scoreBySector[kvp.Key] = _brainUniverseData .Where(brainSentiment => kvp.Value.Contains(brainSentiment.Symbol)) .Select(brainSentiment => brainSentiment.Sentiment30Days) .Average(); } // Select target ETFs var targetSymbols = _scoreBySector.OrderByDescending(x => x.Value).Take(_numEtfs).Select(kvp => kvp.Key);; // Liquidate ETFs that are no longer targeted SetHoldings(Portfolio.Where(kvp => kvp.Value.Invested && !targetSymbols.Contains(kvp.Key)).Select(kvp => new PortfolioTarget(kvp.Key, 0)).ToList()); // Rebalance targeted ETFs var weight = 1.0m / _numEtfs; SetHoldings(targetSymbols.Select(symbol => new PortfolioTarget(symbol, weight)).ToList()); } public override void OnEndOfAlgorithm() { // Save daily portfolio values to ObjectStore ObjectStore.SaveJson<Dictionary<DateTime, decimal>>($"{ProjectId}/portfolioValues", _portfolioValues); } } }