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

TrailStop does not work properly

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

    TrailStop does not work properly

    Hi,
    I am trying to configure a trailing stop in my strategy.

    I want to enable trailing stop, after a certain ticks have been made in the right direction. I added an integer variable TrailProfitTrigger that I want to use as an offset between the Average position price and the current close[0].

    The problem is that the trailing stop is always executed without any reference to the offset in the averge position price and the current close.

    It acts as if I have a fixed TrailingStop that is always been added simultaneously with the orde.

    Here is the code that I am using for my long entries...


    if (Position.MarketPosition == MarketPosition.Long
    && Position.AvgPrice + TrailProfitTrigger * TickSize <= Close[0])


    {
    SetTrailStop("EnterLong", CalculationMode.Ticks, TrailingProfitTaker, false);
    }


    Any Ideas?

    #2
    Hello nikolaalx,

    Thank you for your post.

    In Initialize() are you using SetTrailStop() or SetStopLoss()?

    Comment


      #3
      I do not have either one.

      I place each stop individually when an entry is made as I know the SetStopLoss always overrides the TrailStop...if placed in the intitialize section.

      Here is a copy of my Initialize section:

      protected override void Initialize()
      {

      Add(ROC(ROCPeriod));
      Add(RSI(RSIPeriod, 3));
      Add(VMA(VMAFastPeriod, 9));
      Add(VMA(VMASlowPeriod, 9));
      Add(PeriodType.Minute, SecondBars);



      My biggest concern is why the trail stop is being triggered improperly.

      It is supposed to be triggered when the price of the instrument is in the profit zone. Instead, it is triggered instantly.

      The TrailingStop itself is executed properly. The problem arises from wrong triggering.

      Comment


        #4
        Hello nikolaalx,

        Thank you for your response.

        I place each stop individually when an entry is made
        Can you provide this section of your code? Is this when the entry is placed or in OnExecution() when the entry fills?

        Comment


          #5
          Sure,
          here is the code for the long entry trail stop.

          // Trail Stop Placer

          if (Variable4 == 1 //Var 4 is enabled whenever I have an active entry
          && Position.MarketPosition == MarketPosition.Long)

          {
          PrintWithTimeStamp("Trail Stop Long Placed at " + (Close[0] + TrailProfitTrigger * TickSize));

          if (Position.AvgPrice < Close[0] - TrailProfitTrigger * TickSize )
          {
          SetTrailStop("EnterLong", CalculationMode.Ticks, TrailingProfitTaker, false);
          }

          }



          Originally posted by NinjaTrader_PatrickH View Post
          Hello nikolaalx,

          Thank you for your response.


          Can you provide this section of your code? Is this when the entry is placed or in OnExecution() when the entry fills?

          Comment


            #6
            Hello nikollalx,

            Thank you for your response.

            Would you mind if we tested the full script on our end? You can send the .cs file for the strategy to platformsupport[at]ninjatrader[dot]com with this thread as a reference if you prefer: "http://www.ninjatrader.com/support/forum/showthread.php?t=72458"

            Comment


              #7
              Hello nikolaalx,

              Thank you for your patience.

              If you SetTrailStop in the strategy OnBarUpdate() method, you should always reset the trail stop price/offset value when your strategy is flat otherwise, the last price/offset value set will be used to generate your trail stop order on your next open position.

              You strategy is behaving in the same manner on my end. So each time we take a position it automatically updates to the Trail Stop point needed.

              If you want to wait until a specific level is reached you would want to implement your own trail logic using the Exit methods you wish.

              Comment


                #8
                I currently have the entries organized in the following order:

                1. When entry conditions are met, a limit order is placed and Variable4 is changed to 1.
                2. Then I have one block that monitors if the current market position = Long. Only in the event if the position is long (a.k the limit order has actually been filled and now we have an open position), and in the event Variable4 = 1 (which stands for enabled Trail Stop), then I do a nested check for if (Position.AvgPrice < Close[0] - TrailProfitTrigger * TickSize ) and then place the trail stop.

                With this being said, I am not sure what you mean by "you should always reset the trail stop price/offset value when your strategy is flat". Basically the TrailStop is placed only when the strategy is not flat.

                Also how can I reset the Trail/Offset? I am not sure I understand this....as those are not stored under any variables, They are executed with a fixed offset/fixed price.

                I am sorry if those questions seem stupid, but I have very basic understanding on programming. If you could elaborate and give an example, it will be very much appreciated!

                Here is the code for the Trail placer that I mentioned above.

                if (Variable4 == 1
                && Position.MarketPosition == MarketPosition.Long
                )
                {
                PrintWithTimeStamp("Trail Stop Long Placed at " + (Close[0] + TrailProfitTrigger * TickSize));
                if (Position.AvgPrice < Close[0] - TrailProfitTrigger * TickSize )
                {
                SetTrailStop("EnterLong", CalculationMode.Ticks, TrailingProfitTaker, false);
                }

                }

                Comment


                  #9
                  Hello nikolaalx,

                  Thank you for your response.

                  The SetTrailStop will be used again on the next entry - immediately.

                  For information on resetting the value please visit the following link and take a look at the SetStopLoss() being reset (same idea here): http://www.ninjatrader.com/support/f...ead.php?t=3222

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by kujista, Today, 05:44 AM
                  0 responses
                  5 views
                  0 likes
                  Last Post kujista
                  by kujista
                   
                  Started by ZenCortexCLICK, Today, 04:58 AM
                  0 responses
                  5 views
                  0 likes
                  Last Post ZenCortexCLICK  
                  Started by sidlercom80, 10-28-2023, 08:49 AM
                  172 responses
                  2,281 views
                  0 likes
                  Last Post sidlercom80  
                  Started by Irukandji, Yesterday, 02:53 AM
                  2 responses
                  18 views
                  0 likes
                  Last Post Irukandji  
                  Started by adeelshahzad, Today, 03:54 AM
                  0 responses
                  8 views
                  0 likes
                  Last Post adeelshahzad  
                  Working...
                  X