Overall Statistics
Total Orders
296
Average Win
0.15%
Average Loss
-0.07%
Compounding Annual Return
44.862%
Drawdown
10.800%
Expectancy
1.685
Net Profit
44.862%
Sharpe Ratio
1.806
Sortino Ratio
2.18
Probabilistic Sharpe Ratio
81.782%
Loss Rate
15%
Win Rate
85%
Profit-Loss Ratio
2.17
Alpha
0.047
Beta
1.255
Annual Standard Deviation
0.155
Annual Variance
0.024
Information Ratio
1.082
Tracking Error
0.087
Treynor Ratio
0.223
Total Fees
$313.48
Estimated Strategy Capacity
$270000000.00
Lowest Capacity Asset
BYND X45I7544YKIT
Portfolio Turnover
1.73%
#region imports
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Globalization;
    using System.Drawing;
    using QuantConnect;
    using System.Text.RegularExpressions;
    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.Algorithm.Selection;
    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 QCAlgorithmFramework = QuantConnect.Algorithm.QCAlgorithm;
    using QCAlgorithmFrameworkBridge = QuantConnect.Algorithm.QCAlgorithm;
#endregion

namespace QuantConnect
{
    public class SECReport8KAlgorithm : QCAlgorithm
    {
        private Dictionary<Symbol, Symbol> _datasetSymbolBySymbol = new Dictionary<Symbol, Symbol>();
        private List<Symbol> _longSymbols = new List<Symbol>();
        private bool _rebalance = false;

        public override void Initialize()
        {
            SetStartDate(2019, 1, 1);
            SetEndDate(2019, 12, 31);
            SetCash(100000);

            UniverseSettings.Resolution = Resolution.Daily;
            AddUniverse(CoarseSelector);

            // Request underlying equity data.
            var ibm = AddEquity("IBM", Resolution.Daily).Symbol;
            // Add SEC report 10-Q data for the underlying IBM asset
            var earningsFiling = AddData<SECReport10Q>(ibm, Resolution.Daily).Symbol;
            // Request 120 days of history with the SECReport10Q IBM custom data Symbol.
            var history = History<SECReport10Q>(earningsFiling, 120, Resolution.Daily);
            // Count the number of items we get from our history request
            Debug($"We got {history.Count()} items from our history request");
        }

        public IEnumerable<Symbol> CoarseSelector(IEnumerable<CoarseFundamental> coarse)
        {
            return coarse.Where(x => x.HasFundamentalData)
                .OrderByDescending(x => x.DollarVolume)
                .Take(10).Select(x => x.Symbol);
        }

        public override void OnData(Slice slice)
        {
            // Get all SEC data and loop over it
            foreach (var report in slice.Get<SECReport8K>().Values)
            {
                var underlyingSymbol = report.Symbol.Underlying;
                // Skip the Symbol if it's no longer in the universe
                if (!_datasetSymbolBySymbol.ContainsKey(underlyingSymbol))
                {
                    if (_longSymbols.Contains(underlyingSymbol))
                    {
                        _rebalance = true;
                        _longSymbols.Remove(underlyingSymbol);
                    }
                    continue;
                }

                // Get the length of all contents contained within the report
                var reportTextLength = report.Report.Documents.Select(x => x.Text.Length).Sum();

                if (reportTextLength > 20000)
                {
                    if (!_longSymbols.Contains(underlyingSymbol))
                    {
                        _rebalance = true;
                        _longSymbols.Add(underlyingSymbol);
                    }
                }
                else if (_longSymbols.Contains(underlyingSymbol))
                {
                    _rebalance = true;
                    _longSymbols.Remove(underlyingSymbol);
                }
            }

            if (!_rebalance)
            {
                return;
            }
            _rebalance = false;

            var portfolioTargets = new List<PortfolioTarget>();
            var equalWeighting = _longSymbols.Count > 0 ? 1.0m / _longSymbols.Count : 0m;
            foreach (var kvp in Portfolio)
            {
                var symbol = kvp.Key;
                var securityHolding = kvp.Value;
                var weight = 0m;
                if (_longSymbols.Contains(symbol))
                {
                    weight = equalWeighting;
                }
                else if (!securityHolding.Invested)
                {
                    continue;
                }
                portfolioTargets.Add(new PortfolioTarget(symbol, weight));
            }
            SetHoldings(portfolioTargets);
        }

        public override void OnSecuritiesChanged(SecurityChanges changes)
        {
            foreach (var security in changes.AddedSecurities)
            {
                // If added to universe, adds SECReport8K
                _datasetSymbolBySymbol[security.Symbol] = AddData<SECReport8K>(security.Symbol).Symbol;
            }

            foreach (var security in changes.RemovedSecurities)
            {                
                Symbol reportSymbol;
                if (_datasetSymbolBySymbol.TryGetValue(security.Symbol, out reportSymbol))
                {
                    RemoveSecurity(reportSymbol);
                    _datasetSymbolBySymbol.Remove(security.Symbol);
                }
            }
        }
    }
}