Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problem re-setting StopLoss

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

    Problem re-setting StopLoss

    Greetings,

    I'm trying to modify "StopLoss" so as to create a moving "TrailStop". My goal is to move the StopLoss for every 4 pips in favor of the direction of the trade. Referencing my screenshot it seems this is not being accomplished, and I'm not sure what is being calculated by the strategy. I know that StopLoss is supposed to be evaluated on a tick-by-tick basis so waiting on "bar close" is not the problem.

    The exits/stops do not seem to be happening when they should (when a bar "falls back" to a multiple of 4 pips above entry price...)

    Here's the code I'm using to set (change) the StopLoss:

    NOTE: "inflator2" is a multiple for the number 4, it begins at a value of "1"

    if(Position.MarketPosition == MarketPosition.Long)
    {
    if(subsequentCounter2 == 1)
    {
    if(Close[0] > Position.AvgPrice + ((4 * inflator2) * TickSize))
    {
    TRAILPlace = Position.AvgPrice + ((4 * inflator2) * TickSize);
    SetStopLoss("Condition 2 Entry", CalculationMode.Price, TRAILPlace, true); inflator2 += 1;
    } //Close[0] > Position.AvgPrice + TRAILPlace (TRAILSTOP needs to be moved up...)
    } //subsequentCounter2 is 1 (this position is "open"...)
    } //Position.MarketPosition is Long

    I was under the impression that "Position.AvgPrice" referred to the Entry price...is this not correct? Any hints appreciated...
    Attached Files

    #2
    Your screenshot shows things working as expected. Your code is submitting a modified stoploss orders only for the "Condition 2 Entry". In your picture you can see that you no longer have a "Condition 2 Entry" position as your strategy has already reversed your position with "Condition 5 Entry". In fact, your market position is now a Short so thats why your logic in Position.MarketPosition == MarketPosition.Long isn't working. You are now in a MarketPosition.Short.

    Position.AvgPrice returns the average price of your current position.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Hi,

      Thanks for postings. If Position.AvgPrice is the value for current price then I guess that's my problem, I had been using it thinking it represented entry price...is there a command set for entry price or does it need to be set by a variable within the strategy?

      Here's my logic process thinking when re-setting the StopLoss:

      Let's concentrate only on "Condition 2"--it opened (long) at 1.4756, and closed (about 23 bars later) at 1.4764. My goal is that when the price closes above an entry of a "multiple of 4" pips above entry (so in this case at 1.4760, 1.4764, 1.4768, etc) the StopLoss is reset at that level.

      In looking at the screenshot it can be seen that on the 2nd bar after entry the price closed above 1.4760...thus the StopLoss should have been reset there to 1.4760. This is where the trade should have exited because on the 3rd bar after entry the price fell back down "through" that level which should have taken out the stop...thus for a gain of 4 pips on that trade. Am I correct in assuming StopLoss is calculated tick-by-tick and NOT on bar close?

      Comment


        #4
        No. Let me clarify. Position.AvgPrice is not the current price. It is the average fill price of the current position.

        You'll need to debug the values. Use print functions to determine the value of inflator2 and also TRAILPlace. In your code I don't see anywhere for inflator2 to be reset. I suspect it to just keep incrementing larger and larger per Condition 2 trade.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          I've done this before, and the easiest way for me to debug it is whenever I call SetStopLoss, do this right afterwards:

          DrawDot("stop"+Time[0], 0, yourNewStopVal, Color.Black);

          It is obvious the first time you run it what's happening, because you'll see the adjusted stop values right on the chart.

          Comment


            #6
            Thanks for the repsonses. I've made some changes however unfortunately I'm not seeing the desired StopLoss "resets" at 4 pip increments...my re-worked code is as follows to change the StopLoss:

            NOTE: The variable "EnterPlace2" is the entry price when the trade executed...

            if(subsequentCounter2 == 1)
            {
            if(Close[0] >= EnterPlace2 + ((4 * inflator2) * TickSize))
            {
            TRAILPlace = EnterPlace2 + ((4 * inflator2) * TickSize);
            SetStopLoss("Condition 2 Entry", CalculationMode.Price, TRAILPlace, true); inflator2 += 1;
            DrawDot("stop"+Time[0], 0, TRAILPlace, Color.Blue);
            } //Close[0] > EnterPlace2 + TRAILPlace (TRAILSTOP needs to be moved up...)
            } //subsequentCounter2 is 1 (this position is "open"...)

            ...

            if(subsequentCounter2 == 1){subsequentCounter2 = 0; inflator2 = 1;}

            As can be seen in my screenshot the StopLoss was never "reset" as indicated by the fact there are no "blue dots" on the chart as there would be if it had been changed (as it should have in this case of "Condition 2 Entry")...as well as the fact the position never closed at 1.4760 as it would have had the StopLoss been set...
            Attached Files

            Comment


              #7
              I'm not sure what your logic to reset your subsequentCounter2 is what you want. Right now you have it just to reset itself if it is equal to 1. But with the first code segment your stoploss is only modified if your subsequentCounter2 is equal to 1.

              Instead you probably want logic that resets your counters and multipliers after your position becomes closed or something like that.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                Hmm, I see, I think you're correct. Is there some sort of variable that is set whenever a StopLoss is changed and/or a position is closed...I know about Position.MarketPosition but that will not work for a strategy that has multiple possible trading positions...

                Comment


                  #9
                  In NT6.5 you can probably check out OnPositionUpdate. Other than that you will probably just need to check the Position.Quantity against its previous value to determine if a position was closed out or not. There are no built in variable I am currently aware of to do this task so you will have to roll your own.
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    Hi,

                    Thank you. Are you saying that "Position.Quantity" can be retrieved for past values? For example:

                    if(Position.Quantity[0] > 0){do something}; //current
                    if(Position.Quantity[1] > 0){do something}; //last bar

                    And if that is true, from what I understand there's no way to seperate or "break-out" the Position.Quantity based upon different positions within the same strategy (Position.Quantity for "condition 1", "condition 2", etc.)...thus a strategy with multiple possible trade entry conditions needs to be broken out into different strategies, correct?

                    Comment


                      #11
                      No, that is not possible, you would need to store the prior value in a local int variable.
                      RayNinjaTrader Customer Service

                      Comment


                        #12
                        Thanks, and Position.Quantity takes ALL positions into account, thus there's no way to differentiate multiple conditional positions...correct? I guess the only work-around is to create different strategies for each...?

                        Comment


                          #13
                          Your understanding is correct.
                          RayNinjaTrader Customer Service

                          Comment


                            #14
                            Thanks for your response. I've re-worked my code into a "singular" strategy. I'm still seeing errors in "trailing" StopLoss resets that I cannot explain to save my life. It seems there are times when it works as expected, and other times when it does not...the only thing I can think of that might explain the problem is that the StopLoss is not being calculated/evaluated tick-by-tick, but I believe that should be the case for Stops and Profit targets...

                            My screenshot illustrates my point. The first trade on the chart (from the left) seems to have gone as expected, with a StopLoss moved downwards from the (short) entry. The second trade did not seem to move the StopLoss although (as the info box shows) the close of a bar following trade entry was well beyond the desired 4 pip re-set...

                            Here is my complete strategy code (minus the Print statements...):

                            //"In Variables"
                            double ExitPlace;
                            double EnterPlace;
                            double TRAILPlace;
                            int subsequentCounter = 0;
                            int inflator = 1;

                            //In "OnBarUpdate"
                            if(subsequentCounter == 1)
                            {
                            if(Close[0] <= EnterPlace - ((4 * inflator) * TickSize))
                            {
                            TRAILPlace = EnterPlace - ((4 * inflator) * TickSize);
                            SetStopLoss("Condition 1 Entry", CalculationMode.Price, TRAILPlace, true); inflator += 1;
                            DrawDot("stop" + Time[0], 0, TRAILPlace, Color.Blue);
                            } //Close[0] < TRAILPlace (TRAILSTOP needs to be moved up...)
                            } //subsequentCounter is 1 (this position is "open"...)

                            // Condition set 1
                            if (VarStoch(3, 5, 3, 3).VarStoK[0] >= 97
                            && SMIndex(2, 13, 3, 25).SMI[0] <= SMIndex(2, 13, 3, 25).SMI[1]
                            && subsequentCounter < 1)
                            {
                            EnterPlace = Low[0] - (2 * TickSize);
                            EnterShortStop(1, EnterPlace, "Condition 1 Entry");
                            ExitPlace = High[0] + (1 * TickSize);
                            SetStopLoss("Condition 1 Entry", CalculationMode.Price, ExitPlace, true);
                            subsequentCounter = 1;
                            }

                            if(Position.Quantity == 0)
                            {
                            if(subsequentCounter == 1){subsequentCounter = 0; inflator = 1;}
                            } //reset counter if it needs to be after trade triggers

                            Can somebody PLEASE point out the error here...it's beginning to become very frustrating for a relatively simple concept of simply moving a StopLoss...
                            Attached Files
                            Last edited by Burga1; 12-12-2007, 10:30 AM.

                            Comment


                              #15
                              Unfortunately we are just unable to review user code to debug. We can of course take a glance to see if there is anything that is blatently obvious. I realize that you are frustrated and feel like you are hitting your head against the wall but this is the nature of programming. I have been there many times developing NinjaTrader code.

                              I may sound like a broken record but what I always do is start with something basic. Strip everything down to a point where it works as expected and then add another line of code an so on, and so on...

                              That being said, make sure that you reset your stop loss values when you are flat. See the reference sample section for this.
                              RayNinjaTrader Customer Service

                              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
                              109 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              573 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              575 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X