Overall Statistics
Total Trades
22
Average Win
0.72%
Average Loss
-0.72%
Compounding Annual Return
-1.571%
Drawdown
8.300%
Expectancy
-0.426
Net Profit
-4.115%
Sharpe Ratio
-0.255
Loss Rate
71%
Win Rate
29%
Profit-Loss Ratio
1.01
Alpha
-0.03
Beta
0.167
Annual Standard Deviation
0.055
Annual Variance
0.003
Information Ratio
-0.853
Tracking Error
0.125
Treynor Ratio
-0.085
Total Fees
$22.00
-no value-
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;
            int 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); 
                }
            }
        }
    }
}