Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Moving Stop loss SetStopLoss

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

    Moving Stop loss SetStopLoss

    I am using the following code and my stop loss modifies on the first modification but does not modify on the second and third modifications.
    For example: After 7 ticks profit, the stop is raised to entry price + 2 ticks, BUT after that, my other stoploss modifications do not appear to be working.
    I looked in the message boards and I could not find a clear answer on how to do this. I have looked at the Sample Price Modification Script.

    protected override void Initialize()
    //PROFIT TARGET AND STOP LOSS
    {
    SetProfitTarget("", CalculationMode.Ticks, ProfitTarget);
    SetStopLoss("", CalculationMode.Ticks, stoplossticks, false);
    CalculateOnBarClose = true;
    }

    protected override void OnBarUpdate()
    {
    // Condition set 1
    if ((myEntryOrder == null)
    && CrossAbove(Close, EMA(LengthMA), 1))

    {
    myEntryOrder = EnterLongLimit(0, true, DefaultQuantity, Close[0] + SlipTicks * TickSize, "Long Limit");

    }

    // Resets the stop loss to the original value when all positions are closed
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetStopLoss(CalculationMode.Ticks, stoplossticks);
    }

    // If a long position is open, allow for stop loss modification
    if (Position.MarketPosition == MarketPosition.Long)
    {
    // Once the price is greater than entry price+trailtrigger, set stop loss to breakeven + trail
    if (Close[0] >= (Position.AvgPrice + 7* TickSize))
    {
    SetStopLoss("", CalculationMode.Price, Position.AvgPrice + 2* TickSize, true);
    }
    if (Close[0] >= (Position.AvgPrice + 9* TickSize))
    {
    SetStopLoss("", CalculationMode.Price, Position.AvgPrice + 5* TickSize, true);
    }
    if (Close[0] >= (Position.AvgPrice + 11* TickSize))
    {
    SetStopLoss("", CalculationMode.Price, Position.AvgPrice + 7* TickSize, true);
    }
    }
    }

    #2
    Anybody know why this won't work? Thanks in advance. Greatly appreciate any help.

    Comment


      #3
      Hi Baseheadz,

      The reason you are seeing this is because the condition for the first movement continues to evaluate true at the same time as your other movements. To code the desired sequence you can use bool flags or user variables. You should first start with a two-stage movement until adding additional complexity of the third. Example of this below:

      Code:
      #region Variables
      private bool controlOne = true;
      #endregion
      
      if (Close[0] >= (Position.AvgPrice + 7* TickSize) && controlOne)
      {
      	SetStopLoss("", CalculationMode.Price, Position.AvgPrice + 2* TickSize, true);
      	controlOne = false;
      }
      
      if (Close[0] >= (Position.AvgPrice + 9* TickSize) && !controlOne)
      {
      	SetStopLoss("", CalculationMode.Price, Position.AvgPrice + 5* TickSize, true);
      }
      			
      //Reset your bool flag when flat. 
      if (Position.MarketPosition == MarketPosition.Flat)
      	controlOne = true;
      Ryan M.NinjaTrader Customer Service

      Comment


        #4
        Thanks Ryan, this worked very well. I have the 3-stage SetStopLoss working too.

        Comment


          #5
          Ryan, can I have different text print on the chart for each SetStopLoss modification?
          What is the proper syntax for this?

          Comment


            #6
            Ryan,

            Is it possible to have different names printed on the chart for each different SetStopLoss modification?

            Comment


              #7
              Yes, this is possible. I'm not sure what happened to my previous reply here, sorry. Just add a separate DrawText() with a unique tag for each movement.

              Code:
              if (Close[0] >= (Position.AvgPrice + 7* TickSize) && controlOne)
              {
              	SetStopLoss("", CalculationMode.Price, Position.AvgPrice + 2* TickSize, true);
              	controlOne = false;
                      DrawText("Mov1", "First stop loss movement", 0, High[0] + TickSize, Color.Black);
              
              }
              
              if (Close[0] >= (Position.AvgPrice + 9* TickSize) && !controlOne)
              {
              	SetStopLoss("", CalculationMode.Price, Position.AvgPrice + 5* TickSize, true);
                      DrawText("Mov2", "Second stop loss movement", 0, High[0] + TickSize, Color.Black);
              }
              Ryan M.NinjaTrader Customer Service

              Comment


                #8
                Thanks for that tip.

                Comment


                  #9
                  As stated before, the stop loss is is moving as planned.

                  In real time trading, I've noticed that the stop loss will not move until the bar closes at or above the trail trigger. For instance, if the trail trigger is Entry Price + 20 ticks, the trailing stop will not move until after the close of the bar prints Entry Price + 20.

                  How do I get the trailing stop to move once the price hits Entry Price + 20 Ticks?

                  Do I have to set COBC to false?

                  Thanks

                  Comment


                    #10
                    Originally posted by Baseheadz View Post
                    As stated before, the stop loss is is moving as planned.

                    In real time trading, I've noticed that the stop loss will not move until the bar closes at or above the trail trigger. For instance, if the trail trigger is Entry Price + 20 ticks, the trailing stop will not move until after the close of the bar prints Entry Price + 20.

                    How do I get the trailing stop to move once the price hits Entry Price + 20 Ticks?

                    Do I have to set COBC to false?

                    Thanks
                    Yes, then you can use GetCurrentBid() and GetCurrentAsk() to do touches...

                    Comment


                      #11
                      That worked. Nice.

                      Comment


                        #12
                        Hello,

                        I have my stop

                        stop1=DonchianChannel(4).Upper[0];

                        SetStopLoss("S1", CalculationMode.Price, stop1, false);

                        What do I have to change for my shorts so that stoploss is only realized when CLOSE is above stop1 (and not current price)? As I us Renkos, this is important. Do I understand in this thread that its enough to set COBC=true?

                        Thanks
                        Tony

                        Comment


                          #13
                          Tony, please do not post your question multiple times. I have already answered it on another thread.
                          AustinNinjaTrader Customer Service

                          Comment


                            #14
                            Thanks and sorry.

                            I didn´t see my post in the overview, so I thought its not OK because it was to an older thread.


                            Originally posted by NinjaTrader_Austin View Post
                            Tony, please do not post your question multiple times. I have already answered it on another thread.

                            Comment


                              #15
                              The posts on setStopLoss were really helpful, but I am having a different problem in getting the setStopLoss function to work. The protective stop loss order never triggers when I am running this strategy in Market replay and I can't figure out why. I have the following variable to control where the stop loss price is set at any point and where the order is at. I also track a variable called "currentEntry" which is just a price at to which I can compare the modified stop loss value so I know when to modify it. The idea of the stop loss strategy is that if we enter say on a long entry and then the next bars low exceeds the entry price, then modify the stop loss to the low of the new bar minus some number of trailing ticks. In other words I only want to modify the stop loss when the bars do not overlap.

                              Here is the stop loss code. I call a seperate function to manage the stop from within the OnBarUpdate method so here is that first part.

                              private String entryName = "filledOrder";
                              private double barHigh;
                              private double barLow;
                              private double barOpen;
                              private double barClose;
                              private double lastStopLoss = 0.0;
                              private double currentEntry = 0.0;
                              private double defaultStopLoss = 10;
                              private int trailingStopTicks = 1;
                              private IOrder runningOrder ;

                              protected override void OnBarUpdate()
                              {

                              barHigh = High[0];
                              barLow = Low[0];
                              barOpen = Open[0];
                              barClose = Close[0];


                              if(Position.MarketPosition == MarketPosition.Flat && runningOrder == null)
                              {

                              SetStopLoss(CalculationMode.Ticks, lastStopLoss);

                              //for brevity
                              if(SMA(20) > SMA(5))
                              {
                              lastStopLoss = barLow - (1 * TickSize);
                              currentEntry = Close[0] + (1 * TickSize);
                              SetStopLoss(CalculationMode.Price, lastStopLoss);
                              runningOrder = EnterLongStop(DefaultQuantity, currentEntry, entryName);
                              }
                              }
                              else
                              {
                              //run protective stop functions if in trade
                              if(Position.MarketPosition == MarketPosition.Long && runningOrder != null)
                              {
                              setStopLossStrat2( trailingStopTicks );
                              }
                              }
                              }

                              private void setStopLossStrat2(int trailingValue)
                              {
                              if(barLow > currentEntry)
                              {

                              lastStopLoss = barLow - ( trailingValue * TickSize );
                              currentEntry = Close[0];

                              SetStopLoss(entryName, CalculationMode.Price, lastStopLoss, true );
                              }
                              else
                              {
                              SetStopLoss(entryName, CalculationMode.Price, lastStopLoss, true );
                              }
                              Print(runningOrder.toString());
                              }

                              The stop loss order never gets triggered. Can't figure it out. It traces the stop order on each bar and the values are correct, but then it never triggers. I was reading about order management and it said that a setStopLoss(); may be ignored if the order is not simulated and was actually placed by setting the simulation parameter to false, but even when I set it to true it is failing to trigger the protective stop order. Any help is appreciated. Thank you.
                              Last edited by ggerstein; 02-03-2012, 05:56 PM.

                              Comment

                              Latest Posts

                              Collapse

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