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
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
/*
 * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
 * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
*/

using System;
using QuantConnect.Data;
using QuantConnect.Data.Market;
using QuantConnect.Securities;
using System.Collections.Generic;
using QuantConnect.Data.Consolidators;

namespace QuantConnect.Algorithm.CSharp
{
    /// <summary>
    /// A demonstration of Micro Futures
    /// </summary>
    /// <meta name="tag" content="futures" />
    public class MicroFutures : QCAlgorithm
    {
    	// A list of symbols stored in hashset
        private HashSet<Symbol> _futureContracts = new HashSet<Symbol>();

        public override void Initialize()
        {
            SetStartDate(2019, 9, 19);
            SetEndDate(2019, 9, 23);
            SetCash(1000000);

			// this works for E-Mini S&P 500
			var future_ES = AddFuture("ES", Resolution.Minute, Market.USA, true, 0m);
            future_ES.SetFilter(TimeSpan.Zero, TimeSpan.FromDays(182));

			// this does not work for Micro E-Mini S&P 500
			var future_MES = AddFuture("MES", Resolution.Minute, Market.USA, true, 0m);
            future_MES.SetFilter(TimeSpan.Zero, TimeSpan.FromDays(182));
        }

        public override void OnData(Slice slice)
        {
			foreach(var chain in slice.FutureChains) 
			{
				// find the front contract expiring no earlier than in 90 days
				var contract = (
					from futuresContract in chain.Value.OrderBy(x => x.Expiry)
					where futuresContract.Expiry > Time.Date.AddDays(90)
					select futuresContract
					).FirstOrDefault();
			
				// proceed if there is a new front end contract
				if (contract != null)
				{
					// if necessary, add to hash set and add consolidator
                    if (!_futureContracts.Contains(contract.Symbol))
                    {
                        _futureContracts.Add(contract.Symbol);

                        var consolidator = new TradeBarConsolidator(TimeSpan.FromMinutes(720));
                        consolidator.DataConsolidated += OnDataConsolidated;
                        SubscriptionManager.AddConsolidator(contract.Symbol, consolidator);

                        Log("Added new consolidator for " + contract.Symbol + " :: " + contract.Symbol.Value);
                    }
				}
			}
        }

        public void OnDataConsolidated(object sender, TradeBar quoteBar)
        {
            Log("OnDataConsolidated called for " + quoteBar.Symbol);
        }
	}
}