Problem with series and/or LowPass?

Posted By: CL1

Problem with series and/or LowPass? - 09/09/13 07:14

Hi, I can get some behavior when making series, particularly using LowPass. Often, a series of a series doesn't display on the plot and returns a value of -1. This is more frequent with LowPass. An example with two very similar series functions that give different results:

var fxn1(var* Data,int Period) {
return (Data[0]-MidPoint(Data,Period))/(MaxVal(Data,Period)); }

var fxn2(var* Data,int Period) {
return (Data[0]-MidPoint(Data,Period))/(MaxVal(Data,Period)-MidPoint(Data,Period)); }

function run() {
StartDate = 2002;
LookBack=1440;
set(PLOTPRICE+PLOTNOW);
PlotBars = 20000;
var* Price = series(price());
vars M1=series(fxn1(Price,1440));
vars M2=series(fxn2(Price,1440));
vars M3=series(LowPass(M1,100));
plot("M1",M1[0],NEW,BLUE);
plot("M2",M2[0],NEW,BLUE);
plot("M3",M3[0],NEW,BLUE); }

These all plot fine. However, if you change M3 to be a LowPass series of M2 (instead of M1, ie. fxn2 instead of fxn1), the plot is blank with -1. M1 and M2 are still fine in this case.

Is this user error, or a bug somewhere? Thanks for any help you can provide.
Posted By: CL1

Re: Problem with series and/or LowPass? - 09/09/13 12:43

This should make it more clear. Now both fxn1 and fxn2 have series (M1, M2) and these are then passed through a LowPass filter (M3, M4). Why does M3 plot, but M4 is blank with -1?

var fxn1(var* Data,int Period) {
return (Data[0]-MidPoint(Data,Period))/(MaxVal(Data,Period)); }

var fxn2(var* Data,int Period) {
return (Data[0]-MidPoint(Data,Period))/(MaxVal(Data,Period)-MidPoint(Data,Period)); }

function run() {
StartDate = 2002;
LookBack=1440;
set(PLOTPRICE+PLOTNOW);
PlotBars = 20000;
var* Price = series(price());
vars M1=series(fxn1(Price,1440));
vars M2=series(fxn2(Price,1440));
vars M3=series(LowPass(M1,100));
vars M4=series(LowPass(M2,100));
plot("M1",M1[0],NEW,BLUE);
plot("M2",M2[0],NEW,BLUE);
plot("M3",M3[0],NEW,BLUE);
plot("M4",M4[0],NEW,BLUE); }
Posted By: jcl

Re: Problem with series and/or LowPass? - 09/10/13 11:15

The reason is that your second function, fxn2, can return an invalid floating point value when both dividend and divisor are 0. This disables the lowpass filter, so it does not work for the rest of the data.

This is not really your fault, but a flaw of the lowpass filter indicator which should be able to deal with such situations. I've forwarded a notice to the developers and this will be fixed in the next update.

Anyway when you have a division in your script, always make sure that a division by zero can not happen.
Posted By: CL1

Re: Problem with series and/or LowPass? - 09/11/13 11:32

Hi, thanks I see the issue here, although I am a bit confused how this can happen since the second function will never return 0 (except for massive price gaps, which I don't think I have). Is it more that the function could return 0 in some circumstances?

Additionally, I also see this with DomiantPeriod using some series I have made. These series span the interval [-100,100], and have all values in this range. Using "vars DomPer=series(DominantPeriod(Series_Name,100));" I get a blank graph with -1.

Any thoughts??
Posted By: jcl

Re: Problem with series and/or LowPass? - 09/11/13 12:23

This might be the same problem. Print the output of your series at any bar for checking. Especially at the begin at bar 0, when prices are not yet loaded and indicators are 0, subtracting two values can cause a division by zero.
Posted By: CL1

Re: Problem with series and/or LowPass? - 09/12/13 18:44

Hi, thanks and indeed you are correct that my custom series will have division by zero if all values of pairs are zero (ie. in the beginning during the LookBack period).

Do you know when the next update will be available?

Is there any way to avoid this until the next update arrives? I was thinking a quick check of (if Data[0]==0, return 0; otherwise calculate series). However, this didn't seem to work.

Thanks
Posted By: CL1

Re: Problem with series and/or LowPass? - 09/17/13 06:05

Hi, just to update this works if you use Bar instead of Data.

if (Bar<=LookBack) {
return 0.0;
}
else {
Calculate
}
© 2024 lite-C Forums