Hello,

Can someone share an example of an EMA_short crossover EMA_long for 5 minute candles using only futures (Futures.Indices.SP500EMini) contract data with dictionary for storing indicator/contract symbol data and ordering on an uptrend and downtrend basis.

I am stuck on making IsUpTrend and IsDownTrend and IsReady and placing orders based on that.

Shown below is so far what was put together.

Appreciate if someone out there can please help on this.

Kind Regards


using System;
using System.Linq;
using QuantConnect.Data;
using QuantConnect.Orders;
using QuantConnect.Securities;
using QuantConnect.Indicators;
using QuantConnect.Data.Consolidators;
using QuantConnect.Data.Market;

namespace QuantConnect.Algorithm.CSharp
{
    public class FuturesMomentumAlgorithm : QCAlgorithm
    {

        private const string RootSP500 = Futures.Indices.SP500EMini;
        private readonly HashSet<Symbol> _futureContracts = new HashSet<Symbol>();

        public Symbol _symbol = QuantConnect.Symbol.Create(RootSP500, SecurityType.Future, Market.USA);

        private Dictionary<FuturesContract, ExponentialMovingAverage> _fast;
        private Dictionary<FuturesContract, ExponentialMovingAverage> _slow;

        int _quantity = 1;
        private const decimal _tolerance = 0.001m;
    
        public override void Initialize()
        {
            SetStartDate(year: 2019, month: 1, day: 1);
            SetEndDate(year: 2019, month: 1, day: 30);
            SetCash(startingCash: 25000);
            var futureSP500 = AddFuture(RootSP500);
            futureSP500.SetFilter(TimeSpan.FromDays(0), TimeSpan.FromDays(value: 182));
            SetBenchmark(x => 0);

            _fast = _fast = new Dictionary <FuturesContract, ExponentialMovingAverage>();
            _slow = _slow = new Dictionary <FuturesContract, ExponentialMovingAverage>();

        }
        //event handler for data!
        public void OnHalfHour(object sender, TradeBar bar)
        {
            //Debug(Time.ToString("u") + " " + bar);
        }

        public override void OnData(Slice slice)
        {
            foreach (var chain in slice.FutureChains)
            {
                foreach (var contract in chain.Value)
                {
                    if (!_futureContracts.Contains(contract.Symbol))
                    {
                        
                        _futureContracts.Add(contract.Symbol);
                        
                        var consolidator = new QuoteBarConsolidator(TimeSpan.FromMinutes(5));

                        consolidator.DataConsolidated += OnDataConsolidated;
                        SubscriptionManager.AddConsolidator(contract.Symbol, consolidator);

                        _fast[contract] = (new ExponentialMovingAverage(50));
                        RegisterIndicator(contract.Symbol, _fast[contract], consolidator);
                        
                        _slow[contract] = (new ExponentialMovingAverage(200));
                        RegisterIndicator(contract.Symbol, _slow[contract], consolidator);
                        
                    }   
                }       
            }
        }

        public void OnDataConsolidated(object sender, QuoteBar quoteBar)
        {
            //Log("OnDataConsolidated called");
            
            var _fastSMA = _fast.Where(x => x.Key.Symbol == quoteBar.Symbol).ToList();
            var _slowSMA = _slow.Where(x => x.Key.Symbol == quoteBar.Symbol).ToList();


            public bool IsReady { get { return _fastSMA.IsReady && _slowSMA.IsReady; } }
            public bool IsUpTrend { get { return IsReady && _fastSMA > _slowSMA * (1 + _tolerance); } }
            public bool IsDownTrend { get { return IsReady && _fastSMA < _slowSMA * (1 + _tolerance); } }
              
            /*
            if (!_futureContracts.Contains(contract.Symbol) && IsUpTrend)
            {
                 MarkeOrder(contrac.Symbol, 1);   
            }   
            if (!_futureContracts.Contains(contract.Symbol) && IsDownTrend)
            {
                 LimitOrder(contract.Symbol, 1);   
            }    
            */   

        }
        
        public override void OnEndOfDay()
        {
            Plot("Indicator Signal", "EOD", IsDownTrend ? -1 : IsUpTrend ? 1 : 0);
        }

        public override void OnOrderEvent(OrderEvent orderEvent)
        {
            Log(orderEvent.ToString());
        }
    }
}