Overall Statistics
Total Trades
699
Average Win
3.65%
Average Loss
-0.71%
Compounding Annual Return
18.269%
Drawdown
26.100%
Expectancy
0.902
Net Profit
949.568%
Sharpe Ratio
0.907
Probabilistic Sharpe Ratio
25.416%
Loss Rate
69%
Win Rate
31%
Profit-Loss Ratio
5.12
Alpha
0.064
Beta
0.54
Annual Standard Deviation
0.149
Annual Variance
0.022
Information Ratio
0.025
Tracking Error
0.14
Treynor Ratio
0.251
Total Fees
$1890.85
Estimated Strategy Capacity
$36000000.00
Lowest Capacity Asset
KLAC R735QTJ8XC9X
#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 LongTrendVolatility : QCAlgorithm
    {
        /********** Const ************/
        const string BENCHMARK = "QQQ";
        
        /// Used to Enable Ticker List For Specialized Runs
        const bool ENABLE_TICKER_LIST = false;

        const int ROLLING_WINDOW_COUNT = 21;

        const int NUM_POSITIONS = 12;

        decimal POSITION_SIZE;

        /********** Vars ************/
        int _LastYear = -1;

        // Start Times
        /// START BACK 1 MONTH FOR WARMUP
        DateTime startDate = new DateTime(2008, 1, 1);
        DateTime endDate = new DateTime(2022, 1, 1);

        /********** Structures ************/
        string[] _TickerList = {"NFLX", "AAPL", "MSFT", "GOOG", "NVDA", "TSLA", "AMZN", "FB", "WMT", "F", "ASH", "CAKE", 
        "VECO", "NFLX", "CMG"};

        // Map Symbols to their Rolling Window of Tradebar Data
        Dictionary<Symbol, RollingWindow<TradeBar>> _WindowDict = new Dictionary<Symbol, RollingWindow<TradeBar>>();

        // Map Symbols to their Indicator Data
        Dictionary<Symbol, SymbolData> _SymbolDict = new Dictionary<Symbol, SymbolData>();

        // Industry Group Mapping
        Dictionary<int, string> _Industries = new Dictionary<int, string>();

        // Mapping Stocks To Their Industry Group
        Dictionary<int, List<Security>> _IndustryStocks = new Dictionary <int, List<Security>>();

        Dictionary<Symbol, TradeStruct> _TradeInfo = new Dictionary<Symbol, TradeStruct>();
        /********** Methods ************/

        public override void Initialize()
        {
            POSITION_SIZE = 1m / NUM_POSITIONS;

            SetStartDate(startDate);
            SetEndDate(endDate);
            SetCash(100000);

            UniverseSettings.Resolution = Resolution.Daily;
            UniverseSettings.DataNormalizationMode = DataNormalizationMode.SplitAdjusted;
            EnableAutomaticIndicatorWarmUp = true;

            //AddUniverseSelection(new FineFundamentalUniverseSelectionModel(SelectCoarse, SelectFine));
            //AddUniverseSelection(new FineFundamentalUniverseSelectionModel(SelectCoarse, SelectRussell));
            AddUniverseSelection(new FineFundamentalUniverseSelectionModel(SelectCoarse, SelectNdx100));
            var security = AddEquity(BENCHMARK, Resolution.Daily);

            SetBenchmark(BENCHMARK);

            /// Setup Industry Mapping
            Type type = typeof(MorningstarIndustryCode); // MyClass is static class with static properties
            foreach (var p in type.GetFields( System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public
                                              ))
            {
                object v = p.GetValue(null); // static classes cannot be instanced, so use null...=

                var industryCode = v.ToString().ToInt32();
                _Industries[industryCode] = p.Name;
                _IndustryStocks[industryCode] = new List<Security>();
            }

            //Schedule.On(Schedule.DateRules.EveryDay(),
            //    Schedule.TimeRules.AfterMarketOpen(security.Symbol, 1), LongTrendStrategy);

            //Schedule.On(Schedule.DateRules.EveryDay(),
            //    Schedule.TimeRules.AfterMarketOpen(security.Symbol, 1), LiquidTrendFollowOnPullback);

            Schedule.On(Schedule.DateRules.EveryDay(),
                Schedule.TimeRules.AfterMarketOpen(security.Symbol, 1), Swing);


        }

        public override void OnEndOfAlgorithm()
        {
            Liquidate();
            base.OnEndOfAlgorithm();
        }

        public void LiquidTrendFollowOnPullback()
        {
            if(Time.Date.Subtract(startDate.Date).Days < 31)
            {
                // Log("Skipping");
                return;
            }

            /// Update Stop Loss If Necessary
            /// Close Any Trades and Remove Them If Necessary
            UpdateOpenPositions();

            if(ActiveSecurities[BENCHMARK].Close < _SymbolDict[BENCHMARK].OneHundredSma)
            {
                /// Liquidate();
                return;
            }

            int lNumberOfPositionsToOpen = NUM_POSITIONS - (_TradeInfo.Count);

            var lStocksToTrade = (from pair in _SymbolDict
                where ActiveSecurities.ContainsKey(pair.Key)
                where _WindowDict.ContainsKey(pair.Key)
                where _WindowDict[pair.Key].Count == ROLLING_WINDOW_COUNT
                && _SymbolDict[pair.Key].AverageDollarVolume > 36000000
                && _SymbolDict[pair.Key].FiftySma > _SymbolDict[pair.Key].TwoHundredEma
                && ActiveSecurities[pair.Key].Close > _SymbolDict[pair.Key].FiftySma
                && _SymbolDict[pair.Key].RSI < 50
                && _SymbolDict[pair.Key].AdrPercent > 2.0m
                && !ActiveSecurities[pair.Key].Invested
                orderby _SymbolDict[pair.Key].TwoHundredDayChange descending
                select pair.Key).Take(lNumberOfPositionsToOpen).ToList();

            foreach(var lPotentialTrade in lStocksToTrade)
            {
                // Log("Opening Positions");
                SetHoldings(lPotentialTrade, POSITION_SIZE);

                /// Add Stock and Initial Stop Loss
                decimal lStopLoss = ActiveSecurities[lPotentialTrade].Close - ( (_SymbolDict[lPotentialTrade].Atr) * 5);

                _TradeInfo[lPotentialTrade] = new TradeStruct(lStopLoss, ActiveSecurities[lPotentialTrade].Close);
            }
        }

        public void Swing()
        {
            if(Time.Date.Subtract(startDate.Date).Days < 31)
            {
                // Log("Skipping");
                return;
            }

            /// Update Stop Loss If Necessary
            /// Close Any Trades and Remove Them If Necessary
            UpdateOpenPositions();

            if(ActiveSecurities[BENCHMARK].Close < _SymbolDict[BENCHMARK].TwoHundredEma)
            {
                /// Liquidate();
                return;
            }

            int lNumberOfPositionsToOpen = NUM_POSITIONS - (_TradeInfo.Count);

            var lStocksToTrade = (from pair in _SymbolDict
                where ActiveSecurities.ContainsKey(pair.Key)
                where _WindowDict.ContainsKey(pair.Key)
                where _WindowDict[pair.Key].Count == ROLLING_WINDOW_COUNT
                where pair.Key != BENCHMARK
                && _SymbolDict[pair.Key].AverageDollarVolume > 10000000
                // && _SymbolDict[pair.Key].FiftySma > _SymbolDict[pair.Key].TwoHundredEma
                && ActiveSecurities[pair.Key].Close > _SymbolDict[pair.Key].OneHundredSma
                // && ActiveSecurities[pair.Key].Close > _SymbolDict[pair.Key].TwoHundredEma
                && !ActiveSecurities[pair.Key].Invested
                && _SymbolDict[pair.Key].RSI < 41
                orderby _SymbolDict[pair.Key].TwoHundredDayChange descending
                select pair.Key).Take(lNumberOfPositionsToOpen).ToList();

            foreach(var lPotentialTrade in lStocksToTrade)
            {
                // Log("Opening Positions");
                SetHoldings(lPotentialTrade, POSITION_SIZE * 0.98m);

                /// Add Stock and Initial Stop Loss
                decimal lStopLoss = ActiveSecurities[lPotentialTrade].Close - ( (_SymbolDict[lPotentialTrade].Atr) * 1.5m);

                _TradeInfo[lPotentialTrade] = new TradeStruct(lStopLoss, ActiveSecurities[lPotentialTrade].Close);
            }
        }

        public void LowVolatilityTrendFollowing()
        {
            if(Time.Date.Subtract(startDate.Date).Days < 31)
            {
                // Log("Skipping");
                return;
            }

            /// Update Stop Loss If Necessary
            /// Close Any Trades and Remove Them If Necessary
            UpdateOpenPositions();

            if(ActiveSecurities[BENCHMARK].Close < _SymbolDict[BENCHMARK].TwoHundredEma)
            {
                /// Liquidate();
                return;
            }

            int lNumberOfPositionsToOpen = NUM_POSITIONS - (_TradeInfo.Count);

            var lStocksToTrade = (from pair in _SymbolDict
                where ActiveSecurities.ContainsKey(pair.Key)
                where _WindowDict.ContainsKey(pair.Key)
                where _WindowDict[pair.Key].Count == ROLLING_WINDOW_COUNT
                && _SymbolDict[pair.Key].AverageDollarVolume > 100000000
                && ActiveSecurities[pair.Key].Close > _SymbolDict[pair.Key].TwoHundredEma
                && _SymbolDict[pair.Key].AdrPercent < 4.0m
                && !ActiveSecurities[pair.Key].Invested
                orderby _SymbolDict[pair.Key].RSI ascending
                select pair.Key).Take(lNumberOfPositionsToOpen).ToList();

            foreach(var lPotentialTrade in lStocksToTrade)
            {
                // Log("Opening Positions");
                SetHoldings(lPotentialTrade, POSITION_SIZE);

                /// Add Stock and Initial Stop Loss
                decimal lStopLoss = ActiveSecurities[lPotentialTrade].Close - ( (_SymbolDict[lPotentialTrade].Atr) * 1.5m);

                _TradeInfo[lPotentialTrade] = new TradeStruct(lStopLoss, ActiveSecurities[lPotentialTrade].Close);
            }
        }

        public void UpdateOpenPositions()
        {
            List<Symbol> lClosedPositions = new List<Symbol>();

            foreach(var openPositionPair in _TradeInfo)
            {
                if(!ActiveSecurities.ContainsKey(openPositionPair.Key) || !ActiveSecurities[openPositionPair.Key].Invested)
                {
                    lClosedPositions.Add(openPositionPair.Key);
                    continue;   
                }

                openPositionPair.Value.CurrentHigh = Math.Max(openPositionPair.Value.CurrentHigh, ActiveSecurities[openPositionPair.Key].High);
                openPositionPair.Value.StopLoss = Math.Max(openPositionPair.Value.StopLoss, openPositionPair.Value.CurrentHigh * 0.825m);

                if(ActiveSecurities[openPositionPair.Key].Close < openPositionPair.Value.StopLoss)
                {
                    lClosedPositions.Add(openPositionPair.Key);
                }
            }

            foreach(var lClosedTrade in lClosedPositions)
            {
                _TradeInfo.Remove(lClosedTrade);
                Liquidate(lClosedTrade);
            }
        }

        public void LongTrendStrategy()
        {
            if(Time.Date.Subtract(startDate.Date).Days < 31)
            {
                // Log("Skipping");
                return;
            }

            /// Update Stop Loss If Necessary
            /// Close Any Trades and Remove Them If Necessary
            UpdateOpenPositions();

            if(ActiveSecurities[BENCHMARK].Close < _SymbolDict[BENCHMARK].OneHundredSma)
            {
                Liquidate();
                return;
            }

            int lNumberOfPositionsToOpen = NUM_POSITIONS - (_TradeInfo.Count);

            var lStocksToTrade = (from pair in _SymbolDict
                where ActiveSecurities.ContainsKey(pair.Key)
                where _WindowDict.ContainsKey(pair.Key)
                where _WindowDict[pair.Key].Count == ROLLING_WINDOW_COUNT
                && _SymbolDict[pair.Key].AverageDollarVolume > 50000000
                && _SymbolDict[pair.Key].TwentyFiveSma > _SymbolDict[pair.Key].FiftySma
                && !ActiveSecurities[pair.Key].Invested
                orderby _SymbolDict[pair.Key].TwoHundredDayChange descending
                select pair.Key).Take(lNumberOfPositionsToOpen).ToList();

            foreach(var lPotentialTrade in lStocksToTrade)
            {
                // Log("Opening Positions");
                SetHoldings(lPotentialTrade, POSITION_SIZE);

                /// Add Stock and Initial Stop Loss
                decimal lStopLoss = ActiveSecurities[lPotentialTrade].Close - ( (_SymbolDict[lPotentialTrade].Atr) * 5);

                _TradeInfo[lPotentialTrade] = new TradeStruct(lStopLoss, ActiveSecurities[lPotentialTrade].Close);
            }
        }

        public void WindowBarHandler(object sender, TradeBar windowBar)
        {
            if(!_WindowDict.ContainsKey(windowBar.Symbol))
            {
                _WindowDict[windowBar.Symbol] = new RollingWindow<TradeBar>(ROLLING_WINDOW_COUNT); 
            }

            _WindowDict[windowBar.Symbol].Add(windowBar);
        }

        public override void OnSecuritiesChanged(SecurityChanges changes)
        {
            // if we have no changes, do nothing
            if (changes == SecurityChanges.None) return;

            foreach (var security in changes.RemovedSecurities)
            {
                if(_SymbolDict.ContainsKey(security.Symbol))
                {
                    _SymbolDict.Remove(security.Symbol);
                }

                if(security.Fundamentals != null)
                {
                    var lCode = security.Fundamentals.AssetClassification.MorningstarIndustryCode;
                    if(_IndustryStocks.ContainsKey(lCode))
                    {
                        _IndustryStocks[lCode].Remove(security);
                    }
                }
            }

            foreach (var security in changes.AddedSecurities)
            {
                if(!_SymbolDict.ContainsKey(security.Symbol) && security.IsTradable)
                {
                    security.SetLeverage(1);
                    _SymbolDict.Add(security.Symbol, new SymbolData(security.Symbol, this));

                    var consolidator = new TradeBarConsolidator(TimeSpan.FromDays(1));
                    consolidator.DataConsolidated += WindowBarHandler;

                    SubscriptionManager.AddConsolidator(security.Symbol, consolidator);

                    if(security.Fundamentals != null)
                    {
                        var lCode = security.Fundamentals.AssetClassification.MorningstarIndustryCode;
                        if(_IndustryStocks.ContainsKey(lCode))
                        {
                            _IndustryStocks[lCode].Add(security);
                        }
                    }
                }
            }
        }

        IEnumerable<Symbol> SelectCoarse(IEnumerable<CoarseFundamental> coarse)
        {
            if (Time.Year == _LastYear)
            {
                return Universe.Unchanged;
            }

            var sortedByDollarVolume =
                (from x in coarse
                 where x.HasFundamentalData && x.DollarVolume > 0 && x.Price > 0
                 && DoesTickerExist(x.Symbol.Value)
                 orderby x.DollarVolume descending
                 select x.Symbol).ToList();

            return sortedByDollarVolume;
        }

        IEnumerable<Symbol> SelectFine(IEnumerable<FineFundamental> fine) 
        {
            var filteredFine =
                (from x in fine
                 where (x.CompanyReference.PrimaryExchangeID == "NAS")
                 where x.AssetClassification.MorningstarSectorCode != MorningstarSectorCode.FinancialServices
                 orderby x.MarketCap descending
                 select x.Symbol).Take(100).ToList();

            _LastYear = Time.Year;

            return filteredFine;
        }

        IEnumerable<Symbol> SelectRussell(IEnumerable<FineFundamental> fine) 
        {
            var filteredFine =
                (from x in fine
                 where (x.CompanyReference.PrimaryExchangeID == "NAS" || x.CompanyReference.PrimaryExchangeID == "NYS" )
                 orderby x.MarketCap descending
                 select x.Symbol).Take(1000).ToList();

            _LastYear = Time.Year;

            return filteredFine;
        }

        IEnumerable<Symbol> SelectNdx100(IEnumerable<FineFundamental> fine) 
        {
            var filteredFine =
                (from x in fine
                 where (x.CompanyReference.PrimaryExchangeID == "NAS")
                 where x.AssetClassification.MorningstarSectorCode != MorningstarSectorCode.FinancialServices
                 orderby x.MarketCap descending
                 select x.Symbol).Take(100).ToList();

            _LastYear = Time.Year;

            return filteredFine;
        }

        public bool DoesTickerExist(string ticker)
        {
            if(!ENABLE_TICKER_LIST)
            {
                return true;
            }


        	bool tickerExists = false;
        	foreach(var symbol in _TickerList)
        	{
        		if(ticker == symbol)
                {
                    tickerExists = true;
                    break;
                }
        	}
            return tickerExists;
        }
    }

    public class TradeStruct
    {
        public decimal StopLoss;
        public decimal CurrentHigh;

        public TradeStruct(decimal aStopLoss, decimal aHigh)
        {
            StopLoss = aStopLoss;
            CurrentHigh = aHigh;
        }
    }

    public class SymbolData
    {
        Symbol _Symbol;
        QCAlgorithm _Algorithm;

        public AverageTrueRange Atr;
        public SimpleMovingAverage AveragePrice;

        public SimpleMovingAverage TwentyFiveSma;
        public SimpleMovingAverage FiftySma;
        public ExponentialMovingAverage TwoHundredEma;

        public SimpleMovingAverage OneHundredSma;

        public SimpleMovingAverage AverageDollarVolume;
        public RateOfChangePercent TwoHundredDayChange;
        public RelativeStrengthIndex RSI;
        

        public SymbolData(Symbol aSymbol, QCAlgorithm aAlgorithm)
        {
            _Symbol = aSymbol;
            _Algorithm = aAlgorithm;

            /// Indicator Setup

            RSI = aAlgorithm.RSI(aSymbol, 4);

            Atr = aAlgorithm.ATR(aSymbol, 63, MovingAverageType.Simple, Resolution.Daily);
            AveragePrice = aAlgorithm.SMA(aSymbol, 63, Resolution.Daily);

            TwentyFiveSma = aAlgorithm.SMA(aSymbol, 25, Resolution.Daily);
            FiftySma = aAlgorithm.SMA(aSymbol, 50, Resolution.Daily);

            TwoHundredEma = aAlgorithm.EMA(aSymbol, 200);

            OneHundredSma = aAlgorithm.SMA(aSymbol, 100, Resolution.Daily);

            AverageDollarVolume = aAlgorithm.SMA(aSymbol, 63, Resolution.Daily, x => ( ((TradeBar)x).Volume * ((TradeBar)x).Close ));
            TwoHundredDayChange = aAlgorithm.ROCP(aSymbol, 200);
        }

        public decimal AdrPercent
        {
            get
            {
                return (Atr / AveragePrice) * 100;
            }
        }
    }
}