Announcement

Collapse
No announcement yet.

strategy using trend strength indicator

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

  • #16
    Brad
    It seems that the strategy is working in reverse.
    If you look at the attached image it should be selling at the bar after the one indicated by the cursor in the chart instead it issued a Buy.
    Also - if I understand the strategy correctly it should do this only the first time the condition exists instead it keeps on repeating the order for every time the condition exists.
    Lastly - and yoda has already mentioned this - the protective stop does not appear to be a fixed 2 point from the entry price. In fact even assuming the Buy order was correct the Stop was triggered well below (-5.75 loss on that specific trade).
    Alex
    Attached Files

    Comment


    • #17
      Alexis,
      Actually the code that dloomis had for this strategy had the strategy go long if tti>=99 where it should be short. So just replace the tti>=99 with tti<=1 and vice versa for the strategy to work correctly. Brad was only addressing the exits for me so i dont think he would have noticed that the orignal code he adjusted was backwards.
      However I am in agreement with your other concerns regarding this version of the code as i have commented in my previous couple of posts.
      PS in my system i actually use tti>=80 for going short and tti<=20 for going long. My inputs are not 60,30 they are varied between20,10 10,5 and 5,3 depending on which time frame i am using.
      Yoda

      Comment


      • #18
        Revised Code....

        Thanks all. I really appreciate the follow-up support on this. I really needed to take a break this weekend (with my wife). Working on dinner at the moment.

        Here is some revised code for everyone....

        PHP Code:
        / **************************************************
        Description This Indicator plots Trend Intensity Index indicator
        Provided By 
        Developed by TS SupportLLC for eSignal. (cCopyright 2002
        ***************************************************/
        var 
        PTLevel 1.50;
        var 
        PTPrice 0;
        var 
        StopLevel 2.00;
        var 
        StopPrice 0;

        function 
        preMain() {

        setStudyTitle("Trend Intensity Index");
        setCursorLabelName("TII");
        setStudyMin(-1);
        setStudyMax(101);

        addBand(20PS_SOLID1Color.black);
        addBand(80PS_SOLID1Color.black);
        }

        var 
        nLengthMA 60;
        var 
        nLengthTII 30;

        function 
        main() {
        var 
        vMA call("/Library/MA.efs"nLengthMA);

        var 
        arC close(0, -nLengthTII);
        if(
        arC == null)
        return;

        var 
        i;
        var 
        dSDP 0dSDM 0;

        for(
        0nLengthTIIi++) {
        if(
        arC[i] > vMA) {
        dSDP +=arC[i] - vMA;
        } else if(
        arC[i] < vMA) {
        dSDM += vMA arC[i];
        }
        }
        var 
        tii= (dSDP / (dSDP dSDM)) * 100;

        // test for Stops
        if ((Strategy.isLong()) && (low() <= StopPrice)) {
        Strategy.doSell(Strategy.LIMITStrategy.THISBARStopPrice);
        }
        if ((
        Strategy.isShort()) && (high() >= StopPrice)) {
        Strategy.doCover(Strategy.LIMITStrategy.THISBARStopPrice);
        }

        // test for Profits
        if ((Strategy.isLong()) && (high() >= PTPrice)) {
        Strategy.doSell(Strategy.LIMITStrategy.THISBARPTPrice);
        }
        if ((
        Strategy.isShort()) && (low() <= PTPrice)) {
        Strategy.doCover(Strategy.LIMITStrategy.THISBARPTPrice);
        }

        //  Entry Signals
        if(tii<=1){  // Go Short
          
        Strategy.doShort(""Strategy.MARKETStrategy.NEXTBARStrategy.DEFAULT, 0);
          
        PTPrice open(1) - PTLevel;
          
        StopPrice open(1) + StopLevel;
        }

        if(
        tii>=99){  // Go Long
          
        Strategy.doLong(""Strategy.MARKETStrategy.NEXTBARStrategy.DEFAULT, 0);
          
        PTPrice open(1) + PTLevel;
          
        StopPrice open(1) - StopLevel;
        }


        return (
        dSDP / (dSDP dSDM)) * 100;

        The variables at the top of this file should allow you to change the actions of this system. Let me know if you have any other questions..

        B
        Brad Matheny
        eSignal Solution Provider since 2000

        Comment


        • #19
          Doji,
          When i apply your efs code to my chart i get no data at all for the tii indicator. However if i take out the code for stops and profits the data then shows up. Please see if the same thing happens when you apply it as maybe its something im doing incorrectly.
          Also is there a way to enter only if high of trigger bar is taken out for longs and low of trigger bar is taken out for shorts instead of simply buying at the open of the next bar.
          Thanks,
          yoda

          Comment


          • #20
            Hi,

            There are a few flaw with the code posted. The most obvious is the return statement, but there seems to be others. I'm looking at it now.

            G
            Garth

            Comment


            • #21
              Fixed it...

              Here you go. I'm going to try to attach two files to this.

              This file draws the tii indicator and runs your strategy backtest perfectly.

              the problem was with my stops and PT code. I forgot to include the LABEL portion of the Strategy function - Duh.

              Sorry about that..

              B
              Attached Files
              Brad Matheny
              eSignal Solution Provider since 2000

              Comment


              • #22
                Second file..

                Here is the file that runs the backtest perfectly and draws arrows/diamonds on the chart where the system took trades/got stopped out and hit profit target levels..

                B
                Attached Files
                Brad Matheny
                eSignal Solution Provider since 2000

                Comment


                • #23
                  This works, but it isn't really rigorous in its error checking.
                  G

                  PHP Code:
                  /**************************************************
                  Description : This Indicator plots Trend Intensity Index indicator
                  Provided By : Developed by TS Support, LLC for eSignal. (c) Copyright 2002
                   ***************************************************/
                  var PTLevel 1.50;
                  var 
                  PTPrice 0;
                  var 
                  StopLevel 2.00;
                  var 
                  StopPrice 0;
                  var 
                  nLengthMA 60;
                  var 
                  nLengthTII 30;

                  function 
                  preMain() {

                  setStudyTitle("Trend Intensity Index");
                  setCursorLabelName("TII");
                  setStudyMin(-1);
                  setStudyMax(101);

                  addBand(20PS_SOLID1Color.black);
                  addBand(80PS_SOLID1Color.black);
                  }


                  function 
                  main() {
                      var 
                  vMA call("/Library/MA.efs"nLengthMA);

                      var 
                  arC close(0, -nLengthTII);
                      if(
                  arC == null)
                          return;

                      var 
                  i;
                      var 
                  dSDP 0;
                      var 
                  dSDM 0;

                      for(
                  0nLengthTIIi++) {
                          if(
                  arC[i] > vMA) {
                              
                  dSDP +=arC[i] - vMA;
                          } else if(
                  arC[i] < vMA) {
                              
                  dSDM += vMA arC[i];
                          }
                      }

                      var 
                  tii= (dSDP / (dSDP dSDM)) * 100;
                      
                  // debugPrintln("dSDP = " + dSDM + " dSDM = " + dSDM + " tii = " + tii);

                      
                  var nIndex getCurrentBarIndex();
                   

                      
                  // test for Stops
                      
                  if ((Strategy.isLong()) && (low() <= StopPrice)) {
                          if ((
                  Strategy.doSell("Stop Close"Strategy.LIMITStrategy.THISBARStrategy.ALLStopPrice)) == false){
                              
                  debugPrintln("Error on doSell");
                          }
                      }
                      if ((
                  Strategy.isShort()) && (high() >= StopPrice)) {
                          if ((
                  Strategy.doCover("Stop Close"Strategy.LIMITStrategy.THISBARStrategy.ALLStopPrice)) == false){
                              
                  debugPrintln("Error on doCover");
                          }
                      }
                      
                      
                  // test for Profits
                      
                  if ((Strategy.isLong()) && (high() >= PTPrice)) {
                          if ((
                  Strategy.doSell("Profit Close"Strategy.LIMITStrategy.THISBARStrategy.DEFAULT, PTPrice)) == false){
                              
                  debugPrintln("Error on doSell Profit");
                          }
                      }
                      if ((
                  Strategy.isShort()) && (low() <= PTPrice)) {
                          if ((
                  Strategy.doCover("Profit Close"Strategy.LIMITStrategy.THISBARStrategy.DEFAULT, PTPrice)) == false){
                              
                  debugPrintln("Error on doCover Profit");
                          }
                      }

                      
                  //  Entry Signals
                      
                  if(tii<=1){  // Go Short
                        
                  Strategy.doShort("Short"Strategy.MARKETStrategy.NEXTBAR);
                        
                  PTPrice open(1) - PTLevel;
                        
                  StopPrice open(1) + StopLevel;
                        
                  // debugPrintln(" Short Index = " + nIndex + " StopPrice = " + StopPrice);
                      
                  }   

                      if(
                  tii>=99){  // Go Long
                        
                  Strategy.doLong("Long"Strategy.MARKETStrategy.NEXTBAR);
                        
                  PTPrice open(1) + PTLevel;
                        
                  StopPrice open(1) - StopLevel;
                        
                  // debugPrintln(" Long Index = " + nIndex + " StopPrice = " + StopPrice);
                      
                  }
                     
                      return (
                  tii);

                  Garth

                  Comment


                  • #24
                    Thank you for all your hard work gettin this efs correct. However no one has corrected the most obvious error which is the strategy goes LONG if tii<=1 and goes SHORT when tii>=99. Every code ive seen posted has this reversed.
                    Yoda

                    Comment


                    • #25
                      Also when a trade is entered a new trade cannot be taken till the previous trade has either been stopped or has taken profit. I notice when i run a backtest the strategy is taaking multiple trades in the same direction before the original signals trade has been completed.
                      To reiterate this actual strategy only enters when a bar closes with the specified tti value and the high (longs) or low(shorts) of the trigger bar is taken out on the next bar. Since i did not think this was possible to program i requested going in on the open of the next bar. However the system can only be in one trade at a time.
                      Yoda

                      Comment


                      • #26
                        doji,
                        The yoda2.efs does not display the tii indicator at all on my screen.
                        Also both yoda.efs and yoda2.efs are locked and require a password to unlock so am not able to adjust the parameters to optimize the system.
                        Yoda

                        Comment


                        • #27
                          I notice when i run a backtest the strategy is taaking multiple trades in the same direction before the original signals trade has been completed
                          Yes, I noticed the same thing. That's why I used the ALL flag, so that in theory all positions would be exited rather than just a DEFAULT sized lot.

                          It is trivial to fix this the way you ned it, just add a check for:

                          if (!Strategy.isLong() && !Strategy.isShort){
                          }
                          Garth

                          Comment


                          • #28
                            Hi gspiker,
                            Can you be specific where and how i would add this check in the efs code.
                            Thanks
                            Yoda

                            Comment


                            • #29
                              Try this. Its fixes the conditionals (<=1 >=99), makes it so it SHOULD only be in one trade at a time and adds the breaking the lows for a short and high for a long.

                              I suspect there is somethings still wrong, but I have to pay attention to other things right now. If you figure out a problem when looking at the results, let me know.

                              G

                              PHP Code:
                              /**************************************************
                              Description : This Indicator plots Trend Intensity Index indicator
                              Provided By : Developed by TS Support, LLC for eSignal. (c) Copyright 2002
                               ***************************************************/
                              var PTLevel 1.50;
                              var 
                              PTPrice 0;
                              var 
                              StopLevel 2.00;
                              var 
                              StopPrice 0;
                              var 
                              nLengthMA 60;
                              var 
                              nLengthTII 30;

                              function 
                              preMain() {

                              setStudyTitle("Trend Intensity Index");
                              setCursorLabelName("TII");
                              setStudyMin(-1);
                              setStudyMax(101);

                              addBand(20PS_SOLID1Color.black);
                              addBand(80PS_SOLID1Color.black);
                              }


                              function 
                              main() {
                                  var 
                              vMA call("/Library/MA.efs"nLengthMA);

                                  var 
                              arC close(0, -nLengthTII);
                                  if(
                              arC == null)
                                      return;

                                  var 
                              i;
                                  var 
                              dSDP 0;
                                  var 
                              dSDM 0;

                                  for(
                              0nLengthTIIi++) {
                                      if(
                              arC[i] > vMA) {
                                          
                              dSDP +=arC[i] - vMA;
                                      } else if(
                              arC[i] < vMA) {
                                          
                              dSDM += vMA arC[i];
                                      }
                                  }

                                  var 
                              tii= (dSDP / (dSDP dSDM)) * 100;
                                  
                              // debugPrintln("dSDP = " + dSDM + " dSDM = " + dSDM + " tii = " + tii);

                                  
                              var nIndex getCurrentBarIndex();
                               

                                  
                              // test for Stops
                                  
                              if ((Strategy.isLong()) && (low() <= StopPrice)) {
                                      if ((
                              Strategy.doSell("Stop Close"Strategy.LIMITStrategy.THISBARStrategy.ALLStopPrice)) == false){
                                          
                              debugPrintln("Error on doSell");
                                      }
                                  }
                                  if ((
                              Strategy.isShort()) && (high() >= StopPrice)) {
                                      if ((
                              Strategy.doCover("Stop Close"Strategy.LIMITStrategy.THISBARStrategy.ALLStopPrice)) == false){
                                          
                              debugPrintln("Error on doCover");
                                      }
                                  }
                                  
                                  
                              // test for Profits
                                  
                              if ((Strategy.isLong()) && (high() >= PTPrice)) {
                                      if ((
                              Strategy.doSell("Profit Close"Strategy.LIMITStrategy.THISBARStrategy.DEFAULT, PTPrice)) == false){
                                          
                              debugPrintln("Error on doSell Profit");
                                      }
                                  }
                                  if ((
                              Strategy.isShort()) && (low() <= PTPrice)) {
                                      if ((
                              Strategy.doCover("Profit Close"Strategy.LIMITStrategy.THISBARStrategy.DEFAULT, PTPrice)) == false){
                                          
                              debugPrintln("Error on doCover Profit");
                                      }
                                  }

                                  
                              //  Entry Signals
                                  
                              if (!Strategy.isLong() && !Strategy.isShort()){ // Make sure we are not in a trade
                                      
                              if(tii>=99){  // Go Short
                                          
                              debugPrintln(" tii>=99 Index = " nIndex " StopPrice = " StopPrice);
                                          if (
                              low(1) < low(0)){
                                              
                              Strategy.doShort("Short"Strategy.MARKETStrategy.NEXTBAR);
                                              
                              PTPrice open(1) - PTLevel;
                                              
                              StopPrice open(1) + StopLevel;
                                              
                              debugPrintln(" Short Index = " nIndex " StopPrice = " StopPrice);
                                          }
                                      }   

                                      if(
                              tii<=1){  // Go Long
                                          
                              debugPrintln(" tii<=1 Index = " nIndex " StopPrice = " StopPrice);
                                          if (
                              high(1) > high(0)){
                                              
                              Strategy.doLong("Long"Strategy.MARKETStrategy.NEXTBAR);
                                              
                              PTPrice open(1) + PTLevel;
                                              
                              StopPrice open(1) - StopLevel;
                                              
                              debugPrintln(" Long Index = " nIndex " StopPrice = " StopPrice);
                                          }
                                      }
                                  }
                                 
                                  return (
                              tii);

                              Garth

                              Comment


                              • #30
                                One more...This one will show you visually what the trades would look like (paints a red bar for shorts and green for longs). I also cleaned up some of the issue that would crop up on changes of state of symbols or intervals and fixed the profits to use ALL for the fill size.

                                Still not the most robust error checking...

                                PHP Code:
                                /**************************************************
                                Description : This Indicator plots Trend Intensity Index indicator
                                Provided By : Developed by TS Support, LLC for eSignal. (c) Copyright 2002
                                 ***************************************************/
                                var PTLevel 1.50;
                                var 
                                PTPrice 0;
                                var 
                                StopLevel 2.00;
                                var 
                                StopPrice 0;
                                var 
                                nLengthMA 60;
                                var 
                                nLengthTII 30;
                                var 
                                nInTrade 0;

                                function 
                                preMain() {

                                setStudyTitle("Trend Intensity Index");
                                setCursorLabelName("TII");
                                setStudyMin(-1);
                                setStudyMax(101);

                                addBand(20PS_SOLID1Color.black);
                                addBand(80PS_SOLID1Color.black);
                                }


                                function 
                                main() {
                                    var 
                                vMA call("/Library/MA.efs"nLengthMA);
                                    
                                    if (
                                vMA == null)
                                        return;
                                        
                                    if (
                                getBarState() == BARSTATE_ALLBARS){
                                        
                                PTPrice 0;
                                        
                                StopPrice 0;
                                        
                                nLengthMA 60;
                                        
                                nLengthTII 30;
                                        
                                nInTrade 0;
                                    }
                                    

                                    var 
                                arC close(0, -nLengthTII);
                                    if(
                                arC == null)
                                        return;

                                    var 
                                i;
                                    var 
                                dSDP 0;
                                    var 
                                dSDM 0;

                                    for(
                                0nLengthTIIi++) {
                                        if(
                                arC[i] > vMA) {
                                            
                                dSDP +=arC[i] - vMA;
                                        } else if(
                                arC[i] < vMA) {
                                            
                                dSDM += vMA arC[i];
                                        }
                                    }

                                    var 
                                tii= (dSDP / (dSDP dSDM)) * 100;
                                    
                                // debugPrintln("dSDP = " + dSDM + " dSDM = " + dSDM + " tii = " + tii);

                                    
                                var nIndex getCurrentBarIndex();
                                 

                                    
                                // test for Stops
                                    
                                if ((Strategy.isLong()) && (low() <= StopPrice)) {
                                        
                                nInTrade 0;
                                        if ((
                                Strategy.doSell("Stop Close"Strategy.LIMITStrategy.THISBARStrategy.ALLStopPrice)) == false){
                                            
                                debugPrintln("Error on doSell");
                                        }
                                    }
                                    if ((
                                Strategy.isShort()) && (high() >= StopPrice)) {
                                        
                                nInTrade 0;
                                        if ((
                                Strategy.doCover("Stop Close"Strategy.LIMITStrategy.THISBARStrategy.ALLStopPrice)) == false){
                                            
                                debugPrintln("Error on doCover");
                                        }
                                    }
                                    
                                    
                                // test for Profits
                                    
                                if ((Strategy.isLong()) && (high() >= PTPrice)) {
                                        
                                nInTrade 0;
                                        if ((
                                Strategy.doSell("Profit Close"Strategy.LIMITStrategy.THISBARStrategy.ALLPTPrice)) == false){
                                            
                                debugPrintln("Error on doSell Profit");
                                        }
                                    }
                                    if ((
                                Strategy.isShort()) && (low() <= PTPrice)) {
                                        
                                nInTrade 0;
                                        if ((
                                Strategy.doCover("Profit Close"Strategy.LIMITStrategy.THISBARStrategy.ALLPTPrice)) == false){
                                            
                                debugPrintln("Error on doCover Profit");
                                        }
                                    }

                                    
                                //  Entry Signals
                                    
                                if (!Strategy.isLong() && !Strategy.isShort()){ // Make sure we are not in a trade
                                        
                                if(tii>=99){  // Go Short
                                            
                                debugPrintln(" tii>=99 Index = " nIndex " StopPrice = " StopPrice);
                                            if (
                                low(1) < low(0)){
                                                
                                nInTrade=-1;
                                                
                                Strategy.doShort("Short"Strategy.MARKETStrategy.NEXTBAR);
                                                
                                PTPrice open(1) - PTLevel;
                                                
                                StopPrice open(1) + StopLevel;
                                                
                                debugPrintln(" Short Index = " nIndex " StopPrice = " StopPrice);
                                            }
                                        }   

                                        if(
                                tii<=1){  // Go Long
                                            
                                debugPrintln(" tii<=1 Index = " nIndex " StopPrice = " StopPrice);
                                            if (
                                high(1) > high(0)){
                                                
                                nInTrade=1;
                                                
                                Strategy.doLong("Long"Strategy.MARKETStrategy.NEXTBAR);
                                                
                                PTPrice open(1) + PTLevel;
                                                
                                StopPrice open(1) - StopLevel;
                                                
                                debugPrintln(" Long Index = " nIndex " StopPrice = " StopPrice);
                                            }
                                        }
                                    }
                                   
                                    if (
                                nInTrade == 1){
                                        
                                setBarBgColor(Color.green);
                                    } else if (
                                nInTrade == -1){
                                        
                                setBarBgColor(Color.red);
                                    }
                                    return (
                                tii);

                                Garth

                                Comment

                                Working...
                                X