Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stop modification problem

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

    Stop modification problem

    Is there a reason why the bellow code sometimes works (moves the stoploss correctly) and sometimes does not work at all or only the first step and the others no??

    protected override void Initialize()
    {
    SetStopLoss("", CalculationMode.Ticks, 200, false);
    CalculateOnBarClose = true;
    }

    protected override void OnBarUpdate()
    {
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetStopLoss(CalculationMode.Ticks, 200);
    }

    else if (Position.MarketPosition == MarketPosition.Long)
    {
    if (Close[0] > Position.AvgPrice + 500 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AvgPrice + 50 * TickSize);
    }

    if (Close[0] > Position.AvgPrice + 700 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AvgPrice + 200 * TickSize);
    }

    if (Close[0] > Position.AvgPrice + 1000 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AvgPrice + 600 * TickSize);
    }
    }

    else if (Position.MarketPosition == MarketPosition.Short)
    {
    if (Close[0] < Position.AvgPrice - 500 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AvgPrice - 50 * TickSize);
    }

    if (Close[0] < Position.AvgPrice - 700 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AvgPrice - 200 * TickSize);
    }

    if (Close[0] < Position.AvgPrice - 1000 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AvgPrice - 600 * TickSize);
    }
    }

    #2
    whitegun,

    Consider when if (Close[0] > Position.AvgPrice + 1000 * TickSize).

    In order to get that stop loss in you had to first submit two useless modifications before that. Your brokerage would probably not like 3 consecutive submissions almost simultaneously.

    Restructure your code so it evaluates the largest condition first and then use else-ifs to bring it down.

    Code:
    if (Close[0] > Position.AvgPrice + 1000 * TickSize)
        SetStopLoss(...);
    else if (Close[0] > Position.AvgPrice + 700 * TickSize)
        SetStopLoss(...);
    Run through the logic of this structuring by hand to see the difference.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Hello Josh,

      Probably you misunderstood what the code is supposed to do or I do not understand what you mean.

      If I am positive 50pips I move the stoploss to +5pip from entry if positive 70pips I move the stoploss to +20pip from entry and if positive 100pips I move the stoploss to +50pip from entry.

      If I backtest the code (no broker involved) it moves the stop sometimes correctly and sometimes not or sometimes does not move it at all or sometime jumpes one step.
      Last edited by whitegun; 02-17-2009, 01:15 PM.

      Comment


        #4
        whitegun,

        Please run through the logic on paper. You will see that you need to do it the way I suggested. You should not be submitting modifications so many times within a single OnBarUpdate().
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Hello Josh,

          Now I understand what you mean but it still does not work.

          Please have a look on the sample strategy I added to this post, seems I coded it now like you sugested but it still does not work.

          I am also adding a snapshot with a backtest where the problem appears so you can reproduce my case (the backtest is on the EURUSD
          Attached Files

          Comment


            #6
            whitegun,

            The next step you will want to take is to add Print()s into your if-statements and see if your code is even entering into those code blocks. Then you will want to use TraceOrders = true in Initialize() to see the status of your orders and if they are rejected/cancelled for any reason.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              I think I ran into something like this some time back.

              If I remember correctly, when you're using SetStopLoss in the OnBarUpdate method, you don't want to also invoke it from the Initialize method.

              Instead, you should add your "SetStopLoss(CalculationMode.Ticks, 200);" statement just before the EnterShort() call.

              Comment


                #8
                What could be happening is that the SetStopLoss() is not reset for the next position. Then when the next position is generated you end up submitting an invalid stop price and it is rejected.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_Josh View Post
                  What could be happening is that the SetStopLoss() is not reset for the next position. Then when the next position is generated you end up submitting an invalid stop price and it is rejected.
                  I think that was what I was running into and it was a problem in 6.0.

                  Does 6.5 still work the same way?

                  Or has 6.5 been changed so the SetStopLoss setting specified in Initialize gets used for the intitial stop loss value each time a trade entry is made from OnBarUpdate?

                  Comment


                    #10
                    No change. You need to manually reset before your next entry.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      Josh, but how is it possible to manually reset if you get a new signal in the contrary direction in stead of an exit signal, I mean what NT does is a reverse so you directly pass from being long for example to being short.
                      If you pass directly from short to long or long to short without being flat then the stop can not be reset?

                      Comment


                        #12
                        Please review this reference sample whitegun - http://www.ninjatrader-support2.com/...ead.php?t=3222

                        Just put this reset at the start of your OnBarUpdate() -
                        Code:
                        [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][FONT=Verdana] (Position.MarketPosition == MarketPosition.Flat)[/FONT][/SIZE]
                        [SIZE=2]{[/SIZE]
                        [SIZE=2]SetStopLoss(CalculationMode.Ticks, stoplossticks);[/SIZE]
                        [SIZE=2]}[/SIZE]
                        [/FONT][/SIZE][/FONT]

                        Comment


                          #13
                          Hello Bertrand,

                          I already know how to reset a Stop Loos, I may did not explained myselve clear.

                          I will comment my problem one more time:

                          I thing there is something wrong with NT because it is impossible, no matter how you formulate the logic, to move a Stop Loos more than once, it simply does not work correctly, at least not in a back test.

                          Be able to move the stop loos at least 2 or 3 times is vital for any swing strategy but it is totally impossible (or at least it seams impossible to me) in NT.

                          Comment


                            #14
                            whitegun,

                            Moving stop losses is completely possible. For instance:
                            Code:
                            if (Close[0] > Position.AvgPrice + 10 * TickSize)
                                 SetStopLoss(...);
                            else if (Close[0] > Position.AvgPrice + 5 * TickSize)
                                 SetStopLoss(...);
                            else if (Close[0] > Position.AvgPrice + 2 * TickSize)
                                 SetStopLoss(...);
                            If you have a long and a short I suggest you use two separate stop losses. One tied to the long's entry signal name and one tied to the short's.
                            Josh P.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

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