Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
NaN
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
NaN
Beta
NaN
Annual Standard Deviation
NaN
Annual Variance
NaN
Information Ratio
NaN
Tracking Error
NaN
Treynor Ratio
NaN
Total Fees
$0.00
namespace QuantConnect
{

	
	public class RollingWindowAlgorithm // contains the EMAs for usage
	{
		public RollingWindowAlgorithm(){}


		
		public RollingWindow<TradeBar> History = new RollingWindow<TradeBar>(5);
		
		//MA's
		public ExponentialMovingAverage _8EMA;
		public ExponentialMovingAverage _20EMA;
	}
}
namespace QuantConnect
{
    public class EMACrossLong : QCAlgorithm
    {
        RollingWindowAlgorithm _rwa = new RollingWindowAlgorithm(); // created instance of RollingWindow
        string symbol = "MSFT";
		
		//algorithm PnL settings
        decimal targetProfit = 0.01m; // 1% target
        decimal maximumLoss = 0.005m; // 0.5% stop
        
		// Initialize function ----------------------------------------------------------------------------
        public override void Initialize() // backtest kickstart
        {
            SetStartDate(2012, 3, 12);         
            SetEndDate(2015, 7, 28); 
            SetCash(25000);
            AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);
            var fifteenConsolidator = new TradeBarConsolidator(TimeSpan.FromMinutes(15));
            fifteenConsolidator.DataConsolidated += OnDataFifteen;
            SubscriptionManager.AddConsolidator(symbol, fifteenConsolidator);
            
			_rwa._8EMA = EMA(symbol, 8);
			_rwa._20EMA = EMA(symbol, 20);
        }

        public bool MinimumProfitAchieved
        {
            get {return (Portfolio.TotalUnrealizedProfit / Portfolio.Cash) >= targetProfit;}
        }
        
        public bool MaximumLossAchieved
        {
            get {return (Portfolio.TotalUnrealizedProfit / Portfolio.Cash) <= -maximumLoss;}
        }
        
        // 15m timeframe handler -----------------------------------------------------------------------------
        
        private void OnDataFifteen(object sender, TradeBar consolidated)
        {
        	decimal profit = Portfolio.TotalUnrealizedProfit;
            decimal price = consolidated.Close;
            decimal high = consolidated.High;
            int holdings = Portfolio[symbol].Quantity;
            decimal avg = (consolidated.Open+consolidated.Close)/2;
            decimal percentage = 0;
           
           	//Algorithm Entry Section:==========================================
          	//Entry Scenario Criteria ==========================================
          	
            
            // CM Check - Scenario 1: 8EMA Crossover 20EMA
            
            while (holdings < 1)
	        {
	            	if (_rwa._8EMA >= _rwa._20EMA)
	            {
	            	if (_rwa.History[1].Close > _rwa.History[2].Close)
	            	{
	            		if (avg > _rwa.History[1].Close)
	            		{
	            			percentage = 1.5m;
	            			SetHoldings(symbol, percentage);
	            		}
	            	}
	            }
	            
            }
            
            //Algorithm Exit Section:===========================================
            
            if (MinimumProfitAchieved)
            {
                Order(symbol, -holdings);
            }
            
            if (MaximumLossAchieved)
            {
                Order(symbol, -holdings);
            }
        }
    }
}