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

How "change" quantity property value to Stop Loss Order

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

    How "change" quantity property value to Stop Loss Order

    Hi, I hope you can help me with the following problem.

    1.- I make a purchase using EnterLong(22)

    2.- I establish a StopLoss (model.price, price). This command makes two Stop Loss orders of type "Stop Market" appear in the Ninja Trader Orders tab, one with quantity 19 and the other with quantity 3. In any case, in the Chart Trader I see a Stop Loss line with quantity 22. All right!!

    3.- When the market price reaches a certain value, I do an ExitLong(10) and it is done correctly.

    4.- The problem is that the Stop Loss is still set at 22, when what I want is to change it to 22-10 = 12.

    I have tried to change the the stop loss quantity property, but it does not allow me to set them to 0 depending on their status. I have tried to cancel the stop loss orders, but it doesn't cancel them. I have tried setting another stop loss, but it doesn't work either. I'm a bit desperate, but I understand that there must be some way, especially since through the user interface, deleting a Stop Loss and re-creating another is as simple as using a couple of clicks.

    How could I do it using Ninja Script?

    Thanks in advance.

    A greating.

    #2
    Have you another input that not being used.

    Comment


      #3
      Hello JoseFRS,

      Thanks for your post.

      What specific version of NinjaTrader8 are you using (look under Help>About)

      What data feed do you connect to?

      I've created a short video to show how I have tested this here and cannot recreate your example under the two conditions: https://Paul-ninjaTrader.tinytake.co...Nl8xNzMzNTQ2OQ

      I've attached the test strategy and would ask if you can repeat my results, using sim101, or advise if you get different results. Please be sure to test both conditions as shown in the video.

      JoseFRS1.zip

      Paul H.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by NinjaTrader_PaulH View Post
        Hello JoseFRS,

        Thanks for your post.

        What specific version of NinjaTrader8 are you using (look under Help>About)

        What data feed do you connect to?

        I've created a short video to show how I have tested this here and cannot recreate your example under the two conditions: https://Paul-ninjaTrader.tinytake.co...Nl8xNzMzNTQ2OQ

        I've attached the test strategy and would ask if you can repeat my results, using sim101, or advise if you get different results. Please be sure to test both conditions as shown in the video.

        [ATTACH]n1161835[/ATTACH]
        Hi,

        I'm using Ninja Trader 8 version 8.0.24.2 64-bit and Playback Connection on M6E 06-21.

        I'll try it and i'll response you.

        Thanks.

        Comment


          #5
          Hello again,

          I still have the problem after your instructions and, in any case, the problem is reflected in the Stop Loss, and not in the number of open positions.

          I am attaching a simple and altered version of my original code in which the error can be reproduced, although in this case, the amounts are different from what I indicated before.

          I briefly comment on what the code does (example for a long purchase):

          Given two averages, when a cross is detected between them I set a Virtual Profit Target (not Real) above the opening price of the candle, a Stop Loss below the cross and I buy 22 contracts. All of this is done well and the Stop Loss shows 22 in the Chart Trader. When the market price reaches the established Virtual Profit Target, half of the positions are sold (11) and I try to set the Stop Loss at a different price.

          The problem is that:

          If StopTargetHandling == PerEntryExecution, the Stop Loss is set at the new price but the amount in it shown in the Chart Trader is 20!!
          If StopTargetHandling == ByStrategyPosition, the Stop Loss is removed in the Chart Trader


          I need the Stop Loss to stay at the number of remaining positions: 22-11 = 11.

          A clear example of this behavior can be seen on 04/05/2021 at 16:00 on M6E 06-21 using 60 minutes candles.
          Attached Files

          Comment


            #6
            Hello JoseFRS,

            Thanks for your reply.

            You have a lot going on in that strategy, I am asking that you create a reduced code version to demonstrate the issue simply and quickly as I did with my example which only has a single entry/exit in it.

            I would suggest removing all other code that is not related to the stop and just simple enterlong.

            Thanks in advance.
            ​​​​​​​
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_PaulH View Post
              Hello JoseFRS,

              Thanks for your reply.

              You have a lot going on in that strategy, I am asking that you create a reduced code version to demonstrate the issue simply and quickly as I did with my example which only has a single entry/exit in it.

              I would suggest removing all other code that is not related to the stop and just simple enterlong.

              Thanks in advance.
              ​​​​​​​

              Thanks for your reply. Believe me all the unnecessary code is removed. And really, although it seems too much, if you follow it, it is not that complex. Anyway, I'll try to simplify it even more and resubmit it to you. All the best.

              Comment


                #8
                Ok,

                I have managed to reproduce the problem with much simpler code. I apologize for the above code.

                I beg you to test it with Market Replace on the previous date closest to 04/05/2021 at 4:00 p.m. as shown in the attached image, since if not, it will probably not works fine, since which must be at a market rise.

                The tests are carried out with a simulated account with $ 10,000 of initial balance and connection to Playback Connection with data downloaded from M6E 06-21 of the month of April and 60-minutes candles. I'm using Ninja Trader 8 version 8.0.24.2 64-bit.

                I also attach screenshots of the result setting StopTargetHandling == PerEntryExecution.
                Attached Files

                Comment


                  #9
                  Excuse me,

                  I have just detected that I have another problem and that is that, although it generally works correctly, sometimes when I try to set the Stop Loss to another price, it also fails to set it.

                  Therefore, even if you manage to solve my previous question, it will not be enough for me.

                  For all this, I return to my original idea that it was simpler. I explain it to you below:

                  How can I simulate a click here with Ninja Script for remove Stop Loss? (See attached image)

                  This click ALWAYS WORKS, but by code I have tried to do CancelOrder but not always the orders are able to be canceled, so the amount of StopLoss sometimes is not as I wish. In addition, sometimes it does not allow me the price change. All this without taking into account that Stop Loss request may have been divided into several orders and must be controlled all of them.

                  I need something simpler. Just two steps:

                  1.- Simulate by code the pressing of that X to eliminate all Stop Loss
                  2.- Establish a new Stop Loss at the price I want

                  I know this can be done without hassle with the user interface and I just want to do the same in code.

                  Thanks in advance.

                  I appreciate any advice you can give me.

                  All the best.

                  Comment


                    #10
                    Originally posted by JoseFRS View Post
                    Excuse me,

                    I have just detected that I have another problem and that is that, although it generally works correctly, sometimes when I try to set the Stop Loss to another price, it also fails to set it.

                    Therefore, even if you manage to solve my previous question, it will not be enough for me.

                    For all this, I return to my original idea that it was simpler. I explain it to you below:

                    How can I simulate a click here with Ninja Script for remove Stop Loss? (See attached image)

                    This click ALWAYS WORKS, but by code I have tried to do CancelOrder but not always the orders are able to be canceled, so the amount of StopLoss sometimes is not as I wish. In addition, sometimes it does not allow me the price change. All this without taking into account that Stop Loss request may have been divided into several orders and must be controlled all of them.

                    I need something simpler. Just two steps:

                    1.- Simulate by code the pressing of that X to eliminate all Stop Loss
                    2.- Establish a new Stop Loss at the price I want

                    I know this can be done without hassle with the user interface and I just want to do the same in code.

                    Thanks in advance.

                    I appreciate any advice you can give me.

                    All the best.
                    Excuse me, but I am going a little crazy with this matter.

                    Now again I think that the problem of not being able to change prices is due to what was indicated in the previous post and that you can try with the code that I have attached to it.

                    In any case, I beg you both, to review this code and to answer me how to carry out the two simple steps that I indicate in this one.

                    Thanks again.

                    Comment


                      #11
                      Hello JoseFRS,

                      Thanks for your reply.

                      I was able to replicate with your reduced code on live data, thanks.

                      The proper approach here would be to split your entry into two entries and use the signal names to tie the entries to the two stop-loss orders. So you would create the two stop-loss orders each with the different entry signal names and this would allow you to exit the 10 and the reaming stop will already be the correct quantity.
                      Paul H.NinjaTrader Customer Service

                      Comment


                        #12
                        Ok, Thanks.

                        I'll try it.

                        Comment


                          #13
                          Originally posted by NinjaTrader_PaulH View Post
                          Hello JoseFRS,

                          Thanks for your reply.

                          I was able to replicate with your reduced code on live data, thanks.

                          The proper approach here would be to split your entry into two entries and use the signal names to tie the entries to the two stop-loss orders. So you would create the two stop-loss orders each with the different entry signal names and this would allow you to exit the 10 and the reaming stop will already be the correct quantity.
                          Sorry, but it not works!!

                          If I try:

                          EnterShort(11, "MySignalName1");
                          EnterShort(11, "MySignalName2");

                          the second EnterShort fails!!

                          Keep in mind that I need to enter 22 positions at the same time!!

                          What am I doing wrong?


                          Regardless of whether you tell me if I have correctly interpreted your solution using this, I would like to insist on the point "1.- Simulate by code the pressing of that X to eliminate all Stop Loss" that I told you in the previous post, because I think that would fix all my problems and, if it can be done through the user interface, I understand it must be able to be done by code.

                          Comment


                            #14
                            Hello JoseFRS,

                            Thanks for your reply.

                            With more than one entry per direction, you would need to change the "entry Handling" parameter from "All entries" to "Unique Entries", leave the Entries per direction at 1. By using 1 and unique entries this will allow the strategy to place the 2 entries because of the unique signal names (one per signal name).
                            References:



                            "Regardless of whether you tell me if I have correctly interpreted your solution using this, I would like to insist on the point "1.- Simulate by code the pressing of that X to eliminate all Stop Loss" that I told you in the previous post, because I think that would fix all my problems and, if it can be done through the user interface, I understand it must be able to be done by code."
                            I would suggest adding a chart button that you can then click to perform any action you wish/program. Here is a link to a strategy that uses two buttons that should get you started with the button coding:



                            Please note: The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.
                            Paul H.NinjaTrader Customer Service

                            Comment


                              #15
                              Hi Paul,

                              Thank yoy very much for all.

                              I managed to solve my problems by following your advice.

                              A greeting.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by arvidvanstaey, Today, 02:19 PM
                              4 responses
                              11 views
                              0 likes
                              Last Post arvidvanstaey  
                              Started by samish18, 04-17-2024, 08:57 AM
                              16 responses
                              61 views
                              0 likes
                              Last Post samish18  
                              Started by jordanq2, Today, 03:10 PM
                              2 responses
                              9 views
                              0 likes
                              Last Post jordanq2  
                              Started by traderqz, Today, 12:06 AM
                              10 responses
                              18 views
                              0 likes
                              Last Post traderqz  
                              Started by algospoke, 04-17-2024, 06:40 PM
                              5 responses
                              48 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Working...
                              X