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 3.304 Tracking Error 0.109 Treynor Ratio 0 Total Fees $0.00 Estimated Strategy Capacity $0 Lowest Capacity Asset |
/* * 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 QuantConnect.Interfaces; using QuantConnect.Indicators; using System.Collections.Generic; using QuantConnect.Data.Consolidators; using QuantConnect.Data.Market; using QuantConnect.Securities.Future; using System.Globalization; namespace QuantConnect.Algorithm.CSharp { /// <summary> /// Demonstration of how to rollover futures /// </summary> /// <meta name="tag" content="indicators" /> /// <meta name="tag" content="using data" /> /// <meta name="tag" content="consolidating data" /> public class TradeBarConsolidatorTest : QCAlgorithm { private Future ESSubscribeFuture; private FuturesContract ESFrontMonthContract; private Dictionary<Symbol, TradeBarConsolidator> ESTradeBarConsolidator; /// <summary> /// Initializes the algorithm state. /// </summary> public override void Initialize() { SetStartDate(2021, 3, 15); SetEndDate(2021, 3, 23); SetCash(100000); // Subscribe to futures chain ESSubscribeFuture = AddFuture(Futures.Indices.SP500EMini, Resolution.Minute, Market.CME, false, 0m); // Filter for front month contracts // OnlyApplyFilterAtMarketOpen for performance ESSubscribeFuture.SetFilter(universe => universe.FrontMonth().OnlyApplyFilterAtMarketOpen()); // Define the TradeBar Consolidator ESTradeBarConsolidator = new Dictionary<Symbol, TradeBarConsolidator>(); } public override void OnSecuritiesChanged(SecurityChanges changes) { foreach(var security in changes.AddedSecurities) { ESTradeBarConsolidator.Add(security.Symbol, new TradeBarConsolidator(TimeSpan.FromDays(1))); ESTradeBarConsolidator[security.Symbol].DataConsolidated += (sender, consolidated) => { // When a TradeBar is created, call the event handler OnTradeBarConsolidated(consolidated); }; } foreach(var security in changes.RemovedSecurities) { if (ESTradeBarConsolidator.ContainsKey(security.Symbol)) { ESTradeBarConsolidator.Remove(security.Symbol); } } } /// <summary> /// The framework verifies that this method exists, so we define it. /// </summary> public void OnData(Slice SliceData) { foreach (var chain in SliceData.FutureChains) { // Sort our contracts by Expiry in ascending order var sortedByExpiryContracts = chain.Value.OrderBy(x => x.Expiry).ToList(); // Get first contract from list ESFrontMonthContract = sortedByExpiryContracts[0]; // Get list of TradeBars from the chain TradeBars ESTradeBarList = chain.Value.TradeBars; if (ESTradeBarList.ContainsKey(ESFrontMonthContract.Symbol)) { // Get TradeBar for ES TradeBar ESTradeBar = ESTradeBarList[ESFrontMonthContract.Symbol]; // Update TradeBar Consolidator // When you update with a different symbol, it throws an error // Next line is commented so I can attach the code in the community ESTradeBarConsolidator[ESFrontMonthContract.Symbol].Update(ESTradeBar); } } } public void OnTradeBarConsolidated(TradeBar ESTradeBar) { Debug(ESTradeBar.Symbol); } } }