Overall Statistics
Total Orders
4258
Average Win
0.09%
Average Loss
-0.09%
Compounding Annual Return
-7.897%
Drawdown
12.400%
Expectancy
-0.041
Net Profit
-7.897%
Sharpe Ratio
-1.357
Sortino Ratio
-1.811
Probabilistic Sharpe Ratio
1.233%
Loss Rate
52%
Win Rate
48%
Profit-Loss Ratio
1.00
Alpha
-0.069
Beta
-0.068
Annual Standard Deviation
0.06
Annual Variance
0.004
Information Ratio
-2.115
Tracking Error
0.126
Treynor Ratio
1.197
Total Fees
$4572.36
Estimated Strategy Capacity
$6600000.00
Lowest Capacity Asset
AVGO UEW4IOBWVPT1
Portfolio Turnover
54.67%
#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

using QuantConnect.DataSource;

namespace QuantConnect
{
    public class ExtractAlphaCrossAssetModelAlgorithm : QCAlgorithm
    {
        private DateTime _time = DateTime.MinValue;
        private Dictionary<Symbol, Symbol> _datasetSymbolBySymbol = new Dictionary<Symbol, Symbol>();
        private DataDictionary<ExtractAlphaCrossAssetModel> _points = new DataDictionary<ExtractAlphaCrossAssetModel>();
        
        public override void Initialize()
        {
            SetStartDate(2019, 1, 1);
            SetEndDate(2019, 12, 31);
            SetCash(100000);
            
            AddUniverse(MyCoarseFilterFunction);
        }
        
        private IEnumerable<Symbol> MyCoarseFilterFunction(IEnumerable<CoarseFundamental> coarse)
        {
            return (from c in coarse
                    where c.HasFundamentalData && c.Price > 4
                    orderby c.DollarVolume descending
                    select c.Symbol).Take(100);
        }
        
        public override void OnData(Slice slice)
        {
            if (_time > Time) return;
            
            // Accessing Data
            var points = slice.Get<ExtractAlphaCrossAssetModel>();
            if (points.Count > 0)
            {
                _points = points;
            }
            
            if (Time.TimeOfDay < TimeSpan.FromHours(10))
            {
                return;
            }

            var sortedByScore = from s in _points.Values
                            where (s.Score != null)
                            orderby s.Score descending
                            select s.Symbol.Underlying;
            var longSymbols = sortedByScore.Take(10).ToList();
            var shortSymbols = sortedByScore.TakeLast(10).ToList();

            var portfolioTargets = new List<PortfolioTarget>();
            foreach (var kvp in Portfolio)
            {
                var symbol = kvp.Key;
                var securityHolding = kvp.Value;
                var weight = 0.0m;
                if (longSymbols.Contains(symbol))
                {
                    weight = 0.05m;
                }
                else if (shortSymbols.Contains(symbol))
                {
                    weight = -0.05m;
                }
                else if (!securityHolding.Invested)
                {
                    continue;
                }
                portfolioTargets.Add(new PortfolioTarget(symbol, weight));
            }
            SetHoldings(portfolioTargets);
            
            _time = Expiry.EndOfDay(Time);
        }
        
        public override void OnSecuritiesChanged(SecurityChanges changes)
        {
            foreach(var security in changes.AddedSecurities)
            {
                // Requesting Data
                _datasetSymbolBySymbol[security.Symbol] = AddData<ExtractAlphaCrossAssetModel>(security.Symbol).Symbol;
            }

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