Optimizer fails with exception ArgumentOutOfRangeException
Optimizer fails with exception ArgumentOutOfRangeException
08 Mar 2020, 19:24
Hi,
I was earlier facing this error intermittently both during backtesting and during optimization. Earlier i was getting this error only if i chose to backtest on data older then 1st August 2019.
Now my optimizer is totally not starting due to this error.
Crashed in OnBar with ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: index
I have deleted all pepperstone folders under AppData/Roaming
Please let me know how i can fix this.
Thanks in advance,
Warm Regards,
Vipin.
Replies
driftingprogrammer
10 Mar 2020, 11:31
The problem isolated
Below is the code that runs fine as expected. It can also be backtested and nothing will fail.
But when I run it against the optimiser it fails.
I have this issue on all the 3 computers of mine.
This problem is new started around 48 hours back.
It is related to corrupt data that is being sent to the optimiser.
Even in backtesting this problem ocurs intermittently, at times i was facing this issue while testing for dates earlier then August 2019.
I hope this gets fixed soon as you can imagine not having the optimiser working is a big problem.
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class NewcBot : Robot
{
[Parameter(DefaultValue = 0.0)]
public double Parameter { get; set; }
protected override void OnStart()
{
// Put your initialization logic here
Bars tempBars = MarketData.GetBars(TimeFrame.Minute15);
Print("Printing tik volume " + tempBars.Last(1).TickVolume);
}
protected override void OnTick()
{
// Put your core logic here
}
protected override void OnStop()
{
// Put your deinitialization logic here
}
}
}
Thanks in advance.
@driftingprogrammer
PanagiotisCharalampous
10 Mar 2020, 15:19
Hi driftingprogrammer,
This happens because
Bars tempBars = MarketData.GetBars(TimeFrame.Minute15);
does not return any values in OnStart(). Unfortunately getting past bars from other timeframes is not supported at the moment in Optimization. You can only get bars from other timeframes that are constructed during the time the cBot is running. If your cBot logic allows it, here is a workaround to consider.
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class NewcBot : Robot
{
[Parameter(DefaultValue = 0.0)]
public double Parameter { get; set; }
protected override void OnStart()
{
}
protected override void OnBar()
{
Bars tempBars = MarketData.GetBars(TimeFrame.Minute15);
if (tempBars.Count > 0)
{
Print("Printing tick volume " + tempBars.Count);
}
}
protected override void OnStop()
{
// Put your deinitialization logic here
}
}
}
Best Regards,
Panagiotis
@PanagiotisCharalampous
vipin.bhatia
10 Mar 2020, 15:56
No Problem
PanagiotisCharalampous said:
Hi driftingprogrammer,
This happens because
Bars tempBars = MarketData.GetBars(TimeFrame.Minute15);
does not return any values in OnStart(). Unfortunately getting past bars from other timeframes is not supported at the moment in Optimization. You can only get bars from other timeframes that are constructed during the time the cBot is running. If your cBot logic allows it, here is a workaround to consider.
using System; using System.Linq; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.API.Internals; using cAlgo.Indicators; namespace cAlgo.Robots { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class NewcBot : Robot { [Parameter(DefaultValue = 0.0)] public double Parameter { get; set; } protected override void OnStart() { } protected override void OnBar() { Bars tempBars = MarketData.GetBars(TimeFrame.Minute15); if (tempBars.Count > 0) { Print("Printing tick volume " + tempBars.Count); } } protected override void OnStop() { // Put your deinitialization logic here } } }
Best Regards,
Panagiotis
Hi Panagiotis ,This code works and can be backtested, so I would assume
MarketData.GetBars(TimeFrame.Minute15);
is working fine in OnStart.
Like you mentioned MarketData.GetBars(TimeFrame.Minute15); will anyways not work during optimization, well that is not a problem at all, I can just form my own candles, I prefer them anyways since they are more accurate because I can form timeframe datasets from my present position and not depend on the predefined timeframes of 6, 6:15,6:30,6:45 etc...
Thanks for letting me know, I can go ahead and modify my code but it would have been awesome if there was a warning being printed in the log similar to the one that shows up if you query for the Depth of Market in backtesting.
If I cant use the code inside the optimizer then I would not use that code at all but I would like to point out that MarketData.GetBars(TimeFrame.Minute15); fails intermittently during backtesting, usually for much older dates. It being an intermittent issue lead me to assume it had something to do with the historical data. After removing it I am assuming I will not be facing those intermmitent crashes.
Cheers.
@vipin.bhatia
warnerjonn
29 Jun 2020, 13:57
ArgumentOutOfRangeException
The exception that is thrown when the value of an argument is outside the allowable range of values as defined by the invoked method. Indexing an empty list will always throw an exception. Use a method like Add to append the item to the end of the list, or Insert to place the item in the middle of the list somewhere, etc. You cannot index into a list if that offset doesn't exist.
Typically, an ArgumentOutOfRangeException results from developer error. Instead of handling the exception in a try/catch block, you should eliminate the cause of the exception or, if the argument is returned by a method call or input by the user before being passed to the method that throws the exception, you should validate arguments before passing them to the method.
@warnerjonn
dave.anderson.consulting
21 Dec 2020, 14:00
( Updated at: 21 Dec 2020, 14:59 )
RE:
PanagiotisCharalampous said:
Hi driftingprogrammer,
This happens because
Bars tempBars = MarketData.GetBars(TimeFrame.Minute15);
does not return any values in OnStart(). Unfortunately getting past bars from other timeframes is not supported at the moment in Optimization. You can only get bars from other timeframes that are constructed during the time the cBot is running. If your cBot logic allows it, here is a workaround to consider.
using System; using System.Linq; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.API.Internals; using cAlgo.Indicators; namespace cAlgo.Robots { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class NewcBot : Robot { [Parameter(DefaultValue = 0.0)] public double Parameter { get; set; } protected override void OnStart() { } protected override void OnBar() { Bars tempBars = MarketData.GetBars(TimeFrame.Minute15); if (tempBars.Count > 0) { Print("Printing tick volume " + tempBars.Count); } } protected override void OnStop() { // Put your deinitialization logic here } } }
Best Regards,
Panagiotis
Unfortunately it is sill unclear to me what is really supported and what is not supported regarding getting historical bars of different timeframes while optimization is running.
Does this mean that if I move the code from OnStart to OnTick or OnBar as you suggested, it would work in Optimization? Or getting past bars from other timeframes is 'always' impossible in Optimization? Because in last case, the cTrader Optimization would be useless!
I would greatly appreciate if you explain it in more detail.
@dave.anderson.consulting
PanagiotisCharalampous
21 Dec 2020, 16:00
Hi dave.anderson.consulting,
In optimization, you cannot get bars from other timeframes before the backtesting start date. You can only get such bars from the date and time the execution has started and onwards. I hope it is clearer now.
Best Regards,
Panagiotis
@PanagiotisCharalampous
dave.anderson.consulting
21 Dec 2020, 17:09
RE:
PanagiotisCharalampous said:
Hi dave.anderson.consulting,
In optimization, you cannot get bars from other timeframes before the backtesting start date. You can only get such bars from the date and time the execution has started and onwards. I hope it is clearer now.
Best Regards,
Panagiotis
Hello Panagiotis Charalampous,
Thanks for your quick response.
I think I understand now :-(
The misunderstanding was probably due to my creativity to get around cTrader shortcomings. I wanted to use Optimization for my Renko MTF bot using a mix of the above information and the information I found in your other post at https://ctrader.com/forum/cbot-support/23640.
But unfortunately that is not possible, I now understand.
I come from MT4 algotrading and am pretty good at it. But I am even better at C#. Hence I thought instead of switching to MT5 I can use cTrader. At first it seems that cTrader has many useful features that MT4 and sometimes even MT5 do not have. But in practice, most features still seem immature or incomplete.
@dave.anderson.consulting
PanagiotisCharalampous
09 Mar 2020, 09:10
Hi Vipin,
The error indicates that you are doing something wrong in your code. Please share the cBot code so that we can have a look.
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous