I wonder if someone could help me see the mistake in this code. The code is simply trying to get the Donchian channel values from minute data but for some reason the values come through as zero every minute update.
public class TestAlgorithm : QCAlgorithm {
private const string RootWTI = Futures.Energies.CrudeOilWTI;
public Symbol WTI = QuantConnect.Symbol.Create ( RootWTI, SecurityType.Future, Market.USA );
private HashSet<Symbol> _futureContracts = new HashSet<Symbol>();
private DonchianChannel m_donchian;
public override void Initialize() {
SetStartDate ( 2013, 10, 8 );
SetEndDate ( 2013, 10, 11 );
SetCash ( 1000000 );
SetBenchmark ( x => 0 );
var futureWTI = AddFuture ( RootWTI, Resolution.Minute );
futureWTI.SetFilter ( TimeSpan.Zero, TimeSpan.FromDays ( 30 ) );
var length = 24;
m_donchian = DCH ( WTI, length );
SetWarmup ( length );
}
public override void OnData ( Slice slice ) {
Log ( "OnData called" );
if ( IsWarmingUp ) {
Log ( "Still warming up..." );
return;
}
Log ( String.Format ( "Upper band = {0}", m_donchian.UpperBand.Current.Value ) );
Log ( String.Format ( "Lower band = {0}", m_donchian.LowerBand.Current.Value ) );
}
}
ScalpTrader
Ok so after some trial and error i managed to work this out. Posting my progress here so others can benefit if trying to do the same kinda stuff
using System; using QuantConnect.Data; using QuantConnect.Data.Market; using QuantConnect.Securities; using System.Collections.Generic; using QuantConnect.Data.Consolidators; using QuantConnect.Indicators; namespace QuantConnect.Algorithm.CSharp { public class TestAlgorithm : QCAlgorithm { private const string RootWTI = Futures.Energies.CrudeOilWTI; public Symbol WTI = QuantConnect.Symbol.Create ( RootWTI, SecurityType.Future, Market.USA ); private HashSet<Symbol> _futureContracts = new HashSet<Symbol>(); private Dictionary<Symbol, DonchianChannel> m_donchianChannels = new Dictionary<Symbol, DonchianChannel>(); private int m_period = 24; public override void Initialize() { SetStartDate ( 2018, 1, 1 ); SetWarmUp ( m_period ); var futureWTI_Minute = AddFuture ( RootWTI, Resolution.Minute ); futureWTI_Minute.SetFilter ( TimeSpan.Zero, TimeSpan.FromDays ( 30 ) ); } public override void OnData ( Slice slice ) { foreach ( var chain in slice.FutureChains ) { foreach ( var contract in chain.Value ) { Log ( String.Format ( "{0},Bid={1} Ask={2} Last={3} Volume={4}", contract.Symbol.Value, contract.BidPrice, contract.AskPrice, contract.LastPrice, contract.Volume ) ); if ( !_futureContracts.Contains ( contract.Symbol ) ) { _futureContracts.Add ( contract.Symbol ); var consolidator = new QuoteBarConsolidator ( TimeSpan.FromMinutes ( 5 ) ); consolidator.DataConsolidated += OnDataConsolidated; SubscriptionManager.AddConsolidator ( contract.Symbol, consolidator ); var donchian = new DonchianChannel ( "Donchian_" + contract.Symbol.Value, m_period ); m_donchianChannels.Add ( contract.Symbol, donchian ); RegisterIndicator ( contract.Symbol, donchian, consolidator ); Log ( "Added new consolidator for " + contract.Symbol.Value ); } } } } public void OnDataConsolidated ( object sender, QuoteBar quoteBar ) { Log ( String.Format ( "OnDataConsolidated called" ) ); Log ( String.Format ( "{0}, Open={1} High={2} Low={3} Close={4}", quoteBar.Symbol.Value, quoteBar.Open, quoteBar.High, quoteBar.Low, quoteBar.Close ) ); var indicatorName = m_donchianChannels[quoteBar.Symbol].Name; var upper = m_donchianChannels[quoteBar.Symbol].UpperBand.Current.Value; var lower = m_donchianChannels[quoteBar.Symbol].LowerBand.Current.Value; Log ( String.Format ( "{0} Upper = {1}, lower = {2}", indicatorName, upper, lower ) ); } } } // namespace
ScalpTrader
It's working now as in i get the Donchian channel values every five minutes in the consilator callback. I still seem to have an issue though as the Donchian values i see for the upper and lower channels don't seem to match those from another charting package i use. So something still might be wrong.
ScalpTrader
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!