Announcement

Collapse
No announcement yet.

Strategy analyzer anomaly?

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

  • Strategy analyzer anomaly?

    I have a simple breakout trading system, which enters on a stop when the price is above/below certain levels. I have also set the system to colour bars blue/red depending on whether the system is long/short.

    I have discovered a puzzling inconsistency between this code and the strategy analyzer. I have written the code so that a long position can only be entered if the system is not already long. However, it appears that as far as strategy analyzer is concerned, this is not working properly. For example, I have one trade this morning where a trade was triggered, but the stop loss was hit on the same bar and the trade exited.

    However the entry condition was also true on the following bar. The bar colouring code identified this and flagged the bars accordingly, but the strategy analyzer completely ignored the trade. My suspicion is that strategy analyzer may not be detecting that the trade on the previous bar has exited, though it may of course be something else.

    The entry logic is:
    if(high() > LastHigh && !Strategy.isLong()) {
    Strategy.doLong("SHLong", Strategy.STOP, Strategy.THISBAR,Strategy.DEFAULT,LastHigh)
    ;}

    The bar colouring code is:
    if(Strategy.isLong()){
    setPriceBarColor(Color.blue) ;}

    Any suggestions?

  • #2
    Hello andyw,

    Please post your entire formula so we can do some testing.

    Thanks,
    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
      Many thanks Jason - see below. I've also put the code for the called file "MLR Test.efs" at the bottom


      function preMain() {
      setPriceStudy(true);
      setColorPriceBars(true);
      setDefaultPriceBarColor(Color.grey);
      setStudyTitle("LSSTest");
      setCursorLabelName("LSSTest");



      setDefaultBarFgColor(Color.blue, 0);
      setDefaultBarFgColor(Color.RGB(192,75,0), 1);
      }
      var LastHigh = 0;
      var LastLow = 0;
      var myArray = new Array(10);
      var myValue = null;

      function main(nlengthOpen,noffsetOpen,nlengthHigh,noffsetHi gh,nlengthLow,noffsetLow,nlengthClose,noffsetClose ){

      var offsetOpen = 5;
      if (noffsetOpen != null) offsetOpen = noffsetOpen;
      var offsetHigh = 5;
      if (noffsetHigh != null) offsetHigh = noffsetHigh;
      var offsetLow = 5;
      if (noffsetLow != null) offsetLow = noffsetLow;
      var offsetClose = 5;
      if (noffsetClose != null) offsetClose = noffsetClose;

      if(nlengthOpen == null)
      nlengthOpen = 20;
      if(nlengthHigh == null)
      nlengthHigh = 20;
      if(nlengthLow == null)
      nlengthLow = 20;
      if(nlengthClose == null)
      nlengthClose = 20;

      var MLROpen = call("MLR Test.efs",nlengthOpen,open,noffsetOpen);
      var MLRHigh = call("MLR Test.efs",nlengthHigh,high,noffsetHigh);
      var MLRLow = call("MLR Test.efs",nlengthLow,low,noffsetLow);
      var MLRClose = call("MLR Test.efs",nlengthClose,close,noffsetClose);





      if (high() < high(-1) &&
      high(-1) > high(-2) &&
      high(-2) > high(-3) ){
      LastHigh = high(-1);
      }
      if ( low() > low(-1) &&
      low(-1) < low(-2) &&
      low(-2) < low(-3) ){
      LastLow = low(-1);
      }


      if( high() > LastHigh && !Strategy.isLong()) {
      Strategy.doLong("SwHiLong", Strategy.STOP, Strategy.THISBAR,Strategy.DEFAULT,LastHigh)
      ;}


      if(low() < LastLow){if(!Strategy.isShort()) {
      Strategy.doShort("SwLoShort", Strategy.STOP, Strategy.THISBAR,Strategy.DEFAULT, LastLow);}}

      if(Strategy.isLong() && low() < MLRLow)
      {Strategy.doSell("SwHiSellMLR",Strategy.STOP,Strat egy.THISBAR,Strategy.DEFAULT,MLRLow);}

      if(Strategy.isShort() && high() > MLRHigh)
      {Strategy.doCover("SwLoCoverMLR",Strategy.STOP,Str ategy.THISBAR,Strategy.DEFAULT,MLRHigh);}

      if(Strategy.isLong()){
      setPriceBarColor(Color.blue) ;}
      else if(Strategy.isShort())
      {setPriceBarColor(Color.RGB(192,75,0) );}


      }


      CODE FOR "MLR Test.efs"

      function preMain()
      {
      setStudyTitle("MLRTest");
      setCursorLabelName("MLRTest", 0);
      setDefaultBarFgColor(Color.green, 0);
      setPriceStudy(true);

      }

      var myArray = new Array(30);
      var myValue = null;

      function main(nlength,nval,noffset) {
      var offset = 5;
      if (noffset != null) offset = noffset;
      var nBarState = getBarState();

      if (nBarState == BARSTATE_NEWBAR && myValue != null) {
      myArray.pop();
      myArray.unshift(myValue); }

      if(nval== null) {nval= close;}
      if(nval=="open") {nval=open;}
      if(nval=="high") {nval=high;}
      if(nval=="low") {nval=low;}
      if(nval=="close") {nval=close;}
      if(nlength == null)
      nlength = 20;





      var sum = 0;
      var i = 0;
      var mt = 0;

      for(i = nlength; i > 0; i--)
      sum += (i - (nlength + 1) / 3) * nval(i - nlength);
      wt = 6 / (nlength * (nlength + 1)) * sum
      //return wt;

      myValue = wt;
      if (myValue == null) {
      return;
      }

      if (myArray[offset] != null) {
      return myArray[offset];
      } else {
      return;
      }




      }

      Comment


      • #4
        Hello andyw,

        I did some testing with your formula and changed the logic a bit. You weren't checking to see if a long or short was just entered before checking for a sell or cover condition. For example, if a long position was entered on bar X, your formula was then immediately checking for the sell condition on the same bar. One way to prevent a trade from being closed out on the same bar is to move your sell and cover conditions above your buy and short conditions. This way, your formula will have to at least wait until the next bar to allow a sell or cover.

        I didn't experience the missing trade problem you were experiencing once I made these changes. Try this out and let me know if this formula is now working the way you intended it to. If you find any missing trades in the analyzer again, give me the specific details as to the symbol, interval and time template you are using.

        PHP Code:
        function preMain() {
            
        setPriceStudy(true);
            
        setColorPriceBars(true);
            
        setDefaultPriceBarColor(Color.grey);
            
        setStudyTitle("LSSTest");
            
        setCursorLabelName("LSSTest");

            
        setDefaultBarFgColor(Color.blue0);
            
        setDefaultBarFgColor(Color.RGB(192,75,0), 1);
        }

        var 
        LastHigh 0;
        var 
        LastLow 0;
        var 
        myArray = new Array(10);
        var 
        myValue null;

        function 
        main(nlengthOpen,noffsetOpen,nlengthHigh,noffsetHigh,nlengthLow,noffsetLow,nlengthClose,noffsetClose){
            var 
        offsetOpen 5;
            if (
        noffsetOpen != nulloffsetOpen noffsetOpen;
            var 
        offsetHigh 5;
            if (
        noffsetHigh != nulloffsetHigh noffsetHigh;
            var 
        offsetLow 5;
            if (
        noffsetLow != nulloffsetLow noffsetLow;
            var 
        offsetClose 5;
            if (
        noffsetClose != nulloffsetClose noffsetClose;

            if(
        nlengthOpen == null)
            
        nlengthOpen 20;
            if(
        nlengthHigh == null)
            
        nlengthHigh 20;
            if(
        nlengthLow == null)
            
        nlengthLow 20;
            if(
        nlengthClose == null)
            
        nlengthClose 20;

            var 
        MLROpen call("MLR Test.efs",nlengthOpen,open,noffsetOpen);
            var 
        MLRHigh call("MLR Test.efs",nlengthHigh,high,noffsetHigh);
            var 
        MLRLow call("MLR Test.efs",nlengthLow,low,noffsetLow);
            var 
        MLRClose call("MLR Test.efs",nlengthClose,close,noffsetClose);
            if (
        MLROpen == null || MLRHigh == null || MLRLow == null || MLRClose == null)
                return;

            var 
        h0 high();
            var 
        h1 high(-1);
            var 
        h2 high(-2);
            var 
        h3 high(-3);
            if (
        h0 == null || h1 == null || h2 == null || h3 == null)
                return;
            var 
        l0 low();
            var 
        l1 low(-1);
            var 
        l2 low(-2);
            var 
        l3 low(-3);
            if (
        l0 == null || l1 == null || l2 == null || l3 == null)
                return;

            if (
        h0 h1 && h1 h2 && h2 h3){
                
        LastHigh h1;
            }
            if (
        l0 l1 && l1 l2 && l2 l3){
                
        LastLow l1;
            }

            if(
        Strategy.isLong() && l0 MLRLow){
                
        Strategy.doSell("SwHiSellMLR",Strategy.STOP,Strategy.THISBAR,Strategy.DEFAULT,MLRLow);
            }

            if(
        Strategy.isShort() && h0 MLRHigh){
                
        Strategy.doCover("SwLoCoverMLR",Strategy.STOP,Strategy.THISBAR,Strategy.DEFAULT,MLRHigh);
            } 

            if( 
        h0 LastHigh && LastHigh != && !Strategy.isLong()) {
                
        Strategy.doLong("SwHiLong"Strategy.STOPStrategy.THISBAR,Strategy.DEFAULT,LastHigh);
            }


            if(
        l0 LastLow && LastLow != 0){
                if(!
        Strategy.isShort()) {
                    
        Strategy.doShort("SwLoShort"Strategy.STOPStrategy.THISBAR,Strategy.DEFAULT, LastLow);
                }
            }


            if(
        Strategy.isLong()){
                
        setPriceBarColor(Color.blue) ;
            } else if(
        Strategy.isShort()) {
                
        setPriceBarColor(Color.RGB(192,75,0) );
            }

        Thanks,
        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