Announcement

Collapse
No announcement yet.

SMA / JMA cross over

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

  • SMA / JMA cross over

    Hello,

    Would you please help me with this code that is not working ?
    Thank you

    /*---------------------------------------------------------------------------------------------------
    INDICATOR: PIERRE JMA JMA crossover

    DESCRIPTION: Plots two SMA and two JMA moving averages.
    Trades on both SMA crossing JMA

    ----------------------------------------------------------------------------------------------------*/

    function preMain() {
    setStudyTitle("PIERRE MA5-JMA5 MA10 JMA10 CO V4");
    setDefaultBarFgColor(Color.cyan, 0);
    setDefaultBarFgColor(Color.blue, 1);
    setDefaultBarFgColor(Color.red, 2);
    setDefaultBarFgColor(Color.yellow, 3);
    setDefaultBarThickness(2,0);
    setPlotType(PLOTTYPE_INSTANTCOLORLINE);
    setPriceStudy(true);
    }

    var vJMA1, vJMA2, vSMA1, vSMA2;
    var vHigh;
    var vLow;
    var BarCntr = 0;
    var study1 = null
    var study2 = null
    function main(Price, JMA_len1, JMA_phase1, JMA_len2, JMA_phase2, offset, _BGcolor) {

    //{{Action 1 condition
    if (
    vJMA1 >= vSMA1
    &&
    vJMA2 >= vSMA2
    ) onAction1()
    //}}End Action 1

    //{{Action 2 Condition
    else if (
    vJMA1 < vSMA1
    &&
    vJMA2 < vSMA2
    ) onAction2();
    //}}End Action 2


    /* input validation */

    if (Price == null) Price = "Close"; else if (typeof(Price) != "string") return;
    if (JMA_len1 == null) JMA_len1 = 5; else if (JMA_len1 < 0) JMA_len1 = 0;
    if (JMA_phase1 == null) JMA_phase1 = 0; else JMA_phase1 = Math.min(100,Math.max(-100,JMA_phase1)) ;
    if (JMA_len2 == null) JMA_len2 = 10; else if (JMA_len2 < 0) JMA_len2 = 0;
    if (JMA_phase2 == null) JMA_phase2 = 0; else JMA_phase2 = Math.min(100,Math.max(-100,JMA_phase2)) ;
    if (offset == null) offset = 0; else offset = Math.round(offset) ;
    if (_BGcolor == null) _BGcolor = 1;

    /* initialization */
    if (getBarState() == BARSTATE_ALLBARS) {
    myJMAstudy1 = new JurikJMAStudy(JMA_len1,JMA_phase1,0,Price);
    myJMAstudy2 = new JurikJMAStudy(JMA_len2,JMA_phase2,offset,Price);
    mySMAstudy1 = new MAStudy(5, 0, "Close", MAStudy.SIMPLE);
    mySMAstudy2 = new MAStudy(10, 0, "Close", MAStudy.SIMPLE);
    }
    /* core program */

    vJMA1 = myJMAstudy1.getValue(JurikJMAStudy.JMA) ;
    vJMA2 = myJMAstudy2.getValue(JurikJMAStudy.JMA) ;

    if(study1 == null) study1 = sma(5);
    var vSMA1 = study1.getValue(0);
    if(vSMA1 == null)
    return;

    if(study2 == null) study2 = sma(10);
    var vSMA2 = study2.getValue(0);
    if(vSMA2 == null)
    return;

    /* trading conditions */

    if (vJMA1 == null || vJMA2 == null || vSMA1 == null || vSMA2 == null) return;
    function onAction1() {
    if(!Strategy.ishort()) {
    Strategy.doCover("CrossUp cut shorts", Strategy.OPEN, Strategy.NEXTBAR);
    }
    if(!Strategy.isLong()) {
    setBarFgColor(Color.cyan) ;
    }
    else Strategy.doLong("Crossing Up", Strategy.OPEN, Strategy.NEXTBAR);
    setBarFgColor(Color.cyan) ;
    }

    function onAction2() {
    if(!Strategy.isLong()) {
    Strategy.doSell("CrossDn cut longs", Strategy.OPEN, Strategy.NEXTBAR);
    }
    if(!Strategy.isShort()) {
    setBarFgColor(Color.red) ;
    }
    else Strategy.doShort("Crossing Down", Strategy.OPEN, Strategy.NEXTBAR);
    setBarFgColor(Color.red) ;
    }
    return new Array(vJMA1, vJMA2, vSMA1, vSMA2);
    }

  • #2
    Hello kalachnikov,

    There are several logic errors that need to be addressed. Start with the following corrections and run the test again.

    1. In your trade conditions, which are at the top of main(), you are referring to variables that will not have valid data assigned to them at the beginning of the test. Add a null check for the variables, vJMA1, vSMA1, vJMA2 and vSMA2 to validate that they contain valid data before allowing your trade conditions from being evaluated.

    PHP Code:
    if (vJMA1 != null && vSMA1 != null && vJMA2 != null && vSMA2 != null) {
        
    // your trade conditions for onAction1 and onAction2 here


    2. Your variables, vSMA1 and vSMA2 need to be only declared with the var keyword in the global scope, which the currently are. The problem is inside main() where you are assigning the values to these variables. You are declaring them again as local variables when you assign their values. This is causing a logic problem for the trade conditions because they are seeing them as null values.

    3. Your onAction functions are currently inside main(). Move them outside of main().

    4. In your onAction1() function you have a typo in your first condtion. You are missing a capital S in the .isShort() method. Currently it is .ishort().

    5. In your strategy calls, you are specifying Strategy.OPEN, when is not one of the available fill type constants. Please see our reference articles on the Back Testing Functions for the available constants that can be used.

    6. The logic used within the onAction functions are problematic. I do not have a specific solution to this because I do not know what you are attempting to accomplish. You have a condition, if (!Strategy.isShort()) { Strategy.doCover() }. This reads; "if not short then cover short position." When the strategy is not holding a short position, it cannot cover one. Likewise you also have a condition that first checks to see if you are not long and then adds to the long positions in the else statement associated with this condition. Essentially, every time the condition sees that it is long it will try to add to the long position. I think what might be most helpful to you is to go through our Back Testing Tutorials to see some working examples of how to create trading logic for back testing.


    If you continue to have trouble post your updated code and we'll go from there.
    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
      Thank you Jason for your reply.
      I corrected the code according to your advice as well as downloaded the tutorials. It is still not working.
      Below, the modified code.
      Pierre

      PHP Code:
      /*---------------------------------------------------------------------------------------------------
      INDICATOR:    PIERRE JMA JMA crossover                                                                      
                                                                                                           
      DESCRIPTION:  Plots two SMA and two JMA moving averages.                                                                                      
                    Trades on both SMA crossing JMA
                                                                                                                       
      ----------------------------------------------------------------------------------------------------*/

      function preMain() {
          
      setStudyTitle("PIERRE MA5-JMA5 MA10 JMA10 CO V4");
          
      setDefaultBarFgColor(Color.cyan0);
          
      setDefaultBarFgColor(Color.blue1);
          
      setDefaultBarFgColor(Color.red2);
          
      setDefaultBarFgColor(Color.yellow3);
          
      setDefaultBarThickness(2,0);
          
      setPlotType(PLOTTYPE_INSTANTCOLORLINE);
          
      setPriceStudy(true);
          }

          var 
      vJMA1vJMA2vSMA1vSMA2;
          var 
      vHigh;
          var 
      vLow;
          var 
      BarCntr 0;
          var 
      study1 null
          
      var study2 null

      if (vJMA1 != null && vSMA1 != null && vJMA2 != null && vSMA2 != null) {
          
      //{{Action 1 condition
              
      if (
                  
      vJMA1 >= vSMA1 
                  
      &&
                  
      vJMA2 >= vSMA2
              
      onAction1()
          
      //}}End Action 1
          
          //{{Action 2 Condition
              
      else if (
                  
      vJMA1 vSMA1
                  
      &&
                  
      vJMA2 vSMA2
              
      onAction2();
          
      //}}End Action 2
      }    
               

      function 
      main(PriceJMA_len1JMA_phase1JMA_len2JMA_phase2offset_BGcolor) {


      /* input validation */

          
      if (Price == nullPrice "Close";              else if (typeof(Price) != "string") return;  
          if (
      JMA_len1 == nullJMA_len1 5;             else if (JMA_len1 0JMA_len1 0;
          if (
      JMA_phase1 == nullJMA_phase1 0;         else JMA_phase1 Math.min(100,Math.max(-100,JMA_phase1)) ;
          if (
      JMA_len2 == nullJMA_len2 10;            else if (JMA_len2 0JMA_len2 0;
          if (
      JMA_phase2 == nullJMA_phase2 0;         else JMA_phase2 Math.min(100,Math.max(-100,JMA_phase2)) ;
          if (
      offset == nulloffset 0;                 else offset Math.round(offset) ;
          if (
      _BGcolor == null_BGcolor 1;

      /* initialization */
      if (getBarState() == BARSTATE_ALLBARS) {
              
      myJMAstudy1  = new JurikJMAStudy(JMA_len1,JMA_phase1,0,Price);
              
      myJMAstudy2  = new JurikJMAStudy(JMA_len2,JMA_phase2,offset,Price);
              
      mySMAstudy1  = new MAStudy(50"Close"MAStudy.SIMPLE);
              
      mySMAstudy2  = new MAStudy(100"Close"MAStudy.SIMPLE);   
           }
      /* core program */

          
      vJMA1 myJMAstudy1.getValue(JurikJMAStudy.JMA) ;
          
      vJMA2 myJMAstudy2.getValue(JurikJMAStudy.JMA) ;
          
          if(
      study1 == nullstudy1 sma(5);
          var 
      vSMA1 study1.getValue(0);
          if(
      vSMA1 == null)
          return;

          if(
      study2 == nullstudy2 sma(10);
          var 
      vSMA2 study2.getValue(0);
          if(
      vSMA2 == null)
          return;

      /* trading conditions */

        
      if (vJMA1 == null || vJMA2 == null || vSMA1 == null || vSMA2 == null) return; 
          function 
      onAction1() {
              if(
      Strategy.isShort()) {
                  
      Strategy.doCover("Cut Short"Strategy.MARKETStrategy.THISBAR);
              }
              
      Strategy.doLong("Long Signal"Strategy.CLOSEStrategy.THISBAR);
              
      nEntryPrice close(0);
              
      setBarFgColor(Color.cyan) ;
              
          }
          function 
      onAction2() {
              if(
      Strategy.isLong()) {
                  
      Strategy.doSell("Cut Longs"Strategy.MARKETStrategy.THISBAR);
              }
              
      Strategy.doShort("Crossing Down"Strategy.CLOSEStrategy.THISBAR);
              
      setBarFgColor(Color.red) ;
              
      nEntryPrice close(0);
          }
          return new Array(
      vJMA1vJMA2vSMA1vSMA2); }

      Comment


      • #4
        Hello Pierre,

        1. The trade conditions were moved outside of main(). You need to move them back inside the main() function as the first set of instructions.

        2. This step has not been completed. You are still declaring vSMA1 and vSMA2 inside main(). Remove the "var" keyword inside main where you are assigning their values. They need to be global variables. You currently do have them declared globally (i.e. outside of main) with the var keyword.

        3. Your onAction functions are still inside main(). Move them outside of main().
        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
          Thank you Jason for your help.
          This seems to be working now.
          Pierre

          Comment


          • #6
            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