Announcement

Collapse
No announcement yet.

studies don't display the same when reloaded

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    Hi Steve,

    So I am facing 2 issues:
    i) I get error msg for below line:

    drawTextAbsolute(7, 70, vLSMA1.getValue(0).toFixed(no_of_digits), Color.white, Color.black,

    Text.BOLD|Text.LEFT|Text.RELATIVETOTOP|Text.RELATI VETORIGHT, null, 10, "vLSMA1Trend");

    The error msg is "TypeError: vLSMA1.getValue(0) has no properties"

    I get this error when:
    a) load the chart on a 30min chart
    b) ok when I first load it on a 5min chart using dynamic time template. But when I try to drag the chart to see the earlier time (and thus the chart needs to reload for earlier data), I get the error even for the 5-min chart

    ==> Would you pls suggest a solution?

    ii) the chart shows different background colors after real-load => I will try to solve by comparing the highest/lowest values instead of the real-time value

    ==> Will try the code out next Monday...
    Last edited by Richard Yu; 03-24-2007, 04:30 AM.

    Comment


    • #17
      Hi Richard,

      After sleeping on it, I believe there are solutions that will give you what you want. First, the error can be resolved by a null check. The reload and lag issues can be readily worked out to your satisfaction as well.

      I will post a followup discussion of the issues later on (next day or so) with code.

      Comment


      • #18
        Hi Richard,

        Many of the issues you are seeing with the errors are due to the intervals you are using and how multiple intervals are loaded. There is a great explanation of how this works at this multiple interval syntax link.

        This link also explains my earlier flip-flop on the returned null values and why at times I was not seeing initial null values.

        I rewrote your efs and included it later in the post. I will re-iterate the guidance I posted as a comment in the efs. Regarding reproducing ‘Live’ results when loading historical data…
        Bottom Line ... do not use bar data from external intervals that are in the process of being formed by using ' xxx.getValue(0)'. Your thoughts comparing the highest/lowest values must take this into account as well. When evaluating External Intervals, Use Completed Bar Data Only by using ' xxx.getValue(-1)' and ' xxx.getValue(-2 etc)' for previous completed bars.
        . I recommend a null check similar to what I performed checking the actual data when examining previous bars even when you use setIntervalsBackfill(true).

        This is not an issue with studies that are based on the native chart interval. This is why I made your first EMA interval equal to your chart interval. This allows you to evaluate the most recent data as it arrives. This eliminates the lag issue you discussed as you see changes without waiting for the completed bar. It also is guaranteed to match your historical data.


        Originally posted by Richard Yu
        Hi Steve,

        So I am facing 2 issues:
        i) I get error msg for below line:
        ...
        ==> Would you pls suggest a solution?

        ii) the chart shows different background colors after real-load => I will try to solve by comparing the highest/lowest values instead of the real-time value

        ==> Will try the code out next Monday...

        Here is the re-written efs. I included all of the trace commands and included an updated trace function along with three tips to use it, use debugPrintln, and to turn it off. This is my tool of choice for troubleshooting code and effectively working through issues like this. The trace commands I have placed throughout the code are not required to run the efs. There is an easy way to disable it in the Tips I included.

        Regarding the error that demarcog noted, this was similar to yours. After dealing with the initial nulls returned from the chart, I was also able to reproduce this on an intermittent basis when the last bar was loaded on the chart. This was not a critical error, and was intermittent, so I added a try…catch statement to handle this contingency.

        I hope this works out for you.


        PHP Code:
        var amLib addLibrary("amStudies.efsLib");
        /* best discussion on working with different timeframes and getting repeatable results
        - [url]http://forum.esignalcentral.com/showthread.php?s=&threadid=14890[/url]
        BOTTOM LINE ... DO NOT USE BAR DATA FROM EXTERNAL INTERVALS THAT ARE 
        IN THE PROCESS OF BEING FORMED BY USING ' xxx.getValue(0)' 
        USE ONLY COMLETED BAR DATA FROM EXTERNAL INTERVALS BY USING
        ' xxx.getValue(-1)'  and  ' xxx.getValue(-2)' for previous completed bars  
        */
        var thisSymbol;
        var 
        symbol;
        var 
        thisInterval;
        var 
        nMALength20;
        var 
        vLSMA_vEMA_Diff_Pct 0.1;
        var 
        bInit false;
        var 
        no_of_digits=4;

        var 
        NormalBgColor Color.RGB(127,127,127);
        var 
        Up_Bg_Color Color.green;
        var 
        Down_Bg_Color Color.RGB(255,128,0);
         
        var 
        EMA_Interval_1 10;
        var 
        EMA_Interval_2 30;
        var 
        EMA_Interval_3 60;
        var 
        EMA1_Color Color.yellow;
        var 
        EMA2_Color Color.cyan;
        var 
        EMA3_Color Color.blue;
        var 
        EMA4_Color Color.fushcia;
        var 
        LSMA_Color Color.black;

        function 
        preMain()
        {
         
        setPriceStudy(true);    
         
        setStudyTitle("test");
         
        /*New in EFS2. This function, setIntervalsBackfill(),  is used in preMain() in conjunction with multiple time-frame studies. 
         Using this function will force the study to use the same number of days as the main chart interval, 
         which will backfill the plot for external interval-based indicator to the beginning of the chart's data.*/
         
         
        setIntervalsBackfill(true);

         
        setCursorLabelName("60m EMA",0);
         
        setDefaultBarThickness(3,0);
         
        setDefaultBarFgColor(EMA3_Color,0);
         
        setCursorLabelName("30m EMA",1);
         
        setDefaultBarThickness(3,1);
         
        setDefaultBarFgColor(EMA2_Color,1);
         
        setCursorLabelName(getInterval()+"min EMA",2);//setCursorLabelName("min EMA",2);
         
        setDefaultBarThickness(4,2);
         
        setDefaultBarFgColor(EMA1_Color,2);
         
        setCursorLabelName(getInterval()+"min LSMA",3);//setCursorLabelName("min LSMA",3);
         
        setDefaultBarThickness(3,3);
         
        setDefaultBarFgColor(LSMA_Color,3);
        }

        var 
        bCount1 1;
        var 
        bCount2 1;
        var 
        bCount3 1;
        var 
        nullCheck false;

        var 
        vEMA1=null;
        var 
        vEMA2=null;
        var 
        vEMA3=null;
        var 
        vLSMA1=null;
        var 
        vLSMA2=null;

        function 
        main()

         
         if(!
        bInit) {
          
        trace("69: bInit = "+bInit);
          
        // see this link ... http://forum.esignalcentral.com/showthread.php?s=&threadid=14890
          
        EMA_Interval_1 getInterval(); // if not set to the default chart interval, any semblance of repeatabilitlity of the chart on reload is lost... see link above
          
        vEMA1 ema(nMALengthinv(EMA_Interval_1));trace("72: vEMA1 = "+vEMA1);
          
        vEMA2 ema(nMALengthinv(EMA_Interval_2));trace("73: vEMA2 = "+vEMA2);
          
        vEMA3 ema(nMALengthinv(EMA_Interval_3));trace("74: vEMA3 = "+vEMA3);
          
        vLSMA1 amLib.amLSMA(nMALengthinv(EMA_Interval_1));trace("75: vLSMA1 = "+vLSMA1);
          
        vLSMA2 amLib.amLSMA(nMALengthinv(EMA_Interval_2));trace("76: vLSMA2 = "+vLSMA2);
          
        bInit=true;
         }

         var 
        nState1 getBarStateInterval(EMA_Interval_1);
         var 
        nState2 getBarStateInterval(EMA_Interval_2);
         var 
        nState3 getBarStateInterval(EMA_Interval_3);

         if (
        nState1 == BARSTATE_NEWBAR) {
          
        bCount1++;
          
        trace("86: EMA_Interval_1 = "+EMA_Interval_1+" new bar, barcount1   = "+bCount1);trace("");
         }

         if (
        nState2 == BARSTATE_NEWBAR) {
          
        bCount2++;
          
        trace("91: EMA_Interval_2 new bar, barcount2   = "+bCount2);trace("");
         }
         
         if (
        nState3 == BARSTATE_NEWBAR) {
          
        bCount3++;
          
        trace("96: EMA_Interval_3 new bar, barcount3   = "+bCount3);trace("");
         }
         
         
         if( 
        getBarState() == BARSTATE_ALLBARS ) {
          
          
        thisInterval getInterval();trace("102: thisInterval = "+thisInterval);
          
        thisSymbol getSymbol();trace("103: thisSymbol = "+thisSymbol);
          
        symbol thisSymbol.substring(02);trace("104: symbol = "+symbol);
          
          switch (
        symbol) {
           case 
        "YM":
             
        no_of_digits=0;  trace("108: symbol = "+symbol+" no_of_digits = "+no_of_digits);
             break;
           case 
        "AB":
           case 
        "YG":
             
        no_of_digits=1;  trace("112: symbol = "+symbol+" no_of_digits = "+no_of_digits);
             break;
           case 
        "NQ":
           
        trace("115: ... symbol = "+symbol+" no_of_digits = "+no_of_digits);
           case 
        "ES":
             
        no_of_digits=2;  trace("117: symbol = "+symbol+" no_of_digits = "+no_of_digits);
             break;
           case 
        "QM":
           
        trace("120: ... symbol = "+symbol+" no_of_digits = "+no_of_digits);
           case 
        "QG":
             
        no_of_digits=3;  trace("122: symbol = "+symbol+" no_of_digits = "+no_of_digits);
             break;
           case 
        "ZE":
             
        no_of_digits=4;  trace("125: symbol = "+symbol+" no_of_digits = "+no_of_digits);
             break;  
           default:
           
        trace("128:... default.... symbol = "+symbol+" no_of_digits = "+no_of_digits);
          }
           
        drawTextAbsolute(470"LS"Color.whiteColor.black,
            
        Text.BOLD|Text.LEFT|Text.RELATIVETOTOP|Text.RELATIVETORIGHTnull10"LSMA1ColorCode");
          
        drawTextAbsolute(485"10"Color.blackEMA1_Color,
            
        Text.BOLD|Text.LEFT|Text.RELATIVETOTOP|Text.RELATIVETORIGHTnull10"EMA1ColorCode");
          
        drawTextAbsolute(4100"30"Color.blackEMA2_Color,
            
        Text.BOLD|Text.LEFT|Text.RELATIVETOTOP|Text.RELATIVETORIGHTnull10"EMA2ColorCode");
          
        drawTextAbsolute(4115"60"Color.blackEMA3_Color,
            
        Text.BOLD|Text.LEFT|Text.RELATIVETOTOP|Text.RELATIVETORIGHTnull10"EMA3ColorCode");
         }
         
         
        trace("140: nullCheck = "+nullCheck+" bInit = "+bInit);
         if(!
        nullCheck){

          
        nullCheck = (vEMA1.getValue(-1)!=null);trace("143: vEMA1.getValue(-1) = "+vEMA1.getValue(-1));
          
        nullCheck*= (vEMA2.getValue(-2)!=null);trace("144: vEMA2.getValue(-2) = "+vEMA2.getValue(-2));
          
        nullCheck*= (vEMA3.getValue(-2)!=null);trace("145: vEMA3.getValue(-2) = "+vEMA3.getValue(-2));
          
        nullCheck*= (vLSMA1.getValue(-1)!=null);trace("146: vLSMA1.getValue(-1) = "+vLSMA1.getValue(-1));
          
        nullCheck*= (vLSMA2.getValue(-2)!=null);trace("147: vLSMA2.getValue(-2) = "+vLSMA2.getValue(-2));
          
        trace("148: nullCheck = "+nullCheck+" bInit = "+bInit);

          if(!
        nullCheck){
           
        //bInit = false;
           
        return;
          }
         }

         
         
        TrendBgColor NormalBgColor

         if ( 
        vEMA1.getValue(0)>=vEMA1.getValue(-1) && vEMA2.getValue(-1)>=vEMA2.getValue(-2) && vLSMA2.getValue(-1)>=vLSMA2.getValue(-2))
         
        TrendBgColor Up_Bg_Color;
         else if ( 
        vEMA1.getValue(0)<=vEMA1.getValue(-1) && vEMA2.getValue(-1)<=vEMA2.getValue(-2) && vLSMA2.getValue(-1)<=vLSMA2.getValue(-2))
         
        TrendBgColor Down_Bg_Color;

         var 
        test1 = new Array();
         var 
        n=0;
         
        test1[n++] = vEMA1.getValue(0);
         
        test1[n++] = vEMA1.getValue(-1);
         
        test1[n++] = vEMA1.getValue(0)>=vEMA1.getValue(-1);
         
        test1[n++] = vEMA2.getValue(-1);
         
        test1[n++] = vEMA2.getValue(-2);
         
        test1[n++] = vEMA2.getValue(-1)>=vEMA2.getValue(-2);
         
        test1[n++] = vLSMA2.getValue(-1);
         
        test1[n++] = vLSMA2.getValue(-2);
         
        test1[n++] = vLSMA2.getValue(-1)>=vLSMA2.getValue(-2);
         var 
        cond1 test1[n++] = ( vEMA1.getValue(0)>=vEMA1.getValue(-1) && vEMA2.getValue(-1)>=vEMA2.getValue(-2) && vLSMA2.getValue(-1)>=vLSMA2.getValue(-2) );
         
        test1[n] = Up_Bg_Color;
         var 
        tmp =comma "";
         var 
        n;
         
         do{
          
        tmp+=(comma test1[k-n]);
          
        comma ", ";
         }while(
        n--);
         
         
        trace("185: conditional 1 = "+cond1+" ... "+tmp);

         var 
        test2 = new Array();
         var 
        n=0;
         
        test2[n++] = vEMA1.getValue(0);
         
        test2[n++] = vEMA1.getValue(-1);
         
        test2[n++] = vEMA1.getValue(0)<=vEMA1.getValue(-1);
         
        test2[n++] = vEMA2.getValue(-1);
         
        test2[n++] = vEMA2.getValue(-2);
         
        test2[n++] = vEMA2.getValue(-1)<=vEMA2.getValue(-2);
         
        test2[n++] = vLSMA2.getValue(-1);
         
        test2[n++] = vLSMA2.getValue(-2);
         
        test2[n++] = vLSMA2.getValue(-1)<=vLSMA2.getValue(-2);
         var 
        cond2 test2[n++] = ( vEMA1.getValue(0)<=vEMA1.getValue(-1) && vEMA2.getValue(-1)<=vEMA2.getValue(-2) && vLSMA2.getValue(-1)<=vLSMA2.getValue(-2) );
         
        test2[n] = Up_Bg_Color;
         var 
        tmp =comma "";
         var 
        n;
         
         do{
          
        tmp+=(comma test2[k-n]);
          
        comma ", ";
         }while(
        n--);
         
         
        trace("208: conditional 2 = "+cond2+" ... "+tmp);
         
        trace("209: TrendBgColor  = "+TrendBgColor );
         
        setDefaultBarBgColor(TrendBgColor);  
         
        //setBarBgColor(TrendBgColor);


          
        trace("214: vLSMA1.getValue(-1)   = "+vLSMA1.getValue(-1)+" vLSMA1.getValue(-1).toFixed(no_of_digits) = "+vLSMA1.getValue(-1).toFixed(no_of_digits));

         try{
          
        drawTextAbsolute(770vLSMA1.getValue(-1).toFixed(no_of_digits), Color.whiteColor.black,
          
        Text.BOLD|Text.LEFT|Text.RELATIVETOTOP|Text.RELATIVETORIGHTnull10"vLSMA1Trend");    
          
        drawTextAbsolute(785vEMA1.getValue(-1).toFixed(no_of_digits), Color.blackColor.white,
          
        Text.BOLD|Text.LEFT|Text.RELATIVETOTOP|Text.RELATIVETORIGHTnull10"EMA1Trend");
          
        drawTextAbsolute(7100vEMA2.getValue(-1).toFixed(no_of_digits), Color.blackColor.white,
          
        Text.BOLD|Text.LEFT|Text.RELATIVETOTOP|Text.RELATIVETORIGHTnull10"EMA2Trend");
          
        drawTextAbsolute(7115vEMA3.getValue(-1).toFixed(no_of_digits), Color.blackColor.white,
          
        Text.BOLD|Text.LEFT|Text.RELATIVETOTOP|Text.RELATIVETORIGHTnull10"EMA3Trend");
         }
         catch(
        e){trace("226: error = "+e);}
         
         
          
         if((
        getBarState() == BARSTATE_NEWBAR)){
           
        trace("231: native chart new bar, barcount1   = "+getCurrentBarCount());trace("");
         }
         
         
        trace("234:"); trace("");

         if (
        thisInterval=="1" || thisInterval=="5" || thisInterval=="10"){ trace("236: thisInterval   = "+thisInterval+" typeof(thisInterval) = "+typeof(thisInterval)); trace("");
           return new Array (
        getSeries(vEMA3), getSeries(vEMA2), vEMA1.getValue(0), vLSMA1.getValue(0)); 
         }
         else if (
        thisInterval=="30"){ trace("239: thisInterval   = "+thisInterval+" typeof(thisInterval) = "+typeof(thisInterval)); trace("");
           return new Array (
        getSeries(vEMA3), getSeries(vEMA2), nullnull);
         }
            
         
        trace("243:"); trace("");
        }


        //########### trace section required to be copied complete ##############

        // TIP1   ... to use debugPrintln instead of the trace function, uncomment the next line and  rename 'function trace' to 'function trace1' 
        //var trace = debugPrintln;  // writing 6500 lines increases execution time in this example by 18 seconds

        // TIP2 ...  to completely disable the trace function to save resources, uncomment the next line and  rename the 'function trace' to 'function trace1' 
        //function trace(){};  // when disabling, this saves 0.2 seconds as compared to using the trace function for 6500 lines, effectively reducing  execution time from 1.2 seconds to 1.0 seconds

        // TIP3 ...  to direct output from debugPrintln to save resources, uncomment the next line
        //debugPrintln = trace;

        var traceon true;// previous tracefile is overwritten if this is true
        var nStudy "Richard.txt";// -  name to be used to name tracefile and button - ensure it is a .txt file to ensure notepad auto opens it..
        var tCount 0;

        var 
        tFile = new File(nStudy);
        function 
        trace(data1){
         switch(
        traceon){
          case (
        true): // script is initializing or re-initializing
           
        traceon false;
           
        tFile.open("wt+");//opens and overwrites previous file
           
        break;
          case (
        false):
         if(!
        tFile.exists()){
                
        tFile.open("wt+");
                
        tFile.close();
         }
           
        tFile.open("at+");
           break;
          default: 
         } 
         
        tFile.writeln(data1);
         
        tFile.flush();
         try{
          
        tButton();//call to the button that allows opening text file
         
        }catch(e){
          
        tFile.writeln("ERROR in trace = "+e);
         }
         
        trace trace1;
        }
        function 
        trace1(data1){
         
        tFile.writeln(data1);
         
        tFile.flush();
         if (
        tCount++>10){
          
        tCount 0;
          try{
           
        tButton();//call to the button that allows opening text file
          
        }catch(e){
           
        tFile.writeln("ERROR in trace1= "+e);
          }
         }
        }

        function 
        tButton(){
         var 
        flags = (Text.RELATIVETOLEFT Text.RELATIVETOBOTTOM|Text.FRAME);
         var 
        nText = (" click here to see the "+nStudy+" file " +( "@URL="+getFormulaOutputRoot()+nStudy));
         
        drawTextPixel(5030nText,  Color.rednullflags"Comic Sans MS"13,"trace99");
        }
        function 
        postMain() {
         if(
        tFile.isOpen())tFile.close();//used for trace utility
        }
        //########## section required to be copied complete ################ 

        Comment


        • #19
          Hi Steve,

          Thank you for your help.

          Comment


          • #20
            Hi Richard,

            You are most welcome.

            Comment

            Working...
            X