+1 888 340 6572

# Miscellaneous > Trend StdDev

Article/Author: Origin: Gary Fritz`s site. Coded by Gary Fritz, 07/20/1999

File Includes:

Indicator - Trend StdDev
Function - LastBarOnChart
Function - LastCalcDate
Function - LastCalcTime

Category: Miscellaneous > Trend StdDev

Description:

Computes the standard deviation of Nlengs different lengths of price action: C-C[1], C-C[3], C-C[8], C-C[15], etc. In a pure Random Walk, the N^2-length sample will have a StdDev of N times the StdDev of the 1-length sample. If a particular price example shows larger-than-expected StdDevs it indicates a trending market; if the StdDevs are smaller than the expected values it indicates an anti-trending (reversing) market.

References:
S&C magazine, August 1995, Alex Saitta, Trending on a Historical Basis
S&C magazine, January 1992, E. Michael Poulos, Futures According to Trend Tendency

Usage:

Doesn't start recording data until 100 bars are available. Needs a lot of bars to compute valid data anyway, so just apply it to a long chart. StdDev values are printed to the Print Log.

Inputs:

Price - the data series to use
PlotStD - Plots Std Devs of 9, 25, 49, 81-long samples

EasyLanguage Code:
```INPUTS: PRICE(CLOSE),
PLOTSTD(FALSE);       { PLOT STD DEVS OF 9, 25, 49, 81-LONG SAMPLES }

VARS:  NLENGS(10);            { COMPUTE STDDEV FOR LENGTHS 1:NLENGS^2 }
VARS:  NBARS(0), INDEX(0), BARSBACK(0), DIFF(0), MEAN(0), SIGMA(0), SD1(0);
ARRAYS:  BARSUM[20](0), BARSUM2[20](0);

IF (CURRENTBAR = 1) THEN BEGIN
FOR INDEX = 1 TO NLENGS BEGIN
NBARS = 0;
BARSUM[INDEX] = 0;
BARSUM2[INDEX] = 0;
END;
END;

{ ON EACH BAR, ADD UP SUMS & SUM-OF-SQUARES,
FOR LOOKBACK LENGTHS OF INDEX^2 }

IF (CURRENTBAR > NLENGS*NLENGS) THEN BEGIN
NBARS = NBARS+1;
FOR INDEX = 1 TO NLENGS BEGIN
IF (INDEX = 1) THEN DIFF = (PRICE - PRICE[1]) / PRICE[1]
ELSE DIFF = (PRICE - PRICE[INDEX*INDEX-1]) / PRICE[INDEX*INDEX-1];
BARSUM[INDEX] = BARSUM[INDEX] + DIFF;
BARSUM2[INDEX] = BARSUM2[INDEX] + DIFF*DIFF;
END;
END;

IF LASTBARONCHART AND (NBARS > 1) THEN BEGIN
PRINT("TREND DATA FOR ",GETSYMBOLNAME,":");
IF (DATACOMPRESSION = 4) THEN PRINT("  MONTHLY BARS")
ELSE IF (DATACOMPRESSION = 3) THEN PRINT("  WEEKLY BARS")
ELSE IF (DATACOMPRESSION = 2) THEN PRINT("  DAILY BARS")
ELSE IF (DATACOMPRESSION = 1) THEN PRINT(" ",BARINTERVAL:3:0,"-MINUTE BARS");
PRINT(NBARS:5:0," SAMPLES");

{ AN N^2 DAY TEST, WITH A PERFECT RANDOM WALK, IS EXPECTED TO HAVE
A STANDARD DEVIATION N TIMES LARGER THAN A 1-DAY TEST.
SO THE "EXPECTED" COLUMN IS N, AND THE "ACTUAL" COLUMN IS
N^2-DAY_STDDEV / 1-DAY_STDDEV. }

PRINT("BARS     STDDEV  EXPECTED  ACTUAL");
FOR INDEX = 1 TO NLENGS BEGIN
MEAN = BARSUM[INDEX] / NBARS;
SIGMA = SQUAREROOT((BARSUM2[INDEX]-NBARS*MEAN*MEAN)/(NBARS-1));
IF (INDEX = 1) THEN SD1 = SIGMA;
PRINT(INDEX*INDEX:4:0,"    ",SIGMA:2:5,"    ",INDEX:2:0,"    ",SIGMA/SD1:2:5);
END;
END;

{ IF DESIRED, PLOT 4 OF THE SD'S.  I'M NOT SURE THIS IS USEFUL BUT
I THOUGHT SOMEBODY MIGHT LIKE TO SEE IT.  THE REPEATED CODE IS NOT
VERY PRETTY, BUT EL'S LIMITS MADE IT A PAIN TO DO IT VIA LOOPING. }

IF PLOTSTD AND (NBARS > 10) THEN BEGIN
MEAN = BARSUM[3] / NBARS;
SIGMA = SQUAREROOT((BARSUM2[3]-NBARS*MEAN*MEAN)/(NBARS-1));
PLOT1(SIGMA, "STD3");
MEAN = BARSUM[5] / NBARS;
SIGMA = SQUAREROOT((BARSUM2[5]-NBARS*MEAN*MEAN)/(NBARS-1));
PLOT2(SIGMA, "STD5");
MEAN = BARSUM[7] / NBARS;
SIGMA = SQUAREROOT((BARSUM2[7]-NBARS*MEAN*MEAN)/(NBARS-1));
PLOT3(SIGMA, "STD7");
MEAN = BARSUM[9] / NBARS;
SIGMA = SQUAREROOT((BARSUM2[9]-NBARS*MEAN*MEAN)/(NBARS-1));
PLOT4(SIGMA, "STD9");
END;```