Announcement

Collapse
No announcement yet.

Trailing stops not holding at maximum profit

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

  • Trailing stops not holding at maximum profit

    Hi,


    My efs is supposed to enable the stop to trail the price movement at x percent of profit during a trade. Ideally, I want the stop to trail at 50% of profit after .5 points of profit exists in a trade on the AB #F (e-mini russell 2000).

    The stop DOES trail as profit rises, but the stop level does not hold at its maximum profit level. As profits begin to decrease for the trade, the stop level is adjusting with price still.

    So somehow, my code is not holding the maximum stop level as price movement begins to diminish the profits of the trade.

    My attached efs is based on the code from the Sample_ProfitTargets.efs found in this forum.

    Any Ideas?

    Thanks in advance!

  • #2
    Here is the efs...
    Attached Files

    Comment


    • #3
      Hello wwhiterhino,

      Thank you for posting your code. Regarding your stop logic, what I'm seeing on my end is that your condition for adjusting the trailing stop is not getting evaluated for most of the bars where you are in a trade. Effectively, the stop is remaining fixed at its initial value set when the trade is entered. When the study does process a bar that causes your stop adjustment condition to evaluate to true, your adjustment calculation moves the stop up to a level that takes out the trade. You may need to reevaluate your logic for the trailing stop adjustments. To help you see how your formula is evaluating the stop adjustment condition, place a debugPrintln() statement inside the block of code that recalculates the stop price so you can see where the adjustments occur and what the new stop prices are being set to. The information from the debug statement will be printed to the Formula Output Window (Tools-->EFS).

      Here's an example of a debug statement you could try.

      debugPrintln(getCurrentBarIndex() + " short stop adj: " + nStopLevel);
      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


      • #4
        Thanks Jason for reviewing the code. I put a few debug lines into the code but all it shows me is what I already see on the chart. The stop line is yellow but it never stays at the maximum profit level.

        I did tried a few fixes tonight to try and plot the max stop levels at all times but had to back them out as they broke the code so bad the error window would not even load.

        So, unfortunately, I am back to square one.

        I did see that I need to plot the max stops and not the nStopLevel which adjusts every bar. But that is only half of it. The trades should be exiting when the max stops are hit and that is not happening.

        Attached is the latest code but it still doesn't work properly for the trailing stops as a percent of profit.

        Any further ideas are very welcome. Thanks!
        Attached Files

        Comment


        • #5
          Hello wwhiterhino,

          Here's some more suggestions for you. In short, I do see some logic problems with how the stop variables are being set. Here's what I would do.

          1) First, change all references to nHighStopLevel and nLowStopLevel to just nStopLevel for both the long and short logic. Since a back testing strategy can never be both long and short at the same time you can simplify the logic a bit by just recycling the same stop variable for both sides. You can continue to use nDisplayStop as you are for plotting purposes.

          2) Then add a second global variable called, nStopLevel_1. This will be used to store the previously calculated stop before you calculate a new value for nStopLevel. When the new value for nStopLevel is calculated you will then compare this new value to the previous (unless nStopLevel_1 == null) and keep the max or min value of the two using Math.max() and Math.min().

          PHP Code:
           /*----------------------------------------------------------
                                              ADDED THIS PORTION
                                              
              This portion of the code adjusts the stop levels if there is a profit during the trade.
              ----------------------------------------------------------*/
              
          if (Strategy.isInTrade() == true && (Strategy.isShort() == true) && (nNewTrade == 0)) {
                  
          // Adjust short stop level
                  
          nStopLevel_1 nStopLevel;
                  if(
          nTradeEntryPrice close() > .5) {
                      
          nStopLevel = (nTradeEntryPrice-((nTradeEntryPrice-low(-1))*ntrailStopprofitpercent));
                  }
                  if (
          nStopLevel_1 != null) {
                      
          nStopLevel Math.min(nStopLevel_1nStopLevel);
                  }
                  
          //if(nLowStopLevel > nStopLevel){nLowStopLevel = nStopLevel;//updates lowest stoplevel
                  //}
              
          }

              if (
          Strategy.isInTrade() == true && (Strategy.isLong() == true) && (nNewTrade == 0)) {
                  
          // Adjust long stop level
                  
          nStopLevel_1 nStopLevel;
                  if(
          close() - nTradeEntryPrice .5) {
                      
          nStopLevel = (nTradeEntryPrice+((high(-1)-nTradeEntryPrice)*ntrailStopprofitpercent));
                  }
                  if (
          nStopLevel_1 != null) {
                      
          nStopLevel Math.max(nStopLevel_1nStopLevel);
                  }
                  
          //if(nHighStopLevel < nStopLevel){nHighStopLevel = nStopLevel;//updates highest stoplevel
                  //}  
              

          3) Lastly, when you reset nStopLevel to null also reset nStopLevel_1 to null so that the stop values from the most recently closed position will not be used for the next position.

          Try this out and let us know if this helps.
          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


          • #6
            Thanks Jason, that cleaned up the stops and now they are calculating properly. With the min and max functions, the code runs a lot faster also.

            One thing I am noticing is that the initial stop is being set using the open price from the PREVIOUS bar just before the trade is opened.

            example: if a trade is initiated on bar -100, then the stop is being calculated using the open of bar -101, not -100). Why does it do that?

            Sometimes this sets the initial stop well away from the actual open price of the trade during large bars. Here is the code for that section...

            var bPosition = false;
            if (Strategy.isInTrade() == true && (nNewTrade == 1)) {
            // This sets the expected entry price of the current trade
            nTradeEntryPrice = open(0); //NOTE: open of -1 bar actually
            // This switches off the nNewTrade variable
            nNewTrade = 0; // Turn off NEW TRADE switch
            bPosition = true;

            }

            Attached is the improve code with the suggestions you made included in the code.

            Thanks again!
            Attached Files

            Comment


            • #7
              Hello wwhiterhino,

              You're most welcome.

              I noticed that you are setting nStopLevel to 0 in your reversing trade section on lines 194 and 200. Two lines above each, you are setting the stop value, which then gets changed to 0. Not sure if that is what you intended but you may want to consider removing those two lines that set nStopLevel to 0.

              In regards to where the initial stop is being set, you are correct that it is set relative to the low or high of the bar prior to the entry bar. That is because you are using the MARKET/NEXTBAR entry constants. That logic is fine as it will create the most realistic results due to the fact that your entry signals evaluate the closing values on the current bar. Since this is a back testing formula you have the luxury of knowing what the values are on the next bar, such as the open, which is being used as your entry price. You could simply use open(1) + or - nStopAmt to set the initial stops.
              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