Overall Statistics
Total Trades
7
Average Win
52.1%
Average Loss
-18.8%
Compounding Annual Return
9.913%
Drawdown
34.500%
Expectancy
2.233
Net Profit
631.289%
Sharpe Ratio
0.715
Loss Rate
14%
Win Rate
86%
Profit-Loss Ratio
2.77
Alpha
0.106
Beta
-0.012
Annual Standard Deviation
0.148
Annual Variance
0.022
Information Ratio
0.166
Tracking Error
0.238
Treynor Ratio
-8.759
using QuantConnect.Data.Consolidators;
using QuantConnect.Data.Custom;
using QuantConnect.Indicators;

namespace QuantConnect.Algorithm.Examples
{
    public class SimpleCustomDataIndicators : QCAlgorithm
    {
        private string SPY_QuandlCode = "YAHOO/INDEX_SPY";

        private MovingAverageConvergenceDivergence macd;

        public override void Initialize()
        {
            // set up our analysis span
            SetStartDate(1994, 01, 01);
            SetEndDate(DateTime.Today.AddDays(-1));

            // request SPY data - the data feed does daily, so pick that too
            AddData<Quandl>(SPY_QuandlCode, Resolution.Daily);
            
            macd = new MovingAverageConvergenceDivergence("MACD", 72, 189, 9, MovingAverageType.Exponential);

            // if we want to just pipe data directly from the engine into our indicators,
            // we can use the identity consolidator. this consolidator will send each piece
            // of data 

            var quandlIdentityConsolidator = new IdentityDataConsolidator<Quandl>();

            // then we can register our macd using the identity consolidator

            RegisterIndicator(SPY_QuandlCode, macd, quandlIdentityConsolidator, x => x.Value);
        }

        public void OnData(Quandl data)
        {
            // this is daily data

            // we're doing about more than 4k days, so don't plot every day
            if (data.Time.DayOfYear%2 == 0)
            {
                // plot our macd
                Plot("SPY_MACD", macd, macd.Signal);
                
                // plot the fast/slow parts of the macd along with closing prices
                Plot(SPY_QuandlCode, "Close", data.Value);
                Plot(SPY_QuandlCode, macd.Slow, macd.Fast);
            }

            if (!macd.IsReady) return;
            
            // moving average cross with a 1.5% debouncing tolerance
            
            var quantity = Portfolio[SPY_QuandlCode].Quantity;
            if (quantity <= 0 && macd.Fast > macd.Slow * 1.015m)
            {
                SetHoldings(SPY_QuandlCode, .95);
            }
            else if (quantity >= 0 && macd.Fast * 1.015m < macd.Slow)
            {
                SetHoldings(SPY_QuandlCode, -.95);
            }
        }
    }
}