Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

AllTrades set in OnPositionUpdate?

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

    AllTrades set in OnPositionUpdate?

    Hi,

    Can I get confirmation from someone on when "Performance.AllTrades" is set...? If I've just had a position flattened, is that trade already written into by the time I go into OnPositionUpdate?

    Here's the problem. I need to know the price I exited at... and I've been using Performance.AllTrades to figure out that price (scanning through looking for matching instrument name.) I'm doing this in OnPositionUpdate instead of OnExecution/OnOrderUpdate because of reports those might be wrong on IB.

    This has worked really well in the past, but just threw me a weird exception suggesting Performance.AllTrades was empty at the time when it executed. I'm printing line numbers, so I know it happened on this line:

    // First, we have to find info about the most recent trade...
    Trade latestTrade = null;
    for (int index = Performance.AllTrades.Count-1; index >= 0; index++)
    {
    latestTrade = Performance.AllTrades[index];
    if (latestTrade.Entry.Instrument.FullName == Instrument.FullName)
    break;
    }
    Exception looks like this:

    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index
    at System.Collections.CollectionBase.System.Collectio ns.IList.get_Item(Int32 index)
    at NinjaTrader.Strategy.TradeCollection.get_Item(Int3 2 index)
    at NinjaTrader.Strategy.ProtectDownMulti.OnPositionUp date(IPosition position) in c:\Documents and Settings\Chon\My Documents\NinjaTrader 6.5\bin\Custom\Strategy\ProtectDownMulti.cs:line 942
    System.Collections.IList.get_Item
    Line: 0
    Column: 0

    #2
    I might try to get away from using Performance.AllTrades entirely.

    So, a related question is... inside OnPositionUpdate, is my IOrder object (for the latest order) already filled out with updated details on the exit execution? Can I pull price information from there?

    Just want to avoid a possible race condition.

    Comment


      #3
      heech,

      OnPositionUpdate() will be the last one in the sequence meaning you can use Performance or IOrder and they will be up to date at that moment in time. As far as your exception goes. Please check your for-statement again. You can easily go in with a -1 index as the first run through on that statement.
      Josh P.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by NinjaTrader_Josh View Post
        heech,

        OnPositionUpdate() will be the last one in the sequence meaning you can use Performance or IOrder and they will be up to date at that moment in time. As far as your exception goes. Please check your for-statement again. You can easily go in with a -1 index as the first run through on that statement.
        Don't think it can be -1 index... this code doesn't execute unless there was a previous long order (which is now flat), so there *should* be something in AllTrades, so count should = 1.

        I'll modify the code to use IOrder just in case.

        Comment


          #5
          heech,

          You have no checks there to prevent you from running the for-loop when Performance.AllTrades.Count is 0. This is why you are getting the index out of range.
          Josh P.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_Josh View Post
            heech,

            You have no checks there to prevent you from running the for-loop when Performance.AllTrades.Count is 0. This is why you are getting the index out of range.
            My point is there's no reason AllTrades.Count can be 0, because this code is only getting turned on if there was a prior long order (which is now going flat). So, that's why I asked about the sequence of Performance versus OnOrderUpdate.

            Comment


              #7
              Aside from .Count then please also reconsider your index++ logic. You should be going index--.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_Josh View Post
                Aside from .Count then please also reconsider your index++ logic. You should be going index--.
                DUH. Haha, thanks.

                Comment


                  #9
                  Originally posted by NinjaTrader_Josh View Post
                  OnPositionUpdate() will be the last one in the sequence
                  Where in the Help guide can I find the full list of OnXXX events that are called and the sequence order they are called?

                  This would be great to know and understand.

                  Comment


                    #10
                    You can find the list in the Index page of the Help Guide. Just scroll down to the O section. Unfortunately there is no article about the sequence. Generally its OnOrderUpdate->OnExecution->OnPosition.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_Josh View Post
                      heech,

                      OnPositionUpdate() will be the last one in the sequence meaning you can use Performance or IOrder and they will be up to date at that moment in time. As far as your exception goes. Please check your for-statement again. You can easily go in with a -1 index as the first run through on that statement.
                      Josh,

                      Can you tell me the sequence order between OnExecution, and either Performance or IOrder...? (Is Performance updated by the time OnExecution is called..?) I'm changing my logic around slightly again, and I find it necessary to deal with the above.

                      Thanks.

                      Comment


                        #12
                        Yes it will be updated.
                        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