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

Building a trailing stop that triggers after level reached

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

    Building a trailing stop that triggers after level reached

    Hello,

    I'm trying to build a trailing stop that works similar to the one you can do in ATM. ie. If I'm in profit 10 ticks, set the stop loss at 5 ticks and then move it up as the price moves up, always keeping 5 ticks below the current price or even better. After 10 ticks stop the price at +5 ticks...don't do anything until it reaches 15 ticks and if it does, set stop loss to +10 ticks...if it hits +20 then set stop loss, to +15, etc.

    Let's say I'm just going long for now. Here's my approximate implementation:

    Code:
    // Somewhere in code:
      SetStopLoss(signal, CalculationMode.Price, stop, false);
      EnterLong(signal...);
    (And as an aside...what's the purpose of that last false...? Under what circumstances would I want a simulated stop...?)

    And then...
    Code:
      protected override void OnBarUpdate()
            {
                try
                {
                    if (BarsInProgress == 0)
                    {
                        if (IsFirstTickOfBar)
                        {
                            if(Open[1] > stop)
                            {
                                stop = Open[1] + tenTicks;
                                SetStopLoss(signal, CalculationMode.Price, Open[1] - fiveTicks, false);
                            }
                        }
                    }​
    // more code.
    The problem I'm running into is that if price moves quickly I sometimes set the stoploss above market, which is fine...but it seems to spawn an infinite number of popups with the "Cannot set stop loss above market..." error. I currently have RealtimeErrorHandling.StopCancelCloseIgnoreRejects set and have also tried IgnoreAllErrors but I keep getting that popup. I've tried setting the stop loss to quite wide (Open[1] - atr/2) and I still get these infinite popups. Can I disable those?

    Alternatively, I could use a plain SetStopLoss until I detect that a certain threshold has been reached and then I can cancel the stoploss and use SetTrailingStop instead, but I'm not sure how I'd do that.

    What's the best practice here?

    Thx!

    #2
    Hello -kman-,

    There is no way to disable the error popups for rejections, that is added so you know that the reject happened and can address the situation if needed. The only solution to avoid that is to not place the stop on the wrong side of the market. One way of trying to avoid that is to add a price condition before calling your stop order again to make sure that price is on the right side of the market. You would otherwise need to not try and trail so closely to the current price.

    I've tried setting the stop loss to quite wide (Open[1] - atr/2)
    As you are using the open of the previous bar and an indicator value that could be very different from the Close[0] price where your error would be coming frrom. If the price you calculate is on the wrong side of the last price Close[0] its going to be rejected.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Actually sorry, I am using Close[0] - atr/2.

      Alternatively - can I use ExitLongMIT()? ie...say price is at 4200, and I set an ExitLongMIT at 4190 and at 4210. Will either those immediately? Or only when they touch? And is there a chance that price somehow drops past my exit and I end up in an unexpected state?

      Thanks!

      Comment


        #4
        Hello -kman-,

        Any type of order that requires being on a specific side of the market will bring up that error if you try to change its price to an invalid price. An MIT order is market if touched and uses a stop price for knowing when to convert to a market order. That still needs to be placed on the correct side of the market initially so the price movement can run into the stop price and convert it to a market exit.

        JesseNinjaTrader Customer Service

        Comment


          #5
          Hmmm - ok. And just to be clear, if I've previously called SetStopLoss() there's no way to subsequently change that to a trailing stop using SetTrailStop()...?

          (Thanks again btw! This has been very helpful)

          Comment


            #6
            Hello -kman-,

            SetStopLoss and SetTrailStop are separate methods. If you previously called SetStopLoss you can't convert that to a trailing stop by calling SetTrailStop. You would have to add your own logic to call SetStopLoss again to update the stops price, any time you call the SetStopLoss again while the order is active it will update it. Alternatively you would need to only use SetTrailStop if you wanted a trailing stop that uses the SetTrailStop logic.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Yeah - but then I run into the infinite popup scenario...

              Can I at least get the popup to only fire once somehow? It seems that NT will continue to resubmit the setstoploss modification until it takes. Can I get it to stop once or handle that programatically?

              Comment


                #8
                Hello -kman-,

                If you see an infinite set of popups that would be based on what you are doing in your logic. That popup only happens when you try to do something that is not valid. To avoid that you need to change what you are doing.

                A simple example would be if you used OnEachTick calculation and are using an invalid price each time a tick comes in. The platform is going to obey what you ask and try to submit/change the order to that price, if that price is not valid based on the current market the broker is going to say no with a rejection. The platform has to log what the broker said by the form of a popup message.

                A rejection is not something that should be ignored, a rejection is a serious problem when it comes to live trading. If your order is rejected that may leave a position unprotected so it would be unwise to ignore or otherwise not see that message because it could lead to large account losses. The message is in your benefit so you can manage the situation to avoid losses.

                The best course of action is to avoid what you are doing and come up with a different strategy on what your goal is so that your changes are not being rejected. If you are using OnEachTick calculation a simple way to avoid that is to check that the price you calculate is valid before calling SetStopLoss. If you are using OnBarClose that can be more difficult with the granularity of the series being used. You may need to just use larger offsets in that use case because SetTrailStop and OnbarUpdate logic are updated on each bar close.



                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Well, the goal is to achieve what can be done using the ATM UI, really...With the ATM UI I can say "After I'm in profit 8 ticks, set stop loss to breakeven and then step up every 4 ticks."

                  I'd imagine this is a fairly common thing for people to try to do...?

                  I am using OnBarUpdate to set the new stoploss. Should I be accessing Close[0] to get the latest price then?

                  Comment


                    #10
                    Hello -kman-,

                    You cam make a break even in code as well, you would need to use logic to do that. There is a sample in the help guide that shows a break even being used, you can find that here:



                    That only moves the order to break even, if you wanted to later move the stop again after some amount of profit you would have to make another condition which handles that goal. I am not aware of a specific example that has both of those concepts combined.
                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      Yeah - I ended up just not moving the stop loss at all, and handling it all in code. Keep the stop loss and next target as a float and close things if triggered. This isn't ideal at all, but gets around all the problems we talked about. I'll probably implement setting stop loss at breakeven just in case the strategy breaks.


                      I think the infiinite popups is because I was running in simulation at max speed.Normally I'd get 4 popups per bar, but at max speed I get like 50 bars per second so it feels like it's infinite.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by rhyminkevin, Today, 04:58 PM
                      3 responses
                      48 views
                      0 likes
                      Last Post Anfedport  
                      Started by iceman2018, Today, 05:07 PM
                      0 responses
                      5 views
                      0 likes
                      Last Post iceman2018  
                      Started by lightsun47, Today, 03:51 PM
                      0 responses
                      7 views
                      0 likes
                      Last Post lightsun47  
                      Started by 00nevest, Today, 02:27 PM
                      1 response
                      14 views
                      0 likes
                      Last Post 00nevest  
                      Started by futtrader, 04-21-2024, 01:50 AM
                      4 responses
                      50 views
                      0 likes
                      Last Post futtrader  
                      Working...
                      X