Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

StopLoss issue - NT bug?

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

    StopLoss issue - NT bug?

    I'm developing a trading system.
    I created a variable
    double StopLossLong = Math.Min(value1, value2)
    double StopLossShort = Math.Max(value1, value2)

    then

    if (Position.MarketPosition == MarketPosition.Long)
    {
    SetStopLoss(CalculationMode.Price, StopLossLong);
    }

    if (Position.MarketPosition == MarketPosition.Short)
    {
    SetStopLoss(CalculationMode.Price, StopLossShort);
    }

    Then I printed out the values:
    Print(Time[0] + " " + Close[0] + " " + value1Long + " " + value2Long + " " + StopLossLong + " " + value1Short + " " + value2Short+ " " + StopLossShort);

    If you compare the picture and the log attached (for AUDJPY, but it applies to all pairs) you will see

    12/4/2008 2:30:00 AM Entered internal PlaceOrder() method at 12/4/2008 2:30:00 AM: Action=SellShort OrderType=Market Quantity=25,000 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''
    12/4/2008 3:00:00 AM 60.59 59.31 59.31 59.31 60.79 60.56 60.79
    NT went short at 2:30 AM and the StopLossShort is 60.79.
    Yet, as you can see in my chart, the Stop Loss kicks in at 60.56! Why does this happen?

    Thank you
    Attached Files

    #2
    Hi stefy,

    Thanks for your posting, please check out this reference sample - http://www.ninjatrader-support2.com/...ead.php?t=3222

    It deals with the correct handling of non static stop loss orders.

    Also, where do you place your SetStopLoss commands?

    For dynamic ones, they should be in the OnBarUpdate() to be effective -

    Comment


      #3
      Hi Bertrand,

      I placed the SetStopLoss in OnBarUpdaye(), since it's not a static stop loss, but changes accordingly to the double values.

      I understand I have to reset the StopLoss once all positions are closed (as per your example), even if I'm using a double that may change value at each bar?
      Should I code then:

      if (Position.MarketPosition == MarketPosition.Flat)
      {
      SetStopLoss(CalculationMode.Price, StopLossLong);
      }

      elseif (Position.MarketPosition == MarketPosition.Long)
      {
      {
      SetStopLoss(CalculationMode.Price, StopLossLong);
      }

      }

      Am I correct?

      Thank you

      Comment


        #4
        Hi stefy,

        Correct you want a reset to your initial default value once all positions are cleared and the strategy is flat.

        The different variable types simply hold various kind of data and can change bar by bar if you code them to behave this way - http://www.ninjatrader-support.com/H...sicSyntax.html

        Give your new code change a testrun and enable TraceOrders to see the changes - http://www.ninjatrader-support.com/H...aceOrders.html

        Comment


          #5
          I coded
          if (Position.MarketPosition == MarketPosition.Flat)
          {
          SetStopLoss(
          "Long", CalculationMode.Price, StopLossLong, false);
          }
          elseif (Position.MarketPosition == MarketPosition.Long)

          {

          SetStopLoss(
          "Long", CalculationMode.Price, StopLossLong, false);
          }

          Basically I don't change value, it's always StopLossLong, but if I don't use the above syntax, NT doesn't reset the StopLoss value with a new position.

          I'm also using SetTrailStop: does the same coding apply there as well? I'm using it as follows:
          // Trailing Stop Long
          if (Conditions
          && Position.MarketPosition == MarketPosition.Long)
          {
          SetTrailStop(CalculationMode.Price, Low[
          1]);
          }

          Thank you

          Comment


            #6
            Hi stefy,

            Yes, same would apply to your dynamically changing trailstop - http://www.ninjatrader-support.com/H...TrailStop.html

            I would suggest adding another variable in your code holding your stop default values like longStopDefault and shortStopDefault. Just to keep it clean and clear, then you can reset to those and adjust the 'dynamic ones' as you go.

            Comment


              #7
              I'm trying to use SetTrailStop along with SetStopLoss. I read on NT that it's possible to do that, but NT will give precedence to SetStopLoss.

              I coded the following:

              // Stop Loss Long: MIN(SwingLow, Low)
              if (Position.MarketPosition == MarketPosition.Flat)
              {
              SetStopLoss(
              "Long", CalculationMode.Price, StopLossLong, false);
              }
              elseif (Position.MarketPosition == MarketPosition.Long)

              {
              SetStopLoss(
              "Long", CalculationMode.Price, StopLossLong, false);
              }

              // Trailing Stop Long
              if (Conditions&& Position.MarketPosition == MarketPosition.Flat)
              {
              SetTrailStop(
              "Long", CalculationMode.Price, Low[1], true);
              }
              elseif (Conditions&& Position.MarketPosition == MarketPosition.Long)

              {
              SetTrailStop(
              "Long", CalculationMode.Price, Low[1], true);
              }

              The Trailing Stop never kicks in. Why do you think this happens?

              Thank you

              Comment


                #8
                Where did you read it was possible? In the help article you will find this statement as the first tip: "The SetTrailStop() method can NOT be used concurrently with the SetStopLoss() method for the same position, if both methods are called for the same position (fromEntrySignal) the SetStopLoss() will always take precedence. You can however, use both methods in the same strategy if they reference different signal names". That is why you do not see it trigger.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  I misinterpreted the line "The SetStopLoss() method can NOT be used concurrently with the SetTrailStop() method for the same position, if both methods are called for the same position (fromEntrySignal) the SetStopLoss() will always take precedence. "

                  So, if I have a position with a SetStopLoss, there is no workaround to use a SetTrailStop?

                  As per my example, I see as only alternative to use instead of the trailing stop:

                  if (Conditions
                  && Position.MarketPosition == MarketPosition.Long)
                  {
                  if(Close[0] < Low[1])
                  ExitLong(
                  "", "");
                  }

                  Basically, I want to replicate the SetTrailStop behavior (even if with less flexibility since the condition will be evaluated OnBarClose).
                  Is the above correct or should I use the Crossabove function to keep the first conditions valid and wait for the last condition (Close[0] < Low[1]) to be met?

                  Thank you
                  Last edited by stefy; 12-08-2008, 03:44 PM.

                  Comment


                    #10
                    You will need to custom code the logic yourself. There are several threads around in regards to this. Here is one of them: http://www.ninjatrader-support2.com/...ad.php?t=10344
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      After using the SetStopLoss as I described below
                      // Stop Loss Long
                      if
                      (Position.MarketPosition == MarketPosition.Flat)
                      {
                      SetStopLoss(
                      "Long", CalculationMode.Price, StopLossLong, false
                      );
                      }
                      elseif
                      (Position.MarketPosition == MarketPosition.Long)

                      {
                      SetStopLoss(
                      "Long", CalculationMode.Price, StopLossLong, false
                      );
                      }

                      I'm using the SetTrailStop without calling for the same position:

                      // Trailing Stop Long
                      if (Conditions
                      && Position.MarketPosition == MarketPosition.Flat)
                      {
                      SetTrailStop(CalculationMode.Price, Low[1]);
                      }
                      elseif (Conditions
                      && Position.MarketPosition == MarketPosition.Long)

                      {
                      SetTrailStop(CalculationMode.Price, Low[1]);
                      }

                      In this way I hoped to be able to use both SetStopLoss and SetTrailStop. Am I correct?
                      I analized the log of the files, the SetStopLoss and SetTrailStop are both input correctly, but then the Trail Stop doesn't kick in.
                      For instance (second field is Close[0]):

                      12/2/2008 12:30:00 AM Entered internal SetStopTarget() method: Type=TrailStop FromEntrySignal='' Mode=Price Value=1.8538 Currency=0 Simulated=False
                      12/2/2008 12:30:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Long' Mode=Price Value=0 Currency=0 Simulated=False
                      12/2/2008 12:30:00 AM 1.8524 1.8294 0 0 1.8705 0 1.8705

                      Why it doesn't kick off on the next bar, when Close < SetTrailStop?

                      Thank you

                      Comment


                        #12
                        Hi stefy,

                        The issue is you cannot use them together for one position, which is your case. In SetStopLoss you use 'Long' as the name, in SetTrailStop none (which means use it for all signals) - both for the same long position which does not work.

                        Our advise still stands to use SetStopLoss and to code then your trailing stop logic yourself. You will find some examples in the NinjaScript sharing section of this forum (strategies).

                        Please also review this link previoulsy given by Josh for custom code by a forum member - http://www.ninjatrader-support2.com/...ad.php?t=10344

                        Comment


                          #13
                          Last try: what if I use SetTrailStop() modifying the order as follows:

                          if (Position.MarketPosition == MarketPosition.Flat)
                          {
                          SetTrailStop(
                          "Long", CalculationMode.Price, StopLossLong, false);
                          }
                          elseif (Position.MarketPosition == MarketPosition.Long
                          && Position.AvgPrice < Close[0])

                          {
                          SetTrailStop("Long", CalculationMode.Price, StopLossLong, false);
                          }
                          elseif (Position.MarketPosition == MarketPosition.Long
                          && Position.AvgPrice > Close[0] && Conditions)
                          {
                          SetTrailStop("Long", CalculationMode.Price, Low[1], false);
                          }

                          Comment


                            #14
                            Hi stefy,
                            Looks good, as you now only use the SetTrailStop().

                            You will now need to run your code live with TraceOrders = True, so you can check if your coding logic achieves what it's supposed to.
                            Last edited by NinjaTrader_Bertrand; 12-09-2008, 01:31 PM. Reason: typo

                            Comment


                              #15
                              Bertrand,

                              Typo maybe.

                              TraceOrders = True
                              not
                              TradeOrders = True

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              581 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              338 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              103 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              554 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              552 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X