Overall Statistics
Total Trades
72539
Average Win
0.32%
Average Loss
-0.28%
Compounding Annual Return
52.229%
Drawdown
42.600%
Expectancy
0.070
Net Profit
64084.583%
Sharpe Ratio
1.211
Probabilistic Sharpe Ratio
52.084%
Loss Rate
51%
Win Rate
49%
Profit-Loss Ratio
1.16
Alpha
0.408
Beta
0.169
Annual Standard Deviation
0.347
Annual Variance
0.121
Information Ratio
0.92
Tracking Error
0.374
Treynor Ratio
2.485
Total Fees
$10056254.77
Estimated Strategy Capacity
$17000000.00
Lowest Capacity Asset
RBLX XMP3AJ4KU3C5
Portfolio Turnover
121.05%
#region imports
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using QuantConnect.Algorithm.Framework.Selection;
    using QuantConnect.Util;
    using QuantConnect.Indicators;
    using QuantConnect.Data;
    using QuantConnect.Data.Fundamental;
    using QuantConnect.Data.Market;
    using QuantConnect.Data.UniverseSelection;
    using QuantConnect.Orders.Fees;
    using QuantConnect.Scheduling;
    using QuantConnect.Securities;
#endregion

namespace QuantConnect.Algorithm.CSharp
{
    public class Flock : QCAlgorithm
    {
		List<Symbol> HighDollarVolumeStocks = new List<Symbol>();
		int _coarse_count = 250;
		int _stocks_to_hold = 10;
		int _period = 5;
		decimal Leverage = 0.99m;
		decimal Threshold = 0.0m;
        private bool _selection_flag = false;

        private Dictionary<Symbol, RollingWindow<decimal>> _data = new Dictionary<Symbol, RollingWindow<decimal>>();
        private Dictionary<Symbol, float> _weight = new Dictionary<Symbol, float>();
        private Dictionary<Symbol, decimal> _price = new Dictionary<Symbol, decimal>();
		
        private decimal _capital = 100000;
        private Symbol _benchmark_symbol;
        private List<Decimal> _benchmark_values;

        public override void Initialize()
        {
            UniverseSettings.Resolution = Resolution.Minute;

            SetStartDate(2008, 1, 1);
            SetCash(100000);

            AddUniverseSelection(new FineFundamentalUniverseSelectionModel(CoarseSelectionFunction, FineSelectionFunction));
            Symbol symbol = AddEquity("SPY", Resolution.Minute).Symbol;
            
            SetSecurityInitializer((Security security) => security.SetMarketPrice(GetLastKnownPrice(security)));

            // selection lambda function
            Schedule.On(DateRules.MonthStart(symbol), TimeRules.AfterMarketOpen(symbol), () =>
            {
                _selection_flag = true;
            });

            _benchmark_symbol = AddEquity("TQQQ", Resolution.Daily).Symbol;
            _benchmark_values = new List<decimal>();

            Schedule.On(DateRules.EveryDay(_benchmark_symbol), TimeRules.BeforeMarketClose(_benchmark_symbol), update_eq_chart);
        }

        public void update_eq_chart()
        {
            List<TradeBar> hist = History<TradeBar>(_benchmark_symbol, 2, Resolution.Daily).ToList<TradeBar>();
            if (hist.Count() == 2)
            {
                TradeBar bar = hist[0];
                _benchmark_values.Add(bar.Close);
                decimal benchmark_perf = (_benchmark_values.Last() / _benchmark_values.First()) * _capital;
                Plot("Strategy Equity", _benchmark_symbol.Value, benchmark_perf);
            }
            else{
                Plot("Strategy Equity", _benchmark_symbol.Value, 0);
            }
        }

        public override void OnSecuritiesChanged(SecurityChanges changes)
        {
            foreach (Security security in changes.AddedSecurities){
                // security.SetFeeModel(new CustomFeeModel());
                security.SetMarketPrice(GetLastKnownPrice(security));
            }

            // liquidate first
            Symbol[] invested = (from x in Portfolio where x.Value.Invested select x.Key).ToArray();
            foreach (Symbol symbol in invested)
                if (!_weight.ContainsKey(symbol))
                    MarketOnOpenOrder(symbol, -Portfolio[symbol].Quantity);
                    // Liquidate(symbol);

            // trade execution
            foreach (KeyValuePair<Symbol, float> item in _weight)
            {
                // SetHoldings(item.Key, item.Value);
                decimal q = (Portfolio.TotalPortfolioValue * Leverage * (decimal)item.Value) / _price[item.Key]; //#data[item.Key].Value;
                decimal additional_q = q - Portfolio[item.Key].Quantity;
                MarketOnOpenOrder(item.Key, additional_q);
            }
            
            _weight.Clear();
            _price.Clear();
        }

