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

unexpected SetStopLoss behavior

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

    unexpected SetStopLoss behavior

    Hi:

    using 5-minute bars...

    I do this in my Initialize() method:
    SetStopLoss("enter_long1C", CalculationMode.Ticks, 200, false);

    against an entry order in my OnBarUpdate() method:
    EnterLongLimit(targetpos, limitprice, "enter_long1C");

    To my knowledge there are no other orders either "in force" or "laying off to the side"
    (such as other SetStopLoss or SetProfitTarget's). I'm trading only 1 instrument.
    I only trade 1 contract, and only have 1 position open in one direction (no hedging) at
    any time.

    during backtesting, I incur 19 "stopped" trades on GC between 9/30/12 and 9/26/14

    A.
    each is stopped out only 20 ticks (2 points) away from
    their respective entry prices instead of the expected 200 ticks (20 points).

    B.
    Also, peculiarly, every single one of them is stopped out in the same bar in which
    the trade was opened. they are all "single-bar" trades.

    C.
    a trade exited normally with a 179-tick loss on 3/7/14
    entered at 5:25AM pacific time and exited at 5:45AM pacific time, instead of being
    stopped out at 20 ticks.

    Widening the stop to 2,000 ticks suppresses the symptom... no trades are stopped out.

    Question: why is the backtest imposing 20-tick stops with a 200-tick stoploss?

    thanks so much in advance -- Jim Alexander : )

    #2
    Hello jalexan1,

    I am not able to reproduce this behavior.

    Attached is a script that places the stop at 200 ticks.
    (I tested this on a daily chart so there is enough data for the price to drop 200 ticks)

    Can you test this script without modifying it and send me the output from the Output window?
    Attached Files
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Output from StopLossDistanceTest

      Hi Chelsea:

      I attached the file you requested.

      -- Jim Alexander
      Attached Files

      Comment


        #4
        Hello Jim,

        I'm not certain what instrument you are running this on, however, it does appear the exit was much further than 20 ticks.

        The entry of the first trade is at 1796.8, the exit is at 1776.8,

        From 1796.9 to 1776.8 is 20 points. If the tick size is .1 on this instrument, there are 10 ticks per point. This would mean the exit occurred 200 ticks below the entry.

        Do you still feel that the the stop loss is not exiting 200 ticks behind the entry price?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Odd interaction between StopLossLimit and EnterLongLimit

          Hi Chelsea:

          yes, its still doing the same thing.

          Your StopLossDistanceTest uses an "EnterLong".

          I stated in my first message that I was using an "EnterLongLimit".

          changing your StopLossDistanceTest "EnterLong" to "EnterLongLimit" does not exacerbate the problem.

          I'm attaching my "JimTest" strategy. I've "pared it down" to its "essence". If I simplify it further, it either doesn't provide sufficient functionality to meet my trading goals OR it doesn't demonstrate the problem.

          Notice that it is a multi-instrument strategy, but it only trades GC (instrument 1).

          This "setup" demonstrates the problem:
          1. I attached the "settings" file for the backtest.
          2. Please set "CL 11-16" as the primary instrument.
          3. I also attached the Trades file. Notice that we get multiple 2-point stops in the trace.

          These things "help" (but don't align with what I'm trying to accomplish)
          A.
          If I set "GC 12-16" as the primary (0) (as well as the second (1)) instrument, the problem
          goes away.

          B.
          If I change EnterLongLimit to EnterLong, the problem goes away.

          C.
          Getting rid of the price offset (- 14 * TickSize) in the EnterLongLimit vastly reduces but
          does not completely eliminate the number of times the problem shows up in the Trades log.

          so... do you have any suggestions as to how to get this thing to behave properly? : )

          thanks so much
          -- Jim Alexander : )
          Attached Files

          Comment


            #6
            Hello Jim,

            May I confirm that the behavior you are experiencing is that stop loss orders specifically are filling closer than the amount of ticks they are being set?

            In post #4 I have asked you to confirm if the script that I have made as an example for you is behaving correctly or incorrectly.
            You have provided output that is showing that the stop losses are filling 200 ticks from the entry.
            I was asking if you feel the script I have created as an example for you is behaving differently than expected and if you wanted to further discuss the output.txt file you have posted.
            This is so that we use this as a starting point to understand the behavior that is happening.
            Do you feel that the script I have provided you is filling stop losses closer than 200 ticks from the entry?

            I am testing your (edit) script on my end and I'm finding the script does not place any entry orders. The conditions in the script are never true and no orders are placed at all.

            The script also likely has Exit on close set to true, and I would expect the Exit on close to exit the order instead of the stop loss.

            Your script also has no prints.

            How are you determining that the Stop Loss specifically (and not another order) is filling and is filling closer than the 200 tick distance it was set at?

            Can you provide a screenshot, or an export of the grid of the orders tab, or something to demonstrate the behavior?




            For future reference, to export a NinjaTrader 7 NinjaScript do the following:
            1. Click File -> Utilities -> Export NinjaScript
            2. Enter a unique name for the file in the value for 'File name:'
            3. Select the strategy from the objects list on the left -> click the right facing arrow ">" to add the strategy to the export
            4. Click the 'Export' button -> click 'yes' to add any referenced indicators to the export -> click OK to clear the export location message


            By default your exported file will be in the following location:
            • (My) Documents\NinjaTrader 7\bin\Custom\ExportNinjaScript\<export_file_name.z ip>


            Below is a link to the help guide on Exporting NinjaScripts.
            http://www.ninjatrader.com/support/h...nt7/export.htm
            Last edited by NinjaTrader_ChelseaB; 10-12-2016, 03:21 PM.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi Chelsea:

              thanks for the quick reply.

              your question: "May I confirm that the behavior you are experiencing is that stop loss orders specifically are filling closer than the amount of ticks they are being set?"

              the answer is: yes. confirmed.

              ---------------------------

              your statement: "In post #4 I have asked you to confirm if the script that I have made as an example for you is behaving correctly or incorrectly."

              my reply: yes, your script behaves correctly. it does NOT fill stop orders closer than 200 ticks. all stop orders appear to be filling 'spot-on' at 200 ticks, exactly as expected.

              ---------------------------------------

              your question: "Do you feel that the script I have provided you is filling stop losses closer than 200 ticks from the entry?"

              the answer is: no. your script is operating as expected.

              -----------------------------------

              your statement: "I am testing this script on my end and I'm finding the script does not place any entry orders. The conditions in the script are never true and no orders are placed at all."

              my reply: that's very interesting. on my end it placed 2,916 trades.

              ------------------------------------------

              your statement: "The script also likely has Exit on close set to true, and I would expect the Exit on close to exit the order instead of the stop loss."

              my reply: true. it does sometimes exit on close.

              -------------------------------------------

              your statement: "Your script also has no prints."

              my reply: true. I use the "Trades" log. I'm not as "up-to-speed" on using prints as you are. But I DO thank you profusely for the fine example in your script on how to use them effectively. AND... I attached your "print-thingie" to the end of my script so you have a more familiar debug environment to work with.

              -------------------------------------------

              your question: "How are you determining that the Stop Loss specifically (and not another order) is filling and is filling closer than the 200 tick distance it was set at?"

              my reply: The 29th trade in the "Trades" log which I attached in my previous post shows
              a 20-tick (2-point) stop loss exit at 5:35AM pacific time on 10/5/2012. That's not the only one, but is sufficient to demonstrate the issue. If you hunt through the log, you will find 74 such 2-point fills, as well as six (6) "normal" 20-point (200-tick) fills.

              -------------------------------------------

              your question: "Can you provide a screenshot, or an export of the grid of the orders tab, or something to demonstrate the behavior?"

              my reply: in addition to my properly-export script, I've also attached the
              Trade List, the Order List, the Execution List, and the Settings, as well as screen-shots of the Trades log straight off the strategy analyzer showing the peculiarity, the Strategy Settings, and the "MySession" session I was using.
              Attached Files

              Comment


                #8
                Hello jalexan1,

                Just a heads up, the strategy you have provided in post #7 is not the same strategy as provided in post #5

                I think I am seeing the issue here, and this is a limitation when using the Set methods.

                My guess is that you are adding the strategy to an instrument with a tick size of .01 and placing orders to a secondary series with a point size of .1.

                What is the primary instrument that this strategy is being added to?

                The Set methods like SetStopLoss/SetProfitTarget/SetTrailStop are specific to the primary instrument.

                In historical data, the Set methods will only update when the primary bar is closing (and will not update for a secondary series such as if you add 1 tick granularity).


                The TickSize used for the SetStopLoss method will be taken from the primary series.

                Basically, the entry fills on secondary series and gets a fill price, SetStopLoss calculates a distance using the TickSize of the primary instrument, adds/subtracts this amount from the entry fill price, and places the order with the stop set to that price.

                I am currently testing to see if this affects NinjaTrader 8 as well.

                In the meantime, you can use an Exit method instead of a Set method in the script and this will behave as expected.

                If I find that NinjaTrader 8 is also affected, I will submit a feature request on your behalf to allow Set methods to calculate correctly using the correct TickSize for entries placed to a secondary series
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  odd SetStopLoss behavior: thanks for finding the problem

                  Hi Chelsea:

                  you nailed it. thanks so much.

                  -------------------------

                  your comment: "Just a heads up, the strategy you have provided in post #7 is not the same strategy as provided in post #5"

                  my reply: oops. I'm a bonehead. : )

                  -------------------------

                  your question: "What is the primary instrument that this strategy is being added to?"

                  my answer: CL 11-16. right on the button! tick size is 0.01, just as you deduced!

                  -------------------------

                  your advice: "In the meantime, you can use an Exit method instead of a Set method in the script and this will behave as expected."

                  my response: I've (perhaps erroneously, but we'll see...) concluded that although ExitLong() (at market) works just dandy, ExitLongLimit() (for the case of a long position) doesn't ever fill... never... ever... at least in a CalculateOnBarClose = true; backtest environment. So I'm going to use a SetProfitTarget() order instead. I've concluded that they operate as expected in a backtest environment. and I've determined that I require limit orders to control slippage wherever possible.

                  The order entry code for all of my instruments will look like this, with these statements arranged exactly in the order shown for an example of a long position:

                  SetStopLoss("longentrysignal", CalculationMode.Price, Close[0] - (TickSize * offset) - (TickSize * stop), false);
                  SetProfitTarget("longentrysignal", CalculationMode.Price, Close[0] + (TickSize * big));
                  EnterLongLimit(Close[0] - (TickSize * offset), "longentrysignal");

                  where "offset" is a small number, and "big" is a large number.

                  Then, when its time to close the order, I start the process by executing this statement to update the "standing" profit target order to something closer to where the price is actually trading at that instant to get a quick fill, if possible:

                  SetProfitTarget("longentrysignal", CalculationMode.Price, Close[0]);

                  and if, after 1 bar, the position isn't flat, I just issue an ExitLong() market order to "finish it off".


                  By executing the stop and profit target "standing orders" before executing the entry order, I ensure that old stale SetStopLoss and SetProfitTarget orders are replaced by brand-spanking-new "fresh" ones with prices relevant to the new entry order. The "big" in the SetProfitTarget() just "gets it completely ***out-of-the-way***" while the algorithm attempts to mine as much "trend" as possible out of the trade. (and for the case of a short entry, the price will just be zero). I presume that, even though the ticksize implied when using CalculationMode.Tick may not always be the one I require for a secondary instrument, the value of the "TickSize" built-in variable ***IS*** correct. And if we find that "TickSize" also has issues, I can code my own "JimTickSize<bletch>" for each <bletch> instrument and use that (since they don't change... at least not very often).

                  if you find anything amiss with this "grand scheme", I'd sure like to know...

                  again, thanks so much for all the hard work on this one.

                  -- Jim Alexander

                  Comment


                    #10
                    Hi jalexan1,

                    When you mention that an ExitLongLimit() never fills, I can create an example to demonstrate that they will (in both historical and real-time data).

                    Let me know if you would like this.

                    One caveat with using an Exit order is to ensure the order is not cancelled automatically when the bar it is submitted on closes, you must either use the overload with liveUntilCancelled and have this bool parameter set as true.

                    Try adding the following to a simple script.

                    ExitLongLimit(0, true, 1, High[0] + 5 * TickSize, "profit target", "entry");
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      LiveUntilCancel ExitLongLimit order

                      Hi Chelsea:

                      yes, I would like to see a complete ExitLongLimit() example which, if not filled within 1 entire bar, is superseded immediately after that first bar by an ExitLong() market order without risk of an overfill, using CalculateOnBarClose = true;

                      Here's the way I imagine it: The limit is set to $50.00. The tick size is ten cents. During the first 5m bar of the limit order's tenure, the price wanders around between $50.10 and $50.20. It never "touches" $50.00. I expect it to fill at either $50.10 or $50.20 during that first bar (presuming liquidity is not an issue). This is what would happen in a normal trading account with the exit limit order placed manually at $50.00.

                      Therefore I expect the ExitLongLimit() to fill if the price is above the limit, regardless of whether the price touches its limit or not. My "litmus test" was to set the ExitLongLimit() price to zero. The price will ALWAYS be above zero.. But... it never filled. I was informed by "Paul" in our exchange

                      that this "touching the price" behavior was necessary for a fill. This is where it appears to be a poor fit for my objectives.

                      Therefore... since I can't seem to rely on the ExitLongLimit() to fill if my price is better than my limit, I felt forced to use a SetProfitTarget() order to accomplish the same goal, and it seems to work just fine. : )

                      thanks for all the good help, its been a pleasure -- Jim Alexander : )

                      Comment


                        #12
                        Hi jalexan1,

                        Attached is a thorough example I've put together to demonstrate using a few concepts:
                        • Using Exit orders for the profit target and stop loss instead of Set methods
                        • Logic to simulate local oco when using the managed approach
                        • Logic for a target chase / stop trail
                        • Using the tick size of a secondary series for calculations
                        • Implementing intra-bar granularity
                        • Identifying when an exit on close has occured and resetting for an entry


                        The versions for NinjaTrader 8 can be found in the post linked below.


                        (Update Sept 12th, 2018: In ProfitChaseStopTrailExitOrdersExample_NT7 made some corrections to the exitOnCloseWait and to the logic that exits the position if a protective order is cancelled or rejected. Also, added ProfitChaseStopTrailUnmanagedExample_NT7.)
                        Attached Files
                        Last edited by NinjaTrader_ChelseaB; 09-12-2018, 10:52 AM.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Hi jalexan1,

                          When attempting to further document this behavior, I am finding that I was incorrect. I'm not able to demonstrate this limitation of SetStopLoss using the primary instruments tick size.

                          Attached is a simple strategy to demonstrate. This strategy places an order to the GC 12-16 added as a secondary instrument. Please open an output window and add this to a CL 12-16 and wait for a bar to close (the script currently prevents trades from occurring historically). The print shows the fill price of the entry order and then the working price of the stop loss.

                          Can you test this script and see if the stop loss is being placed to an incorrect price on your end?
                          Attached Files
                          Chelsea B.NinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by thanajo, 05-04-2021, 02:11 AM
                          3 responses
                          467 views
                          0 likes
                          Last Post tradingnasdaqprueba  
                          Started by Christopher_R, Today, 12:29 AM
                          0 responses
                          10 views
                          0 likes
                          Last Post Christopher_R  
                          Started by sidlercom80, 10-28-2023, 08:49 AM
                          166 responses
                          2,235 views
                          0 likes
                          Last Post sidlercom80  
                          Started by thread, Yesterday, 11:58 PM
                          0 responses
                          4 views
                          0 likes
                          Last Post thread
                          by thread
                           
                          Started by jclose, Yesterday, 09:37 PM
                          0 responses
                          9 views
                          0 likes
                          Last Post jclose
                          by jclose
                           
                          Working...
                          X