Overall Statistics
Total Trades
236
Average Win
0.09%
Average Loss
-0.06%
Compounding Annual Return
-0.028%
Drawdown
0.800%
Expectancy
-0.046
Net Profit
-0.341%
Sharpe Ratio
-0.067
Probabilistic Sharpe Ratio
0.000%
Loss Rate
62%
Win Rate
38%
Profit-Loss Ratio
1.50
Alpha
-0.001
Beta
0.004
Annual Standard Deviation
0.003
Annual Variance
0
Information Ratio
-0.637
Tracking Error
0.146
Treynor Ratio
-0.054
Total Fees
$236.00
Estimated Strategy Capacity
$1500000000.00
Lowest Capacity Asset
NUGT US9M4E7GANAD
#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
/*
 * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
 * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

using System.Collections.Concurrent;

namespace QuantConnect.Algorithm.CSharp
{
    /// <summary>
    /// Tests a wide variety of liquid and illiquid stocks together, with bins
    /// of 20 ranging from micro-cap to mega-cap stocks.
    /// </summary>
    public class MACDCrossOverAlgorithmMultipleStocks : QCAlgorithm
    {
        public List<SymbolData> Data;
        
        private const decimal TargetPercent = 0.1m;

        public override void Initialize()
        {
            SetStartDate(2011, 1, 1);
            SetWarmup(1000);
            SetCash(10000);            

            Data = new List<SymbolData> {
                new SymbolData(this, AddEquity("TQQQ", Resolution.Daily).Symbol),
                new SymbolData(this, AddEquity("SQQQ", Resolution.Daily).Symbol),
                new SymbolData(this, AddEquity("NUGT", Resolution.Daily).Symbol),
                new SymbolData(this, AddEquity("SPY", Resolution.Daily).Symbol)
            };
        }

        public override void OnData(Slice data)
        {

            foreach (var sd in Data)
            {
                if (!data.Bars.ContainsKey(sd.Symbol))
                {
                    continue;
                }
                if (!Portfolio.Invested && sd.IsCrossed && data.Bars[sd.Symbol].Price > sd.EMA200)
                {
                    SetHoldings(sd.Symbol, 0.01);
                }
                else if (Portfolio.Invested && !sd.IsCrossed)
                {
                    Liquidate(sd.Symbol);
                }
            }
        }

        public class SymbolData
        {
            public Symbol Symbol;
            public readonly ExponentialMovingAverage EMA200;
            public readonly MovingAverageConvergenceDivergence MACD;
            public bool IsCrossed => SignalDeltaPercent > 0;

            public SymbolData(QCAlgorithm algorithm, Symbol symbol) {
                Symbol = symbol;
                MACD = algorithm.MACD(symbol, 12, 26, 9, MovingAverageType.Exponential, Resolution.Daily);
                EMA200 = algorithm.EMA(symbol, 200, Resolution.Daily);
            }
            public decimal SignalDeltaPercent
            {
                get
                {
                    if (MACD.Fast == 0) return 0;
                    return (MACD - MACD.Signal) / MACD.Fast;
                }
            }
        }

     
    }
}