Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to SetStopLoss and SetTrailStop

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

    How to SetStopLoss and SetTrailStop

    I want to have my strategy place an order initially with a stoploss (the SetStopLoss() method is called in the Initialize() tag). Then, when the price gets to a predetermined target, I want to change that stoploss to a trailing stop.

    The problem I'm having is that when my strategy calls the SetTrailStop() method, it's ignored. Yes, I have already read the help page for SetTrailStop() thoroughly. It says that the SetStopLoss() takes priority over SetTrailStop(). So how do I cancel the original Stoploss order so that I can place a SetTrailStop? I tried this but it didn't work:

    CancelOrder(myEntry); // Cancel working stoploss order
    SetTrailStop(CalculationMode.Ticks, 4); // Change to a trailing stop

    #2
    Unfortunately once placed you can't cancel it. What you will want to do is manually code your own stop logic. It will behave as a regular ExitLongStop() till you reach your desired price. Then you have a loop or however you want it that will keep modifying the price of your ExitLongStop() and behave as if it were a trailing stop.

    The core framework for this is in the reference sample for OnOrderUpdate() and OnExecution(). Please take a look at that one.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by Josh View Post
      Unfortunately once placed you can't cancel it. What you will want to do is manually code your own stop logic. It will behave as a regular ExitLongStop() till you reach your desired price. Then you have a loop or however you want it that will keep modifying the price of your ExitLongStop() and behave as if it were a trailing stop.

      The core framework for this is in the reference sample for OnOrderUpdate() and OnExecution(). Please take a look at that one.
      Sorry, but that's ridiculous. I'm not going to code my own trailing stop logic when it exists already in a method. A possibility is to not use the SetStopLoss() method at all and just make the strategy send an exit order when a stop price is hit, like a simulated stoploss (the risk here is that if the computer/connection/strategy goes down, you are not protected with a standing stoploss order). So I would only be using SetTrailStop(), and in the help guide, it says:
      • Should you call this method to dynamically change the trail stop price in the strategy OnBarUpdate() method, you should always reset the trail stop price/offset value when your strategy is flat otherwise, the last price/offset value set will be used to generate your trail stop order on your next open position

      OK, so how do you "reset" the trail stop price/offset value so that there is NO trailing stop set for the next order until you call the SetTrailStop() method again?

      Life would be much easier if it was just possible to incorporate ATM Strategies into strategy code.

      Comment


        #4
        [quote=cassb;48408]
        OK, so how do you "reset" the trail stop price/offset value so that there is NO trailing stop set for the next order until you call the SetTrailStop() method again?
        quote]

        You can't. What you could do is submit ExitLongStop() and just modify the price when you need to.
        RayNinjaTrader Customer Service

        Comment


          #5
          [quote=NinjaTrader_Ray;48431]
          Originally posted by cassb View Post
          OK, so how do you "reset" the trail stop price/offset value so that there is NO trailing stop set for the next order until you call the SetTrailStop() method again?
          quote]

          You can't. What you could do is submit ExitLongStop() and just modify the price when you need to.
          OK, then I don't understand what that sentence means in the help guide. Anyway, what I'll do when I'm flat is SetTrailStop(CalculationMode.Ticks, 9999) to "reset" the trailing stop until next order.

          Can we get ATM Strategies available in strategy code some day, Ray?

          Comment


            #6
            They are available now --> Check the Help Guide. There is not direct link I can paste otherwise I would have done so.
            RayNinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Ray View Post
              They are available now --> Check the Help Guide. There is not direct link I can paste otherwise I would have done so.

              Is that a recent addition to NT? Because I thought I had already gone through this discussion in the following thread and the answer was "no, you cannot place orders from a strategy using an ATM Strategy to manage it."



              If this is indeed possible, then can you tell me what to search for in the Help Guide so I can find it? I have searched and only come up with ATMStrategyCreate(), which is not what I want to do.

              Thanks Ray!

              Comment


                #8
                If you want a NinjaScript strategy to create an ATM strategy which can be managed manually then use the method you found. This only works in real-time and not in a backtest.
                RayNinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_Ray View Post
                  If you want a NinjaScript strategy to create an ATM strategy which can be managed manually then use the method you found. This only works in real-time and not in a backtest.
                  Ray, that's not what I want.

                  What I want is to be able to place an order from my strategy code using an existing ATM Strategy that I created in the SuperDOM. If that is not possible, is it possible to have the strategy create a ATM Strategy in the Initialize tag that is an exact copy of an existing SuperDOM ATM Strategy? If so, is there a help guide for that?

                  Why can't I call my existing SuperDOM ATM Strategy as a method?

                  Comment


                    #10
                    Sorry, I thought what I have written is exactly what you want?

                    Let me clarify:

                    - If you want to run a NinjaScript strategy
                    - If you want this strategy to check for a condition that then creates an order that spawns an AMT strategy
                    - So, instead of placing an order manually in the DOM, the NinjaScript strategy generates the order

                    ---> Yes, you can do this and have been able to do this since Day 1 of NinjaScript strategies.

                    Please see the following resources -

                    - Help Guide section NinjaScript > NinjaScript Language Reference > Custom Strategy Methods and Properties > AtmStrategyMethods
                    - See the reference sample that installs with NinjaTrader (Tools > Edit NinjaScript > Strategy > SampleAtmStrategy)
                    RayNinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_Ray View Post
                      Sorry, I thought what I have written is exactly what you want?

                      Let me clarify:

                      - If you want to run a NinjaScript strategy
                      - If you want this strategy to check for a condition that then creates an order that spawns an AMT strategy
                      - So, instead of placing an order manually in the DOM, the NinjaScript strategy generates the order

                      ---> Yes, you can do this and have been able to do this since Day 1 of NinjaScript strategies.

                      Please see the following resources -

                      - Help Guide section NinjaScript > NinjaScript Language Reference > Custom Strategy Methods and Properties > AtmStrategyMethods
                      - See the reference sample that installs with NinjaTrader (Tools > Edit NinjaScript > Strategy > SampleAtmStrategy)

                      Oh man, I've been under the impression since that thread in April that you cannot use your existing ATM Stratgies within strategy code, and that ATMStrategyCreate() was to CREATE a completely new strategy within the code, have to manually manage that strategy within code, and then the ATM Strategy that was created goes away once the strategy code halts.

                      Ugh... I've been rewriting all the work I put into my ATM Strategies into this strategy code I'm working on all this time. Oh well... at least I learned how to program in C#.

                      Comment


                        #12
                        Originally posted by cassb View Post
                        Oh man, I've been under the impression since that thread in April that you cannot use your existing ATM Stratgies within strategy code, and that ATMStrategyCreate() was to CREATE a completely new strategy within the code, have to manually manage that strategy within code, and then the ATM Strategy that was created goes away once the strategy code halts.

                        Ugh... I've been rewriting all the work I put into my ATM Strategies into this strategy code I'm working on all this time. Oh well... at least I learned how to program in C#.

                        Along this same line in this thread... I'm using AtmStrategyCreate() to 'create' orders that are managed by my ATM Strategies (thank you for clarifying that!).

                        Tell me how this works now, because my code was using the OnExecution() method which doesn't seem to work now with the ATM Strategies. I figure the AtmStrategyCreate() call will spawn a 'subprocess' that then runs the ATM Strategy, so the strategy code really doesn't know anything about the execution of the orders or when they are closed, right? Is there a call or method or something I can use that will tell me when an ATM Strategy-managed order closes and what the profit/loss was from that position?

                        Comment


                          #13
                          All the "traditional" NinjaScript Strategy management features do not work with ATM strategies. When you have an ATM strategy you will need to work within the confines of those. Please see the Help Guide for all methods starting with GetAtm...() and AtmStrategy...().
                          Josh P.NinjaTrader Customer Service

                          Comment


                            #14
                            Originally posted by Josh View Post
                            All the "traditional" NinjaScript Strategy management features do not work with ATM strategies. When you have an ATM strategy you will need to work within the confines of those. Please see the Help Guide for all methods starting with GetAtm...() and AtmStrategy...().
                            I'm back to managing my orders without ATM Strategies, since I can't know when a ATM Strategy closes and what the PnL was for it. :-(

                            So can I have a SetTrailStop() active and also have a ExitLongStop() active as well on the same entry signal?

                            Comment


                              #15
                              Sure you can know an ATM strategy's info. Use GetAtmStrategyEntryOrderStatus()

                              There is a bunch of stuff for realized PnL and unrealized PnL too. Please look at the Help Guide for all the GetAtmStrategy...() methods.
                              Josh P.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              639 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              366 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              107 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              569 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              572 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X