Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trouble with setting trailing stops and establishing stoplosses

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

    Trouble with setting trailing stops and establishing stoplosses

    I have successfully constructed a strategy. I am modifying it a little to include a trailing stop once a price target is met as well as a flat stop once price goes a little past entry level.

    Basically, there are 3 price levels involved in this strategy: entryLevel, exitLevel, and stopLevel. On the long side, once price crosses entrylevel, a long order is triggered. If the price reaches pulls back and reaches the stopLevel, the long is closed. If the price reaches the price target, I wanted a trailing stop issued. I wanted the trail to be the same distance between the entryLevel and stopLevel. Additionally, I wanted there to be a stop for a minimal gain to be ready when I am long and the price exceeds 2 points above the entryLevel.

    Here is the code. The parts that I have added and subsequently had troubles with are bolded. This is all in OnBarUpdate() by the way:

    Code:
    if (Position.MarketPosition == MarketPosition.Flat)
                {
                    if (MyIndicator.LongChanceInProgress)
                    {    
                    if (GetCurrentAsk() > MyIndicator.entryLevel && Open [0] <=MyIndicator.entryLevel && GetCurrentAsk() < MyIndicator.exitLevelLong)
                    {
                    SendMail("[email protected]","[email protected]", Instrument.MasterInstrument.Name, "Buy Signal - Target"+" "+MyIndicator.exitLevelLong);
                    EnterLong(1);
                    }
                    }
                }
            
                else if (Position.MarketPosition == MarketPosition.Long)
                {
                    if (GetCurrentAsk() > MyIndicator.exitLevelLong)
                    {
                    SendMail("[email protected]","[email protected]", Instrument.MasterInstrument.Name, "Long Target reached - Trailing Stop Set");
                    [B]SetTrailStop(CalculationMode.Ticks, (MyIndicator.entryLevel - MyIndicator.stopLevelLong)); //used to be just ExitLong(1)[/B]
                    }
                    else if (GetCurrentAsk() < MyIndicator.stopLevelLong)
                    {
                    SendMail("[email protected]","[email protected]", Instrument.MasterInstrument.Name, "Close Long - Stopped Out");
                    ExitLong(1);
                    }
                    [B]else if (Close [0] <= (MyIndicator.entryLevel + 2) && Open [0] >= (MyIndicator.entryLevel + 2))[/B]
                    {
                    SendMail("[email protected]","[email protected]", Instrument.MasterInstrument.Name, "Close Long - Stopped for minimal gain");
                    ExitLong(1); 
                    }
                }

    What ends up happening when I test this strategy is that if the target is reached, the strategy immediately exits the position instead of setting a trailing stop.

    Also, When the price is ascending and reaches entryLevel +2 it immediately exits my long instead of what its supposed to do, which is exiting my long if the price is descending and hits entryLevel + 2. So in essence the price can never reach the target with me being long. (I had to disable this part in order to test if the trailing stop worked)

    I have stared at this code for hours and am unable to figure out what I did wrong. Help?

    #2
    Anybody have any ideas?

    Comment


      #3
      Hello sra18376,

      Thank you for your post.

      For SetTrailStop(CalculationMode.Ticks, (MyIndicator.entryLevel - MyIndicator.stopLevelLong)); you will need to divide the value of the price levels by ticks. So it would be the following:
      Code:
      SetTrailStop(CalculationMode.Ticks, (MyIndicator.entryLevel - MyIndicator.stopLevelLong)/TickSize);
      For else if (Close [0] <= (MyIndicator.entryLevel + 2) && Open [0] >= (MyIndicator.entryLevel + 2)) we would need to multiply 2 by TickSize to get the proper value and since we are looking for descending we would likely want to check the previous close as well:
      Code:
      else if (Close [0] <= (MyIndicator.entryLevel + 2 * TickSize) && Open [0] >= (MyIndicator.entryLevel + 2 * TickSize && Close[1] > (MyIndicator.entryLevel + 2 * TickSize)))
      For information on TickSize please visit the following link: http://www.ninjatrader.com/support/h...7/ticksize.htm

      Please let me know if you have any questions.

      Comment


        #4
        Thanks so much Patrick. I will try this out tomorrow and update with results. This could be the solution to some serious frustration!

        Comment


          #5
          PS. I am only using this strategy on ES. is the tick size still an issue?

          Comment


            #6
            works like a charm thanks again

            Comment


              #7
              Hello sra18376,

              TickSize, just ensures we are using the correct values.

              Comment


                #8
                OK, new problem. Once the SetTrailStop has been entered and the stop is hit, my position closes like it should. But then the next time it goes long, the same trailing stop is immediately issued, instead of waiting for the price to go above exitLevel. Is there a way to "reset" the trailing stop order once it has been issued once before?

                Comment


                  #9
                  Hello sra18376,

                  Thank you for your response.

                  Yes, you would want to do this when Position.MarketPosition == MarketPosition.Flat, and set the SetTrailStop() to a default value. An example of this is seen using the SetStopLoss() method in the reference sample at the following link: http://www.ninjatrader.com/support/f...ead.php?t=3222

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                  0 responses
                  648 views
                  0 likes
                  Last Post Geovanny Suaza  
                  Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                  0 responses
                  369 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by Mindset, 02-09-2026, 11:44 AM
                  0 responses
                  108 views
                  0 likes
                  Last Post Mindset
                  by Mindset
                   
                  Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                  0 responses
                  572 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by RFrosty, 01-28-2026, 06:49 PM
                  0 responses
                  574 views
                  1 like
                  Last Post RFrosty
                  by RFrosty
                   
                  Working...
                  X