Overall Statistics
namespace QuantConnect.Algorithm.CSharp
{

    public class FutureChainProviderAlgorithm : QCAlgorithm
    {
        private Symbol _futureContract = string.Empty;
        private readonly HashSet<Symbol> _contractsAdded = new HashSet<Symbol>();
        public Symbol SP500 = QuantConnect.Symbol.Create("ES", SecurityType.Future, Market.USA);

        public override void Initialize()
        {
            SetStartDate(2015, 12, 24);
            SetEndDate(2015, 12, 24);
            SetCash(100000);
            AddEquity("SPY", Resolution.Minute);
            
        }

        public override void OnData(Slice data)
        {

            if (!(Securities.ContainsKey(_futureContract) && Portfolio[_futureContract].Invested))
            {
                var futureChains = FutureChainProvider.GetFutureContractList(SP500, Time);
                Debug(futureChains.ToString());

                var contracts = (from symbol in futureChains
                                where ((symbol.ID.Date - data.Time).TotalDays < 100 && (symbol.ID.Date - data.Time).TotalDays > 10)
                                select symbol);

                if (contracts.Count() != 0)
                {
                    _futureContract = contracts.OrderBy(x => x.ID.Date)
                                          .FirstOrDefault();
                    if (_contractsAdded.Add(_futureContract))
                    {
                        // use AddFutureContract() to subscribe the data for specified contract
                        AddFutureContract(_futureContract, Resolution.Minute);
                    }
                }
                else _futureContract = string.Empty;
            }
            if (Securities.ContainsKey(_futureContract) && !Portfolio[_futureContract].Invested)
            {
                MarketOrder(_futureContract, 1);
            }
        }
    }
}