• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

UnsetStopLoss()

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

    UnsetStopLoss()

    Firstly apologies for all my questions about StopLosses. I'm gradually figuring things out.

    Right now, my test strategy I'm working on starts-out without a stop loss; once I'm in a position and the position has profit, I call SetStopLoss(CalculationMode.Price, price) to introduce a stop loss at a specific price (based on an indicator or bar high/low or similar). Right now I'm using the stoploss to bank profits and closing the position at market with a loss if I get an opposing signal.

    The problem I'm having is that subsequent market positions are using the prior market position's stop price; even though I want each new market position to start without one (for now, at least).

    I can't find any methods that enable me to do this, and SetStopLoss(CalculationMode.Price, double.NaN) and a few other things I've tried don't seem to work.

    How can I clear a StopLossValue so that a new position starts without one?

    #2
    Hello kevinenergy,

    Thanks for your post.

    The Set methods (SetStopLoss, SetProfitTarget, SetTrailStop) all will retain the last value used and these orders are implemented as quickly as possible when an entry order is filled. There is no means to zero that out or turn off.

    In the help guide section for each of these is this tip/note: "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"

    A way to reset this is to check to see if you are in a flat market position and then set the stop loss to a known value such as 20 ticks. For example:

    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetStopLoss(CalculationMode.Ticks, 20); // initial stop position for each entry, regardless of direction.
    }

    Another way is in the block of code where you place your entry, to first set the stoploss to some value, for example:

    if (Your conditions to enter long)
    {
    SetStopLoss(CalculationMode.Price, Close[0] - 20 * TickSize); //set initial stop first
    EnterLong();
    }
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hi Paul.

      Thanks for responding. The docs say the following:

      "Entry() methods will reverse the position automatically. For example if you are in a 1 contract long position and now call EnterShort() -> you will see 2 executions, one to close the prior long position and the other to get you into the desired 1 contract short position."

      https://ninjatrader.com/support/help...d_approach.htm

      How I am I supposed to use this feature, with a price-based stop loss if I cannot clear the previous price?

      I understand why with relative stop values such as CalculationMode.(Tick|Currency|Pip|Percent) it makes sense to store the value and use it for the new position, since these still work when the position reverses.

      But for absolute stop prices using CalculationMode.Price it makes no sense. Since the Stop Price in a long position can never be valid if the position is reversed.

      I can't change the stop price via SetStopLoss BEFORE I reverse, since the new price won't be valid. And if I try to reverse without doing it, then the reversal will result in a rejection as the stop price is not valid.

      This seems like a totally impossible situation.

      Comment


        #4
        Hello kevinenergy,

        Thanks for your reply.

        It is your code that is calling the entry method, therefore you can set the stoploss value before placing the entry order. If you are unsure of what the price will be then you can use the first example I provided where you set the stoploss using CalculationMode.Ticks and set it to some number of ticks that you are comfortable with. When using Tick mode, the SetStopLoss method will automatically set it to the correct side for the entry that is made (long or short). After the entry is made you can then adjust the stop loss as you wish. It is perfectly acceptable to use CalculationMode.Ticks in one place and CalculationMode.Price in another.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Hi Paul,

          I have taken your advice and in cases when the MarketPosition has changed, I am setting the StopLoss to an arbitrary Tick value before entering the new position and then setting the stop loss to the real value; this ensures that the new position is opened with a valid stop price.

          Thank you for helping me to get this working.

          By way of feedback I think that the caching of StopLosses with CalculationMode.Price is a serious design error and should be reconsidered by your developers. A long stop loss with CalculationMode.Price will never ever be valid when the position is reversed to short or vice versa. Setting the Stoploss temporarily using CalculationMode.Ticks to get around this is a Hack. The docs are also not clear on any of this.

          Thank you again for the support and helping me find a working solution for this.

          Code:
                      if (mySignal== MarketPosition.Long)
                      {
                          // if it is a new long position
                          if (Position.MarketPosition != MarketPosition.Long)
                          {
                              // HACK: set stop to arbitrary distance away in case the
                              // MarketPosition is currently short, with a cached stop price value that is invalid for Long.
                              SetStopLoss(CalculationMode.Ticks, 100);
                              
                              EnterLong();
          
                              // get and set real stop price for a long position
                              var actualStopPrice = GetStopPrice(MarketPosition.Long);
                              SetStopLoss(CalculationMode.Price, actualStopPrice);
                              
                          }
                         else { 
                               // existing long position  - check for new stop price and update
                                var newStopPrice = GetStopPrice(MarketPosition.Long);
                                SetStopLoss(CalculationMode.Price, newStopPrice);
                          }
                      }

          Comment


            #6
            Hello kevinenergy,

            Thanks for your reply.

            Glad that worked out for you.
            Paul H.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by alfonso, Today, 10:21 AM
            0 responses
            1 view
            0 likes
            Last Post alfonso
            by alfonso
             
            Started by saeed, Today, 10:20 AM
            2 responses
            4 views
            0 likes
            Last Post NinjaTrader_RyanS  
            Started by ntbone, Yesterday, 05:21 PM
            1 response
            5 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by ntbone, Yesterday, 04:05 PM
            1 response
            5 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by tonynt, Today, 09:35 AM
            1 response
            11 views
            0 likes
            Last Post NinjaTrader_ChrisL  
            Working...
            X