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());
}
}
}
Spacetime
I very well appreciate your time and efforts.
I understood how it works now.
Thank You!
Spacetime
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!