Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SetStopLoss accepted with wrong Quantity, in real time

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

    SetStopLoss accepted with wrong Quantity, in real time

    Good morning,

    I have a big problem with the quantities associated with stoploss.
    I'm using my written strategy for Ninjatrader 7, managed approach.
    The strategy accepts multiple entries, each with Quantity = 10.
    After the fill of each input, I set the stop with SetStopLoss (in the OnOrderUpdate when OrderState = Filled), specifying the name of the associated input, for example: SetStopLoss ("LongFlat_0", CalculationMode.Ticks, LongFlatTicks, false).
    In the log I see the stoloss accepted with quantity = 10.
    When a second entry named "LongCross_0" arrives, something strange happens:
    the stoploss associated with the first entry is automatically changed, I think it is recalculated based on the new AvgFillPrice, why?
    But the most serious thing is that his quantity is 20 and not 10!
    Then the stoploss associated with the second input is accepted, correctly with quantity = 10.
    This causes a serious problem in case the stoploss is reached: the 20 long contracts will be closed but the 10 remaining stops will generate 10 short entries!
    (as it happened a few days ago).

    Am I doing something wrong? What should I do to avoid this problem?
    This doesn't happen in backtest, only in realtime.
    The parameter "Stop & target submission" is settted to "By Startegy Position".

    Thanks
    Carlo

    Attached is today's log.
    Last edited by NinjaTrader_PatrickG; 12-16-2019, 07:55 AM.

    #2
    Hello carlot,

    Thanks for your post.

    We removed your log file from the post because it may contain sensitive information. Please do not post log or trace files in a public forum.

    The Set methods, such as SetStopLoss() will retain their last value used and will be immediately and automatically applied upon an order fill. I would suggest ensuring that the SetStopLoss() for each order is set up prior to the actual entry fill. This is covered in the help guide for each Set method, "Should you call this method to dynamically change the stop loss price in the strategy OnBarUpdate() method, you should always reset the stop loss price/offset value when your strategy is flat otherwise, the last price/offset value set will be used to generate your stop loss order on your next open position" Reference: https://ninjatrader.com/support/help...etstoploss.htm

    Please adjust your strategy and retest.
    Last edited by NinjaTrader_PaulH; 12-16-2019, 08:31 AM. Reason: Changed references from NinjaTrader8 to NinjaTrader7
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hello Paul H.

      Thank You for the quick response.

      In my previous version of the strategy I setted up the stop loss prior to the actual entry call function (EnterLong), but with the same result.

      Please note that I'm working with different name for each enter, and a specific stoploss setted for each enter name.
      When the second long enters, the stoploss setted for the first enter are setted also for the second long enter?
      How to set a specific and indipendent stoploss for each enter?

      Also I know that I have to "reset" the stoploss, that means to set a stop loss far from the price when flat, but is not my problem.

      Thank You

      Comment


        #4
        Hello carlot,

        Thanks for your reply.

        I've created a simple test strategy to demonstrate two separate named entries and their separate stop loss settings. To test this, please connect to real time time data (or market replay) and use the sim101 account. Create two charts both with 30 second bars (a fast updating chart to demonstrate quickly). On one chart enable the chart trader (to see the orders on the chart more easily), on the other apply the strategy (NOTE: Set the entries per direction to 2) and enable the strategy. On the close of the first real time bar, 10 contracts will be placed with a 10 tick stop loss, 4 bars later a second set of 10 contracts will be placed with their own stop loss at 30 ticks.

        You also may want to review https://ninjatrader.com/support/help...of_stop_lo.htm for further example of how to apply the set methods.

        If the examples do not help, please modify the provided test script to demonstrate what you are doing.


        TwoOrdersExample.zip
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Thank You for the test strategy.

          It works correctly.

          But please run it specifying the barsInProgressIndex:

          EnterLong(0, 10, "firstEntry");
          and
          EnterLong(0, 10, "secondEntry");

          and set the parameter "Stop & target submission" to "ByStartegyPosition", you will see my problem.


          I use barsInProgressIndex because I use three timeframes in the strategy. (When is necessary to specificate the barsInProgressIndex in the enters commands?)
          Also I use the parameter "Stop & target submission" setted to "ByStartegyPosition", because setted to "PerEntryExecution" has given me some problems in the past, in case of partial fiil entries.

          Comment


            #6
            Hello carlot,

            Thanks for your reply.

            At your direction i modified the example strategy for additional data series (I added 3) which included separating the code to only run on BarsInProgress of 0, changing the BarSinceEntry to also recognize BarsInProgress 0 and submitted the orders to BarsInProgress 0. I changed to "ByStrategyPosition" and repeated the test and the results were exactly the same, no issues.

            I would suggest you debug your strategy logic by using print statements to understand what is happening within your strategy. We have assembled some debugging tips here: https://ninjatrader.com/support/help...script_cod.htm

            As you are working with a MultiTimeframe strategy also recommend a review of this section of the help guide: https://ninjatrader.com/support/help...nstruments.htm
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Hello Paul,

              please can you just try your first test strategy, with these modifications in the EnterLong commands, and ByStartegyPosition parameter? This give me the issue,

              protected override void OnBarUpdate()
              {
              if (Historical) return;

              if (Position.MarketPosition == MarketPosition.Flat)
              {
              SetStopLoss("firstEntry", CalculationMode.Ticks, 10, false);
              EnterLong(0, 20, "firstEntry");
              }

              if (Position.MarketPosition == MarketPosition.Long && BarsSinceEntry("firstEntry") == 4)
              {
              SetStopLoss("secondEntry", CalculationMode.Ticks, 30, false);
              EnterLong(0, 20, "secondEntry");
              }
              }

              Thanks

              Comment


                #8
                Hello carlot,

                Thanks for your reply.

                I did modify the strategy as you requested and did not have any issues.

                Note that with added data series that you have to also change the BarsSinceEntry. Here is what i tested with:

                protected override void Initialize()
                {
                CalculateOnBarClose = true;
                Add(PeriodType.Minute, 1);
                Add(PeriodType.Minute,5);
                Add(PeriodType.Minute, 15);
                }
                protected override void OnBarUpdate()
                {
                if (Historical) return;

                if (BarsInProgress != 0) return;

                if (Position.MarketPosition == MarketPosition.Flat)
                {
                SetStopLoss("firstEntry", CalculationMode.Ticks, 10, false);
                //EnterLong(10, "firstEntry");
                EnterLong(0, 10, "firstEntry");
                }

                if (Position.MarketPosition == MarketPosition.Long && BarsSinceEntry(0,"firstEntry",0) == 4)
                {
                SetStopLoss("secondEntry", CalculationMode.Ticks, 30, false);
                EnterLong(0, 10, "secondEntry");
                }
                }


                The strategy was applied to 30 second bars of YM 03-20 with entries per direction to 2 and ByStartegyPosition.

                Please test with the code shown and advise your results
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  Dear Paul,

                  you did not use the first version of strategy.

                  Did not see any stoploss accepted with quantity 20?

                  Please try this, that stops long after two longs. When the stops are reached, you will have a short with quantity 10.


                  #region Variables
                  // Wizard generated variables
                  // User defined variables (add any user defined variables below)

                  private bool firstEntryDone = false;

                  #endregion

                  /// <summary>
                  /// This method is used to configure the strategy and is called once before any strategy method is called.
                  /// </summary>
                  protected override void Initialize()
                  {
                  CalculateOnBarClose = true;
                  firstEntryDone = false;
                  }

                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
                  protected override void OnBarUpdate()
                  {
                  if (Historical) return;

                  if (Position.MarketPosition == MarketPosition.Flat && !firstEntryDone)
                  {
                  SetStopLoss("firstEntry", CalculationMode.Ticks, 7, false);
                  EnterLong(0, 10, "firstEntry");
                  }

                  if (Position.MarketPosition == MarketPosition.Long && BarsSinceEntry("firstEntry") == 2)
                  {
                  SetStopLoss("secondEntry", CalculationMode.Ticks, 10, false);
                  EnterLong(0, 10, "secondEntry");
                  }
                  }


                  I can send you my log that show the issue

                  Thanks

                  Comment


                    #10
                    Hello carlot,

                    Thanks for your reply.

                    I've created a short video to show exactly what was done. I did modify the code to add another bool as the second entry would place another set of orders after the first entry was closed and 2 bars had passed. https://Paul-ninjaTrader.tinytake.co...NF8xMjEzMzI3Mw
                    Paul H.NinjaTrader Customer Service

                    Comment


                      #11
                      Hello Paul,

                      thanks for the video.

                      Sorry, I forgot to set firstEntryDone = true...


                      I found the condition when the problem arises: when the entries are partially filled (filled in two or more steps).

                      Please, could you try several times, until the entries are partially filled, especially the second entry?

                      Thanks

                      Comment


                        #12
                        Hello carlot,

                        Thanks for your reply and patience to explain.

                        I was able to recreate the issue of two 10 contract entries and one 10 contract stop and then a 20 contract stop was created on the 2nd entry.

                        We will review internally. I will update this thread when I have further information.

                        Thanks in advance for your patience.

                        Paul H.NinjaTrader Customer Service

                        Comment


                          #13
                          Hello Paul,

                          I'm glad you saw the problem.

                          Now I can talk to you about another big problem, the reason why I tried to use the "Stop & target submission" parameter set "ByStartegyPosition" instead of the default "PerEntryExecution".

                          Same starting situation, but with "PerEntryExecution" parameter:

                          - first entry with partial fills (i.e. 6 + 4), stoploss associated with partial acceptances (6 + 4).
                          - second entry with partial fills (i.e. 6 + 4), stoploss associated with partial acceptances (6 + 4).

                          In this case ("PerEntryExecution" parameter) the total quantity of accepted stoplosses will be correct (20), but the problems arrive when the stops are reached:
                          When the stoploss of the second entry is reached, it closes only a part of its quantities (i.e. 4), and closes the remaining 6 quantities by taking them from the first entry!
                          I experienced this in real time, with the real account ..
                          I can send you the log.

                          For me it is difficult to reproduce the problem.
                          So I ask you if you can please test with the "PerEntryExecution" parameter and see if something unexpected happens when the stops are reached.
                          Also try adding a third entry, to increase the chance of generating problems.

                          Thanks

                          Comment


                            #14
                            Hello carlot,

                            Thanks for your reply.

                            Please modify the test script I provided earlier and advise if you can hit the issue you are now reporting.

                            Paul H.NinjaTrader Customer Service

                            Comment


                              #15
                              Dear Paul,

                              I'm asking you to check, verify, the second problem I'm reporting.

                              No script modifications are required, just set "PerEntryExecution".

                              Thanks

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by devatechnologies, 04-14-2024, 02:58 PM
                              3 responses
                              19 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by tkaboris, Today, 08:01 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post tkaboris  
                              Started by BarzTrading, Today, 07:25 AM
                              1 response
                              11 views
                              1 like
                              Last Post NinjaTrader_Clayton  
                              Started by EB Worx, 04-04-2023, 02:34 AM
                              7 responses
                              161 views
                              0 likes
                              Last Post VFI26
                              by VFI26
                               
                              Started by Mizzouman1, Today, 07:35 AM
                              1 response
                              9 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Working...
                              X