Announcement

Collapse
No announcement yet.

Intraday Standard Deviation

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Intraday Standard Deviation

    Hi Guys, back again with another Standard Deviation Dillemma....

    The Following Code's Syntax seems to check out ok, but when I apply it to a chart, I get nothing. No errors, just no indicators....

    Anyidea where I went wrong?

    PHP Code:
    function preMain() {
     
    setPriceStudy(true);
    setStudyTitle("Standard Deviation ");
    setCursorLabelName("UpperStdDev2"0);
    setCursorLabelName("UpperStdDev1"1);
        
    setCursorLabelName("MovAv"2);
        
    setCursorLabelName("LowerStdDev1"3);
        
    setCursorLabelName("LowerStdDev2"4);
        
    setDefaultBarFgColor(Color.green0);
        
    setDefaultBarFgColor(Color.blue1);
        
    setDefaultBarFgColor(Color.red2);
        
    setDefaultBarFgColor(Color.blue3);
        
    setDefaultBarFgColor(Color.green4);
        
    setPlotType(PLOTTYPE_LINE,0);
        
    setPlotType(PLOTTYPE_LINE,1);
        
    setPlotType(PLOTTYPE_LINE,2);
        
    setPlotType(PLOTTYPE_LINE,3);
        
    setPlotType(PLOTTYPE_LINE,4);
        
    setDefaultBarThickness(1,0);
        
    setDefaultBarThickness(1,1);
        
    setDefaultBarThickness(1,2);
        
    setDefaultBarThickness(1,3);
        
    setDefaultBarThickness(1,4);
    }

    function 
    main(nLengthsLength,sPriceSource) {
    var 
    vRange ="close";
    if (
    nLength == nullnLength 1000;
    if (
    sLength == nullsLength 100;
    if (
    sPriceSource == nullsPriceSource vRange;

    var 
    aSource getValue(sPriceSource0, -nLength);
    if (
    aSource == null) return null;
    var 
    bSource getValue(sPriceSource0, -sLength);
    if (
    bSource == null) return null;

    var 
    sumX 0;
    var 
    sumX2 0;
    var 
    sumY 0;
    var 
    sumY2 0;
    for (
    0nLength; ++i) {
    sumX += aSource[i];
    sumX2 += (aSource[i] * aSource[i])
    }
    for (
    0nLength; ++j) {
    sumY += bSource[j];
    sumY2 += (bSource[j] * bSource[j])
    }

    var 
    meanX = (sumX/nLength);
    var 
    stdev Math.sqrt((sumX2/nLength) - (meanX*meanX));
    var 
    meanY = (sumY/sLength);
    var 
    stdevY Math.sqrt((sumY2/sLength) - (meanY*meanY));
    var 
    MAV sma(20)
    var 
    UpperStdDev1 MAV + (((stdev*0.8)+(stdevY*0.2))*0.8);
    var 
    MovAv MAV
    var LowerStdDev1 MAV - (((stdev*0.8)+(stdevY*0.2))*0.8);
    var 
    UpperStdDev2 MAV + (((stdev*0.8)+(stdevY*0.2))*1.2);
    var 
    LowerStdDev2MAV - (((stdev*0.8)+(stdevY*0.2))*1.2);

    return new Array(
    UpperStdDev2,UpperStdDev1MovAvLowerStdDev1LowerStdDev2);


  • #2
    Re: Intraday Standard Deviation

    saitokun
    The best way to determine where an issue may be is to use debug statements to check the values of the variables. So if you added a debugPrintln(myVariableName) after each line in which you declare a variable or assign a value to it you would eventually find that sumY and sumY2 are returning Nan
    This is because the condition in the for loop statement in line 48 is incorrectly set to nLength instead of sLength
    Once you make that change you will see that the debug statement will report a value instead of Nan [for that variable and any other subsequent variable that depends on it] and the formula will plot the lines
    Alex


    Originally posted by saitokun
    Hi Guys, back again with another Standard Deviation Dillemma....

    The Following Code's Syntax seems to check out ok, but when I apply it to a chart, I get nothing. No errors, just no indicators....

    Anyidea where I went wrong?

    PHP Code:
    function preMain() {
     
    setPriceStudy(true);
    setStudyTitle("Standard Deviation ");
    setCursorLabelName("UpperStdDev2"0);
    setCursorLabelName("UpperStdDev1"1);
        
    setCursorLabelName("MovAv"2);
        
    setCursorLabelName("LowerStdDev1"3);
        
    setCursorLabelName("LowerStdDev2"4);
        
    setDefaultBarFgColor(Color.green0);
        
    setDefaultBarFgColor(Color.blue1);
        
    setDefaultBarFgColor(Color.red2);
        
    setDefaultBarFgColor(Color.blue3);
        
    setDefaultBarFgColor(Color.green4);
        
    setPlotType(PLOTTYPE_LINE,0);
        
    setPlotType(PLOTTYPE_LINE,1);
        
    setPlotType(PLOTTYPE_LINE,2);
        
    setPlotType(PLOTTYPE_LINE,3);
        
    setPlotType(PLOTTYPE_LINE,4);
        
    setDefaultBarThickness(1,0);
        
    setDefaultBarThickness(1,1);
        
    setDefaultBarThickness(1,2);
        
    setDefaultBarThickness(1,3);
        
    setDefaultBarThickness(1,4);
    }

    function 
    main(nLengthsLength,sPriceSource) {
    var 
    vRange ="close";
    if (
    nLength == nullnLength 1000;
    if (
    sLength == nullsLength 100;
    if (
    sPriceSource == nullsPriceSource vRange;

    var 
    aSource getValue(sPriceSource0, -nLength);
    if (
    aSource == null) return null;
    var 
    bSource getValue(sPriceSource0, -sLength);
    if (
    bSource == null) return null;

    var 
    sumX 0;
    var 
    sumX2 0;
    var 
    sumY 0;
    var 
    sumY2 0;
    for (
    0nLength; ++i) {
    sumX += aSource[i];
    sumX2 += (aSource[i] * aSource[i])
    }
    for (
    0nLength; ++j) {
    sumY += bSource[j];
    sumY2 += (bSource[j] * bSource[j])
    }

    var 
    meanX = (sumX/nLength);
    var 
    stdev Math.sqrt((sumX2/nLength) - (meanX*meanX));
    var 
    meanY = (sumY/sLength);
    var 
    stdevY Math.sqrt((sumY2/sLength) - (meanY*meanY));
    var 
    MAV sma(20)
    var 
    UpperStdDev1 MAV + (((stdev*0.8)+(stdevY*0.2))*0.8);
    var 
    MovAv MAV
    var LowerStdDev1 MAV - (((stdev*0.8)+(stdevY*0.2))*0.8);
    var 
    UpperStdDev2 MAV + (((stdev*0.8)+(stdevY*0.2))*1.2);
    var 
    LowerStdDev2MAV - (((stdev*0.8)+(stdevY*0.2))*1.2);

    return new Array(
    UpperStdDev2,UpperStdDev1MovAvLowerStdDev1LowerStdDev2);

    Comment


    • #3
      Thanks very much Alex, still quite new to programming. Thanks for the tip on Debugging. Probably more important the the actual error here. (But thanks for that too)

      Comment


      • #4
        saitokun
        You are welcome
        Alex


        Originally posted by saitokun
        Thanks very much Alex, still quite new to programming. Thanks for the tip on Debugging. Probably more important the the actual error here. (But thanks for that too)

        Comment

        Working...
        X