        IEnumerable<Symbol> CoarseSelectionFunction(IEnumerable<CoarseFundamental> coarse)
        {
            // store daily stock prices
            foreach(CoarseFundamental stock in coarse){
                Symbol symbol = stock.Symbol;
                if (_data.ContainsKey(symbol))
                    _data[symbol].Add(stock.AdjustedPrice);
            }

            // if (!_selection_flag)
            //     return Universe.Unchanged;
            
            Symbol[] selected = coarse.Where(x=> x.Symbol.Value != "AMC" && 
                x.Symbol.Value != "GME" && x.Symbol.Value != "UVXY").OrderByDescending(x=>x.DollarVolume).Select(x=>x.Symbol).Take(_coarse_count).ToArray<Symbol>();
            
            foreach (Symbol symbol in selected){
                if (_data.ContainsKey(symbol))
                    continue;
                
                _data[symbol] = new RollingWindow<decimal>((int)_period);
                IEnumerable<TradeBar> history = History<TradeBar>(symbol, _period, Resolution.Daily);
                if (history.IsNullOrEmpty()){
                    Log($"Not enough data for {symbol} yet");
                    continue;
                }

                foreach (TradeBar bar in history) {
                   _data[symbol].Add(bar.Close);
                }
                
                // TEST
                // IEnumerable<TradeBar> m_history = History<TradeBar>(symbol, _period, Resolution.Minute);
            }

            return selected.Where(x=>_data[x].IsReady).ToList<Symbol>();
        }

        IEnumerable<Symbol> FineSelectionFunction(IEnumerable<FineFundamental> fine)
        {
            // List<Symbol> fine_symbols = fine.Select(x=>x.Symbol).ToList<Symbol>();

            Dictionary<Symbol, decimal> avg_ratio = new Dictionary<Symbol, decimal>();

            foreach (FineFundamental stock1 in fine)
            {
                Symbol symbol1 = stock1.Symbol;

                decimal[] prices1 = _data[symbol1].ToArray<decimal>();
                decimal averagePrice1 = prices1.Average();
                decimal[] normalizedPrices1 = prices1.Select(x => x / averagePrice1).ToArray();
                decimal sumRatios = 0;

                foreach (FineFundamental stock2 in fine)
                {
                    Symbol symbol2 = stock2.Symbol;

                    if (symbol1 != symbol2)
                    {
                        decimal[] prices2 = _data[symbol2].ToArray<decimal>();
                        decimal averagePrice2 = prices2.Average();
                        decimal[] normalizedPrices2 = prices2.Select(x => x / averagePrice2).ToArray();
                        decimal[] differences = normalizedPrices1.Zip(normalizedPrices2, (x, y) => x - y).ToArray();
                        decimal maxDifference = differences.Max();
                        decimal minDifference = differences.Min();
                        decimal differenceRange = maxDifference - minDifference;
                        decimal currentDifference = normalizedPrices1.First() - normalizedPrices2.First();
                        
                        if (differenceRange != 0)
                        {
                            decimal ratio = currentDifference / differenceRange;
                            sumRatios += ratio;
                        }
                    }
                }
                decimal avg_ratio_value = sumRatios / (fine.Count() - 1);
                if (avg_ratio_value != 0)
                {
                    avg_ratio[symbol1] = avg_ratio_value;
                    _price[symbol1] = prices1.First();
                }
            }
            
            Symbol[] _long = new Symbol[] {};

            if (avg_ratio.Count >= _stocks_to_hold)
            {
                _long = avg_ratio.OrderByDescending(x => Math.Abs(x.Value)).ToArray().Take(_stocks_to_hold).Select(x=>x.Key).ToArray<Symbol>();
                foreach(Symbol symbol in _long)
                {
                    if (avg_ratio[symbol] < 0)
                        _weight[symbol] = 1.0f / (float)(_long.Count());
                        
                    else if (avg_ratio[symbol] > 0)
                        _weight[symbol] = -1.0f / (float)(_long.Count());
                }
            }

            return _weight.Keys;
        }

        public void OnData(TradeBars data)
        {
            return;
            // liquidate first
            // Symbol[] invested = (from x in Portfolio where x.Value.Invested select x.Key).ToArray();
            // foreach (Symbol symbol in invested)
            //     if (!_weight.ContainsKey(symbol))
            //         Liquidate(symbol);

            // // trade execution
            // foreach (KeyValuePair<Symbol, float> item in _weight)
            //     if(data.ContainsKey(item.Key))
            //     {
            //         // SetHoldings(item.Key, item.Value);
            //         decimal q = (Portfolio.TotalPortfolioValue * (decimal)item.Value) / data[item.Key].Value;
            //         MarketOnOpenOrder(item.Key, q);
            //     }
            
            // _weight.Clear();
        }
    }
    
    public class CustomFeeModel : FeeModel {
        public override OrderFee GetOrderFee(OrderFeeParameters parameters) {
            // custom fee math
            var fee = parameters.Security.Price * parameters.Order.AbsoluteQuantity * 0.00005m;
            return new OrderFee(new CashAmount(fee, "USD"));
        }
    }
}