Overall Statistics
Total Orders
591
Average Win
0.18%
Average Loss
-0.12%
Compounding Annual Return
24.321%
Drawdown
16.300%
Expectancy
0.273
Net Profit
11.929%
Sharpe Ratio
0.829
Sortino Ratio
1.11
Probabilistic Sharpe Ratio
41.960%
Loss Rate
48%
Win Rate
52%
Profit-Loss Ratio
1.47
Alpha
-0.159
Beta
1.556
Annual Standard Deviation
0.231
Annual Variance
0.053
Information Ratio
-0.202
Tracking Error
0.166
Treynor Ratio
0.123
Total Fees
$750.58
Estimated Strategy Capacity
$120000000.00
Lowest Capacity Asset
KO R735QTJ8XC9X
Portfolio Turnover
11.81%
#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 BrainMLRankingDataAlgorithm : QCAlgorithm
    {
        private Dictionary<Symbol, Symbol> _symbolByDatasetSymbol = new Dictionary<Symbol, Symbol>();
        
        public override void Initialize()
        {
            SetStartDate(2021, 1, 1);
            SetEndDate(2021, 7, 8);
            SetCash(100000);
            
            var tickers = new List<string>() {"AAPL", "TSLA", "MSFT", "F", "KO"};
            foreach (var ticker in tickers)
            {
                // Requesting data
                var symbol = AddEquity(ticker, Resolution.Daily).Symbol;
                var datasetSymbol = AddData<BrainStockRanking2Day>(symbol).Symbol;
                _symbolByDatasetSymbol.Add(datasetSymbol, symbol);
                
                // Historical data
                var history = History<BrainStockRanking2Day>(datasetSymbol, 365, Resolution.Daily);
                Debug($"We got {history.Count()} items from our history request for {symbol}");
            }
        }

        public override void OnData(Slice slice)
        {
            // Collect rankings for all symbols
            var points = slice.Get<BrainStockRanking2Day>();
            if (points == null)
            {
                return;
            }
            var symbols = new List<Symbol>();
            var ranks = new List<decimal>();
            foreach (var point in points.Values)
            {
                symbols.Add(_symbolByDatasetSymbol[point.Symbol]);
                ranks.Add(point.Rank);
            }
            
            // Rank each symbol's Brain ML ranking relative to the other symbols
            if (ranks.Count() == 0) return;
            var sortedRanksTemp = new List<decimal>(ranks); 
            sortedRanksTemp.Sort();
            var sortedRanks = new List<decimal>();
            for (var i = 0; i < symbols.Count(); i++)
            {
                sortedRanks.Add(sortedRanksTemp.IndexOf(ranks[i]) + 1);
            }
            
            // Place orders -- give higher weight to symbols with higher Brain ML Ranking
            for (var i = 0; i < symbols.Count(); i++)
            {
                var rank = sortedRanks[i];
                var weight = rank / sortedRanks.Sum();
                SetHoldings(symbols[i], weight);
            }
        }
    }
}