Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.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 Keltner_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;
        KeltnerChannels kch;
        
        //Initialize the data and resolution you require for your strategy:
        public override void Initialize() 
        {          
            SetStartDate(2016, 06, 01);
            SetEndDate(DateTime.Now);  
            SetCash(25000);
            AddSecurity(SecurityType.Equity, symbol, Resolution.Second);
            kch =KCH(symbol, 20, 1.5m, MovingAverageType.Simple);
            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) 
                {
            		//Log("Keltner "+kch.UpperBand.Current.Value);
                    //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); 
                }
            }
        }
    }
}