Announcement

Collapse
No announcement yet.

using getGlobalValue to insure var is the same

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

  • using getGlobalValue to insure var is the same

    I have 2 scripts. The 2nd script (in study pane) is using values from the 1st (in price pane) and I want to make sure the parameters are the same.

    The script test2 uses efsExternal to call test1. I am using getGlobalValue to retrieve the parameter from test1 and use it in test2.

    For example: test1 plots an SMA of nLen. Test2 tries to retrieve the nLen value via getGlobalValue("gMALen"). If the value of nLen changes (in test1) then test2 should be reloaded with the new nLen.

    The problem is the study in test2 is not using the correct value for nLen. The var x0 is getting the proper value from the Global but that value is not being used by the efsExternal call.

    PHP Code:
    // test1.efs plots an SMA on the price pane
    var xStudy null;
    var 
    bInit false;
    var 
    aFPArray = new Array();

    function 
    preMain() {
        
    setPriceStudy(true);
        
    setCursorLabelName("MA"0)
        
    x=0;
        
    aFPArray[x] = new FunctionParameter("nLen"FunctionParameter.NUMBER);
        
    withaFPArray[x] ) {
            
    setName("MA length");
            
    setLowerLimit(1);        
            
    setDefault(10);  
        }    
    }

    function 
    main(nLen) {
        if(
    bInit == false){
            
    setGlobalValue"gMALen"nLen ); 
            
    xStudy sma(nLen)
            
    bInit true;
        }

        var 
    nMA xStudy.getValue(0
        return ( 
    nMA );

    PHP Code:
    // test2.efs uses an SMA to create a study
    // the SMA here should be of the same length as 
    // the SMA being plotted by test1 

    debugClear();
    var 
    xStudy null;
    var 
    bInit false;
    var 
    x1 1;
    var 
    x0 1;
    var 
    nState;

    function 
    preMain() {
        
    setPriceStudy(false);    
    }

    function 
    main() {       
        
    nState getBarState();
        if (
    nState == BARSTATE_NEWBAR) {
            
    x1 x0;
            
    x0 getGlobalValue"gMALen" ); 
            
    debugPrintln("old="+x1+"  new="+x0);
            if (
    x1 != x0) { reloadEFS(); }

        }
        if(
    bInit == false){
            
    xStudy efsExternal("test1.efs"x0)
            
    bInit true;
        }

        
    // do stuff with xStudy values

        
    var myPlot xStudy.getValue(0);
        
    setBarBgColor(Color.RGB(220,220,220));
       
        return (
    myPlot);


  • #2
    Hi pj909,

    As I remember, on a reloadEFS() command, variables declared outside main() are not reinitialized, but retain their last value. e.g. once bInit is set to true, it will remain so ,unless you reset to false.

    The best place to do this is in your conditional that contains reloadEFS(), but prior to the reload command.

    Hope this helps.

    Steve

    Comment


    • #3
      Thanks Steve

      Adding bInit = false before the reload seems to work. But there are a few bars where the MA is different from the result being shown in the indicator pane. Different by a penny and not every bar. Very strange - there must be some setting I need to make the same but I don't know what it would be.

      Some additional observations: If test1 is loaded after test2, test2 will not update until NEWBAR. In order to have test2 start with the value from test1 it must be loaded 2nd and the assigment of both the global var and the series needs to be duplicated at ALLBARS. As in:
      PHP Code:
          if (nState == BARSTATE_ALLBARS) {   // to get the current value in the other efs
              
      x0 getGlobalValue"gMALen" );
              
      xStudy efsExternal("test1.efs"x0)
          }
          
          if(
      bInit == false){
              
      xStudy efsExternal("test1.efs"x0)
              
      bInit true;
          }
          
          if (
      nState == BARSTATE_NEWBAR) {  // check and see if user changed nLen in the other efs
              
      x1 x0;
              
      x0 getGlobalValue"gMALen" ); 
              if (
      x1 != x0) {
                  
      bInit false
                  
      reloadEFS();
              }
          } 

      PJ

      Comment

      Working...
      X