Announcement

Collapse
No announcement yet.

strategy using trend strength indicator

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

  • #31
    gspiker,
    Your last code does exactly what i need . Now i can fine tune for objectives and stops. The first adjustemnet anyone using this method should do is change the tii<=1 to tii<= 20,15 or 10 and change tii>=99 to tii>=80, 85 or 90.
    Next depending on what time frame you are using i would change the 60,30 to 10,5 or 20,10 or 5,3.
    The exception to buying if high or low is exceeded is the daily rth chart. In this timeframe you would either buy or sell the open the next day depending on the tii value being >85 or <15 or whatever paremeters you choose.
    Once again thanks for all the work everyone has done on this strategy
    Yoda

    Comment


    • #32
      gspiker,
      There is one thing that needs to be adjusted on the efs . When i backtest it does enter on bars that print below the low or high of the trigger. However, instead of entering at that level the efs enters on the open of that bar.
      For example if a bar has a tii of 100 and low of that bar is 850. the next bar opens at 851. If the next bar prints below 850 instead of making my entry point as 849.75 it takes the open of that bar at 851 as my entry which of course makes the results unrealistic.
      Besides that the efs works fantastic
      Yoda

      Comment


      • #33
        I can fix that, should be easy. I have some other stuff to do first, will post update later tonight.
        Garth

        Comment


        • #34
          Hi,

          Please read all this post. There are some issues you need to understand.

          This version has:

          1) Entry based on low/high of previous bar. Since we don't get tick data in backtest mode, I created a new variable (nSWAGPrice) that is set to the amount you think would be a good SWAG at the fill price if you wait for the price to drop below/rise above the lows/highs of the previous bar. Currently set to .25.

          2) I added a new condition for stops since I felt that the way we were doing it wasn't realistic. Now I test to see not only if the price has dropped through(longs)/ risen above(shorts) the stop price, but I check to see if the open has gapped through the stop price. If it does I sell at the open price.


          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;
          var 
          nSWAGPrice 0.25 // Price to add/subtract from high/low for entry on backtest

          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;
                  
              var 
          entry;
                  
              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)) {
                  
          // debugPrintln("Long Stop Loss");
                  
          nInTrade 0;
                  if (
          open() < StopPrice// Stop at stop price, unless the bar gaps lower, in which case sell on gap
                      
          StopPrice open();
                  if ((
          Strategy.doSell("Stop Close"Strategy.LIMITStrategy.THISBARStrategy.ALLStopPrice)) == false){
                      
          debugPrintln("Error on doSell Stop");
                  }
              }
              if ((
          Strategy.isShort()) && (high() >= StopPrice)) {
                  
          // debugPrintln("Short Stop Loss");
                  
          nInTrade 0;
                  if (
          open() > StopPrice// Stop at stop price, unless the bar gaps above, in which case cover on gap
                      
          StopPrice open();
                  if ((
          Strategy.doCover("Stop Close"Strategy.LIMITStrategy.THISBARStrategy.ALLStopPrice)) == false){
                      
          debugPrintln("Error on doCover Stop");
                  }
              }
              
              
          // test for Profits
              
          if ((Strategy.isLong()) && (high() >= PTPrice)) {
                  
          // debugPrintln("Long Profit Stop");
                  
          nInTrade 0;
                  if ((
          Strategy.doSell("Profit Close"Strategy.LIMITStrategy.THISBARStrategy.ALLPTPrice)) == false){
                      
          debugPrintln("Error on doSell Profit");
                  }
              }
              if ((
          Strategy.isShort()) && (low() <= PTPrice)) {
                  
          // debugPrintln("Short Profit Stop");
                  
          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
                      
          if (low(1) < low(0)){
                          
          entry=low(0)-nSWAGPrice
                          nInTrade
          =-1;
                          
          Strategy.doShort("Short"Strategy.MARKETStrategy.NEXTBARStrategy.DEFAULT, entry);
                          
          PTPrice entry PTLevel;
                          
          StopPrice entry StopLevel;
                          
          // debugPrintln(" Short Index = " + nIndex + " StopPrice = " + StopPrice + " entry = " + entry);
                      
          }
                  }   

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

          Garth

          Comment


          • #35
            gspiker,
            When i backtest your latest verison my results come out wacky.
            First it varies the number of contracts in the trades from 1 to 840 instead of keeping it at just one.Maybe this has more to do with my settings than your code but this has not occured on any of your previuos versions.
            Second each trade should have either exactly a 1.5 point gain or 2 point loss which was the case in your previous version but not in this version.
            Third, the SWAG varaible does not seem to have any effect on the entry price as all entries are still at the open of the next bar when i look at the trades. The strange thing is when i look at your code it looks like it should work fine.
            Once again thanks for all your hard work on this strategy
            Yoda

            Comment


            • #36
              First it varies the number of contracts in the trades from 1 to 840 instead of keeping it at just one.
              This could be my fault...let me look into it.

              Second each trade should have either exactly a 1.5 point gain or 2 point loss which was the case in your previous version but not in this version.
              This would be expected due to the fact I now take into account gaps (at least for stops...I need to do the same for profits - right now I am only applying penatly side of gaps, not the bonus side). Usually there are two ways of trading stops:
              1) Wait for the close of the bar to determine if the stop should be taken.
              2) Take any price below the stop price

              I assumed you were doing #2 based on the way you described the study. Given that, most traders trading this way, on a gap lower than their stop for a long (higher for a short) will sell on the open of the gap rather than playing games and seeing of they can get their target stop price. Hence there will be times when you don't get filled at the 2.00 stop...it may be more when a strong gap down happens.

              G
              Garth

              Comment


              • #37
                This fixes the problem with the # of contracts/shares.
                I also added the gap code for profits.

                If you don't want the gap code, comment out the lines that reset StopPrice and PTPrice right before the doSell and doCover in the Stops and Profit test sections. However, I have to caution this will not make for a very real scenerio as this will let you cover at better price than may be possible once the loss stops are triggerd.



                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;
                var 
                nSWAGPrice 0.25 // Price to add/subtract from high/low for entry on backtest

                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;
                        
                    var 
                entry;
                        
                    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)) {
                        
                // debugPrintln("Long Stop Loss");
                        
                nInTrade 0;
                        if (
                open() < StopPrice// Stop at stop price, unless the bar gaps lower, in which case sell on gap
                            
                StopPrice open();
                        if ((
                Strategy.doSell("Stop Close"Strategy.LIMITStrategy.THISBARStrategy.ALLStopPrice)) == false){
                            
                debugPrintln("Error on doSell Stop");
                        }
                    }
                    if ((
                Strategy.isShort()) && (high() >= StopPrice)) {
                        
                // debugPrintln("Short Stop Loss");
                        
                nInTrade 0;
                        if (
                open() > StopPrice// Stop at stop price, unless the bar gaps above, in which case cover on gap
                            
                StopPrice open();
                        if ((
                Strategy.doCover("Stop Close"Strategy.LIMITStrategy.THISBARStrategy.ALLStopPrice)) == false){
                            
                debugPrintln("Error on doCover Stop");
                        }
                    }
                    
                    
                // test for Profits
                    
                if ((Strategy.isLong()) && (high() >= PTPrice)) {
                        if (
                open() > PTPrice// Stop at stop price, unless the bar gaps lower, in which case sell on gap
                            
                PTPrice open();
                        
                // debugPrintln("Long Profit Stop");
                        
                nInTrade 0;
                        if ((
                Strategy.doSell("Profit Close"Strategy.LIMITStrategy.THISBARStrategy.ALLPTPrice)) == false){
                            
                debugPrintln("Error on doSell Profit");
                        }
                    }
                    if ((
                Strategy.isShort()) && (low() <= PTPrice)) {
                        if (
                open() < PTPrice// Stop at stop price, unless the bar gaps above, in which case cover on gap
                            
                PTPrice open();
                        
                // debugPrintln("Short Profit Stop");
                        
                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
                            
                if (low(1) < low(0)){
                                
                entry=low(0)-nSWAGPrice
                                nInTrade
                =-1;
                                
                Strategy.doShort("Short"Strategy.MARKETStrategy.NEXTBARStrategy.DEFAULT, entry);
                                
                PTPrice entry PTLevel;
                                
                StopPrice entry StopLevel;
                                
                // debugPrintln(" Short Index = " + nIndex + " StopPrice = " + StopPrice + " entry = " + entry);
                            
                }
                        }   

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

                Garth

                Comment


                • #38
                  gspiker,
                  Im using this strategy to play es all sessions intraday so the odds of a gap that exceeds my stop is very very small. However in order to apply this method to a daily chart your gap code is very helpful even though if i was working this off a daily chart my profit objective would be 7 points and my stop be 10 points.
                  When i comment out the gap code the strategy is still printing the open of the next as my entry instead of the high or low of the previous bar. So it seems the SWAG variable is either not working
                  in the way i anticipated or maybe its impossible for an efs code to perform such an operation.

                  Once again thanks for your work here,
                  Yoda

                  Comment


                  • #39
                    Yodda,

                    There could easily be a bug in my code. Let me take a look.

                    Garth
                    Garth

                    Comment


                    • #40
                      Yoda,

                      It was my mistake, I should have changed the entrys from Market to Limit...

                      I also fixed the visual que when you load the efs on the chart. it was signaling one bar early (neat trick if you can do it)...

                      Here is the new code...

                      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.5;
                      var 
                      PTPrice 0;
                      var 
                      StopLevel 2.00;
                      var 
                      StopPrice 0;
                      var 
                      nLengthMA 60;
                      var 
                      nLengthTII 30;
                      var 
                      nInTrade 0;
                      var 
                      nSWAGPrice 0.25 // Price to add/subtract from high/low for entry on backtest

                      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;
                              
                          var 
                      entry;
                          var 
                      i;
                          var 
                      dSDP 0;
                          var 
                      dSDM 0;

                          if (
                      nInTrade == 1){
                              
                      setBarBgColor(Color.green);
                          } else if (
                      nInTrade == -1){
                              
                      setBarBgColor(Color.red);
                          }
                         
                          if (
                      getBarState() == BARSTATE_ALLBARS){
                              
                      PTPrice 0;
                              
                      StopPrice 0;
                              
                      nLengthMA 60;
                              
                      nLengthTII 30;
                              
                      nInTrade 0;
                          }
                          
                          var 
                      arC close(0, -nLengthTII);
                          if(
                      arC == null)
                              return;
                        
                          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)) {
                              
                      // debugPrintln("Long Stop Loss");
                              
                      nInTrade 0;
                              if (
                      open() < StopPrice// Stop at stop price, unless the bar gaps lower, in which case sell on gap
                                  
                      StopPrice open();
                              if ((
                      Strategy.doSell("Stop Close"Strategy.LIMITStrategy.THISBARStrategy.ALLStopPrice)) == false){
                                  
                      debugPrintln("Error on doSell Stop");
                              }
                          }
                          if ((
                      Strategy.isShort()) && (high() >= StopPrice)) {
                              
                      // debugPrintln("Short Stop Loss");
                              
                      nInTrade 0;
                              if (
                      open() > StopPrice// Stop at stop price, unless the bar gaps above, in which case cover on gap
                                  
                      StopPrice open();
                              if ((
                      Strategy.doCover("Stop Close"Strategy.LIMITStrategy.THISBARStrategy.ALLStopPrice)) == false){
                                  
                      debugPrintln("Error on doCover Stop");
                              }
                          }
                          
                          
                      // test for Profits
                          
                      if ((Strategy.isLong()) && (high() >= PTPrice)) {
                              if (
                      open() > PTPrice// Stop at stop price, unless the bar gaps lower, in which case sell on gap
                                  
                      PTPrice open();
                              
                      // debugPrintln("Long Profit Stop");
                              
                      nInTrade 0;
                              if ((
                      Strategy.doSell("Profit Close"Strategy.LIMITStrategy.THISBARStrategy.ALLPTPrice)) == false){
                                  
                      debugPrintln("Error on doSell Profit");
                              }
                          }
                          if ((
                      Strategy.isShort()) && (low() <= PTPrice)) {
                              if (
                      open() < PTPrice// Stop at stop price, unless the bar gaps above, in which case cover on gap
                                  
                      PTPrice open();
                              
                      // debugPrintln("Short Profit Stop");
                              
                      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
                                  
                      if (low(1) < low(0)){
                                      
                      entry=low(0)-nSWAGPrice;
                                      
                      nInTrade=-1;
                                      
                      Strategy.doShort("Short"Strategy.LIMITStrategy.NEXTBARStrategy.DEFAULT, entry);
                                      
                      PTPrice entry PTLevel;
                                      
                      StopPrice entry StopLevel;
                                      
                      // debugPrintln(" Short Index = " + nIndex + " StopPrice = " + StopPrice + " entry = " + entry);
                                  
                      }
                              }   

                              if(
                      tii<=1){  // Go Long
                                  
                      if (high(1) > high(0)){
                                      
                      entry high(0)+nSWAGPrice;
                                      
                      nInTrade=1;
                                      
                      Strategy.doLong("Long"Strategy.LIMITStrategy.NEXTBARStrategy.DEFAULT, entry);
                                      
                      PTPrice entry PTLevel;
                                      
                      StopPrice entry StopLevel;
                                      
                      // debugPrintln(" Long Index = " + nIndex + " StopPrice = " + StopPrice + " entry = " + entry);
                                  
                      }
                              }
                          }
                         
                          
                          return (
                      tii);

                      Garth

                      Comment


                      • #41
                        gs,
                        Thank you very much. Where in the code would you suggest i add my code for color coding the price bars based on if the TII is increasing or decreasing.
                        Yoda

                        Comment


                        • #42
                          Yoda,

                          You are trying to paint the price bars colors, right?

                          OK, first of all, I think you need to make sure setPriceStudy(true);
                          is set in your preMain(). Since I haven't played with this much, I don't know for sure, but I'm 90% sure this is true.

                          Also, if you setPriceStudy(true), you will want to not return the values of tii...since it will be all out of scale with the price.

                          Second, I would put it near the end of the study and just check for your condition before returning...

                          HTH...

                          Garth
                          Garth

                          Comment


                          • #43
                            gs,
                            The statements setPriceStudy(true) and setColorPriceBars(true) and setDefaultPriceBarColor are already in my efs as i have created several efs studies that color code my bars based on the level of an indicator.
                            My problem is still getting the efs to recognize a variable that repesents the previous bars and current bars values of the tti so that i can subtract them from each other for my conditon.
                            It seems everytime i put in that condition the program wont return any data at all.
                            Thanks
                            Yoda

                            Comment

                            Working...
                            X