Overall Statistics
Total Trades
239
Average Win
1.16%
Average Loss
-0.7%
Compounding Annual Return
-69.666%
Drawdown
31.300%
Expectancy
-0.168
Net Profit
-26.034%
Sharpe Ratio
-2.497
Loss Rate
69%
Win Rate
31%
Profit-Loss Ratio
1.65
Alpha
-0.857
Beta
0.806
Annual Standard Deviation
0.424
Annual Variance
0.18
Information Ratio
-2.463
Tracking Error
0.328
Treynor Ratio
-1.314
using System;
using QuantConnect.Data.Market;
using QuantConnect.Indicators;
using QuantConnect.Algorithm;
using System.Collections.Generic;

namespace QuantConnect {

	/// <summary>
	/// Demonstration of QuantConnect Intraday Charting on an Overlay Chart. Based on "QCU Custom Charting Example".
	/// </summary>
	public class IntradayChartExample : QCAlgorithm
	{
		// Intraday Chart
		bool 			idTradedToday = false;
		string 			idTriggerChart = "firstTrade"; // Show the first day the algo traded.
		DateTime? 		idChartTradeDate = null;
		Resolution 		idChartResolution = Resolution.Minute;
		string 			idChartSymbol = "IBM"; // "EURUSD"
		SecurityType 	idChartSymbolType = SecurityType.Equity; // SecurityType.Forex

		// Indicators
		ExponentialMovingAverage emaShort;
		ExponentialMovingAverage emaLong;

        // Trading
        decimal tolerance = 0.00015m; //0.02% safety margin in prices to avoid bouncing.

		public override void Initialize()
		{
			SetStartDate(2011, 7, 20);
			SetEndDate(2011, 10, 20);
			AddSecurity(idChartSymbolType, idChartSymbol, resolution: idChartResolution);

			// Intraday Charting
			Chart intradayChart = new Chart(idChartSymbol + " 1d", ChartType.Overlay);
			intradayChart.AddSeries(new Series(idChartSymbol, SeriesType.Line)); // FAIL: Only 1 SeriesType.Candle is drawn even though we plot every minute.
			intradayChart.AddSeries(new Series("Buy", SeriesType.Scatter));
			intradayChart.AddSeries(new Series("Sell", SeriesType.Scatter));

			// Poor Man's Candlesticks. Disable price/close Line when using.
			// intradayChart.AddSeries(new Series("High", SeriesType.Line));
			// intradayChart.AddSeries(new Series("Low", SeriesType.Line));

			AddChart(intradayChart);

			// Indicators
			emaShort = EMA(idChartSymbol, 15, idChartResolution);
			emaLong = EMA(idChartSymbol, 50, idChartResolution);

		}

		public void OnData(TradeBars data)
		{
			try
			{
				if (Securities[idChartSymbol].Exchange.ExchangeOpen == false) return; // Wait for equities market to open to trade
				if (!emaShort.IsReady || !emaLong.IsReady) return;

                // Trading
				var holding = Portfolio[idChartSymbol];
				if (holding.Quantity <= 0 && Math.Round(emaShort * (1.0m - tolerance),2)  > Math.Round(emaLong,2))
				{
					SetHoldings(idChartSymbol, 1.0);
					idTradedToday = true;
					if (this.idChartingEnabled(data.Time))
					{
						Plot(idChartSymbol + " 1d", "Buy", data[idChartSymbol].Price);
						// Log("***** " + data.Time.ToString("T") + " BUY EMA crossover, price: " + Math.Round(data[idChartSymbol].Price,2) + " short: " + Math.Round(emaShort,3) + " short With Tol: " + Math.Round(emaShort * (1.0m - tolerance),3) + " long: " + Math.Round(emaLong,3) + " tolerance: " + tolerance.ToString() + " qty: " + holding.Quantity);
					}
				}
				else if (holding.Quantity > 0 && emaShort <= emaLong)
				{
					Liquidate(idChartSymbol);
					idTradedToday = true;
					if (this.idChartingEnabled(data.Time))
					{
						Plot(idChartSymbol + " 1d", "Sell", data[idChartSymbol].Price);
						// Log("***** " + data.Time.ToString("T") + " SELL EMA crossover, price: " + Math.Round(data[idChartSymbol].Price,2) + " fast: " + Math.Round(emaShort,2) + " EMA slow: " + Math.Round(emaLong,2) + " qty: " + holding.Quantity);
					}
				}


				// Plot Intraday Price and Indicators
				if (this.idChartingEnabled(data.Time))
				{
					Plot(idChartSymbol + " 1d", idChartSymbol, data[idChartSymbol].Price);
					Plot(idChartSymbol + " 1d", emaShort);
	            	Plot(idChartSymbol + " 1d", emaLong);

					// Poor Man's Candlesticks - Disable price/close Line when using.
					// Plot(idChartSymbol + " 1d", "High", data[idChartSymbol].High);
					// Plot(idChartSymbol + " 1d", "Low", data[idChartSymbol].Low);
				}
			}
			catch (Exception err)
			{
				Error("OnData Err: " + err.Message);
			}
		}

		public override void OnEndOfDay()
		{
			try
			{
				idTradedToday = false;
			}
			catch (Exception err)
			{
				Error("OnEndOfDay Err:" + err.Message);
			}
		}

		private bool idChartingEnabled(DateTime marketDateTime)
		{
			switch (idTriggerChart)
			{
				case "firstTrade":
					if (idTradedToday == true && idChartTradeDate.HasValue == false)
					{
						idChartTradeDate = marketDateTime;
						return true;
					}
					else if (idChartTradeDate.HasValue && marketDateTime.Date <= idChartTradeDate.Value.Date)
					{
						return true;
					}
				break;
			}
			return false;
		}
	}

}