Announcement

Collapse
No announcement yet.

variable scope issue?

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

  • variable scope issue?

    Background.
    I have an efs ... it is a symbol with 2ma's as NPS. I've modified it several times adding some extra features and it works quite well.

    I'm now trying to see if I can improve its execution time.
    The efs has an array that is set at the longest of the two moving averages. Then, I sum of the number of periods for each ma in the array, and divide the periods to get the moving average.

    Here is the idea for a speed up... the summation occurs on each trade or tick. The thought is to calculate temporary sums on a BARSTATE_NEWBAR event, using one less period than the moving average has. During that bar, the total summation will the take the temporary sum and add the current close value and then calculate the ma by dividing by the total number of periods.

    But something is not working correctly.. It appears to be related to the lifetime of the variable.

    in the code attached, one of the ma calculations is disabled until I get the other one working.

    What happens is that the vSumTemp appears to work on the BARSTATE_NEWBAR event, but the value is not stored properly.

    Any ideas?

    bigtee

    PHP Code:
    /***********************************************
    /*****************************************************************
    Provided By : eSignal. (c) Copyright 2003  modified by  7/24/10
    *****************************************************************/
    // this one has variable symbol input and changes titles  accordingly... final efs1 version... can be used for NYSE, NDX, DOW
    // add ability to invert the color of the slope of the slow ma ...i.e.  when using the VIX

    var vLoaded false;
    var 
    cSym "";
    var 
    vSym null;
    var 
    cPds null;
    var 
    cZero null;

    function 
    preMain() {

        
    setIntervalsBackfill(true); 

    //askForInput("Symbol");

        //setComputeOnClose(true);   ///  change if want it to 
        
    if (vLoaded == false) {    
            
    setStudyTitle(" ... Add Symbol");
            
    setCursorLabelName("Add Symbol");
            
    setDefaultBarFgColor(Color.black);
            
            var 
    fp1 = new FunctionParameter("n1Pds"FunctionParameter.NUMBER);  // periods for ma number 1
                
    fp1.setName("MA1-Length"); //optional name to show in Edit Studies
                
    fp1.setLowerLimit(1);  
                
    fp1.setDefault(3); //Edit this value to set a new default 

            
    var fp2 = new FunctionParameter("n2Pds"FunctionParameter.NUMBER);  // periods for ma number 1
                
    fp2.setName("MA2-Length"); 
                
    fp2.setLowerLimit(1);  
                
    fp2.setDefault(20); 

            var 
    fp3 = new FunctionParameter("sColor"FunctionParameter.COLOR); // color for symbol
                
    fp3.setName("Symbol-Color");
                
    fp3.setDefault(Color.red); 
     
            var 
    fp4 = new FunctionParameter("ma1Color"FunctionParameter.COLOR); //color for Moving Average  #1
                
    fp4.setName("MA1-Color"); 
                
    fp4.setDefault(Color.blue); 
     
            var 
    fp5 = new FunctionParameter("ma2Color"FunctionParameter.COLOR); //color for Moving Average  #1
                
    fp5.setName("MA2-Color"); 
                
    fp5.setDefault(Color.yellow); 

            var 
    fp6 = new FunctionParameter("sWidth"FunctionParameter.NUMBER); //for Symbol width
                
    fp6.setName("Symbol-Width"); 
                
    fp6.setDefault(1);

            var 
    fp7 = new FunctionParameter("ma1Width"FunctionParameter.NUMBER);  // plot width for mov ave #1
                
    fp7.setName("MA1-Width"); 
                
    fp7.setDefault(2); 

            var 
    fp8 = new FunctionParameter("ma2Width"FunctionParameter.NUMBER);  // plot width for mov ave #2
                
    fp8.setName("MA2-Width"); 
                
    fp8.setDefault(2);
                
            var 
    fp9 = new FunctionParameter("colorma2"FunctionParameter.STRING);  // color ma2 slope?
                
    fp9.setName("colorMA2?"); 
                
    fp9.setDefault("NO");
                
    fp9.addOption("NO");
                
    fp9.addOption("YES");
                
    fp9.addOption("INVERT"); 

        } else {        
            if (
    vSym != null && vSym != "") { 
                
    setShowTitleParametersfalse );
                
    setStudyTitle(vSym ); 
                
    setCursorLabelName(vSym,0);
                
    setCursorLabelName("MA1 of "+vSym,1);
                
    setCursorLabelName("MA2 of "+vSym,2);
                
    setDefaultBarFgColor(Color.red,0);
                
    setDefaultBarFgColor(Color.blue,1);
                
    setDefaultBarFgColor(Color.yellow,1);
                
    setDefaultBarThickness(2,0);
                
    setDefaultBarThickness(1,1);
                
    setDefaultBarThickness(1,2);
                
    setPlotType(PLOTTYPE_INSTANTCOLORLINE2);  ///
                
            
    } else {
                
    setStudyTitle(" ... Add Symbol");        
                
    setCursorLabelName("Add Symbol");
            }
        }

    }

    var 
    vSum null;
    var 
    vADD null;
    var 
    aADD null;  // set array value to null so it is global and later can be set to variable # periods wanted
    var bEdit true;
    var 
    nSym null;
    var 
    aLength null;
    var 
    vMA1 null//
    var vMA2 null//
    var vMA1old null//
    var vMA2old null;  //
    var vSumTemp;
    var 
    vSum2Temp;


    function 
    main(Symbol1n1Pdsn2Pds,sColor,ma1Color,ma2Color,sWidth,ma1Width,ma2Width,colorma2) {
      
     var 
    aLength Math.max(n1Pdsn2Pds);   
      
        if (
    vLoaded == false) {
            
    cSym getSymbol();
            
            if (
    Symbol1 == null) {
                
    vSym null;
            } else {
                
    vSym Symbol1;
            }
       

                 
            if (
    aADD == null ){      // set array length to periods wanted
                
    aADD = Array(aLength);
            }
            
            
    vLoaded true;
            
            
    preMain();  
        }

        var 
    nState getBarState();   

        if (
    vSym == null) return;

        
    //if (nState == BARSTATE_NEWBAR && vADD != null) {
        
    if (nState == BARSTATE_NEWBAR ) {
            
    aADD.pop();
            
    aADD.unshift(vADD);
            
    vMA1old vMA1;  //
            
    vMA2old vMA2;  //
        
    }
       
        
    vADD close(01vSym);  // get the foreign symbol value
     //   vADD = close(vSym);

       
        
    if (vADD == null) return;
        
    aADD[0] = vADD[0];
     
    //   aADD[0] = vADD;
        
        
    if (aADD[aLength 1] == null) return;
     
     
        if (
    nState == BARSTATE_NEWBAR ) {   // temp sum values for n-1 periods
            
    var x;
            var 
    vSumTemp 0;
            var 
    vSum2Temp 0;
            for(
    x=0;x<n1Pds-1;x++){
                
    vSumTemp += aADD[x];   // calculates the temp sum for all periods but current..Speed up
            
    }    
                
            
    //  for(x=0;x<n2Pds-1;x++){
            //      vSum2Temp += aADD[x];   // calculates the temp sum for second ma for all periods but current...Speed up
            //  }    
        
    }
      
        
    vSum vSumTemp vADD;   // new for speed up
       
        
    vMA1 vSum n1Pds;
     
       
    //debugPrintln(vMA1);
        
    var x;
        var 
    vSum 0;
        var 
    vSum2 0;
     
    // below is what is replace by  the code in the BARSTATE_NEWBAR section just above  
     //   for(x=0;x<n1Pds;x++){
     //       vSum += aADD[x];   // calculates the sum
     //   }
     
     //   vMA1 = vSum / n1Pds;
        
        
    for(x=0;x<n2Pds;x++){
            
    vSum2 += aADD[x];   // calculates the sum for second ma
        
    }
        
        
    vMA2 vSum2 n2Pds;
        
        
    setBarFgColor(sColor0); //the number assigns the color to the first item in the return
        
    setBarFgColor(ma1Color1);
        
    //setBarFgColor(ma2Color, 2);
        
    setBarThickness(sWidth,0);
        
    setBarThickness(ma1Width,1);
        
    setBarThickness(ma2Width,2);
        
          
        
    //color ma2 (long one) based on its slope  

        
    if (colorma2 == "YES") {
            if ( 
    vMA2 vMA2old ) {
                
    setBarFgColor(Color.green,2); 
                }
            if ( 
    vMA2 vMA2old ) {
                
    setBarFgColor(Color.red,2); 
                }
            }
            
            
    // INVERT color of slope if colorma2= INVERT...  use for things like VIX
            
    else if (colorma2 == "INVERT") {    
                if ( 
    vMA2 vMA2old ) {
                    
    setBarFgColor(Color.green,2); 
                }
                if ( 
    vMA2 vMA2old ) {
                    
    setBarFgColor(Color.red,2); 
                }
            
            } else {
            
                
    setBarFgColor(ma2Color2);
                }
        
    //    debugPrintln(aADD);
        
       // if (aADD.length > nPds) aADD.pop();  // clean up code

        
    return new Array (vADD[0], vMA1vMA2);  
    }



    ****************** 

  • #2
    Problem has been solved.

    no further help required.

    bigtee

    Comment

    Working...
    X