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
Probabilistic 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
-26.517
Tracking Error
0.065
Treynor Ratio
0
Total Fees
$0.00
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 Symbol lastSymbol = null;
        private Dictionary<Symbol, TradeBarConsolidator> consolidatorBySymbol = new Dictionary<Symbol, TradeBarConsolidator>();
        
        private bool dataUpdated = false;

        /// 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, 25); SetEndDate(DateTime.Now.Date.AddDays(0));
            SetStartDate(2020, 8, 20); SetEndDate(2020, 8, 31);
            //SetStartDate(2020, 8, 25); SetEndDate(2020, 8, 31);

            //Cash allocation
            SetCash(10000);

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

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

            _sma = SMA(_quandlCode, 1);
        }
        
        public void OnDailyData()
        {
        	Debug("------------------------------------");
        	if(!dataUpdated) return;

        	foreach(var kvp in consolidatorBySymbol)
        	{
        		Debug(kvp.Key.Value+": "+kvp.Value.WorkingBar.ToString());
        	}
        	
        	for(int i=0; i < consolidatorBySymbol.Count()-1; i++)
        	{
        		var tempKey = consolidatorBySymbol.ElementAt(i).Key;
        		consolidatorBySymbol[tempKey] = new TradeBarConsolidator(24*60);
        	}
        	
        	dataUpdated = false;
        }
        
        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;
			    	if(addedSymbol == Symbol("SPY")) continue;
			    	
			    	var consolidator = new TradeBarConsolidator(24*60);  
                    //consolidator.DataConsolidated += OnDataConsolidated;
                    //SubscriptionManager.AddConsolidator(addedSymbol, consolidator);
                    
                    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;
			    	//SubscriptionManager.RemoveConsolidator(removedSymbol, consolidatorBySymbol[removedSymbol]);
			    	
			    	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.Keys.Contains(contract.Symbol))
            		{
            			consolidatorBySymbol[contract.Symbol].Update(data.Bars[contract.Symbol]);
            			
            			dataUpdated = true;
            		}
            	}
            }
        }

        /// Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol
        public void OnData(Quandl data)
        {

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