Overall Statistics
using System;
using System.Globalization;
using QuantConnect.Data;
using QuantConnect.Data.Market;

namespace QuantConnect.Algorithm.CSharp.My_Algorithms
{
    public class ES5minData : TradeBar
    {
        public decimal UpperShadow { get; set; }
        public decimal LowerShadow { get; set; }
        public decimal HighLow { get; set; }
        public decimal RealBody { get; set; }
        public decimal UpperShadowPercent { get; set; }
        public decimal LowerShadowPercent { get; set; }

        public override DateTime EndTime
        {
            get { return (Time + Period); }
            set { Time = (value - Period); }
        }

        public new TimeSpan Period
        {
            get { return TimeSpan.FromMinutes(5); }
        }

        public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLiveMode)
        {
            return new SubscriptionDataSource("https://www.dropbox.com/s/2jiaz2b9nv04doj/ES%202015-01-02%20-%202015-12-31%20-%20EST.csv?dl=1",
               /* "https://www.dropbox.com/s/2til1kzb6s4snpw/ES%202016-01-04%20-%202016-12-30%20-%20EST.csv?dl=1",*/ SubscriptionTransportMedium.RemoteFile);
        }

        public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode)
        {
            ES5minData cmBar = new ES5minData();

            try
            {
                var data = line.Split(',');
                //Required.
                cmBar.Symbol = "ES";

                if (data[1].Length == 5)
                {
                    var theDate = DateTime.ParseExact(data[0], "yyyyMMdd", CultureInfo.InvariantCulture);
                    var theTime = TimeSpan.ParseExact(data[1].Insert(0, "0"), "hhmmss", CultureInfo.InvariantCulture);
                    cmBar.Time = theDate + theTime;
                }
                else
                {
                    var theDate = DateTime.ParseExact(data[0], "yyyyMMdd", CultureInfo.InvariantCulture);
                    var theTime = TimeSpan.ParseExact(data[1], "hhmmss", CultureInfo.InvariantCulture);
                    cmBar.Time = theDate + theTime;
                }

                cmBar.Open = Convert.ToDecimal(data[2], CultureInfo.InvariantCulture);
                cmBar.High = Convert.ToDecimal(data[3], CultureInfo.InvariantCulture);
                cmBar.Low = Convert.ToDecimal(data[4], CultureInfo.InvariantCulture);
                cmBar.Close = Convert.ToDecimal(data[5], CultureInfo.InvariantCulture);
                cmBar.Volume = Convert.ToInt64(data[6], CultureInfo.InvariantCulture);

                cmBar.Value = cmBar.Close;

                if (cmBar.Close > cmBar.Open)
                {
                    cmBar.UpperShadow = (cmBar.High - cmBar.Close);
                    cmBar.LowerShadow = (cmBar.Open - cmBar.Low);
                    cmBar.RealBody = (cmBar.Close - cmBar.Open);
                }
                else
                {
                    cmBar.UpperShadow = (cmBar.High - cmBar.Open);
                    cmBar.LowerShadow = (cmBar.Close - cmBar.Low);
                    cmBar.RealBody = (cmBar.Open - cmBar.Close);
                }
                cmBar.HighLow = (cmBar.High - cmBar.Low);

                cmBar.UpperShadowPercent = (cmBar.UpperShadow / cmBar.HighLow * 100);
                cmBar.LowerShadowPercent = (cmBar.LowerShadow / cmBar.HighLow * 100);
            }
            catch
            {

            }
            return cmBar;
        }
    }
}
using System;
using QuantConnect.Indicators;
using QuantConnect.Orders;

namespace QuantConnect.Algorithm.CSharp.My_Algorithms
{ 
    public class ScRbThruAndMostBelowT_afterNmsRbAboveT_TaboveSMA : QCAlgorithm
    {
        private string _symbol = "ES";
        ExponentialMovingAverage _ema;
        SimpleMovingAverage _sma;

        public RollingWindow<ES5minData> BarHistory = new RollingWindow<ES5minData>(3);
        public RollingWindow<decimal> EmaHistory = new RollingWindow<decimal>(3);
        public RollingWindow<decimal> SmaHistory = new RollingWindow<decimal>(3);

        private DateTime liquidationTime;

        public override void Initialize()
        {
            SetStartDate(2015, 01, 04);  //Set Start Date 
            SetEndDate(2015, 12, 31);    //Set End Date
            SetCash(100000);             //Set Strategy Cash
             
            AddData<ES5minData>(_symbol);

            _ema = EMA(_symbol, 8);
            _sma = SMA(_symbol, 40);
        }
        
        public void OnData(ES5minData data)   
        {
            if (data.Time.TimeOfDay < new TimeSpan(9, 30, 00) ||
                data.Time.TimeOfDay > new TimeSpan(12, 00, 00))
                return;

            BarHistory.Add(data);
            EmaHistory.Add(_ema);
            SmaHistory.Add(_sma);                

            if (!BarHistory.IsReady) return;           

            // priorBar[0] = present Bar; priorBar[1] = previous Bar;  
            
            decimal lowTail = BarHistory[0].LowerShadowPercent.RoundToSignificantDigits(4);

            // if 1. [0]Candle's RealBody (>= 0.75) is Down on higher vol + thru EMA-8 & most(2/3) below it , Low Tail is b/n 15-35%, + after 
            //    2. prior Candle [1] is down on lower vol, RealBody >= 0.50 (not Doji) + [1]RB <= [0]RB + above EMA; and 
            //    3. EMA-8 (or T-line) - above SMA-40 (exclude overnight data - 16:10pm to 9:30pm) 
            //    4. expect a Move Up 

            if (BarHistory[0].RealBody > (decimal)0.75 && BarHistory[0].Close < BarHistory[0].Open && BarHistory[0].Volume > BarHistory[1].Volume &&
                BarHistory[0].LowerShadowPercent >= 15 && BarHistory[0].LowerShadowPercent <= 35 && BarHistory[0].RealBody >= BarHistory[1].RealBody &&
                BarHistory[0].Open > EmaHistory[0] && ((BarHistory[0].Open - BarHistory[0].Close)*2/3 + BarHistory[0].Close) < EmaHistory[0] && 
                BarHistory[1].Close < BarHistory[1].Open && BarHistory[1].Volume < BarHistory[2].Volume && BarHistory[1].RealBody > (decimal)0.50 && 
                BarHistory[1].Close > EmaHistory[1] && EmaHistory[0] > SmaHistory[0])
            {                
                Debug(BarHistory[0].Time + " -found ScRbThruAndMostBelowT_afterNmsRbAboveT_TaboveSMA ");                   
                if (!Portfolio.HoldStock && Time >= BarHistory[0].EndTime) // if not invested + at or after Bar's Close Time
                {
                    SetHoldings(_symbol, 1); // Go Long 100% of portfolio cash value                      
                }                    
            }           
            
            if (Portfolio.HoldStock && Time >= liquidationTime) // if invested + now at or after liqudation Time
            {
                SetHoldings(_symbol, 0, true); // Liquidate 100% of position              
            }
        }

        public override void OnOrderEvent(OrderEvent orderEvent) 
        {
            if (orderEvent.Status.IsFill()) // if order filled, then ...
            {
                liquidationTime = Time.AddMinutes(5); // ... liquidation time = 5 min after our candle's close.
            }
        }
    }
}