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

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."



      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 RideMe, 04-07-2024, 04:54 PM
            5 responses
            28 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Started by f.saeidi, Today, 08:13 AM
            1 response
            4 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by DavidHP, Today, 07:56 AM
            1 response
            6 views
            0 likes
            Last Post NinjaTrader_Erick  
            Started by kujista, Today, 06:23 AM
            3 responses
            9 views
            0 likes
            Last Post kujista
            by kujista
             
            Started by Mindset, Yesterday, 02:04 AM
            2 responses
            18 views
            0 likes
            Last Post NinjaTrader_RyanS  
            Working...
            X