Announcement

Collapse
No announcement yet.

Help: Script Stops Plotting at Bar Index -668

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

  • Help: Script Stops Plotting at Bar Index -668

    Hi,

    I copied a sample script from another post, then modified it to help me calculate and plot the differences of two stock's 4-day return.
    It seems to be working. However, the script stops plotting at Bar Index -668.
    I tested with other stocks, the location that the script stops plotting is different for different stocks.

    What did I do wrong?
    Can anyone help me solve this problem? Thanks




    function preMain() {
    setCursorLabelName("Ratio");
    setStudyTitle("Ratio");

    var fp1 = new FunctionParameter("Sym1", FunctionParameter.STRING);
    fp1.setName("Symbol1");
    fp1.setDefault("SPY");

    var fp2 = new FunctionParameter("Sym2", FunctionParameter.STRING);
    fp2.setName("Symbol2");
    fp2.setDefault("QQQ");
    }

    function main(Sym1, Sym2) {

    var vSymbol1 = getValue("Close", 0, Sym1);
    var vSymbol14 = getValue("Close", -4, Sym1);

    var vSymbol2 = getValue("Close", 0, Sym2);
    var vSymbol24 = getValue("Close", -4, Sym2);

    var vRatio1 = (vSymbol1 - vSymbol14)/vSymbol14*100;
    var vRatio2 = (vSymbol2 - vSymbol24)/vSymbol24*100;
    var vRatio = vRatio1 - vRatio2;


    return vRatio;

    }

    Click image for larger version

Name:	Stops_Plotting.png
Views:	1
Size:	73.5 KB
ID:	246821

  • #2
    pisces029
    With regards to the issue you are seeing that is because you have the Time Template set to Dynamic which means that it will initially download enough bars to fill the chart (300 is the default minimum) and request an equivalent amount of data for the external symbols and/or intervals
    If you then scroll back the chart it will request additional data for the symbol in the chart but it does not generate a request of additional data for the external symbols and/or intervals called by the efs hence this does not return a plot prior to a certain bar
    The solution is to set the Time Template to load a predefined number of days (see the related article in the eSignal KnowledgeBase on Time Templates). You could set it to use a specific number of bars but in this case you need to adapt each interval to request an equivalent number of bars [300 bars on a 1 minute interval covers far less time than 300 bars on a 30 minute interval]
    Another [simpler to implement] solution is to add a setIntervalsBackfill(true) statement in preMain() (see the related article in the EFS KnowledgeBase for a description of the function). Keep in mind though that you could be requesting a lot of data in this case
    That said you may also want to rewrite the script using the efs2 rather than the legacy functionality [ie close() instead of getValue()] the main reason being that the efs2 functions maintain the proper alignment between external symbols and/or intervals as they sync by date and time rather than just by bar index (see this article in the EFS KnowledgeBase for the syntax and usage examples of the efs2 close() function)
    Alex


    Originally posted by pisces029 View Post
    Hi,

    I copied a sample script from another post, then modified it to help me calculate and plot the differences of two stock's 4-day return.
    It seems to be working. However, the script stops plotting at Bar Index -668.
    I tested with other stocks, the location that the script stops plotting is different for different stocks.

    What did I do wrong?
    Can anyone help me solve this problem? Thanks




    function preMain() {
    setCursorLabelName("Ratio");
    setStudyTitle("Ratio");

    var fp1 = new FunctionParameter("Sym1", FunctionParameter.STRING);
    fp1.setName("Symbol1");
    fp1.setDefault("SPY");

    var fp2 = new FunctionParameter("Sym2", FunctionParameter.STRING);
    fp2.setName("Symbol2");
    fp2.setDefault("QQQ");
    }

    function main(Sym1, Sym2) {

    var vSymbol1 = getValue("Close", 0, Sym1);
    var vSymbol14 = getValue("Close", -4, Sym1);

    var vSymbol2 = getValue("Close", 0, Sym2);
    var vSymbol24 = getValue("Close", -4, Sym2);

    var vRatio1 = (vSymbol1 - vSymbol14)/vSymbol14*100;
    var vRatio2 = (vSymbol2 - vSymbol24)/vSymbol24*100;
    var vRatio = vRatio1 - vRatio2;


    return vRatio;

    }

    Comment


    • #3
      Alex,

      Thanks for your help. I added a setIntervalsBackfill(true) statement in preMain() and it works!

      Then I rewrote the script using the efs2 as you suggested and there seems to be a problem with my FunctionParameter Objects.

      Can you please help me find my mistake?
      (I'm very sorry to bother you with these beginner questions.)


      function preMain() {

      setCursorLabelName("Ratio");
      setStudyTitle("Ratio");

      setIntervalsBackfill(true);

      var fp1 = new FunctionParameter("Sym1", FunctionParameter.STRING);
      fp1.setName("Symbol1");
      fp1.setDefault("SPY");

      var fp2 = new FunctionParameter("Sym2", FunctionParameter.STRING);
      fp2.setName("Symbol2");
      fp2.setDefault("QQQ");
      }

      function main(Sym1, Sym2) {

      var vSymbol1 = close(0, sym("Sym1") );
      var vSymbol14 = close(-4, sym("Sym1") );

      var vSymbol2 = close(0, sym("Sym2") );
      var vSymbol24 = close(-4, sym("Sym2") );

      var vRatio1 = (vSymbol1 - vSymbol14)/vSymbol14*100;
      var vRatio2 = (vSymbol2 - vSymbol24)/vSymbol24*100;
      var vRatio = vRatio1 - vRatio2;


      return vRatio;

      }

      Comment


      • #4
        pisces029
        Replace each instance of close(X, sym("SymX")) with close(X, sym(SymX))
        You need to pass the parameter [which is itself a string of the symbol name] else you are passing symbols named Sym1 or Sym2 which are invalid as indicated by the error message in the Formula Output window
        Alex


        Originally posted by pisces029 View Post
        Alex,

        Thanks for your help. I added a setIntervalsBackfill(true) statement in preMain() and it works!

        Then I rewrote the script using the efs2 as you suggested and there seems to be a problem with my FunctionParameter Objects.

        Can you please help me find my mistake?
        (I'm very sorry to bother you with these beginner questions.)


        function preMain() {

        setCursorLabelName("Ratio");
        setStudyTitle("Ratio");

        setIntervalsBackfill(true);

        var fp1 = new FunctionParameter("Sym1", FunctionParameter.STRING);
        fp1.setName("Symbol1");
        fp1.setDefault("SPY");

        var fp2 = new FunctionParameter("Sym2", FunctionParameter.STRING);
        fp2.setName("Symbol2");
        fp2.setDefault("QQQ");
        }

        function main(Sym1, Sym2) {

        var vSymbol1 = close(0, sym("Sym1") );
        var vSymbol14 = close(-4, sym("Sym1") );

        var vSymbol2 = close(0, sym("Sym2") );
        var vSymbol24 = close(-4, sym("Sym2") );

        var vRatio1 = (vSymbol1 - vSymbol14)/vSymbol14*100;
        var vRatio2 = (vSymbol2 - vSymbol24)/vSymbol24*100;
        var vRatio = vRatio1 - vRatio2;


        return vRatio;

        }

        Comment

        Working...
        X