Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Trailing Stop

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

    Trailing Stop

    I am trying to implement the following (Strategy wizard if possible)

    I put out a trailing stop say 15 ticks from entry....

    Once I'm 12 ticks in profit, I want to move my trailing stop to breakeven...

    And for every additional 2 ticks, I want to move my trailing stop up by 1 tick...

    If I have to code it is there maybe some reference I could use?

    #2
    Hi BigDog008, I would suggest going into the NinjaScript editor to realize your custom trailing stop algorithm.

    This reference sample will be a good start - http://www.ninjatrader-support2.com/...ead.php?t=3222

    Please also check the trailing stop scripts from sharing section for further refinement - http://www.ninjatrader-support2.com/...ng+stop&desc=1
    BertrandNinjaTrader Customer Service

    Comment


      #3
      I'm trying to do a trailing stop consisting of last bar close +/- ATR depending on positions and keep getting this error...

      Operator '-' cannot be applied to operands of type 'double' and 'NinjaTrader.Indicator.ATR'

      My code is:

      SetTrailStop("Long C", CalculationMode.Price, Close[1] - ATR(Cci), false);

      any ideas?

      Comment


        #4
        You cannot use SetTrailStop(). If you want a stop where you can control the prices you need to be submitting explicit Exit() orders and just modifying the prices in accordance with the ATR().
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Josh,

          could you point me to an example? I'm pretty new to actually coding the strategy rather than using the wizard.

          Comment


            #6
            Unfortunately there are no examples.

            Untested code:
            Code:
            if (Position.MarketPosition == MarketPosition.Long)
                 ExitLongStop(Close[1] - ATR(Cci)[0], "Long C");
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              I still seem to be having difficulty... would really appreciate it if I could get some help on this....

              Essentially I'm trying to tighten my trailing stops as I get deeper in profit... contract is ES 03-09

              Code:
              /// <summary>
                      /// Called on each bar update event (incoming tick)
                      /// </summary>
                      protected override void OnBarUpdate()
                      {
                          // Resets the stop loss to the original value when all positions are closed
                          if (Position.MarketPosition == MarketPosition.Flat)
                          {
                              SetTrailStop("Long A", CalculationMode.Percent, 0.004, false);
                              SetTrailStop("Long B", CalculationMode.Percent, 0.004, false);
                              SetTrailStop("Long C", CalculationMode.Percent, 0.004, false);
                              SetTrailStop("Short A", CalculationMode.Percent, 0.004, false);
                              SetTrailStop("Short B", CalculationMode.Percent, 0.004, false);
                              SetTrailStop("Short C", CalculationMode.Percent, 0.004, false);
                          }
                          // Trailing Stop Adujustment for Long Position (8 ticks Profit, Breakeven TrailStop)
                          if (Position.MarketPosition == MarketPosition.Long)
                          {
                              // Once the price is greater than entry price + 8 ticks, set stop loss to breakeven
                              if (Close[0] > Position.AvgPrice + 8 * TickSize)
                              {
                                  SetTrailStop("Long B", CalculationMode.Price, Position.AvgPrice, false);
                                  SetTrailStop("Long C", CalculationMode.Price, Position.AvgPrice, false);
                              }
                          }
              
                          // Trailing Stop Adujustment for Short Position (8 ticks Profit, Breakeven TrailStop)
                          if (Position.MarketPosition == MarketPosition.Short)
                          {
                              // Once the price is less than entry price - 8 ticks, set stop loss to breakeven
                              if (Close[0] < Position.AvgPrice - 8 * TickSize)
                              {
                                  SetTrailStop("Short B", CalculationMode.Price, Position.AvgPrice, false);
                                  SetTrailStop("Short C", CalculationMode.Price, Position.AvgPrice, false);
                              }
                          }
              essentially I want to pull my trailing stop from 0.4% to breakeven (8 ticks profit) to say current price +/- ATR when 16 ticks in profit... trading a total of 3 contracts...

              really really appreciate any guidance on this

              Comment


                #8
                BigDog008,

                Please do not use SetTrailStop(). Use ExitLongStop to explicitly manage your orders. Alternatively, you can use SetStopLoss() and move SetStopLoss() around, but you CANNOT use SetTrailStop().
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Just for my knowledge then, SetTrailStop is not an updateable function? Is it something which is simply initially set and then left alone?

                  Therefore, for order management purposes, I should stick to the functions you mentioned?

                  Also, can i set both SetTrailStop() and SetStopLoss() initially on the same signal? The idea is I set both, as I move closer to +8 ticks in profit my SetTrailStop moves up, and then once I am greater than +8 ticks in profit I begin to move my SetStopLoss()

                  Appreciate it
                  Last edited by BigDog008; 03-03-2009, 08:34 AM.

                  Comment


                    #10
                    BigDog008,

                    SetTrailStop() is something you would want to set up and then leave it alone. It's logic is to trail on every tick. Modifying may or may not necessarily provide the behavior you would expect. You can do it, but just be aware of the caveats of it. The nature of the order is that it is constantly being modified. Your manual modifications may overwrite or be overwritten by the trailing stop logic and it has potential to get muddled up. Instead, run your own logic on SetStopLoss() or ExitLongStop().

                    You cannot have both SetTrailStop() and SetStopLoss() on a single order simultaneously.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      If I wanted to use the print command to debug.... and I want to print the price I entered for different signals... ie

                      Code:
                                      
                      EnterLong(DefaultQuantity, "Long A");
                      EnterLong(DefaultQuantity, "Long B");             
                      EnterLong(DefaultQuantity, "Long C");
                      and also want to print the updated stop loss values for:

                      Code:
                      SetTrailStop("Long A", CalculationMode.Percent, 0.004, false);
                      SetTrailStop("Long B", CalculationMode.Percent, 0.004, false);
                      SetTrailStop("Long C", CalculationMode.Percent, 0.004, false);
                      how would I do that?

                      Comment


                        #12
                        If you want to track your individual signals you need to use IOrder objects. Then you can access the IOrder's properties to get their fill prices.

                        The way to get the trail stop prices is running your own tracker variable that mimics the trail stop logic to determine the price. Please see this thread: http://www.ninjatrader-support2.com/...ad.php?t=10344
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          ExitLongStop problem

                          I don't know why this ExitLongStop don't work, I don't use SetStopLoss

                          ..
                          ..
                          .
                          .



                          SetProfitTarget("BL",CalculationMode.Ticks,(Ptarge t*TickSize));
                          EnterLong("BL");

                          }
                          }
                          }
                          }

                          //Trailing Stops

                          if (Position.MarketPosition==MarketPosition.Long)
                          {
                          double perf=Position.GetProfitLoss(Close[0], PerformanceUnit.Points);
                          if(perf>20)
                          {
                          double advv=(MAX(Low,5)[0])-(TickSize*8);
                          DrawText("aa"+CurrentBar,"*",0,advv,Color.Red);

                          ExitLongStop(advv);






                          Originally posted by NinjaTrader_Josh View Post
                          BigDog008,

                          SetTrailStop() is something you would want to set up and then leave it alone. It's logic is to trail on every tick. Modifying may or may not necessarily provide the behavior you would expect. You can do it, but just be aware of the caveats of it. The nature of the order is that it is constantly being modified. Your manual modifications may overwrite or be overwritten by the trailing stop logic and it has potential to get muddled up. Instead, run your own logic on SetStopLoss() or ExitLongStop().

                          You cannot have both SetTrailStop() and SetStopLoss() on a single order simultaneously.

                          Comment


                            #14
                            I suggest you use Print statements to make sure your conditions actually trigger the ExitLongStop placement...also working with TraceOrders = true in the Initialize() will be helpful to check if and why it may get ignored - http://www.ninjatrader-support.com/H...aceOrders.html

                            BertrandNinjaTrader Customer Service

                            Comment


                              #15
                              Conditions met, I have debug. It just not close position, values are true.
                              Its working with stoploss, but i don't want use stop loss, because i want see when it's hit by trailing profit.

                              Originally posted by NinjaTrader_Bertrand View Post
                              I suggest you use Print statements to make sure your conditions actually trigger the ExitLongStop placement...also working with TraceOrders = true in the Initialize() will be helpful to check if and why it may get ignored - http://www.ninjatrader-support.com/H...aceOrders.html

                              http://www.ninjatrader-support2.com/...ead.php?t=3627

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by carnitron, 04-27-2024, 08:42 PM
                              2 responses
                              18 views
                              0 likes
                              Last Post carnitron  
                              Started by cmtjoancolmenero, 04-25-2024, 03:58 PM
                              13 responses
                              49 views
                              0 likes
                              Last Post cmtjoancolmenero  
                              Started by sgordet, Today, 10:40 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post sgordet
                              by sgordet
                               
                              Started by RaygunWizzle, Today, 09:30 AM
                              2 responses
                              19 views
                              0 likes
                              Last Post bltdavid  
                              Started by Ashkam, Today, 09:28 AM
                              0 responses
                              15 views
                              0 likes
                              Last Post Ashkam
                              by Ashkam
                               
                              Working...
                              X