ArithmeticException overflow  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
3strategy
Posts: 22
Joined: 31 Dec 2014
Has thanked: 2 times
Been thanked: 1 time

ArithmeticException overflow

Postby 3strategy » 06 Jan 2015

When trying to run the optimizer on 4 Signals: SRI_LE, SRI_SE, MACD_LE, MACD_SE, I encountered the error System.ArithmeticException: Overflow or underflow in the arithmetic operation. at PowerLanguage.CExecutionInfo.get_MaxBarsBack()
I got this from the MACD and from the RSI.
1) Why am I getting this? - What should I do to fix it? (*** see my bypass below)
2) Why is ExecInfo.MaxBarsBack required by the "CrossesOver" and "CrossesUnder" functions?
Below is some additional information:
- I'm using these Signals untouched, as they come from the installation.
- I remember that when running the same optimization the day before I enterred my registration, it worked without a problem.
- I set the MaxBarsBack to 350, and then to 800.
- I'm letting MACD use a maximum length of 800 in the optimization and RSI uses length up to 250.
- *** I cab bypass the MACD error by changing its code as follows
*** NOTE: with the "bypass" the indicators go bad, and fire a signal on each bar ***:

Code: Select all

if (Bars.CurrentBar > 2 && m_MACD_diff.CrossesOver(0,1));// ExecInfo.MaxBarsBack))
- I can bypass this RSI error by changing its code as follows:

Code: Select all

//if (this.CrossesOver(m_myrsi,OverSold)){
if(m_myrsi.CrossesOver(OverSold,2)){
- A reboot did not help.
- I'm using 750 days of 15 minute candles.
Attachments
Capture Max Bars Back1.PNG
(14.35 KiB) Downloaded 877 times
Last edited by 3strategy on 06 Jan 2015, edited 1 time in total.

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1542 times
Been thanked: 1565 times
Contact:

Re: ArithmeticException overflow

Postby JoshM » 06 Jan 2015

1) Why am I getting this? - What should I do to fix it? (*** see my bypass below)
An arithmetic OverflowException is triggered when a data type is assigned a value that is outside it's range (too high or too low). The error message suggests this happens at line 59 in the MACD_LE strategy. Perhaps it uses a short or int there instead of a double?
2) Why is ExecInfo.MaxBarsBack required by the "CrossesOver" and "CrossesUnder" functions?
The `ExecInfo.MaxBarsBack` property returns the MaxBarsBack setting, I'm not sure where it says that this is "required", but this property can indeed be used for the `CrossesOver` and `CrossesUnder` function. In that case, the function has the same MaxBarsBack like the signal (or indicator).
- I set the MaxBarsBack to 350, and then to 800.
Why so high? Here's more on MaxBarsBack btw: How scripts work.

PS: MultiCharts .NET and MultiCharts are two different platforms. MultiCharts .NET questions are normally placed in the other forum than this one.

3strategy
Posts: 22
Joined: 31 Dec 2014
Has thanked: 2 times
Been thanked: 1 time

Re: ArithmeticException overflow

Postby 3strategy » 06 Jan 2015

@JoshM
I'm not sure where it says that this is "required", but this property can indeed be used for the `CrossesOver` and `CrossesUnder` function. In that case, the function has the same MaxBarsBack like the signal (or indicator).
The CrossesOver/Under functions requires "MaxBarsBack" as a parameter. I don't understand why exactly.
Why so high?
I'm using high values for anything that is based on Exponential moving average when using a short duration like 15 minutes (while the indicator was initially conceived for daily bars). This smooths the indicator and should not affect performance.
The error message suggests this happens at line 59 in the MACD_LE strategy. Perhaps it uses a short or int there instead of a double?

Line 59 is:

Code: Select all

if (Bars.CurrentBar > 2 && m_MACD_diff.CrossesOver(0, ExecInfo.MaxBarsBack))
What should i do?

davewolfs
Posts: 89
Joined: 06 Feb 2013
Has thanked: 2 times
Been thanked: 11 times

Re: ArithmeticException overflow

Postby davewolfs » 06 Jan 2015

The Arithmetic Exception is a result of MultiCharts setting the MXCSR register in the FPU to a value other than it's default of 00001FA0. I have no idea why MC.NET does this and am contact with their support team. Basically rather than the system gracefully handling Underflow/Overflow it halts and throws an exception.

The easiest way to reproduce this is to create an indicator that takes the square root of a negative number e.g. Math.Sqrt(-2) will cause the exception to occur. Under normal circumstances this would just produce Double.NaN. As you have encountered MC throws the exception because a NaN is being generated and evaluated in your calculation for whatever reason.

What baffles me is that this even exists. Either it is new, very few users of MC are doing Mathematical Calculations or MC users prefer to have ArithmeticExceptions thrown? If you have code which expects NaN's to be produced and handles them appropriately it will fail miserably on MC.

For those who want to get a round this and turn off this non traditional behavior for their indicators you can run the following on your start calc.

Code: Select all

public static class FloatingPointControl
{
internal const uint _ALL = 0xFFFFF;
internal const uint _RC_NEAR = 0x00000000;
internal const uint _EM_INVALID = 0x00000010;
internal const uint _EM_UNDERFLOW = 0x00000002;
internal const uint _EM_ZERODIVIDE = 0x00000008;
internal const uint _EM_OVERFLOW = 0x00000004;
internal const uint _EM_INEXACT = 0x00000001;
internal const uint _EM_DENORMAL = 0x00080000;
internal const uint _MCW_EM = 0x0008001f;
internal const uint _CW_DEFAULT = (_RC_NEAR + _EM_INVALID + _EM_ZERODIVIDE + _EM_OVERFLOW + _EM_UNDERFLOW + _EM_INEXACT + _EM_DENORMAL);

[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
internal static extern uint _controlfp(uint newControl, uint mask);
}
Run this on start calc:

Code: Select all

FloatingPointControl._controlfp(FloatingPointControl._CW_DEFAULT, FloatingPointControl._MCW_EM);
If you would like to preserve the value in MC but run on each calc bar iteration do this in CalcBar:

Code: Select all

// At beginning of calc bar
try {
var fpOriginal = FloatingPointControl._controlfp(0, 0);
FloatingPointControl._controlfp(FloatingPointControl._CW_DEFAULT, floatingPointControl._MCW_EM);

...
do your code
...
// At end of calc bar
}
finally
{
FloatingPointControl._controlfp(fpOriginal, FloatingPointControl._ALL);
}
}

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: ArithmeticException overflow  [SOLVED]

Postby Henry MultiСharts » 12 Jan 2015

3strategy, if you still have the issue - please us (support@multicharts.com) the following information for further analysis:
- workspace you are using;
- in QuoteManager select the symbols you are using, make a right click on it->Export data->Export instrument (with data). Send us the QMD export file for analysis;
- in PowerLanguage .NET editor->File->Export->export the studies you are using for replicating this behavior; send us the pln file;
- specify the version and build number of MultiCharts you are running (in MultiCharts go to Help tab-> About);
- instructions for replicating the error.

If the file size is >10 mb please upload it to any file sharing hosting and send me the download link.

HellGhostEvocatorX
Posts: 74
Joined: 10 Feb 2022
Has thanked: 44 times
Been thanked: 8 times

Re: ArithmeticException overflow

Postby HellGhostEvocatorX » 23 Jun 2022

I don't know if it's still relevant, but I also got an overflow. the reason was probably that I

[inputs]
public int slowlengthx { get; set; }

copied from another strategy, renaming fixed the whole thing, as you can see I just added an x ​​;).


Return to “MultiCharts .NET”