Overall Statistics
Total Trades
26
Average Win
0.84%
Average Loss
-0.66%
Compounding Annual Return
1.263%
Drawdown
7.700%
Expectancy
-0.273
Net Profit
4.577%
Sharpe Ratio
0.26
Loss Rate
68%
Win Rate
32%
Profit-Loss Ratio
1.27
Alpha
-0.008
Beta
0.201
Annual Standard Deviation
0.053
Annual Variance
0.003
Information Ratio
-0.861
Tracking Error
0.111
Treynor Ratio
0.069
Total Fees
$26.00
namespace QuantConnect 
{
    /*
    *   QuantConnect University: 50-10 EMA - Exponential Moving Average Cross
    *   
    *   The classic exponential moving average cross implementation using a custom class.
    *   The algorithm uses allows for a safety margin so you can reduce the "bounciness" 
    *   of the trading to confirm the crossing.
    */
    public class QCUMovingAverageCross : QCAlgorithm 
    { 
        //Define required variables:
        int quantity = 0;
        decimal price = 0;
        decimal tolerance = 0m; //0.1% safety margin in prices to avoid bouncing.
        string symbol = "SPY";
        DateTime sampledToday = DateTime.Now;
        
        //Set up the EMA Class:
        ExponentialMovingAverage emaShort;
        ExponentialMovingAverage emaLong;
        
        //Initialize the data and resolution you require for your strategy:
        public override void Initialize() 
        {          
            SetStartDate(2014, 01, 01);
            SetEndDate(DateTime.Now);  
            SetCash(25000);
            AddSecurity(SecurityType.Equity, symbol, Resolution.Second);
            
            emaShort = EMA(symbol, 10, Resolution.Daily);
            emaLong = EMA(symbol, 50, Resolution.Daily);
        }
        
        //Handle TradeBar Events: a TradeBar occurs on every time-interval
        public void OnData(TradeBars data) {
            
            //One data point per day:
            if (sampledToday.Date == data[symbol].Time.Date) return;
            
            //Only take one data point per day (opening price)
            price = Securities[symbol].Close;
            sampledToday = data[symbol].Time;
            
            //Wait until EMA's are ready:
            if (!emaShort.IsReady || !emaLong.IsReady) return;
            
            //Get fresh cash balance: Set purchase quantity to equivalent 10% of portfolio.
            decimal cash = Portfolio.Cash;
            var holdings = Portfolio[symbol].Quantity;
            quantity = Convert.ToInt32((cash * 0.5m) / price);
            
            if (holdings > 0 || holdings == 0) {
                //If we're long, or flat: check if EMA crossed negative: and crossed outside our safety margin:
                if ((emaShort * (1+tolerance)) < emaLong) 
                {
                    //Now go short: Short-EMA signals a negative turn: reverse holdings
                    Order(symbol, -(holdings + quantity));
                    Log(Time.ToShortDateString() + " > Go Short > Holdings: " + holdings.ToString() + " Quantity:" + quantity.ToString() + " Samples: " + emaShort.Samples);
                }
                
            } else if (holdings < 0 || holdings == 0) {
                //If we're short, or flat: check if EMA crossed positive: and crossed outside our safety margin:
                if ((emaShort * (1 - tolerance)) > emaLong) 
                {
                    //Now go long: Short-EMA crossed above long-EMA by sufficient margin
                    Order(symbol, Math.Abs(holdings) + quantity);
                    Log(Time.ToShortDateString() + "> Go Long >  Holdings: " + holdings.ToString() + " Quantity:" + quantity.ToString() + " Samples: " + emaShort.Samples); 
                }
            }
        }
    }
}