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

A "trailing stop" order to ENTER a trade?

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

    A "trailing stop" order to ENTER a trade?

    I am searching for a way to ENTER a trade with a "trailing stop" order.

    The trailing stop in the ATM is only designed to EXIT a position,

    Instead of a trailing stop to EXIT a position, I want a way to manually initiate a 13 tick, trailing stop order to ENTER a position.

    For example, as prices are moving up, a Sell Stop Market Order would follow, tick-by-tick, 13 ticks behind. As soon as the market reverses 13 ticks, the standing order would be triggered.

    How can I get this function created in the strategy builder?

    Thank you so much for your time!
    Last edited by Scalp & Run; 04-14-2021, 09:54 AM.

    #2
    Hi Scalp & Run,
    It can be done with the Strategy Builder.
    1) Create a variable (double) for the short trailing trigger price, set it to current price +offset of -13 ticks (short trigger).
    Then on each bar, do the following:
    2) If the price +offset of -13 ticks is higher than your short trigger (because the price went up further before the entry condtition for your trade was met), update the short trigger variable, i.e. set it to (higher) current price +offset of -13 ticks.
    3) If the price doesn't mark a new high, but crosses below your short trigger, go short.
    4) Once you exited your trade, start over again and initialize your short trigger (see above).
    NT-Roland

    Comment


      #3
      Hello Scalp & Run,

      Thank you for your post.

      This would be possible to accomplish in the Strategy Builder using the steps provided by NT-Roland.

      You would need to create a double variable in the Inputs and Variables screen of the Strategy Builder. The double variable would be used to hold the short trailing trigger price by assigning the variable a value of Close[0] + an offset of -13 ticks.

      You would then create a condition that checks if the current price (Close[0]) + offset of -13 ticks is greater than your double variable and update the variable to hold the new Close[0] + offset of -13 ticks.

      Another condition would be made to check if the price + offset of -13 ticks is less than or equal to your variable and if the current price crosses below your variable then enters a short position.

      See the help guide documentation and the publicly available training video, Strategy Builder 301, below for information about using the Strategy Builder.
      Strategy Builder - https://ninjatrader.com/support/help...gy_builder.htm
      Strategy Builder 301 — https://www.youtube.com/watch?v=_KQF2Sv27oE&t=13s

      Let us know if we may assist further.

      Brandon H.NinjaTrader Customer Service

      Comment


        #4
        Thank you for elaborating on the instructions!

        Could a screen-share session be scheduled so you can show me how to accomplish these steps in the Strategy Builder?

        If not, could you create a video to show me exactly how to perform each step correctly?

        Comment


          #5
          Hello Scalp & Run,

          Thank you for your note.

          NinjaScript and Strategy Builder related items are only handled through email/forums as we find this is the best way to provide support for these items.

          Also note that in the support department at NinjaTrader it is against our policy to create, debug, or modify, code or logic for our clients. Further, we do not provide C# programming education services in our support. This is so that we can maintain a high level of service for all of our clients as well as our partners.

          That being said, see the attached video below demonstrating how this could be accomplished. Also, see the attached strategy example used in the video demonstration.


          Let us know if we may assist further.
          Attached Files
          Brandon H.NinjaTrader Customer Service

          Comment


            #6
            Thank you so much Brandon!

            I'll try it and let you know how it goes.

            Comment


              #7
              I completed the steps provided in your helpful video.
              • If the steps only apply to short trades, please provide the steps for long trades.
              • What do I need to do with the .zip file?
              • How do I initiate a trailing stop short trade order?
              • How do I initiate a trailing stop long trade order?
              • When the order is triggered, will my ATM strategy trigger as well?
              Last edited by Scalp & Run; 04-15-2021, 07:44 AM. Reason: Clarifying questions

              Comment


                #8
                Hello Scalp & Run,

                Thank you for your note.

                If the steps only apply to short trades, please provide the steps for long trades.

                To apply the attached example in my previous post to long orders, you would need to follow the same steps as the ones taken for short orders and call EnterLongStopMarket() instead of EnterShortStopMarket() and apply the appropriate offset for your long stop order (ex: an offset of +13 instead of -13). EnterLongStopMarket() would be used to place a long stop order whereas EnterShortStopMarket() is used to place a short stop order.

                What do I need to do with the .zip file?

                The zip file was attached to demonstrate an example of how to enter a position using a trailing stop order. You could import the script to your platform (Tools > Import > NinjaScript AddOn) and open the Strategy Builder to view how the script is set up.

                How do I initiate a trailing stop short trade order?

                The example attached in my previous post demonstrates how to place a short trailing stop order to enter a position. If you would like to attach a trailing stop order to your initial trailing stop order once it is filled you would have to unlock the code from the Strategy Builder using the 'Unlock code' button and manually program this. After unlocking the code, you would create an order object for your entry order and stop order. You would check if the order.Name == the entry order's signalName and assign our entry order object. Then, you would use the OnExecutionUpdate() method to manage your stop order.

                See the SampleOnOrderUpdate example script for an example of using OnExecutionUpdate() - https://ninjatrader.com/support/help...and_onexec.htm

                How do I initiate a trailing stop long trade order?

                See the answer to the first question above for how to place a long trailing stop order to enter a position. If you would like to attach a long trailing stop order to your initial trailing stop order once it is filled you would have to unlock the code from the Strategy Builder using the 'Unlock code' button and manually program this. You would need to take similar steps as the question above and use OnExecutionUpdate() to place and manage a long stop order.

                When the order is triggered, will my ATM strategy trigger as well?

                An ATM strategy will not trigger when an entry order is placed and filled. You would need to unlock the code from the Strategy Builder to accomplish using ATM Strategy templates in a NinjaScript. See the SampleAtmStrategy script that comes default with NinjaTrader for an example of how to use ATM Strategy templates in a NinjaScript. To view the example, open a New > NinjaScript Editor window, open the Strategies folder, and select the SampleAtmStrategy script.

                See this help guide documentation for more information about ATM strategy methods - https://ninjatrader.com/support/help...gy_methods.htm

                Let us know if we may assist further.
                Brandon H.NinjaTrader Customer Service

                Comment


                  #9
                  Hello Brandon,

                  What I don't understand is how does this strategy place the first order?
                  When I run the strategy in YM 1min i.e. it doesn't place any orders.

                  Kind regards

                  Comment


                    #10
                    Hello Pladijske,

                    Thanks for your note.

                    Are you referring to the EnterWithTrailingStop example script linked in post #5?

                    If so, you could view the strategy by opening New > Strategy Builder window and selecting the EnterWithTrailingStop strategy. Then, navigate to the Condition and Actions section to see the conditions used to place orders.

                    In the strategy, we can see that if the current market position is Flat and the current State is State.Realtime and if MyBool is true, we assign a value to the ShortTriggerPrice vairable and call EnterShortStopMarket() to place our order. If the current Close price is greater than the previous Close price, we assign a new value to the ShortTriggerPrice variable. Then we check if we are still in a Flat position and call our entry order method again with the new value held in the ShortTriggerPrice variable.

                    Note when running the script it is important to keep an eye on the Log tab of the Control Center for any errors that may occur.

                    Let us know if we may assist further.
                    Brandon H.NinjaTrader Customer Service

                    Comment


                      #11
                      Bumping this thread.

                      There are some really interesting possibilities with this that I'd like to explore. I found this thread because wanted to "chase" a long or short entry as price moves in the direction of a trade. Wouldn't it be cool to chase, enter when price comes back, hit a profit target, then chase another entry- rinse, repeat, until stopout?

                      I'd like to understand more about how the coding described above works. I've put together the code in Strategy Building from Brandon's video, but I'm confused about how it actually works.

                      Questions:

                      1) Where do you put your custom entry logic?

                      Keeping things simple, say your logic were:

                      Open[1] > Close[1]
                      && Open[0] > Close[0]

                      Where would you specify that in relation to the three conditional statements created by Brandon?
                      Would you create an "if" statement for the entry logic, and then nest the offset order logic within that?

                      2) Will this type of entry setup only work on real-time data? Meaning it can't be backtested?

                      3) How would you re-enter the trailing entry logic step upon hitting, specifically, the profit target for this setup's stop entry order?

                      Thanks,
                      Craig

                      Comment


                        #12
                        Hi Craig, thanks for writing in. The entry order in this exapmle is placed in Set 1 and the trailing is set up in Set 3 with a condition in between to set a the trigger price.

                        It will only work in real time beacuse of the extra conditions in the entry condition: State == State.Realtime

                        You can set up a stop and target for this entry in the "Stops and Target" section of the builder.

                        Kind regards,

                        -ChrisL

                        Chris L.NinjaTrader Customer Service

                        Comment


                          #13
                          Thanks Chris. I will experiment with this.

                          Do you have any resources to point me toward that would help me develop for one trade:

                          1a. An initial fixed stop specific to a labeled trade entry (same way that stops and profit targets can be assigned to a string describing the entry)

                          or

                          1b. A set of conditions that invalidate a specific setup entry (say, a certain bar pattern instead of an outright loss of points).

                          2. A stop that trails from a specified profit level.

                          For example, I have certain trade setups for which the setup is invalidated by conditions that may develop quickly after the trade is entered, but which do not yet hit a price based threshold for stop.

                          I'm still a beginner NT script writer. I knew nothing about any of this three weeks ago, but paid a programmer to develop a script that did what I want. Through a lot of reverse engineering, reading, and playing with the strategy builder and code editor, I've managed to build several strategies, including one monster with 750+ lines of code. I'm progressing to the point where I want to do unmanaged orders, which I suspect is where this post is leading me.

                          This monster strategy has about 15-20 different conditional entries that I would like to manage in different ways that go beyond the standard managed approaches. I have conditional exits set up and they work well, but I would like to be able to attach them to some entries, but not others.
                          Last edited by innuendo212; 12-28-2022, 02:01 PM.

                          Comment


                            #14
                            Hi innuendo212, thanks for your reply. I do not have specific examples for what you are asking. It would take a specific setup and know-how in using the builder to create the actions that you want. Keep in mind though, the strategy builder is meant to create simple strategies. If you need to manage orders in a complex way, such as canceling them on demand and knowing when orders fill or become canceled, you will need to develop scripts in the NinjaScript language through the NinjaScript editor. We have a collection of examples showing various different things that can be done in the condition builder here:
                            https://ninjatrader.com/support/help...gybuilder_cond ition_builder.htm

                            Kind regards,

                            -ChrisL
                            Chris L.NinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by NinjaTrader_BrandonH View Post
                              Hello Pladijske,

                              Thanks for your note.

                              Are you referring to the EnterWithTrailingStop example script linked in post #5?

                              If so, you could view the strategy by opening New > Strategy Builder window and selecting the EnterWithTrailingStop strategy. Then, navigate to the Condition and Actions section to see the conditions used to place orders.

                              In the strategy, we can see that if the current market position is Flat and the current State is State.Realtime and if MyBool is true, we assign a value to the ShortTriggerPrice vairable and call EnterShortStopMarket() to place our order. If the current Close price is greater than the previous Close price, we assign a new value to the ShortTriggerPrice variable. Then we check if we are still in a Flat position and call our entry order method again with the new value held in the ShortTriggerPrice variable.

                              Note when running the script it is important to keep an eye on the Log tab of the Control Center for any errors that may occur.

                              Let us know if we may assist further.
                              Hi Brandon,

                              I tried adding the long side to your example but can't seem to get it working right. In theory, as soon as a short trade is entered, the trailing long stop entry should come up on the chart and follow the price down until it reverses 13 ticks(or whichever setting we set it at) and then go long. And when it goes long, the trailing short stop entry should come up on the screen. Will this example do this? One other thing i noticed is that its OnBarClose, so I changed that to OnPriceChange so it would trail live but it's still only doing it OnBarClose, is there something else that needs to be entered in the conditions in order to the OnPriceChange or OnEachTick to work? Thanks​

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by kujista, Today, 06:23 AM
                              5 responses
                              15 views
                              0 likes
                              Last Post kujista
                              by kujista
                               
                              Started by f.saeidi, Today, 10:19 AM
                              1 response
                              4 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by traderqz, Yesterday, 09:06 AM
                              2 responses
                              16 views
                              0 likes
                              Last Post traderqz  
                              Started by traderqz, Today, 12:06 AM
                              3 responses
                              6 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by RideMe, 04-07-2024, 04:54 PM
                              5 responses
                              28 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Working...
                              X