Announcement

Collapse
No announcement yet.

MA cross, problem in back test

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

  • MA cross, problem in back test

    Hi, I'm trying to back test a simple moving average cross with 10 and 20 as the periods. It appears to work, but if I look at the trades in the back test they don't seem to be right, especially the first few. I've searched esignal.com but with no luck for this simple formula. Could anybody help?, thanx...LJD

  • #2
    LJD
    You may need to post the code you are using with a description of the problem (including the symbol and interval on which you are running the back test)
    Alex

    Comment


    • #3
      Hi, this is the formula I created in the wizard with 7 & 14 as the periods. I'm trading FTSE futures (Z Z5-EEI) and applied it to a 5 minute chart. The first 3 trades do not line up with a MA cross. Thanx...LJD
      PHP Code:
      //{{EFSWizard_Description
      //
      //    This formula was generated by the Alert Wizard
      //
      //}}EFSWizard_Description


      //{{EFSWizard_Declarations
      var vSMA7 = new MAStudy(70"Close"MAStudy.SIMPLE);
      var 
      vSMA14 = new MAStudy(140"Close"MAStudy.SIMPLE);
      var 
      vLastAlert = -1;
      //}}EFSWizard_Declarations


      function preMain() {
         
      /**
          *  This function is called only once, before any of the bars are loaded.
          *  Place any study or EFS configuration commands here.
          */
      //{{EFSWizard_PreMain
          
      setPriceStudy(true);
          
      setStudyTitle("MA 7-14");
          
      setCursorLabelName("MA7"0);
          
      setCursorLabelName("MA14"1);
          
      setDefaultBarStyle(PS_SOLID0);
          
      setDefaultBarStyle(PS_SOLID1);
          
      setDefaultBarFgColor(Color.blue0);
          
      setDefaultBarFgColor(Color.red1);
          
      setDefaultBarThickness(10);
          
      setDefaultBarThickness(11);
          
      setPlotType(PLOTTYPE_LINE0);
          
      setPlotType(PLOTTYPE_LINE1);
      //}}EFSWizard_PreMain

      }

      function 
      main() {
         
      /**
          *  The main() function is called once per bar on all previous bars, once per
          *  each incoming completed bar, and if you don't have 'setComputeOnClose(true)'
          *  in your preMain(), it is also called on every tick.
          */

      //{{EFSWizard_Expressions
          //{{EFSWizard_Expression_1
              
      if (
                  
      vSMA7.getValue(MAStudy.MA) > vSMA14.getValue(MAStudy.MA)
              ) 
      onAction1()
          
      //}}EFSWizard_Expression_1
          
          //{{EFSWizard_Expression_2
              
      else if (
                  
      vSMA7.getValue(MAStudy.MA) < vSMA14.getValue(MAStudy.MA)
              ) 
      onAction2();
          
      //}}EFSWizard_Expression_2
          
      //}}EFSWizard_Expressions


      //{{EFSWizard_Return
          
      return new Array(
              
      vSMA7.getValue(MAStudy.MA),
              
      vSMA14.getValue(MAStudy.MA)
          );
      //}}EFSWizard_Return

      }

      function 
      postMain() {
         
      /**
          *  The postMain() function is called only once, when the EFS is no longer used for
          *  the current symbol (ie, symbol change, chart closing, or application shutdown).
          */
      }

      //{{EFSWizard_Actions
          //{{EFSWizard_Action_1
          
      function onAction1() {
              if (
      vLastAlert != 1Strategy.doLong(""Strategy.CLOSEStrategy.THISBARStrategy.DEFAULT, 0);
              
      vLastAlert 1;
          }
          
      //}}EFSWizard_Action_1
          
          //{{EFSWizard_Action_2
          
      function onAction2() {
              if (
      vLastAlert != 2Strategy.doShort(""Strategy.CLOSEStrategy.THISBARStrategy.DEFAULT, 0);
              
      vLastAlert 2;
          }
          
      //}}EFSWizard_Action_2
          
      //}}EFSWizard_Actions 

      Comment


      • #4
        LJD
        The reason why that occurrs is shown in the following image and is due to how the conditions are set in the strategy



        To resolve this you need to first ensure that the value of the longest of the averages is equal to null at the prior bar in which case no commands are executed (see Set1 in the following image).



        Then you also need to make sure that you properly define the crossing of the averages as a unique condition as shown in the following image.



        Add the reverse logic for the short side in Set3 and you should see that the first trade will occurr on the first actual crossing of the averages
        Alex

        Comment


        • #5
          Hi Alex, is there any chance you could simply post the correct formula? That way I'd know it would be correct. I'm new to trying to write these things,
          Thank...LJD

          Comment


          • #6
            LJD
            I would suggest that you try to implement the changes yourself as this will better help you learn how to code.
            If you run into problems post your code and someone may be available to guide you through fixing it.
            Alex

            Comment


            • #7
              Hi Alex, after much scratching of my head I simply reversed the conditions you had in set 2 to make up set 3. ie where you had strag is long it was replaced with strag is short, and the "<" became ">" and vice-versa. I think this is equal to you saying simply reverse the logic in set 3, though to be honest I still don't follow the logic. I understand the principle when it's in plain English, but not when it's written like this.

              This is how it looks now, it seems to be right, if you get a chance could you have a look at it and tell me if you think it's correct.
              Thanx & rgds....LJD

              PHP Code:
              //{{EFSWizard_Description
              //
              //    This formula was generated by the Alert Wizard
              //
              //}}EFSWizard_Description


              //{{EFSWizard_Declarations
              var vSMA7 = new MAStudy(70"Close"MAStudy.SIMPLE);
              var 
              vSMA14 = new MAStudy(140"Close"MAStudy.SIMPLE);
              var 
              vLastAlert = -1;
              //}}EFSWizard_Declarations


              function preMain() {
                 
              /**
                  *  This function is called only once, before any of the bars are loaded.
                  *  Place any study or EFS configuration commands here.
                  */
              //{{EFSWizard_PreMain
                  
              setPriceStudy(true);
                  
              setStudyTitle("MA 7 14");
                  
              setCursorLabelName("MA 7"0);
                  
              setCursorLabelName("MA 14"1);
                  
              setDefaultBarStyle(PS_SOLID0);
                  
              setDefaultBarStyle(PS_SOLID1);
                  
              setDefaultBarFgColor(Color.blue0);
                  
              setDefaultBarFgColor(Color.red1);
                  
              setDefaultBarThickness(10);
                  
              setDefaultBarThickness(11);
                  
              setPlotType(PLOTTYPE_LINE0);
                  
              setPlotType(PLOTTYPE_LINE1);
              //}}EFSWizard_PreMain

              }

              function 
              main() {
                 
              /**
                  *  The main() function is called once per bar on all previous bars, once per
                  *  each incoming completed bar, and if you don't have 'setComputeOnClose(true)'
                  *  in your preMain(), it is also called on every tick.
                  */

              //{{EFSWizard_Expressions
                  //{{EFSWizard_Expression_1
                      
              if (
                          
              vSMA14.getValue(MAStudy.MA, -1) == null
                      
              onAction1()
                  
              //}}EFSWizard_Expression_1
                  
                  //{{EFSWizard_Expression_2
                      
              else if (
                          
              Strategy.isLong() == false &&
                          
              vSMA7.getValue(MAStudy.MA, -1) < vSMA14.getValue(MAStudy.MA, -1) &&
                          
              vSMA7.getValue(MAStudy.MA) > vSMA14.getValue(MAStudy.MA)
                      ) 
              onAction2()
                  
              //}}EFSWizard_Expression_2
                  
                  //{{EFSWizard_Expression_3
                      
              else if (
                          
              Strategy.isShort() == false &&
                          
              vSMA7.getValue(MAStudy.MA, -1) > vSMA14.getValue(MAStudy.MA, -1) &&
                          
              vSMA7.getValue(MAStudy.MA) < vSMA14.getValue(MAStudy.MA)
                      ) 
              onAction3();
                  
              //}}EFSWizard_Expression_3
                  
              //}}EFSWizard_Expressions


              //{{EFSWizard_Return
                  
              return new Array(
                      
              vSMA7.getValue(MAStudy.MA),
                      
              vSMA14.getValue(MAStudy.MA)
                  );
              //}}EFSWizard_Return

              }

              function 
              postMain() {
                 
              /**
                  *  The postMain() function is called only once, when the EFS is no longer used for
                  *  the current symbol (ie, symbol change, chart closing, or application shutdown).
                  */
              }

              //{{EFSWizard_Actions
                  //{{EFSWizard_Action_1
                  
              function onAction1() {
                      
              vLastAlert 1;
                  }
                  
              //}}EFSWizard_Action_1
                  
                  //{{EFSWizard_Action_2
                  
              function onAction2() {
                      
              Strategy.doLong(""Strategy.CLOSEStrategy.THISBARStrategy.DEFAULT, 0);
                      
              vLastAlert 2;
                  }
                  
              //}}EFSWizard_Action_2
                  
                  //{{EFSWizard_Action_3
                  
              function onAction3() {
                      
              Strategy.doShort(""Strategy.CLOSEStrategy.THISBARStrategy.DEFAULT, 0);
                      
              vLastAlert 3;
                  }
                  
              //}}EFSWizard_Action_3
                  
              //}}EFSWizard_Actions 

              Comment


              • #8
                LJD
                It is correct and in fact if you notice the first trade will now occur only on the first crossing of the two averages.
                As to a plain English description here is what the efs is doing.
                In Set1 it simply checks if at the prior bar (hence the -1) the 14MA is returning null (ie it is not yet plotting). If that is true the efs does nothing and does not evaluate any further conditions.
                If instead that is not true then the strategy passes on to Set2 and checks if the strategy is not already long and if 7MA at the prior bar is below the 14MA at the prior bar AND if the 7MA at the current bar is above the 14MA at the current bar. If this is true it then executes the command to go long and stops evaluating further conditions else it moves on to Set3 where it essentially checks for the reverse conditions and goes short if those are true.
                Hope this helps
                Alex

                Comment

                Working...
                X