Back

Problem backtesting an algorithm + how to setup stop loss orders

Hello,
I am trying to backtest an algorithm I am experimenting with. Its basically an EMA cross algorithm
based on the example given and while it compiles ok, it takes no trades when backtested and
produces no results. There is a runtime error :=

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

Which I am not sure what it means. There is a variable "i" in the program but it is initialized to 0.

Also, I would like help in setting up stop loss orders. I would like to know how to
setup a stop loss order for a long and a short position based on ATR.

So something like this EasyLanguage code :=

ATR = AvgTrueRange(10);
...
If Condition1 Then
Buy ("Long") Next Bar at Open Tomorrow + Trigger * ATR Stop;

Thank
Roger
Update Backtest








Hi Ross,

You're binding to the events every loop, and *after* the IsReady function. So once you get to the window[0] section, there's actually no data in the rolling windows.

I'd recommend moving the code below into Initialize which makes it work fine:
// Do our calculations.
// this next line says each time we get a new ema value, add it to our window
Avg1.Updated += (sender, args) => window.Add(args);
// this next line says each time we get a new ema value, add it to our window
Avg2.Updated += (sender, args) => window2.Add(args);
// this next line says each time we get a new atr value, add it to our window
atr.Updated += (sender, args) => atr_win.Add(args);


See this thread for lots of examples of stop, limit and market orders:
https://www.quantconnect.com/forum/discussion/623/feature-ordertickets-and-updating-orders#Item_20
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.


Wow that's cool. I did what you said and I can now see results when I backtest :-) I will look into stop loss discussion next. Thanks a lot.
0

Hi, looks like I added StopLimitOrders ok. I got two errors for insufficient funds to complete orders, but otherwise looks ok. If you want, you can do a code review of the algorithm as it is now. Cheers.
0

Make sure you cancel old stop loss orders that are no longer needed.
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.


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