Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

3 Different Orders using OnOrderUpdate

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

    3 Different Orders using OnOrderUpdate

    Hello All-

    I have been trying to get my the exits of my strategy to work exactly as I want them to which has led me to trying to use the OnOrderUpdate and OnExecutionUpdate.

    As I am reading and trying to get a grip on the overview of how to use these methods and apply them to what I want them to do for my strategy, I am getting hung up on the following part from the Ninjatrader Advacned Order Handling guide, specifically the text I colored red.


    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity , int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
    {
    // Assign entryOrder in OnOrderUpdate() to ensure the assignment occurs when expected.
    // This is more reliable than assigning Order objects in OnBarUpdate, as the assignment is not gauranteed to be complete if it is referenced immediately after submitting


    if (order.Name == "AAPL Order" && orderState != OrderState.Filled)
    entryOrder = order;

    }

    What is the order that entryOrder is being set to? I sort of have an idea of what is being done. But in my strategy I could have up to 3 different entries, 1 entry using the 15 minute chart, 1 entry using the 30 minute chart and 1 entry using the 60 minute chart. This is where I am getting confused.

    I am thinking I would create 3 different Order Variables maybe FifteenEntryOrder, ThiryEntryOrder, etc. But how would I assign the variables?

    Let me know if this question even makes sense. I am still learning how to update orders using these methods.

    Thank you,

    Nick

    #2
    Hello njmeyer713,

    the entryOrder is being set to the order that was passed into the override. If you look at the first parameter of OnOrderUpdate that is the order which the update is relevant to.

    If you had 3 separate orders that you wanted to track you would have 3 separate Order variables. Then you would find them the same way that you have shown, using the orders signal name. order or the parameter of OnOrderUpdate will change based on the order that caused the event. if the orders name was AAPL Order then we know that order in the parameter is the apple order.

    Code:
    if (order.Name == "AAPL Order" && orderState != OrderState.Filled)
    entryOrder = order;
    
    if (order.Name == "Other Order" && orderState != OrderState.Filled)
    otherOrder = order;

    Comment


      #3
      Jesse-

      Thank you for the response. I will get to work on trying to put the code together.

      Another question for you if you don't mind. I haven't been able to find the answer I am looking for.

      I want the strategy to take a maximum of 2 contracts per timeframe at any one time. So I could be 6 total contracts of CL, for example, but I only want each of the 3 timeframes to trade 2 max.

      How and where in the script would I do this? I have messed around with different entry handling and I can't quite seem to get it right.

      Thank you,

      Nick

      Comment


        #4
        Hello njmeyer713,

        You could do that using the order variables. If the order variable is not null then you have an order that is active. In that use case you would have 3 or 6 variables depending on the quantity per order. If you did 1 quantity per order that would be 6 total variables because you would have 6 total orders.

        I would suggest taking a look at this sample for a breakeven: https://ninjatrader.com/support/help...and_onexec.htm

        The OnBarUpdate logic in that script works based on the order variable being populated or not, if the order variable is not populated the entry logic is executed. Your use case would be similar. The entry logic can only work if the order was not yet populated, the only way its populated is by being submitted.




        Comment


          #5
          Jesse-

          Thank you for the help. I went ahead and basically copied the code from SampleOnOrderUpdate, added and renamed the variables to get a start on my strategy.

          In a perfect trade, I go long 2 contracts of CL for example, Initial stop is at the Low of the previous bar (@"15LowStop") and the limit order for half the position is set at 16 ticks profit (15ScalpExit). Once price has moved 16 ticks in my favor I sell half of my position size.

          By copying and editing the Sample code, I am able to get his far. However I do not know what code or where to put it to say something along the lines of "If "15ScalpExit" has executed, then do the following"

          What I want is for the remaining contract to have a breakeven stop set at the entry price of the contact and to have a trailing stop that is set to the low of the previous bar as, hopefully, the bars continue upwards with no lower lows for several bars. Eventually exiting when price reverses and takes out a previous low.

          What bit of code and where do I need to do this in the context of the SampleOnOrderUpdate example script?

          Thank you,

          Nick

          Comment


            #6
            Hey Jesse-

            I don't mean to be a bother but I was hoping you would get a chance to get back to me on my last post this morning.


            Thank you in advance-

            Nick

            Comment


              #7
              Hello njmeyer713,

              By copying and editing the Sample code, I am able to get his far. However I do not know what code or where to put it to say something along the lines of "If "15ScalpExit" has executed, then do the following"
              The sample code shows how to know if the order was submitted you would use the same type of condition that is in OnBarUpdate which uses the entryOrder variable. That checks if the order is null meaning not submitted, you can use != to check the inverse or that it is not null:

              if (yourOrderVariable != null)

              To know it filled or to observe that event you need to use OnExecutionUpdate and watch for the filled event.


              What I want is for the remaining contract to have a breakeven stop set at the entry price of the contact and to have a trailing stop that is set to the low of the previous bar as, hopefully, the bars continue upwards with no lower lows for several bars. Eventually exiting when price reverses and takes out a previous low.
              You can see the samples OnBarUpdate code for an example of creating a break even using an order object.

              To have the remaining contract as a separate action would entail that you had submitted two entries with unique signal names. You could then exit each contract individually, if you wanted to do a break even then it would be the same code from the sample on whichever order object you wanted to manipulate.


              Comment


                #8
                Jesse-

                Thank you for the response.

                Can you take a look at the attached screenshot please?

                This is under the OnBarUpdate. The first if statement handles the entry and stop when price hasn't reached my scalp profit target of 16 ticks.

                The last if statement handles stops after the scalp is reached. The problem I am having is that when I backtest strategy is causing a glitching action on the chart where their is only one stop that jumps back and forth between Low[1] and Average Position Size.

                I realize that I am submitting 2 stopMarket orders but I have used this same bit of code in previous strategies without the glitching issue.

                Is there something I am missing? The current strategy does use OnOrderUpdate and OnExecutionUpdate where my previous ones did not. Maybe something with that?

                Thank you,

                Nick
                Attached Files

                Comment


                  #9
                  Hello njmeyer713,

                  To have two stopmarket orders you would need two entries, the stops currently target the same entry.

                  Comment


                    #10
                    Jesse-

                    Thank you for the response.

                    I am attempting to do that now however I cannot get the strategy to take that second entry. Here is what I have.

                    Code:
                     if ((High[1] <= High[2])
                    && (Low[1] >= Low[2])
                    
                    
                    {
                    EnterLongLimit(Convert.ToInt32(Quantity), (High[1] + (LimitOrderSlippage * TickSize)) , @"15Long1-2U");
                    EnterLongLimit(Convert.ToInt32(Quantity), (High[1] + (LimitOrderSlippage * TickSize)) , @"15Long1-2URunner");
                    }
                    Only the 15Long1-2U is trading.

                    I want each entry to take 1 contract. How do I do this?

                    Thank you,

                    Nick

                    Comment


                      #11
                      Hello njmeyer713,

                      For that use case you would need to turn on:

                      Code:
                      EntryHandling = EntryHandling.UniqueEntries;


                      There is also a sample of scaling using signal names in the following page: https://ninjatrader.com/support/help...a_position.htm

                      Comment


                        #12
                        Jesse-

                        Thank you for the response. Silly error on my end. I wasn't refreshing the whole strategy on my chart when I was changing the Entry Handling. T

                        On a different note, can you use ChangeOrder() in the OnExecutionUpdate method?

                        Thank you,

                        Nick

                        Comment


                          #13
                          Hello njmeyer713,

                          Yes you can use ChangeOrder in OnExecutionUpdate assuming the order being changed is still active and working.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by NullPointStrategies, Yesterday, 05:17 AM
                          0 responses
                          64 views
                          0 likes
                          Last Post NullPointStrategies  
                          Started by argusthome, 03-08-2026, 10:06 AM
                          0 responses
                          139 views
                          0 likes
                          Last Post argusthome  
                          Started by NabilKhattabi, 03-06-2026, 11:18 AM
                          0 responses
                          75 views
                          0 likes
                          Last Post NabilKhattabi  
                          Started by Deep42, 03-06-2026, 12:28 AM
                          0 responses
                          45 views
                          0 likes
                          Last Post Deep42
                          by Deep42
                           
                          Started by TheRealMorford, 03-05-2026, 06:15 PM
                          0 responses
                          50 views
                          0 likes
                          Last Post TheRealMorford  
                          Working...
                          X