Announcement

Collapse
No announcement yet.

Crossover

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

  • Crossover

    Good Morning,

    Why does the following test code correctly produce a solid thick line as per the logic when applied to a 1 min chart while the thick line oscillates between thick and thin during the same period when applied to a 50 tick chart?

    Best Regards,

    Alan


    PHP Code:
    var xMA1 null;
    var 
    xMA2 null;
    var 
    xMA3 null;
    var 
    xMA4 null;
    var 
    aa1 null;
    var 
    aa11 null;
    var 
    ww =  null;
    var 
    pp null;
    var 
    qq null;

    function 
    preMain() {
        
        
    setPriceStudy(false);
        
    setStudyTitle("CROSSOVER");
        
    setCursorLabelName("MA1"0);
        
    setDefaultBarStyle(PS_SOLID0);
        
    setDefaultBarStyle(PS_SOLID1);
        
    setDefaultBarStyle(PS_SOLID2);
        
    setDefaultBarStyle(PS_SOLID3);
        
    setDefaultBarStyle(PS_SOLID4);
        
    setDefaultBarFgColor(Color.red0);
        
    setDefaultBarFgColor(Color.blue1);
        
    setDefaultBarFgColor(Color.black2);
        
    setDefaultBarFgColor(Color.black3);
        
    setDefaultBarFgColor(Color.black4);
        
    setDefaultBarThickness(10);
        
    setDefaultBarThickness(11);
        
    setDefaultBarThickness(32);
        
    setDefaultBarThickness(13);
        
    setDefaultBarThickness(14);
        
    setPlotType(PLOTTYPE_LINE0);
        
    setPlotType(PLOTTYPE_LINE1);
        
    setPlotType(PLOTTYPE_LINE2);
        
    setPlotType(PLOTTYPE_LINE3);
        
    setPlotType(PLOTTYPE_LINE4);
        
        
    Symbol1 "YM U6";
        
    Interval1 "1";
    }

    function 
    main() {

    if(
    getBarStateInterval("1")==BARSTATE_NEWBAR){

    var 
    vSymbol1 Symbol1+","+Interval1;
    xMA1 offsetSeries(eval(sma)(3,eval(low)(sym(vSymbol1))),0);
    xMA2 offsetSeries(eval(sma)(3,eval(high)(sym(vSymbol1))),0);
    xMA3 offsetSeries(eval(sma)(15,eval(low)(sym(vSymbol1))),0);
    xMA4 offsetSeries(eval(sma)(15,eval(high)(sym(vSymbol1))),0);
        
    var 
    MA1 xMA1.getValue(0);
    var 
    MA2 xMA2.getValue(0);
    var 
    MA3 xMA3.getValue(0);
    var 
    MA4 xMA4.getValue(0);

    pp = -1;
    qq 1;
    ww 0;

    if(
    MA1 >= MA3) {
                
    aa1 0.5;
                
    setBarThickness(2,0);


    if((
    MA1 >= MA4)) {
                
    setBarThickness(6,0);


    if(
    MA3 MA1) {
                
    setBarThickness(1,0);
                
    aa1 0;


    if(
    MA4 >= MA2) {
                
    aa11 = -0.5;
                
    setBarThickness(2,1);

     
    if(
    MA3 >= MA2) {
                
    aa11 = -0.5;
                
    setBarThickness(6,1);


    if(
    MA2 MA4) {
                
    aa11 0;
                
    setBarThickness(1,1);
    }

    }

        return new Array (
    aa1aa11wwppqq);   


  • #2
    Hello Alan,

    First let me suggest an alternative to your series initializations. You may find the code below to be more efficient. Since you were not specifying an offset value for the offsetSeries() function, you could remove it.



    As for your line thicknesses on a 50T chart, your code is doing what it is supposed to. The problem is that you are comparing a minute based interval to a tick-based interval. This is comparing apples to oranges, which will not be able to produce a consistent result that you are expecting. To see which conditions are evaluating to true you can place some debugPrintln() statements inside the conditions. Print out the current bar index with getCurrentBarIndex() and the values of the MA studies. You could also modify your return statement to just plot the MA studies so you can get a better visual of when they are crossing.

    Another thing to consider is that your conditions are all evaluated independently in succession. Therefore, the last one to evaluate to true will be the thickness set for the 0 and 1 return indexes, respectively.

    When working with multiple time-frame studies you should stick with like interval types. Compare minute to minute or tick to tick, for example. The reason a comparison of minute to tick interval studies produces the type of result you are seeing with this formula is because there is no consistency between the amount of time each 50T bar covers whereas a minute based interval does contain consistent time spans for each bar. For example, on a 50T bar that covers 3 minutes of time, there will be two of the 1-minute bars that will not be seen by your formula. On a 50T bar that covers 10 seconds, some number of bars before and/or after it would be matched up to the same 1-minute bar, which essentially would repeat the same 1-minute value for multiple bars on the 50T chart.
    Jason K.
    Project Manager
    eSignal - an Interactive Data company

    EFS KnowledgeBase
    JavaScript for EFS Video Series
    EFS Beginner Tutorial Series
    EFS Glossary
    Custom EFS Development Policy

    New User Orientation

    Comment


    • #3
      Jason;

      My objective is to calculate a minute based value inside a tick based efs so the logic in the tick efs can evaluate both conditions. This eliminates having to transfer values between charts.

      After reviewing the results of the minute based calculations in the tick efs via data export, the problem is the <null> value returned when there are more than 1 tick bars per minute. When this occurs, the bar is added to the tick chart but no moving average is plotted. When the next tick bar triggers the minute calculation, the line plotted on the chart is extended to the current bar. The <null> value impacts the evaluation logic which theats it as 0.

      Is there a simple way in the sample code I posted to get the value for the moving average for MA1(-1) or MA1(-2) so I could assign a number to the current MA1(0) to eliminate the <null> value?

      Thanks again for the help.

      Best Regards,

      Alan

      Comment


      • #4
        Hello Alan,

        There isn't a method for inserting a bar element into the sma Series. However, what should work for you in this case is to create a global variable that corresponds to each of your MA1, MA2, MA3 and MA4 local variables, which also need to be changed to globals. Name the new ones something like MA1_last etc. Just before your .getValue() assignments set the _last globals to the current value of the MA1, 2, 3 and 4 variables, but only if they are not null. Then just after you perform the .getValue() assignments check to see if they are null and if so reassign them to the _last variables, which will contain a valid number. If the most recent .getValue() assignment is not null, that will be the value used for the current tick bar. Otherwise, it would be assigned to the last known valid number.

        PHP Code:

        // before main
        var MA1 null;
        var 
        MA2 null;
        var 
        MA3 null;
        var 
        MA4 null;

        var 
        MA1_last null;
        var 
        MA2_last null;
        var 
        MA3_last null;
        var 
        MA4_last null;

        function 
        main() { 
        ...
        ...

        if (
        MA1 != nullMA1_last MA1;
        if (
        MA2 != nullMA2_last MA2;
        if (
        MA3 != nullMA3_last MA3;
        if (
        MA4 != nullMA4_last MA4;

        MA1 xMA1.getValue(0);
        MA2 xMA2.getValue(0);
        MA3 xMA3.getValue(0);
        MA4 xMA4.getValue(0);

        if (
        MA1 == nullMA1 MA1_last;
        if (
        MA2 == nullMA2 MA2_last;
        if (
        MA3 == nullMA3 MA3_last;
        if (
        MA4 == nullMA4 MA4_last;

        if (
        MA1 == null || MA2 == null || MA3 == null || MA4 == null) return;

        ...   
        ...


        Jason K.
        Project Manager
        eSignal - an Interactive Data company

        EFS KnowledgeBase
        JavaScript for EFS Video Series
        EFS Beginner Tutorial Series
        EFS Glossary
        Custom EFS Development Policy

        New User Orientation

        Comment


        • #5
          Jason,

          Thank you for the detailed guidence. I have modified the sample code I posted and determined via the data export that the <NULL> have been replaced with the previous average. When the <NULL> is removed, the logic correctly determines if aa1 should be 0.5 or 1.5. When it plots 1.5 using the replacement value, it does not execute the line thickness command. The only place a thickness or one is specified is in the initialization. When I change this value, the thickness of the line where the null was removed changes to the new initialized thickness further indicating it is not executing the line command in the logic area. What is causing this issue?

          Thank you for the assitance.

          Best Regards,

          Alan

          PHP Code:
          var MA1 null;
          var 
          MA2 null;
          var 
          MA3 null;
          var 
          MA4 null;

          var 
          MA1_last null;
          var 
          MA2_last null;
          var 
          MA3_last null;
          var 
          MA4_last null;

          var 
          xMA1 null;
          var 
          xMA2 null;
          var 
          xMA3 null;
          var 
          xMA4 null;

          var 
          aa1 null;
          var 
          aa11 null;
          var 
          ww =  null;
          var 
          pp null;
          var 
          qq null;

          function 
          preMain() {
              
              
          setPriceStudy(false);
              
          setStudyTitle("CROSSOVER1Minfor20T1xxx");
              
          setCursorLabelName("MA1"0);
              
          setDefaultBarStyle(PS_SOLID0);
              
          setDefaultBarStyle(PS_SOLID1);
              
          setDefaultBarStyle(PS_SOLID2);
              
          setDefaultBarStyle(PS_SOLID3);
              
          setDefaultBarStyle(PS_SOLID4);
              
          setDefaultBarFgColor(Color.red0);
              
          setDefaultBarFgColor(Color.blue1);
              
          setDefaultBarFgColor(Color.black2);
              
          setDefaultBarFgColor(Color.black3);
              
          setDefaultBarFgColor(Color.black4);
              
          setDefaultBarThickness(10);
              
          setDefaultBarThickness(11);
              
          setDefaultBarThickness(12);
              
          setDefaultBarThickness(13);
              
          setDefaultBarThickness(14);
              
          setPlotType(PLOTTYPE_LINE0);
              
          setPlotType(PLOTTYPE_LINE1);
              
          setPlotType(PLOTTYPE_LINE2);
              
          setPlotType(PLOTTYPE_LINE3);
              
          setPlotType(PLOTTYPE_LINE4);
              
              
          Symbol1 "YM U6";
              
          Interval1 "1";
          }

          function 
          main() {

          if(
          getBarStateInterval("1")==BARSTATE_NEWBAR){

          var 
          vSymbol1 Symbol1+","+Interval1;

          xMA1 sma(3,low(sym(vSymbol1)));
          xMA2 sma(3,high(sym(vSymbol1)));
          xMA3 sma(15,low(sym(vSymbol1)));
          xMA4 sma(15,high(sym(vSymbol1)));

          if (
          MA1 != nullMA1_last MA1;
          if (
          MA2 != nullMA2_last MA2;
          if (
          MA3 != nullMA3_last MA3;
          if (
          MA4 != nullMA4_last MA4;

          MA1 xMA1.getValue(0);
          MA2 xMA2.getValue(0);
          MA3 xMA3.getValue(0);
          MA4 xMA4.getValue(0);

          if (
          MA1 == nullMA1 MA1_last;
          if (
          MA2 == nullMA2 MA2_last;
          if (
          MA3 == nullMA3 MA3_last;
          if (
          MA4 == nullMA4 MA4_last;

          pp = -1;
          qq 1;
          ww 0;

          if(
          MA1 >= MA3) {
                    
          aa1 0.5;
                    
          setBarThickness(2,0);


          if((
          MA1 >= MA4)) {
                     
          aa1 1.5;
                     
          setBarThickness(6,0);
          }

          if(
          MA3 MA1) {
                   
          aa1 0;
                   
          setBarThickness(1,0);


          if(
          MA4 >= MA2) {
                    
          aa11 = -0.5;
                    
          setBarThickness(2,1);

           
          if(
          MA3 >= MA2) {
                    
          aa11 = -1.5;
                    
          setBarThickness(6,1);


          if(
          MA2 MA4) {
                   
          aa11 0;
                   
          setBarThickness(1,1);
          }

          }

          return new Array (
          aa1aa11wwppqq);   

          Comment


          • #6
            Hello Alan,

            You have two conditions in main() that can set the bar thickness to 1 for both return index 0 and 1. If none of your conditions evaluate to true, the lines will have a thickness of 1 because of your defaults in preMain(). If you want the thickness to persist for bars that follow, even if none of the conditions are true on the bars that follow, then use setDefaultBarThickness() within your conditions in main().

            PHP Code:
            if(MA3 MA1) {
                     
            aa1 0;
                     
            setBarThickness(1,0);

            and

            PHP Code:
            if(MA2 MA4) {
                     
            aa11 0;
                     
            setBarThickness(1,1);

            These conditions are evaluating to true quite often, which will override the other thickness values because these two conditions are evaluated after the others that can set the thickness to 2 or 6.

            To help you see when each of the conditions are executing, add the following debugPrintln() statements to your code and view the results in the formula output window.

            PHP Code:
            ...
                    if(
            MA1 >= MA3) {
                              
            aa1 0.5;
                              
            setBarThickness(2,0);
                             
            debugPrintln("bar " getCurrentBarIndex() + " ID:0  set bar thickness to 2");
                    } 
                    
                    if((
            MA1 >= MA4)) {
                               
            aa1 1.5;
                               
            setBarThickness(6,0);
                             
            debugPrintln("bar " getCurrentBarIndex() + " ID:0  set bar thickness to 6");
                    }
                    
                    if(
            MA3 MA1) {
                             
            aa1 0;
                             
            setBarThickness(1,0);
                             
            debugPrintln("bar " getCurrentBarIndex() + " ID:0  set bar thickness to 1");
                    } 
                    
                    if(
            MA4 >= MA2) {
                              
            aa11 = -0.5;
                              
            setBarThickness(2,1);
                             
            debugPrintln("bar " getCurrentBarIndex() + " ID:1  set bar thickness to 2");
                    } 
                     
                    if(
            MA3 >= MA2) {
                              
            aa11 = -1.5;
                              
            setBarThickness(6,1);
                             
            debugPrintln("bar " getCurrentBarIndex() + " ID:1  set bar thickness to 6");
                    } 
                    
                    if(
            MA2 MA4) {
                             
            aa11 0;
                             
            setBarThickness(1,1);
                             
            debugPrintln("bar " getCurrentBarIndex() + " ID:1  set bar thickness to 1");
                    } 
            Jason K.
            Project Manager
            eSignal - an Interactive Data company

            EFS KnowledgeBase
            JavaScript for EFS Video Series
            EFS Beginner Tutorial Series
            EFS Glossary
            Custom EFS Development Policy

            New User Orientation

            Comment


            • #7
              Jason,

              Thank you for the review. I have tested the code I posted on a 1 minute chart with out the getBarStateInterval (GBSI) command. The logic for the line thickness calculates correctly as demonstrated in the attached file.

              When I apply the same code using the GBSI command as posted on a 60T ym u6 chart, the line thicknesses are changed to the default thickness of 1 as mentioned in my prior post as indicated in my next post. (I could not fit both onto one post due to size.)

              When I move the logic section outside of the GBSI command I get the bottom plot on the second post where the line thicknesses are computed correctly and match the 1 minute chart in this post.

              Line vaules of 0, 0.5 and 1.5 are all caluclated the same in the 3 plots. It is only the line thicknesses which change. Please note that the line thicknesses change to 1 (default) when the values are 0.5 and 1.5 on the line corresponding to the bars where the values for <NULL> were changed to the prior value and verified as such via data export.

              If this is correct, are there any other commands which must be placed outside of the GBSI command to preform correctly?

              Thank you for your assistance.

              Best Regards,

              Alan
              Attached Files

              Comment


              • #8
                Jason,

                This is the second post referenced in the preceeding post.

                Alan
                Attached Files

                Comment


                • #9
                  Hello Alan,

                  With your code inside the check for getBarStateInterval(), did you replace the setBarThickness() calls with setDefaultBarThickness()? This will allow the thickness of the lines to persist for the bars in between instances where the newbar state from getBarStateInterval() occurs, which is when any new line thicknesses would be set.
                  Jason K.
                  Project Manager
                  eSignal - an Interactive Data company

                  EFS KnowledgeBase
                  JavaScript for EFS Video Series
                  EFS Beginner Tutorial Series
                  EFS Glossary
                  Custom EFS Development Policy

                  New User Orientation

                  Comment


                  • #10
                    Jason,

                    I tested the setDefaultBarThickness in the logic I posted and it works great when the logic is inside the getBarStateInterval.

                    Thank you for the help.

                    Best Regards,

                    Alan

                    Comment


                    • #11
                      You're most welcome.
                      Jason K.
                      Project Manager
                      eSignal - an Interactive Data company

                      EFS KnowledgeBase
                      JavaScript for EFS Video Series
                      EFS Beginner Tutorial Series
                      EFS Glossary
                      Custom EFS Development Policy

                      New User Orientation

                      Comment

                      Working...
                      X