Hi,
I used the Data sample for retrieving price. It keeps giving me an error. Is the sample outdated?
QUANTCONNECT COMMUNITY
Hi,
I used the Data sample for retrieving price. It keeps giving me an error. Is the sample outdated?
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.
H Huxley Li
public override void OnData(Slice data) {
//via a tradebar dictionary (symbol - bar)
data.Bars["JDST"].Close;
//Or via a ticks list:
data.Ticks["JDST"][0].Close;
}
This is what was copied and modified, the project couldn't compile, so I can't get a backtest.
Alexandre Catarino
Could you please share the whole algorithm in question?
Also it was not clear whether you are running it locally or at the cloud.
H Huxley Li
namespace QuantConnect { /* * The underlying QCAlgorithm class is full of helper methods which enable you to use QuantConnect. * We have explained some of these here, but the full algorithm can be found at: * https://github.com/QuantConnect/QCAlgorithm/blob/master/QuantConnect.Algorithm/QCAlgorithm.cs */ public class RollingWindowAlgorithm : QCAlgorithm { public const string Symbol = "JDST"; public ExponentialMovingAverage Fast; public ExponentialMovingAverage Slow; public MovingAverageConvergenceDivergence DailyMacd; public Momentum DailyMomentum; public RelativeStrengthIndex DailyRSI; //Initialize the data and resolution you require for your strategy: public override void Initialize() { //Start and End Date range for the backtest: SetStartDate(2017, 1, 1); SetEndDate(2017, 2, 27); //Cash allocation SetCash(10000); //Add as many securities as you like. All the data will be passed into the event handler: AddSecurity(SecurityType.Equity, Symbol, Resolution.Minute); // define our 15 minute consolidator, this makes 15min bars from 1min bars var fifteenMinute = new TradeBarConsolidator(TimeSpan.FromMinutes(15)); // register the consolidator to receive data for our 'Symbol' SubscriptionManager.AddConsolidator(Symbol, fifteenMinute); // attach our 15 minute event handler, the 'OnFifteenMinuteData' will be called // at 9:45, 10:00, 10:15, ect... until 4:00pm fifteenMinute.DataConsolidated += OnFifteenMinuteData; // define our 15 minute fast EMA Fast = new ExponentialMovingAverage(8); // define our 15 minute slow EMA Slow = new ExponentialMovingAverage(10); // we can also define some daily indicators DailyMomentum = MOM(Symbol, 10); DailyMacd = MACD(Symbol, 12, 26, 9, MovingAverageType.Wilders, Resolution.Daily); DailyRSI = RSI(Symbol, 14, MovingAverageType.Simple, Resolution.Minute); } const decimal tolerance = 0.001m; //public override void Initialize() { //AddEquity("IBM", Resolution.Minute); //} // Accessing requested data public override void OnData(Slice data) { //via a tradebar dictionary (symbol - bar) data.Bars["JDST"].Close; //Or via a ticks list: data.Ticks["JDST"][0].Close; } public void OnFifteenMinuteData(object sender, TradeBar bar) { // update our indicators Fast.Update(Time, bar.Close); Slow.Update(Time, bar.Close); var quantity = Portfolio[Symbol].Quantity; // short or flat and longer term up //if (quantity <= 0 && DailyRSI > 25 && DailyMomentum > 0) if (quantity <= 0 && DailyRSI > 25) { // check for short term up //if (Fast > Slow*(1+tolerance)) if (data.Ticks[Symbol][0].Close > Fast) { // move everything into a long position SetHoldings(Symbol, 1.0); } } // long or flat and longer term down //else if (quantity >= 0 && DailyRSI < 80 && DailyMomentum < 0) else if (quantity >= 0 && DailyRSI < 80) { // check for short term down if (Fast < Slow*(1-tolerance)) { // move everything into a short position SetHoldings(Symbol, -1.0); } } // check for exit conditions else if (quantity != 0) { // check for long exit if (quantity > 0 && Fast < Slow*(1-tolerance)) { Liquidate(Symbol); } // check for short exit else if (quantity < 0 && Fast > Slow*(1+tolerance)) { Liquidate(Symbol); } } } //Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol. public void OnData(TradeBars data) { } } }
voila, many thanks
Alexandre Catarino
In these lines:
data.Bars["JDST"].Close; data.Ticks["JDST"][0].Close;
there are two errors. First, they are not valid C# statements, because we are not assigning, calling, incrementing or decrementing. I believe you want to assign therese fields to another variable. Also, the Tick object does not have the Close member. The current price of a Tick is Price or Value:
var barClose = data.Bars["JDST"].Close; var tickPrice = data.Ticks["JDST"][0].Price;
Note that since you have't subscribed to tick resolution data, you does not have "JDST" in the bar.Ticks object, so the second line will throw an error. To prevent it, please check whether "JSDT" is a key of the Ticks dictionary:
var tickPrice = data.Ticks.ContainsKey("JDST") ? data.Ticks["JDST"][0].Price : 0m;
A third error is here:
public void OnFifteenMinuteData(object sender, TradeBar bar) { /* ... */ if (bar.Ticks[Symbol][0].Price > Fast) { /* ... */ } /* ... */ }
We are trying to access Ticks object in a TradeBar object. It sould be:
public void OnFifteenMinuteData(object sender, TradeBar bar) { /* ... */ if (bar.Close > Fast) { /* ... */ } /* ... */ }
Please mouse over on the error icons on the left of the editor to undertand what error we got in your code.
H Huxley Li
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!