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 Prevent a StopLoss from Adjusting On Every OnBarUpdate

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

    How To Prevent a StopLoss from Adjusting On Every OnBarUpdate

    I'd like to mange my stop loss by hand. However, the strategy keeps snapping it back into it's original position on the close of every bar. Can you remind me the method used to prevent this from happening. Again, I love the way it's bring me into the trade but I want full control of my stop once I'm in the market. I believe there is some bool that can be set but I can't remember and the docs are always a bit of a mystery.

    Thank you!

    Code:
    if ((Position.MarketPosition == MarketPosition.Flat)
    && (Condition == true))
    
    {
    
    SetProfitTarget(@"scalp", CalculationMode.Ticks, 7);
    SetStopLoss(@"scalp", CalculationMode.Ticks, MyStop, false);
    EnterLong(DefaultQuantity * 2, @"scalp");
    
    SetProfitTarget(@"runner", CalculationMode.Ticks, 100);
    SetStopLoss(@"runner", CalculationMode.Ticks, MyStop, false);
    EnterLong(DefaultQuantity, @"runner");
    
    
    }

    #2
    Hello coopgrafik,

    With set methods these are tied to OnBarUpdate.

    Instead, use exit orders with liveUntilCancelled as true. These will not update back to the original price when OnBarUpdate runs.
    https://ninjatrader.com/support/help...itlongstop.htm
    https://ninjatrader.com/support/foru...604#post478604
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks, Chelsea,

      However, I'm clicking around but the docs simply say:

      ExitLongStop(stopPrice);

      I just need a little more info than that. Of course, the docs state that this method accepts 4 arguments but there's just this one example. Also, does this method only calculate based on price versus ticks?

      In my code, MyStop equals 17 ticks. I'm not really sure how to convert to a price-based stop that sets up to 17 behind the price to a price-based formula. Something like??? (Position.AveragePrice - (MyStop * TickSize))

      Is there any way you can give me a leg up and covert the line below to the line of code I should use?

      SetStopLoss(@"scalp", CalculationMode.Ticks, MyStop, false);

      Comment


        #4
        Hello coopgrafik,

        The overloads are listed in the help guide.
        ExitLongStop(double stopPrice)
        ExitLongStop(int quantity, double stopPrice)
        ExitLongStop(double stopPrice, string fromEntrySignal)
        ExitLongStop(double stopPrice, string signalName, string fromEntrySignal)
        ExitLongStop(int quantity, double stopPrice, string signalName, string fromEntrySignal)
        ExitLongStop(int barsInProgressIndex, bool liveUntilCancelled, int quantity, double stopPrice, string signalName, string fromEntrySignal)

        Use the overload with the liveUntilCancelled parameter. (This is demonstrated in the ProfitChaseStopTrailExitOrdersExample_NT7)

        For example:
        ExitLongStop(0, true, 1, Position.AveragePrice - MyStop * TickSize, "exit", "scalp");
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hey Chelsea,

          When I added that line of code the complier threw out an error.

          "The name 'ExitLongStop' does not exist in the current context"

          Looking at the docs I see that ExitLongStop is not actually a method in the library. Did you mean ExitLongStopMarket?

          Also, I fiddled around with ExitLongStopMarket but it's not dropping a stop in the correct place. I'm calling Position.AveragePrice before the I'm even in the market as the next line of code is EnterLong.

          ExitLongStopMarket(0, true, 1, GetCurrentAsk() - 17 * TickSize, "exit", "scalp");

          Is this is a better version? I just want you to know that I'm trying this but it's still not working at all.

          In the code below I need to place a split order: two unique entries with two unique stops.

          Can you help me revise the 2 stops below to work so the algo brings me in but I still have the ability to manage the stops without then "snapping" back into place on every onBarUpdate?

          Thank you.

          Code:
           if ((Position.MarketPosition == MarketPosition.Flat)
          
          && (Conditions)
          
          {
          
          SetProfitTarget(@"scalp", CalculationMode.Ticks, 7);
          SetStopLoss(@"scalp", CalculationMode.Ticks, 17, false); // replace this
          // ExitLongStopMarket(0, true, 1, Position.AveragePrice - 17* TickSize, "exit", "scalp"); // this does not work
          EnterLong(DefaultQuantity * 2, @"scalp");
          
          SetProfitTarget(@"runner", CalculationMode.Ticks, 100);
          SetStopLoss(@"runner", CalculationMode.Ticks, 17, false); // this does not work
          // ExitLongStopMarket(0, true, 1, Position.AveragePrice - 17 * TickSize, "exit", "runner"); // this does not work
          EnterLong(DefaultQuantity, @"runner");
          
          
          }
          Last edited by coopgrafik; 08-03-2021, 09:05 PM.

          Comment


            #6
            Hello coopgrafik,

            ExitLongStop() is in the NinjaTrader 7 help guide.
            https://ninjatrader.com/support/help...itlongstop.htm

            ExitLongStopMarket() is a NinjaTrader 8 method and not for NinjaTrader 7.

            Further, you will need to use Position.AvgPrice.
            https://ninjatrader.com/support/help...7/avgprice.htm


            The entries have unique signal names and I would expect these both to be submitted if EntriesPerDirection is set to 2.
            https://ninjatrader.com/support/help...rdirection.htm

            The ExitLongStop() calls will be called after the entry is filled. This could be in OnOrderUpdate(), or OnExecution(), or in OnBarUpdate after the position is taken.

            Try submitting the exit orders in OnOrderUpdate().
            https://ninjatrader.com/support/help...rderupdate.htm

            if (order.Name == "scalp" && order.OrderState == OrderState.Filled)
            {
            ExitLongStop(order.AvgFillPrice - 17 * TickSize, "scalp");
            }
            Last edited by NinjaTrader_ChelseaB; 08-04-2021, 06:32 AM.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Chelsea,

              Thank you once again for your response but I'm on NT8 (lifetime) so this is why I'm getting the error. I'm not sure why you're making the assumption that I'm using the older software.

              So back to my issue. I simply want to to be able to move my stop without it being adjusted and reset after each OnBarUpdate.

              What I hear you telling me is that this will work in NT8. I just want to make sure we're onthe same page.

              /// OnOrderUpdate - open

              SetProfitTarget(@"scalp", CalculationMode.Ticks, 7);
              if (order.Name == "scalp" && order.OrderState == OrderState.Filled)
              {
              ExitLongStop(order.AvgFillPrice - 17 * TickSize, "scalp");
              }
              EnterLong(DefaultQuantity * 2, "scalp");

              /// OnOrderUpdate - close





              Comment


                #8
                Hello coopgrafik,

                This was posted in the NinjaTrader 7 Strategy Development section of the forums indicating this is for NinjaTrader 7. I have moved this thread to the NinjaTrader 8 Strategy Development section of the forums.
                https://ninjatrader.com/support/foru...gy-development


                Below are links to the NinjaTrader 8 help guide.
                https://ninjatrader.com/support/help...stopmarket.htm
                https://ninjatrader.com/support/help...erageprice.htm
                https://ninjatrader.com/support/help...rdirection.htm
                https://ninjatrader.com/support/help...rderupdate.htm

                As well as links to NinjaTrader 8 examples.
                https://ninjatrader.com/support/foru...596#post806596
                https://ninjatrader.com/support/foru...269#post802269


                Yes, this will work in NinjaTrader 8. Use ExitLongStopMarket() with isLiveUntilCancelled as true in OnOrderUpdate().
                EnterLongStopMarket(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double stopPrice, string signalName)

                I am also including a link to a forum post with helpful information about getting started with NinjaScript and C#.
                https://ninjatrader.com/support/foru...040#post786040
                Last edited by NinjaTrader_ChelseaB; 08-04-2021, 07:18 AM.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Chelsea, Sorry for incorrectly posting in the NT7 forum.

                  On the code below per your instruction I am using:
                  • ExitLongStopMarket()
                  • isLiveUntilCancelled = true
                  • I have switch back to Position.AveragePrice
                  However, the stops are not being set. Of course I have reviewed the documentation many times but it's not helpful if the method has 6 arguments but only provides this one very basic example: ExitLongStopMarket(stopPrice);

                  I'm 53 and started coding in the early 80s but there is only so much I can do as the NT library is proprietary. I'm trying to understand.

                  So I'm sorry I have to ask again. Can you explain why my stops are not being set with the code I've posted below. I'm doing everything I can on my part to be clear.


                  Code:
                  protected override void OnBarUpdate()
                  {
                  
                  if (BarsInProgress != 0)
                  return;
                  
                  if ((Position.MarketPosition == MarketPosition.Flat)
                  
                  && (Conditions))
                  
                  {
                  
                  SetProfitTarget("scalp", CalculationMode.Ticks, 7);
                  ExitLongStopMarket(0, true, 1, Position.AveragePrice - 17 * TickSize, "exit", "scalp");
                  EnterLong(DefaultQuantity * 2, "scalp");
                  
                  SetProfitTarget("runner", CalculationMode.Ticks, 100);
                  ExitLongStopMarket(0, true, 1, Position.AveragePrice - 17 * TickSize, "exit2", "runner");
                  EnterLong(DefaultQuantity, "runner");
                  
                  }
                  Last edited by coopgrafik; 08-04-2021, 08:36 AM.

                  Comment


                    #10
                    Hello coopgrafik,

                    Set methods and exit methods work differently. A Set method would be called before the entry. The Exit method would be called after

                    Further, you would not use both. You would use one or the other. The Set methods will revert back to the set price when OnBarUpdate runs, so you would not want to use these.

                    Instead these would be called after the entry order OrderState is OrderState.Filled.

                    Move the ExitLongStopMarket to OnOrderUpdate instead of OnBarUpdate.

                    When the entry order, in OnOrderUpdate() is OrderState.Filled, call the ExitLongStopMarket() method.

                    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
                    {
                    // check if the current order matches the orderName passed in "EnterLong"()
                    // Assign entryOrder in OnOrderUpdate() to ensure the assignment occurs when expected.
                    // This is more reliable than assigning Order objects in OnBarUpdate, as the assignment is not guaranteed to be complete if it is referenced immediately after submitting
                    (edited)
                    if (order.Name == "scalp")
                    {
                    ExitLongStopMarket(0, true, 1, order.AverageFillPrice - 17 * TickSize, "exit", "scalp");
                    }
                    }

                    Please let me know if this is not clear.
                    Last edited by NinjaTrader_ChelseaB; 08-04-2021, 10:32 AM.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Chelsea,

                      When I copy and paste your code I get an error. Please see the screenshot. Not sure what to do. Please advise. Thank you.
                      Attached Files

                      Comment


                        #12
                        Hello coopgrafik,

                        Apologies I was copying that from the previous post on nt7.

                        That should be order.AverageFillPrice.

                        if (order.Name == "scalp")
                        {
                        ExitLongStopMarket(0, true, 1, order.AverageFillPrice - 17 * TickSize, "exit", "scalp");
                        }
                        }
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Thanks again but I'm sorry to report that a stop is not being set. This is the set up I have below. I'm sorry I'm at a loss. See the screenshot attached and you can see how there is no stop being set.


                          Code:
                          protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
                          {
                          
                          if (order.Name == "scalp")
                          {
                          ExitLongStopMarket(0, true, 1, order.AverageFillPrice - 17 * TickSize, "exit", "scalp");
                          }
                          
                          }
                          
                          protected override void OnBarUpdate()
                          {
                          
                          if (BarsInProgress != 0) return;
                          if (CurrentBars[0] < 5) return;
                          
                          if ((Position.MarketPosition == MarketPosition.Flat)
                          && (Close[0] > Close[1]))
                          {
                          SetProfitTarget("scalp", CalculationMode.Ticks, 7);
                          EnterLong(DefaultQuantity * 2, "scalp");
                          }
                          
                          }
                          Attached Files

                          Comment


                            #14
                            Hello coopgrafik,

                            I overlooked the strategy position is not updated until the OnExecutionUpdate event.

                            Attached is an example script and a short video showing it work.
                            https://drive.google.com/file/d/1N3v...w?usp=drivesdk
                            Attached Files
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              Welp, I see it working for you but not for me. Can you have a look at my strategy and let me know what I'm doing wrong. I just don't see a difference. Thank you
                              Attached Files

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by KirkHammett, Today, 03:23 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post KirkHammett  
                              Started by eleven, Yesterday, 10:06 PM
                              0 responses
                              35 views
                              0 likes
                              Last Post eleven
                              by eleven
                               
                              Started by ezrollin, Yesterday, 08:12 PM
                              7 responses
                              72 views
                              0 likes
                              Last Post bltdavid  
                              Started by kyamato, 11-23-2021, 10:51 PM
                              6 responses
                              91 views
                              0 likes
                              Last Post jeronymite  
                              Started by fscabrera03, Yesterday, 07:14 PM
                              0 responses
                              49 views
                              0 likes
                              Last Post fscabrera03  
                              Working...
                              X