Announcement

Collapse
No announcement yet.

Standard Deviation

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

  • Standard Deviation

    Hi Guys, Ive had a go at the following code, the idea is to chart the Daily Std Dev on an intraday chart, but Im not having much luck with the following code. Im pretty sure it has something to do with the following line, because whe nI remove the inv("D") section, it charts the intraday StdDev ok.
    PHP Code:
    var aSource getValue"close"0, -30,inv("D")); 
    function preMain() {

    setPriceStudy(true);
    setStudyTitle("Standard Deviation 2");
    setCursorLabelName("StdUpper2",0);
    setCursorLabelName("StdUpper1", 1);
    setCursorLabelName("StdMiddle", 2);
    setCursorLabelName("StdLower1", 3);
    setCursorLabelName("StdLower2", 4);
    setDefaultBarFgColor(Color.blue, 0);
    setDefaultBarFgColor(Color.red, 1);
    setDefaultBarFgColor(Color.green, 2);
    setDefaultBarFgColor(Color.red, 3);
    setDefaultBarFgColor(Color.blue, 4);
    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);

    }




    var bInit = false;
    var vSymbol = null;

    function main() {
    if(bInit==false){
    bInit = true;
    }
    var aSource = getValue( "close", 0, -30,inv("D"));
    if (aSource == null) return null;

    var sumX = 0;
    var sumX2 = 0;
    for (i = 0; i < 30; ++i) {
    sumX += aSource[i];
    sumX2 += (aSource[i] * aSource[i]);
    }
    var meanX = (sumX/30);
    var stdev = Math.sqrt((sumX2/30) - (meanX*meanX));
    var MAV = sma(20);
    var StdUpper1 = MAV + (stdev*0.5);
    var StdMiddle = MAV;
    var StdLower1 = MAV - (stdev*0.5);
    var StdUpper2 = MAV + (stdev*1);
    var StdLower2= MAV - (stdev*1);

    return new Array(StdUpper2,StdUpper1, StdMiddle, StdLower1, StdLower2);
    }

  • #2
    saitokun,

    The global function getValue() doesn't take sym() or inv() as a parameter - only a Symbol string, which can only be a symbol. Also, numBars should be positive.

    I'd use a series object instead. Modified (and working):
    PHP Code:
    function preMain() {
        
    setPriceStudy(true);
        
    setStudyTitle("Standard Deviation 2");
        
    setCursorLabelName("StdUpper2",0);
        
    setCursorLabelName("StdUpper1"1);
        
    setCursorLabelName("StdMiddle"2);
        
    setCursorLabelName("StdLower1"3);
        
    setCursorLabelName("StdLower2"4);
        
    setDefaultBarFgColor(Color.blue0);
        
    setDefaultBarFgColor(Color.red1);
        
    setDefaultBarFgColor(Color.green2);
        
    setDefaultBarFgColor(Color.red3);
        
    setDefaultBarFgColor(Color.blue4);
        
    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);
    }


    var 
    bInit false;
    var 
    vSymbol null;
    var 
    oDailySeries null;


    function 
    main() {
        if(
    bInit==false){
            
    oDailySeries close (inv("D"));
            
    bInit true;
            return; 
    // likely don't have data yet from server
        
    }

    //    var aSource = getValue( "close", 0, -30,inv("D"));
    //    if (aSource == null) return null;

        
    var sumX 0;
        var 
    sumX2 0;
        for (
    030; ++i) {
            var 
    val oDailySeries.getValue (-i);
            
    sumX += val;
            
    sumX2 += (val val);
            
    //sumX += aSource[i];
            //sumX2 += (aSource[i] * aSource[i]);
        
    }
        var 
    meanX = (sumX/30);
        var 
    stdev Math.sqrt((sumX2/30) - (meanX*meanX));
        var 
    MAV sma(20);
        var 
    StdUpper1 MAV + (stdev*0.5);
        var 
    StdMiddle MAV;
        var 
    StdLower1 MAV - (stdev*0.5);
        var 
    StdUpper2 MAV + (stdev*1);
        var 
    StdLower2MAV - (stdev*1);

        return new Array(
    StdUpper2,StdUpper1StdMiddleStdLower1StdLower2);

    Last edited by shortandlong; 01-19-2010, 01:43 PM.

    Comment

    Working...
    X