Announcement

Collapse
No announcement yet.

Multiple time periods

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

  • Multiple time periods

    I load the following EFS on a chart of ym z8 on a 20t period. After several minuts if I reload the same EFS the line and values move as if it is calulating different prior values. Is there an error in the script? It should be the difference between two simple moving averages calculated on a 1 minute basis plotted on a 20t chart.

    Best Regards,

    Alan


    setPriceStudy(false);
    setStudyTitle(" Logic 96b");
    setCursorLabelName("MA1", 0);
    setDefaultBarStyle(PS_SOLID, 0);
    setDefaultBarStyle(PS_SOLID, 1);
    setDefaultBarStyle(PS_SOLID, 2);
    setDefaultBarStyle(PS_SOLID, 3);
    setDefaultBarFgColor(Color.blue, 0);
    setDefaultBarFgColor(Color.black, 1);
    setDefaultBarFgColor(Color.black, 2);
    setDefaultBarFgColor(Color.black, 3);
    setDefaultBarThickness(3, 0);
    setDefaultBarThickness(1, 1);
    setDefaultBarThickness(1, 2);
    setDefaultBarThickness(1, 3);
    setPlotType(PLOTTYPE_LINE, 0);
    setPlotType(PLOTTYPE_LINE, 1);
    setPlotType(PLOTTYPE_LINE, 2);
    setPlotType(PLOTTYPE_LINE, 3);

    Symbol1 = getSymbol();
    Interval1 = "1";
    }

    function main() {

    TT3 = 0;
    WW3 = 25;
    QQ3 = -25;

    var vSymbol1 = Symbol1+","+Interval1;

    if (bInit == false) {
    xMA2 = eval(sma)(3,eval(high)(sym(vSymbol1)));
    xMA4 = eval(sma)(15,eval(high)(sym(vSymbol1)));
    }

    if(getBarStateInterval("1")==BARSTATE_NEWBAR){
    MA2 = xMA2.getValue(0);
    MA4 = xMA4.getValue(0);
    }

    wer = MA2 - MA4;

    return new Array (wer, TT3, QQ3, WW3);
    }

  • #2
    Re: Multiple time periods

    Alan
    That is happening because in real time you are retrieving the current values of the averages on the first tick of a new bar ie when it just formed.
    When you instead reload the efs it will retrieve the values of the averages based on completed historical bars which are going to be different than those retrieved in real time when those bars were just formed.
    Replace the following lines of code
    PHP Code:
    if(getBarStateInterval("1")==BARSTATE_NEWBAR){
    MA2 xMA2.getValue(0);
    MA4 xMA4.getValue(0);

    with the following
    PHP Code:
    if(getBarStateInterval("1")==BARSTATE_NEWBAR){
    MA2 xMA2.getValue(-1);
    MA4 xMA4.getValue(-1);

    and the plot created in real time should match that after a reload. This is because at the beginning of a new external interval you will now be retrieving the values of the averages at the closing of the prior bar
    Alex


    Originally posted by Alan2004
    I load the following EFS on a chart of ym z8 on a 20t period. After several minuts if I reload the same EFS the line and values move as if it is calulating different prior values. Is there an error in the script? It should be the difference between two simple moving averages calculated on a 1 minute basis plotted on a 20t chart.

    Best Regards,

    Alan


    setPriceStudy(false);
    setStudyTitle(" Logic 96b");
    setCursorLabelName("MA1", 0);
    setDefaultBarStyle(PS_SOLID, 0);
    setDefaultBarStyle(PS_SOLID, 1);
    setDefaultBarStyle(PS_SOLID, 2);
    setDefaultBarStyle(PS_SOLID, 3);
    setDefaultBarFgColor(Color.blue, 0);
    setDefaultBarFgColor(Color.black, 1);
    setDefaultBarFgColor(Color.black, 2);
    setDefaultBarFgColor(Color.black, 3);
    setDefaultBarThickness(3, 0);
    setDefaultBarThickness(1, 1);
    setDefaultBarThickness(1, 2);
    setDefaultBarThickness(1, 3);
    setPlotType(PLOTTYPE_LINE, 0);
    setPlotType(PLOTTYPE_LINE, 1);
    setPlotType(PLOTTYPE_LINE, 2);
    setPlotType(PLOTTYPE_LINE, 3);

    Symbol1 = getSymbol();
    Interval1 = "1";
    }

    function main() {

    TT3 = 0;
    WW3 = 25;
    QQ3 = -25;

    var vSymbol1 = Symbol1+","+Interval1;

    if (bInit == false) {
    xMA2 = eval(sma)(3,eval(high)(sym(vSymbol1)));
    xMA4 = eval(sma)(15,eval(high)(sym(vSymbol1)));
    }

    if(getBarStateInterval("1")==BARSTATE_NEWBAR){
    MA2 = xMA2.getValue(0);
    MA4 = xMA4.getValue(0);
    }

    wer = MA2 - MA4;

    return new Array (wer, TT3, QQ3, WW3);
    }

    Comment


    • #3
      Alex,

      Thank you for the suggestion. I will test it tonight.

      I want to bring to your attention a strange occurance. I was reading my emails when your responce was received on email. I looked for several minutes at the responce because the change from and to codes were identical. I then went to the forum and noticed the suggested code had a "-1" inside the () versus the "0" in the email. The email is attached for your review.

      Best Regards,

      Alan


      Hello Alan2004,

      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      https://forum.esignal.com/node/29359

      Alexis C. Montenegro has just replied.

      Here are the contents of the post:
      [quote]
      Alan
      That is happening because you are retrieving the current value of the averages at the beginning of a new bar.
      When you instead reload the efs it will retrieve the values of the averages based on completed historical bars which are going to be different than those retrieved in real time when those bars were just formed.
      Replace the following lines of code
      PHP Code:
      if(getBarStateInterval("1")==BARSTATE_NEWBAR){
      MA2 xMA2.getValue(0);
      MA4 xMA4.getValue(0);

      with the following
      PHP Code:
      if(getBarStateInterval("1")==BARSTATE_NEWBAR){
      MA2 xMA2.getValue(0);
      MA4 xMA4.getValue(0);

      and the plot created in real time will match that after a reload. This is because at the beginning of a new external interval you will now be retrieving the values of the averages at the closing of the prior bar Alex

      Comment


      • #4
        Alan
        Actually that was not a strange occurrence. After I posted I noticed that I had omitted to change the 0 to -1 so I reposted the same exact message with the new values and deleted the prior message
        Alex

        Comment


        • #5
          I tested the (-1) option and found there is aproximately a 1 minute lag betwene running the code on a one minute chart and on a 20t chart. I am trying to get the cross over from positive to negative to be the same time on both charts. Is this possible?

          If not, is there a way to automatically reload the efs on the 20t chart ever time the 1 minute calculation is finished so only the last line is incorrect until it is realoaded?

          Best Regards,

          Alan

          Comment


          • #6
            Alan
            The lag is due to the fact that you are now retrieving the values of the averages at the prior bar (in effect displacing them forward by one bar). The reason I suggested this change was to correct the logic error you had in your formula where you were retrieving the current values at BARSTATE_NEWBAR hence causing a discrepancy between the plots returned in real time and on a reload.
            If instead you want to calculate the averages [and the difference between them] in real time then you need to retieve the values of the averages and perform your calculations on every tick and not at BARSTATE_NEWBAR. In addition this needs to be done in a separate function or efs that you call from your main function using the efsInternal() or efsExternal() functions and to which you pass the external symbol using the sym() function (see the basic example in the image enclosed below).
            For the description and syntax of the efsInternal() and efsExternal() functions see the Function Reference in the EFS KnowledgeBase. Also run a search through this forum as you will find many other examples of their use.
            Alex




            Originally posted by Alan2004
            I tested the (-1) option and found there is aproximately a 1 minute lag betwene running the code on a one minute chart and on a 20t chart. I am trying to get the cross over from positive to negative to be the same time on both charts. Is this possible?

            If not, is there a way to automatically reload the efs on the 20t chart ever time the 1 minute calculation is finished so only the last line is incorrect until it is realoaded?

            Best Regards,

            Alan

            Comment


            • #7
              Alex,

              I have applied your suggestions to the following EFS. As I scroll through the charts, I get the exact same values in both the 1 minute and 20t charts as displayed in the right hand margin. The 20t values do not change when the 20t efs is reloaded which is good.

              Is this the most effecient way for the code to execute? Any other suggestions for improvements?

              Best Regards,

              Alan



              var vSymbol1 = null;
              var meVar = null;
              var myAverage1 = null;
              var myAverage2 = null;

              function preMain() {

              setPriceStudy(false);
              setStudyTitle(" Logic 96bbbnew");
              setCursorLabelName("MA1", 0);
              setDefaultBarStyle(PS_SOLID, 0);
              setDefaultBarStyle(PS_SOLID, 1);
              setDefaultBarStyle(PS_SOLID, 2);
              setDefaultBarStyle(PS_SOLID, 3);
              setDefaultBarFgColor(Color.blue, 0);
              setDefaultBarFgColor(Color.black, 1);
              setDefaultBarFgColor(Color.black, 2);
              setDefaultBarFgColor(Color.black, 3);
              setDefaultBarThickness(3, 0);
              setDefaultBarThickness(1, 1);
              setDefaultBarThickness(1, 2);
              setDefaultBarThickness(1, 3);
              setPlotType(PLOTTYPE_LINE, 0);
              setPlotType(PLOTTYPE_LINE, 1);
              setPlotType(PLOTTYPE_LINE, 2);
              setPlotType(PLOTTYPE_LINE, 3);

              Symbol1 = getSymbol();
              Interval1 = "1";
              }

              function main() {

              var vSymbol1 = Symbol1+","+Interval1;

              var meVar = efsInternal("myFunction",sym(vSymbol1));

              TT3 = 0;
              WW3 = 20;
              QQ3 = -20;

              return new Array (meVar, TT3, QQ3, WW3);
              }

              function myFunction(){
              var myAverage1 = sma(3,high())
              var myAverage2 = sma(15,high())
              return myAverage1.getValue(0)-myAverage2.getValue(0)
              }

              Comment


              • #8
                Alan
                That was a basic example to illustrate the logic required to accomplish what you wanted. To make it more efficient you may now want to add the necessary initialization routines such as the one Jason showed you in another thread (you can find it by searching through your own posts).
                At this point I believe you should have sufficient information and examples available to you to modify or enhance your efs to suit your requirements
                Alex


                Originally posted by Alan2004
                Alex,

                I have applied your suggestions to the following EFS. As I scroll through the charts, I get the exact same values in both the 1 minute and 20t charts as displayed in the right hand margin. The 20t values do not change when the 20t efs is reloaded which is good.

                Is this the most effecient way for the code to execute? Any other suggestions for improvements?

                Best Regards,

                Alan



                var vSymbol1 = null;
                var meVar = null;
                var myAverage1 = null;
                var myAverage2 = null;

                function preMain() {

                setPriceStudy(false);
                setStudyTitle(" Logic 96bbbnew");
                setCursorLabelName("MA1", 0);
                setDefaultBarStyle(PS_SOLID, 0);
                setDefaultBarStyle(PS_SOLID, 1);
                setDefaultBarStyle(PS_SOLID, 2);
                setDefaultBarStyle(PS_SOLID, 3);
                setDefaultBarFgColor(Color.blue, 0);
                setDefaultBarFgColor(Color.black, 1);
                setDefaultBarFgColor(Color.black, 2);
                setDefaultBarFgColor(Color.black, 3);
                setDefaultBarThickness(3, 0);
                setDefaultBarThickness(1, 1);
                setDefaultBarThickness(1, 2);
                setDefaultBarThickness(1, 3);
                setPlotType(PLOTTYPE_LINE, 0);
                setPlotType(PLOTTYPE_LINE, 1);
                setPlotType(PLOTTYPE_LINE, 2);
                setPlotType(PLOTTYPE_LINE, 3);

                Symbol1 = getSymbol();
                Interval1 = "1";
                }

                function main() {

                var vSymbol1 = Symbol1+","+Interval1;

                var meVar = efsInternal("myFunction",sym(vSymbol1));

                TT3 = 0;
                WW3 = 20;
                QQ3 = -20;

                return new Array (meVar, TT3, QQ3, WW3);
                }

                function myFunction(){
                var myAverage1 = sma(3,high())
                var myAverage2 = sma(15,high())
                return myAverage1.getValue(0)-myAverage2.getValue(0)
                }

                Comment

                Working...
                X