[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Divide by Zero



PureBytes Links

Trading Reference Links

While John is trying to fix his floating point error, I'm trying to fix a Divide-by-zero error message when adding "Standard Error Bands" indicator to a TS2ki chart.

Anyone describing the problem and fix is Hero at my house. (Until my wife gets back)

Here's the Stocks & Comod Mag's version which also triggers the same error message as the version supplied with TS2ki:

TRADESTATION

To create the standard error bands described by Jon Andersen in "Standard error bands" in the September 1996 STOCKS & COMMODITIES, you must calculate the beta and alpha coefficients of the linear regression. First, create the calcB and calcA user functions, which refer to the beta and alpha coefficients. Be sure to create and verify these functions before building the indicator. Create calcB first, then calcA. These functions are used in calculating the regression coefficients for the standard error bands.




Type: User Function
Name: calcB

Inputs: Length(Numeric);
Vars: X(0);

X = BarNumber;

Value1 = Summation(X * Close, Length) - (Length * Average(X, Length) *Average(Close, Length));
Value2 = Summation(Square(X), Length) - (Length * Square(Average(X, Length)));

calcB = Value1/Value2;

Type: User Function
Name: calcA

Inputs: Length(Numeric);
Vars: X(0);

X = BarNumber;

calcA = Average(Close, Length) - (calcB(Length) * Average(X, Length));

Next, build the standard error bands indicator. The "length" value specifies the period of the linear regression line; the default is 21. The SDeg value specifies the smoothing factor that is used to smooth the linear regression and standard error bands; the default is 3.




Type: Indicator
Name: Std Error Bands Smooth

Inputs: Length(21), SDeg(3);
Vars: LinRegY(0), X(0), StdErr(0), LinRegS(0), SErr(0);

LinRegY = LinearRegValue(Close, Length, 0);
X = CurrentBar;

IF CurrentBar > Length then Begin
Value1 = (Summation(Square(Close), Length)) - ((calcA(Length) *
Summation(Close, Length))) - ((calcB(Length) *
Summation(X * Close, Length)));
Value2 = Length - 2;
StdErr = SquareRoot(Value1 / Value2);
LinRegS = Average(LinRegY, SDeg);
SErr = 2 * Average(StdErr, SDeg);

Plot1(LinRegS, "LinRegS");
Plot2(LinRegS + SErr, "+StdErrS");
Plot3(LinRegS - SErr, "-StdErrS");
End;

The indicator for the related %A standard error bands is built using the code below. It's best plotted with the bar type set to "histogram."




Type: Indicator
Name: Standard Error Bands %A

Inputs: Length(21), SDeg(3);
Vars: LinRegY(0), X(0), StdErr(0), LinRegS(0), SErr(0), PcntA(0);

LinRegY = LinearRegValue(Close, Length, 0);
X = CurrentBar;

IF CurrentBar > Length then Begin
Value1 = (Summation(Square(Close), Length)) - ((calcA(Length) *
Summation(Close, Length))) - ((calcB(Length) *
Summation(X * Close, Length)));
Value2 = Length - 2;
StdErr = SquareRoot(Value1 / Value2);
LinRegS = Average(LinRegY, SDeg);
SErr = 2 * Average(StdErr, SDeg);
PcntA = (Close - (LinRegS-SErr)) / ((LinRegS+SErr)-(LinRegS-SErr)) * 100;

Plot1(PcntA, "%A");
End;

This code is available at both the Omega Research Forum on America On-Line as well as at Omega's Web site.

-- Gaston Sanchez, Omega Research
Internet: http://www.omegaresearch.com


TRADESTATION

    To create the standard error bands described by Jon Andersen in "Standard error bands" in the September 1996 STOCKS & COMMODITIES, you must calculate the beta and alpha coefficients of the linear regression. First, create the calcB and calcA user functions, which refer to the beta and alpha coefficients. Be sure to create and verify these functions before building the indicator. Create calcB first, then calcA. These functions are used in calculating the regression coefficients for the standard error bands.




Type: User Function
Name: calcB

Inputs: Length(Numeric);
Vars: X(0);

X = BarNumber;

Value1 = Summation(X * Close, Length) - (Length * Average(X, Length) *Average(Close, Length));
Value2 = Summation(Square(X), Length) - (Length * Square(Average(X, Length)));

calcB = Value1/Value2;

Type: User Function
Name: calcA

Inputs: Length(Numeric);
Vars: X(0);

X = BarNumber;

calcA = Average(Close, Length) - (calcB(Length) * Average(X, Length));

    Next, build the standard error bands indicator. The "length" value specifies the period of the linear regression line; the default is 21. The SDeg value specifies the smoothing factor that is used to smooth the linear regression and standard error bands; the default is 3.




Type: Indicator
Name: Std Error Bands Smooth

Inputs: Length(21), SDeg(3);
Vars: LinRegY(0), X(0), StdErr(0), LinRegS(0), SErr(0);

LinRegY = LinearRegValue(Close, Length, 0);
X = CurrentBar;

IF CurrentBar > Length then Begin
     Value1 = (Summation(Square(Close), Length)) - ((calcA(Length) * 
              Summation(Close, Length))) - ((calcB(Length) * 
              Summation(X * Close, Length)));
     Value2 = Length - 2;
     StdErr = SquareRoot(Value1 / Value2);
    LinRegS = Average(LinRegY, SDeg);
       SErr = 2 * Average(StdErr, SDeg);

       Plot1(LinRegS, "LinRegS");
       Plot2(LinRegS + SErr, "+StdErrS");
       Plot3(LinRegS - SErr, "-StdErrS");
End;

    The indicator for the related %A standard error bands is built using the code below. It's best plotted with the bar type set to "histogram."




Type: Indicator
Name: Standard Error Bands %A

Inputs: Length(21), SDeg(3);
Vars: LinRegY(0), X(0), StdErr(0), LinRegS(0), SErr(0), PcntA(0);

LinRegY = LinearRegValue(Close, Length, 0);
X = CurrentBar;

IF CurrentBar > Length then Begin
    Value1 = (Summation(Square(Close), Length)) - ((calcA(Length) * 
             Summation(Close, Length))) - ((calcB(Length) * 
             Summation(X * Close, Length)));
    Value2 = Length - 2;
    StdErr = SquareRoot(Value1 / Value2);
    LinRegS = Average(LinRegY, SDeg);
    SErr = 2 * Average(StdErr, SDeg);
    PcntA = (Close - (LinRegS-SErr)) / ((LinRegS+SErr)-(LinRegS-SErr)) * 100;

    Plot1(PcntA, "%A");
End;

    This code is available at both the Omega Research Forum on America On-Line as well as at Omega's Web site.

        -- Gaston Sanchez, Omega Research
        Internet: http://www.omegaresearch.com