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

How to Place Stop Loss below Low of Signal Bar

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

    How to Place Stop Loss below Low of Signal Bar

    Hello,

    I have a relatively simple code that I am backtesting using tick charts on equity indexes..

    My question is how can I set my stop loss 1 tick below the low of the signal bar. For example, a bar prints and signals my long entry, which I would enter upon the open of the next bar.. Now I want my stop loss 1 tick below the bar that signaled my entry.

    Example: Signal Bar: High - 102
    Close - 101
    Open - 99
    Low - 97
    Entry Bar (Next bar): Open - 101

    I will enter a long position at 101.. Assuming the minimum tick size here is 1.00, I want to place my stop loss at 96, since previous bar low was 97.

    I assumed this could be accomplished using SetStopLoss(@"LONG", CalculateMode.Price, Low[1] - 1);

    However this method does not seem to work.

    Any help appreciated.

    #2
    Hello murraym324,

    This would be close. You would want to multiply by the ticksize however instead of using a full point. Also, the low of the signal bar would use 0 bars ago (for the current bar). The order would be placed after the bar closes if Calculate is set to .OnBarClose.

    SetStopLoss(@"LONG", CalculateMode.Price, Low[0] - 1 * TickSize);

    Also, this should be set just before the entry order is placed (and not set in Initialize()). This is because once set, the SetStopLoss() cannot be unset for that signal name and if the price isn't updated before the entry is placed it will continue using the old price value for new orders.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you for the fast reply. I will explore this solution.

      Comment


        #4
        Hi. Thanks for your help.
        I have tried this code but it gives me the following error:

        SetStopLoss(@"LONG", CalculateMode.Price, Low[0] - 2 * TickSize);

        no overload for the SetStopLoss method accepts 3 arguments.

        Comment


          #5
          Hello julifro,

          Below is a public link to the help guide on SetStopLoss().


          There isn't an overload that has fromSignalName, CalculationMode, price. From the way you are using this you would also need to supply true or false for the simulated bool. (Likely false)
          SetStopLoss(string fromEntrySignal, CalculationMode mode, double value, bool simulated)
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            These are the orders I use. The purchase is made but I can't get you to put the stop under the entry bar. Place the stop two ticks under any bar. I don't know what to do anymore. This is the code I use

            SetStopLoss("long",CalculationMode.Price,Low[0]-2*TickSize, false);

            EnterLongStop(High[0] + TickSize*2,"long");

            Comment


              #7
              Hello julifro,

              May I confirm your inquiry is about how to place an order immediately at the time a condition is true without waiting for the bar to close?

              If so, this requires running the action intra-bar.

              If not, please detail the issue and the desired behavior.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                No. My query is to put the stop under the entry bar. The entry order is executed correctly but the stop order is not placed under the entry bar

                Comment


                  #9
                  Hello julifro,

                  The stop loss is placed immediately when the entry order fills. However, it will not fill until it hits a valid price.

                  Since the entry order is a stop order, you would need to wait to place the stop loss until the order fills so that you can get the low of the bar it filled on. I would recommend submitting the stop loss in OnOrderUpdate().

                  If your strategy is running with CalculateOnBarClose as true, the Low - 2 * TickSize will refer to the most recently closed bar, not the bar of the entry.

                  To reference the Low of the currently building bar, you would need to run the script with CalculateOnBarClose set to false, and this would only occur in real-time. The stop loss would then not be submitted until the entry order fills.

                  Historically, you would need to add 1 tick intra-bar granularity and you would also need to track the first tick and the lowest low if you wanted to reference the low of the currently building bar.

                  (edited, video corrected)
                  Below is a link to a demonstration in real-time.
                  Last edited by NinjaTrader_ChelseaB; 11-21-2018, 03:33 PM.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Thank you for your reply.
                    English is not my mother tongue and I don't know how to explain it. Excuse me
                    I have this code for ninjatrader 7:

                    if (Close[0] > High[1])
                    {
                    EnterLongStop((High[0] + TickSize*2),"long");
                    SetStopLoss("long", CalculationMode.Price,(Low[0]- 2*TickSize), false);
                    }

                    With this code the stop is set to Low[0] correctly.
                    But I want the stop to occur under the input bar when the condition is given if(Close[0]>High[1])

                    Thank you very much

                    Comment


                      #11
                      Hello julifro,

                      My apologies. The video I created is not displaying the screen correctly. I have re-recorded the video.




                      Also I am attaching the script I have used to demonstrate.
                      Attached Files
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Thank you very much for your help and your video.
                        Last edited by julifro; 11-24-2018, 01:52 AM.

                        Comment


                          #13
                          If I wanted to use this same concept of a stop loss, but instead of including the logic in the primary bars array, I want to use it on my secondary data series. 1 tick bars.

                          So once im in a long position, I want to set the stop loss below the low of the signal bar, in this case it would be Lows[0][1]

                          That is, the low of the previous bar on the primary data series.

                          I understand this logic would need to go under if (BarsInProgress == 1)

                          And therefore I would need to place the order using ExitLongStopMarket.

                          This is all working fine, but when I try to adjust that stop order to the low of last primary bar, it is not working correctly.

                          It currently looks like this

                          stopOrder = ExitLongStopMarket(1, true, stopOrder.Quantity, Lows[0][1], "MyStop", "MyEntry")

                          Any idea how to correct this? The backtest results clearly show the stop command is not being interpreted correctly.

                          Comment


                            #14
                            Hello murraym324 ,

                            The example I have provided places the order in OnOrderUpdate and would not be when a bar is updating. It would instead use the information of the last updated bar. (Meaning there is no BarsInProgress check because this is not running when a bar is processing in OnBarUpdate)

                            The goal of the previous posts in this thread was to wait until the entry order fills with an order that will not fill immediately when submitted and may fill a few bars later and then selecting the low of the bar the order filled on for the stop loss, instead of selecting the low of the bar at the time of the entry submission.

                            Is this also what you are trying to accomplish?


                            Are you wanting to place your action in OnBarUpdate instead?

                            You would use BarsInProgress in OnBarUpdate to choose which series is processing when the action is triggered. An Entry/Exit method order can be submitted to any added series when the action is triggered. Meaning you could submit the order to the added series when the primary is processing or vice versa.


                            Are you adding intra-bar granularity and submitting?
                            If so, yes, an ExitLongStopMarket() order would necessary as Set methods will only fill (or trail) when the primary series is updating and it is not possible to submit the order to the 1 tick series.


                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              Yes, I am indeed adding intra-bar granularity.

                              Thank you for your insight, your solution was exactly what I needed.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by GussJ, 03-04-2020, 03:11 PM
                              16 responses
                              3,279 views
                              0 likes
                              Last Post Leafcutter  
                              Started by WHICKED, Today, 12:45 PM
                              2 responses
                              19 views
                              0 likes
                              Last Post WHICKED
                              by WHICKED
                               
                              Started by Tim-c, Today, 02:10 PM
                              1 response
                              8 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by Taddypole, Today, 02:47 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post Taddypole  
                              Started by chbruno, 04-24-2024, 04:10 PM
                              4 responses
                              51 views
                              0 likes
                              Last Post chbruno
                              by chbruno
                               
                              Working...
                              X