Overall Statistics
Total Trades
127
Average Win
3.65%
Average Loss
-2.38%
Compounding Annual Return
2.284%
Drawdown
31.900%
Expectancy
0.208
Net Profit
28.227%
Sharpe Ratio
0.225
Loss Rate
52%
Win Rate
48%
Profit-Loss Ratio
1.54
Alpha
-0.006
Beta
0.394
Annual Standard Deviation
0.108
Annual Variance
0.012
Information Ratio
-0.393
Tracking Error
0.135
Treynor Ratio
0.062
Total Fees
$630.03
namespace QuantConnect
{
    /// <summary>
    /// MACD Example Algorithm
    /// </summary>
    public class MACDTrendAlgorithm : QCAlgorithm
    {
        private DateTime previous;
        private MovingAverageConvergenceDivergence macd;
        private string symbol = "SPY";

        /// <summary>
        /// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
        /// </summary>
        public override void Initialize()
        {
            SetStartDate(2004, 01, 01);
            SetEndDate(2015, 01, 01);

            AddSecurity(SecurityType.Equity, symbol, Resolution.Daily);

            // define our daily macd(12,26) with a 9 day signal
            macd = MACD(symbol, 9, 26, 9, MovingAverageType.Exponential, Resolution.Daily);
        }

        /// <summary>
        /// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
        /// </summary>
        /// <param name="data">TradeBars IDictionary object with your stock data</param>
        public void OnData(TradeBars data)
        {
            // only once per day
            if (previous.Date == Time.Date) return;

            if (!macd.IsReady) return;

            var holding = Portfolio[symbol];

            decimal signalDeltaPercent = (macd - macd.Signal)/macd.Fast;
            var tolerance = 0.0025m;

            // if our macd is greater than our signal, then let's go long
            if (holding.Quantity <= 0 && signalDeltaPercent > tolerance) // 0.01%
            {
                // longterm says buy as well
                SetHoldings(symbol, 1.0);
            }
            // of our macd is less than our signal, then let's go short
            else if (holding.Quantity >= 0 && signalDeltaPercent < -tolerance)
            {
                Liquidate(symbol);
            }

            // plot both lines
            Plot("MACD", macd, macd.Signal);
            Plot(symbol, "Open", data[symbol].Open);
            Plot(symbol, macd.Fast, macd.Slow);

            previous = Time;
        }
    }
}