Overall Statistics
using System;
using System.Collections.Generic;
using System.Linq;
using QuantConnect.Data;
using QuantConnect.Interfaces;
using QuantConnect.Securities;
using QuantConnect.Securities.Future;

namespace QuantConnect.Algorithm.CSharp
{
    public class FuturesTest : QCAlgorithm
    {
/*    	
        private SimpleMovingAverage _sma;
        string _quandlCode = "CFTC/088691_F_L_ALL";
*/

        private const string RootGold = Futures.Metals.Gold;
        private Dictionary<Symbol, TradeBarConsolidator> consolidatorBySymbol = new Dictionary<Symbol, TradeBarConsolidator>();

        /// Initialize the data and resolution you require for your strategy:
        public override void Initialize()
        {
            //Start and End Date range for the backtest:
            SetStartDate(2020, 8, 20); SetEndDate(DateTime.Now.Date.AddDays(-1));
            //SetStartDate(2020, 8, 21); SetEndDate(2020, 8, 31);

            //Cash allocation
            SetCash(10000);

            var futureGold = AddFuture(RootGold, Resolution.Minute);
            futureGold.SetFilter(0, 182);
            
            Schedule.On(DateRules.EveryDay(futureGold.Symbol), TimeRules.At(17, 00, TimeZones.Chicago), () => // 
            {
            	OnExchangeOpenData();
            });

/*
            //Add Generic Quandl Data:
            AddData<QuandlSEC>(_quandlCode, Resolution.Daily);

            _sma = SMA(_quandlCode, 1);
*/
        }
        
        public void OnExchangeOpenData()
        {
        	Debug("---------------------------------------------------------------------");
        	if(consolidatorBySymbol.Values.First().WorkingBar == null) return;		// avoid access before workingbars are filled

        	foreach(var kvp in consolidatorBySymbol)
        	{
        		Debug(kvp.Key.Value+": "+kvp.Value.WorkingBar.ToString());
        	}
        	
        	// reset consolidators by creating new ones
        	for(int i=0; i < consolidatorBySymbol.Count(); i++)
        	{
        		var tempKey = consolidatorBySymbol.ElementAt(i).Key;
        		consolidatorBySymbol[tempKey] = new TradeBarConsolidator(24*60);	// 24h * 60min bars = 1d max
        	}
        }
        
        public override void OnSecuritiesChanged(SecurityChanges changes)
		{
			if(changes.AddedSecurities.Count > 0)
			{
			    for(int i=0; i < changes.AddedSecurities.Count; i++) 
			    {
			    	var addedSymbol = changes.AddedSecurities[i].Symbol;
			    	var consolidator = new TradeBarConsolidator(24*60);				// 24h * 60min bars = 1d max

                    consolidatorBySymbol[addedSymbol] = consolidator;

                    Debug("Added new consolidator for " + addedSymbol.Value);
			    }
			}
			
			if(changes.RemovedSecurities.Count > 0)
			{
			    for(int i=0; i < changes.RemovedSecurities.Count; i++) 
			    {
			    	var removedSymbol = changes.RemovedSecurities[i].Symbol;

			    	consolidatorBySymbol.Remove(removedSymbol);
			    	
			    	Debug("Removed consolidator for: "+changes.RemovedSecurities[i].Symbol.Value);
			    }
			}
		}
		
        public void OnData(Slice data)
        {
            foreach(var chain in data.FutureChains)
            {
            	foreach(var contract in chain.Value)
            	{
            		if(data.Bars.Keys.Contains(contract.Symbol))
            		{
            			consolidatorBySymbol[contract.Symbol].Update(data.Bars[contract.Symbol]);
            		}
            	}
            }
        }

/*
        public void OnData(Quandl data)
        {

            Plot("My Indicators", "MACD Signal", _sma);
        }
*/
    }
/*    
    public class QuandlSEC : Quandl {
        public QuandlSEC() : base(valueColumnName: "open interest") 
        {
        }
    }
*/
}