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
using System;
using System.Linq;
using QuantConnect.Indicators;
using QuantConnect.Models;

namespace QuantConnect
{
	using QuantConnect.Securities;
	using QuantConnect.Models;
	
	public partial class MovingAverageDemo : QCAlgorithm, IAlgorithm
	{
		string symbol = "SPY";
		
		ExponentialMovingAverage emaFast = new ExponentialMovingAverage(10);
		ExponentialMovingAverage emaSlow = new ExponentialMovingAverage(50);
		
		
		
		public override void Initialize()
		{
			
			SetStartDate(2014, 01, 01);
			SetEndDate(2014, 04, 01);
			SetCash(50000);
			AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);
			
		}
		
		public void OnTradeBar(Dictionary<string, TradeBar> data)
		{
			decimal price = data[symbol].Close;
			emaFast.AddSample(price);
			emaSlow.AddSample(price);
			
			if (emaFast.EMA > emaSlow.EMA)
			{
				Order(symbol, 100);
			}
			else if (emaFast.EMA < emaSlow.EMA)
			{
				Order(symbol, -100);
			}
		}
		
	}
}
using System;
using System.Collections;
using System.Collections.Generic;

namespace QuantConnect {

    using QuantConnect.Securities;
    using QuantConnect.Models;

    public class ExponentialMovingAverage
    {
      private decimal _period;
      private decimal _ema;
      
      private int _samples;
      
      public decimal EMA
      {
      	get{return _ema;}
      }
      
      public ExponentialMovingAverage(decimal period)
      {
      	
      	this._period = period;
      	this._samples = 0;
      	
      }
      
      public decimal AddSample(decimal price)
      {
      	
      	if (_samples == 0)
      	{
      		_ema = price;
      	}
      	else
      	{
      		_ema = (1/_period)*price+((_period-1)/_period)*_ema;
      	}
      	
      	
      	_samples++;
      	return _ema;
      	
      }
      
      
    }
    

}