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

ChangeOrder()

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

    ChangeOrder()

    Hi I would like some help with my code please:
    Click image for larger version  Name:	image.png Views:	0 Size:	12.6 KB ID:	1238924
    ​​Im looking to change the stop price of the unfilled order to 4 ticks higher, (It would be EMA2[0] + (-12*TickSize)), in the scenario the else if applies.
    Thank you
    Attached Files
    Last edited by Gianni_Destroyer; 03-08-2023, 01:42 PM.

    #2
    Hello Gianni_Destroyer,

    To ensure the order is not filled, assign the order to a variable in OnOrderUpdate().

    Then check the order.OrderState is OrderState.Working.

    The help guide has sample code of assigning an order to a variable.




    If you wanted to change the order to be 4 ticks higher than its current working stop price, add 4 ticks to the price of the stop.

    if (entryOrder != null && entryOrder.OrderState == OrderState.Working /* && conditions to trigger price change here*/)
    {
    EnterShortStopMarket(0, true, DefaultQuantity, entryOrder.StopPrice + 4 * TickSize, "ashe1");
    }
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi ChelseaB, thank you for answering, but I am still having trouble to achieve what im looking for. This is for an entry going long but it is the same logic.

      Click image for larger version

Name:	image.png
Views:	118
Size:	46.1 KB
ID:	1239178​The original order still does not move down 4 ticks before being filled if the current close is lower than the EMA specified.

      Comment


        #4
        Hello Gianni_Destroyer,

        How do you know this has occurred?

        Add prints to the script and enable TraceOrders to understand behavior. See the forum post below and watch the training videos.


        One line above the condition, print the time of the bar, print a label for entryOrderLong.OrderState, print the value for entryOrderLong.OrderState, print a label for == OrderState.Working, print a label for the Close[0] print the value of the Close[0], print a label for less than EMA2[0], print the value of EMA2[0].
        Then print the entryOrderLong.StopPrice minus 4 ticks.

        Save the output from the output window to a text file, and attach this to your next post.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi, I´m sorry for answering some days later, but I´ve been running with this problem and I do not know how to fix it:
          Click image for larger version

