Overall Statistics
Total Trades
1387
Average Win
1.95%
Average Loss
-0.82%
Compounding Annual Return
19.738%
Drawdown
29.900%
Expectancy
0.531
Net Profit
2466.103%
Sharpe Ratio
0.869
Probabilistic Sharpe Ratio
16.925%
Loss Rate
55%
Win Rate
45%
Profit-Loss Ratio
2.39
Alpha
0.102
Beta
0.563
Annual Standard Deviation
0.172
Annual Variance
0.03
Information Ratio
0.398
Tracking Error
0.163
Treynor Ratio
0.265
Total Fees
$8932.42
Estimated Strategy Capacity
$6300000.00
Lowest Capacity Asset
CHWY X5BUF5UE90F9
#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 = "SPY";
        /// 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 = 8;
        const int NUM_POSITIONS = 16;

        decimal POSITION_SIZE;

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

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

        /********** Structures ************/
        string[] _TickerList = {"NFLX", "MED", "AMZN", "NUS", "TPX", "TUP", "SHOO", "CROX", 
            "F", "FDS", "RJF", "GNW", "MIDD", "LULU", "CAR", "MELI", "DDS", "ODP", "RPM", 
            "NEU", "DECK", "SKX", "PVH", "HBI", "BC", "DAN", "SWK", "CHKP", "SWKS", "MRVL"};

        // 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, SelectRussell500));
            AddUniverseSelection(new FineFundamentalUniverseSelectionModel(SelectCoarse, SelectRussell));
            ///AddUniverseSelection(new FineFundamentalUniverseSelectionModel(SelectCoarse, SelectRussellModified));
            ///AddUniverseSelection(new FineFundamentalUniverseSelectionModel(SelectCoarse, SelectNdx100));
            //AddUniverseSelection(new FineFundamentalUniverseSelectionModel(SelectCoarse, SelectNdx200));
            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), LowVolTrend);

            SetWarmUp(new TimeSpan(252, 0, 0, 0));
        }

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

        public void LowVolTrend()
        {
            if(IsWarmingUp)
            {
                return;
            }

            /// Update Stop Loss If Necessary
            /// Close Any Trades and Remove Them If Necessary
            UpdateOpenPositions();
            
            /*if(ActiveSecurities[BENCHMARK].Close < _SymbolDict[BENCHMARK].OneHundredSma)
            {
                return;
            }*/

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

            int lNumberOfPositionsToOpen = NUM_POSITIONS - (_TradeInfo.Count);

            if(lNumberOfPositionsToOpen == 0)
            {
                return;
            }

            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 > 50000000
                /// && _SymbolDict[pair.Key].AverageDollarVolume > 7500000
                && !ActiveSecurities[pair.Key].Invested
                && ActiveSecurities[pair.Key].Close < 175
                && _SymbolDict[pair.Key].TwentyFiveSma > _SymbolDict[pair.Key].FiftySma
                && ActiveSecurities[pair.Key].Close > _SymbolDict[pair.Key].TwoHundo
                orderby _SymbolDict[pair.Key].TwoHundredDayChange descending
                /// && _SymbolDict[pair.Key].TwentyFiveSma < _SymbolDict[pair.Key].FiftySma /// 
                select pair.Key).Take(lNumberOfPositionsToOpen).ToList();

            foreach(var lPotentialTrade in lStocksToTrade)
            {
                var lTarget = POSITION_SIZE * 0.98m;
                var lQuantity = CalculateOrderQuantity(lPotentialTrade, lTarget);
                bool lCanTrade = true;

                if(ActiveSecurities[lPotentialTrade].Close * lQuantity > Portfolio.MarginRemaining)
                {
                    lCanTrade = false;
                }
                if(lCanTrade)
                {
                    // Log("Opening Positions");
                    SetHoldings(lPotentialTrade, POSITION_SIZE * 0.98m);

                    /// Add Stock and Initial Stop Loss
                    decimal lStopLoss = Math.Max(ActiveSecurities[lPotentialTrade].Close - ( (_SymbolDict[lPotentialTrade].Atr) * 4.5m), ActiveSecurities[lPotentialTrade].Close * 0.86m);
                    /// decimal lStopLoss = ActiveSecurities[lPotentialTrade].Close - ( (_SymbolDict[lPotentialTrade].Atr) * 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 ||
                   !_SymbolDict.ContainsKey(openPositionPair.Key))
                {
                    lClosedPositions.Add(openPositionPair.Key);
                    continue;   
                }

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

                var trailingMultiplier = ActiveSecurities[BENCHMARK].Close < _SymbolDict[BENCHMARK].TwoHundo ? 0.85m : 0.8m;
                openPositionPair.Value.StopLoss = Math.Max(openPositionPair.Value.StopLoss, openPositionPair.Value.CurrentHigh * trailingMultiplier);

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

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

        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" || x.CompanyReference.PrimaryExchangeID == "NYS" 
                        || x.CompanyReference.PrimaryExchangeID == "ASE")
                 where x.Price > 7 && x.Price < 150
                 orderby x.MarketCap descending
                 select x.Symbol).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> SelectRussell500(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(500).ToList();

            _LastYear = Time.Year;

            return filteredFine;
        }

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

            var russell = (from x in filteredFine
                where x.Price < 300
                select x.Symbol).ToList();

            _LastYear = Time.Year;

            return russell;
        }

        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;
        }

        IEnumerable<Symbol> SelectNdx200(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(200).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 TwoHundo;
        public ExponentialMovingAverage TwentyOneEma;
        public ExponentialMovingAverage QuarterEma;

        public SimpleMovingAverage OneHundredSma;

        public SimpleMovingAverage AverageDollarVolume;
        public RateOfChangePercent TwoHundredDayChange;
        public RelativeStrengthIndex RSI;
        public RateOfChangePercent FiftyDayChange;
        public RateOfChangePercent TwentyWeekChange;
        public Maximum TwentyWeekHigh;
        public Minimum TwoHundredDayLow;
        public SimpleMovingAverage HighLowRatioAverage;
        

        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);
            OneHundredSma = aAlgorithm.SMA(aSymbol, 100, Resolution.Daily);
            TwoHundo = aAlgorithm.SMA(aSymbol, 200, Resolution.Daily);
            AverageDollarVolume = aAlgorithm.SMA(aSymbol, 63, Resolution.Daily, x => ( ((TradeBar)x).Volume * ((TradeBar)x).Close ));
            TwoHundredDayChange = aAlgorithm.ROCP(aSymbol, 200);
            QuarterEma = aAlgorithm.EMA(aSymbol, 63);
            HighLowRatioAverage = aAlgorithm.SMA(aSymbol, 21, Resolution.Daily, x => ((TradeBar)x).High / ((TradeBar)x).Low);
            // TwoHundredDayLow = aAlgorithm.MIN(aSymbol, 200);

            
            /*FiftyDayChange = aAlgorithm.ROCP(aSymbol, 50);
            TwentyWeekChange = aAlgorithm.ROCP(aSymbol, 100);
            TwentyWeekHigh = aAlgorithm.MAX(aSymbol, 100, Resolution.Daily, x => ( ((TradeBar)x).Close ));
            TwoHundredEma = aAlgorithm.EMA(aSymbol, 200);
            TwentyOneEma = aAlgorithm.EMA(aSymbol, 21);
            ;*/
        }

        public decimal AdrPercent
        {
            get
            {
                return 100 * (HighLowRatioAverage - 1);
            }
        }
    }
}