Back

Need help with a backtest error

Runtime Error: Must be between 0 and Count 0
Parameter name: i
1 (Open Stacktrace)

I am new to C# and do not understand why my code is throwing this error. Any and all help is appreciated.
Update Backtest








When using RollingWindow classes you need to check if they are ready before using them (since on the first run it won't have any data). I believe you would do something like this:
if (!_rwa.History.IsReady) return;

Also `holdings` variable isn't updated inside the while loop so if the history bug is fixed the algorithm would lock up forever until it times out.
0

@Damien Peck

I have taken your feedback into consideration but I was still unable to get the algorithm to successfully backtest (the error is now the fact that the backtest runs but trades are never entered/exited).

Having reviewed other working community algorithms, I am suspecting that the error lies in my negligence of the OnData(TradeBar data) method. Should I be using OnData for trade execution/management while using the OnDataFifteen Method for all else?
0

Please look at my edits to your project.
0


@Dimitri Pavlenkov

I have given your edits a cursory skim and plan on dissecting it in depth once I have the time. As for now, thank you for the bugfix.

I see that you have chosen to forgo to OnData method. Could you explain to me the it's importance and when I should use it because I have seen multiple shared algorithms that use consolidators where some people rely heavily on OnData while others like yourself opt for focusing the algorithm payload in the OnDataFifteen (or whatever other timeframe).
0

It depends on your desired time frame. If you want to trade 15min bars but your data is 1min, then you use consolidator. If your data is 1 min and 1 min is what you want , then use OnData. Another approach is to use small timeframe to place trades but still use consolidators to have a broader view of the market and/or to run indicators of higher time frame bars. The main thing is the system is flexible enough to satisfy most requirements.
1

Hey DJ, the OnData method is the default entry point for data into your algorithm. We natively support resolutions of tick, second, minute, and day. If you'd like to use other resolutions, you'll need to do something like @Dmitri suggested and define a consolidator to pipe the custom resolution data to a method in your algorithm, so it really depends on what data you need and how frequently you need it that determines whether or not the meat of your implementation should be in OnData(TradeBars). That being said, there are ways to put everything in OnData even if you're using consolidators, I personally like to keep code for different time frames in different methods/classes for organization purposes.
0

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.


@Dmitri, just an FYI, we recently changed the behavior of SetHoldings in this change to be unlevered, so absolute values greater than 1 are allowed and imply leverage on top of your portfolio value.

For example, if I have 100k in cash and no holdings, I can do SetHoldings("SPY", 2.0); and that will buy 200k worth of SPY (assuming I have set my leverage to the default of 2x for equities).
1

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.


@MichaelH -- good to know, thanks.
1

@MichaelH

Thanks for the clarification. I have another question regarding OnData and consolidated data handling methods.

Michael, I noticed in the ComplexIndicator code you shared that in your declaration of the consolidated data handler, you chose to write :
"fifteenMinuteConsolidator.DataConsolidated += OnFiftenMinutesSPY;"
which became the handler
"public void OnFiftenMinutesSPY(object sender, TradeBar data)"

while Dmitri chose to declare in a more complex method whose syntax I am not familiar with:
"fifteenConsolidator.DataConsolidated += (s, e) => OnDataFifteen((TradeBar)e);"
which became the handler
"public void OnDataFifteen(TradeBar consolidated)"

Could you explain to mean the reasoning behind this difference as well as the significant behind the use of "(s,e)" and the "e" following TradeBar in "OnDataFifteen((TradeBar)e)"

It would be much appreciated.
0

@Nick, The DataConsoildated event handler is defined here. It uses a "DataConsolidatedHandler" type.

The DataConsolidatedHandler type is defined here, this follows the "sender, eventObj" pattern which is common of event handlers.

The EventObject in this case is the more generic "BaseData" which underpins all QC data. To make this work Dimitri cast the object to a TradeBar. Michael's event handler fit the pattern so didn't require any casting.
1

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.


Update Backtest





0

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.


Loading...

This discussion is closed