Name:	image.png
Views:	120
Size:	21.5 KB
ID:	1240096​I enable the strategy, to see the prints you told me to add, and automatically the strategy disables leaving this error message.

          Comment


            #6
            Hello Gianni_Destroyer,

            The error message means the code has attempted to use a variable with a null value.

            Below is a link to a forum post that discusses.
            Hello, I keep running into an error of "Object reference not set to an instance of an object." when I set the entryOrder to Null after it has been cancelled. Below is the block of code that I found in several of the example strategies. protected override void OnOrderUpdate(Order order, double limitPrice,


            What is the specific line of code in OnBarUpdate() with the variable that has a null value?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              ​The code is the same one as I attached you in my second post. It used to work but suddenly this error appears and I didn´t change anything. I assign entryOrderLong to be null and if this is true and the other coditions apply then I enterLongStopMarket. In onOrderUpdate I assign the null variable (entryOrderLong) to be the current order:
              Click image for larger version

Name:	image.png
Views:	102
Size:	165.4 KB
ID:	1240203
              Attached Files

              Comment


                #8
                Hello Gianni_Destroyer,

                What is the specific line of code causing the error?

                Which variable is null?


                You can add a print before each line to see which print is the last print to appear before the error appears.
                You could comment out code until the error stops and look at the last line that was commented out.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  I don´t know whta is the specific line causing the error. I placed loads of prints and none appears in the output.
                  The null variable is entryOrderLong. I am really stuck here, sorry and thanks for the help.

                  Comment


                    #10
                    Hello Gianni_Destroyer,

                    If you comment out all code in OnBarUpdate(), does the error still occur?

                    If you uncomment the first line in OnBarUpdate(), does the error occur?


                    If you have identified the line causing the error and have determined that the use of the entryOrderLong variable is causing the error, then ensure the variable is not null before using the variable.

                    if (entryOrderLong != null)
                    Print(entryOrderLong.ToString());
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi ChelseaB,

                      Yes, if I comment out all the code in OnBarUpdate() the error still occurs and if I uncomment the first line it still has the error. The only variable that is null is entryOrderLong so I assume that is causing the problem. What I don´t uderstand is the fact that in this example https://ninjatrader.com/support/help...rderupdate.htm they assign a null variable in the same way I did.

                      Comment


                        #12
                        I would not recommend counting on a short-circuit skipped evaluation of "if (entryorderlong !=null && entryorderlong.whatever == whatever)" - as a matter of good practice you probably should be checking for null and then, only if that instance is not null, checking its properties.

                        Also, make sure it's not one of your indicator instance variables like EMA1 etc that is null.
                        Bruce DeVault
                        QuantKey Trading Vendor Services
                        NinjaTrader Ecosystem Vendor - QuantKey

                        Comment


                          #13
                          Hello Gianni_Destroyer,

                          If you have commented out all code in OnBarUpdate() and the error is still occurring, then you have not compiled the script and reloaded.

                          The error is in OnBarUpdate(). If all code in OnBarUpdate() is commented out and the script is compiled, there would be no error able to come from OnBarUpdate() because there is no code in OnBarUpdate().

                          Be sure that after you have commented out all of the code, that you compile, and then reload NinjaScript on the chart. You would be looking to ensure there are no new errors appearing (with the current date and timestamp on the Log tab of the Control Center).
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Hi, I was able to fix the error, thank you!
                            Anyways, I'am still having problems when trying to change the price of an order that´s been accepted yet not filled. I want the unfilled order price to be 4 ticks lower than its initial price if a condition is met, here is the code:
                            Click image for larger version

Name:	image.png
Views:	101
Size:	17.7 KB
ID:	1241380​The problem I'am having is that once the condition is met an error occurs stating I cannot change the price of the order to a price level lower than the market price. But I placed a print to see this, and as I will show you, the first stopPrice - 4 ticks gives a price higher than the Close, so i dont understand how to fix this.

                            Click image for larger version

Name:	image.png
Views:	92
Size:	32.8 KB
ID:	1241384
                            p.d = I named low the current price
                            ​I also don't understand why does the entryOrderLong.StopPrice - 4 ticks changes its price because the initial order doesn´t change price.
                            Thank you!​​
                            Attached Files

                            Comment


                              #15
                              Hello Gianni_Destroyer,

                              A buy stop order must have the stop price above the current ask, or this will be rejected by the brokerage.

                              You will need a condition to ensure that entryOrderLong.StopPrice + (-4 * TickSize) is greater than GetCurrentAsk().
                              Hi I have a sellshort StopLimit order that i change if unfilled. I tick below the last bar's low. I keep getting the stop price can't be changed above the market but the price value it quotes ( the one I want) is below the market. Currently I am on the simulated feed. I appreciate in real market situations this could happen


                              If it is not, meaning this calculated price is equal to the current ask or below, I would recommend you instead cancel this order, and place a market order instead, which will fill immediately at market price.
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by burtoninlondon, Today, 12:38 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post burtoninlondon  
                              Started by AaronKoRn, Yesterday, 09:49 PM
                              0 responses
                              12 views
                              0 likes
                              Last Post AaronKoRn  
                              Started by carnitron, Yesterday, 08:42 PM
                              0 responses
                              11 views
                              0 likes
                              Last Post carnitron  
                              Started by strategist007, Yesterday, 07:51 PM
                              0 responses
                              13 views
                              0 likes
                              Last Post strategist007  
                              Started by StockTrader88, 03-06-2021, 08:58 AM
                              44 responses
                              3,982 views
                              3 likes
                              Last Post jhudas88  
                              Working...
                              X