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

Event Driven Sequencing OnExecution, OnPositionUpdate and OnBarUpdate.

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

    Event Driven Sequencing OnExecution, OnPositionUpdate and OnBarUpdate.

    Can you tell me the order of event driven sequencing in NT? Specifically I am interested in OnExecution, OnPositionUpdate and OnBarUpdate.

    If I have a position and a limit order is filled to get me flat, which of these will trigger first, second & third?
    Where is my best chance of finding (Position.MarketPosition == MarketPosition.Flat) first?

    Also when in the sequence is AccountItem.CashValue updated so that I accurately get (GetAccountValue(AccountItem.CashValue) after going flat?

    Thanks.
    Tony

    #2
    Tony,

    As NinjaTrader is multi-threaded, there is no gurantee that one event will be called before another in your example. The only exception to this is that OnExecution would always be called after OnOrderUpdate.

    As far as OnBarUpdate, OnPositionUpdate, or OnExecution, they can all come in at different times depending on market dynamics.

    Your TradeCollection will be updated after OnPositionUpdate is called.
    MatthewNinjaTrader Product Management

    Comment


      #3
      Sorry to resurrect an old thread but it's really close to my question: are OnBarUpdate() events serialized? Let's say OnBarUpdate() event triggered and half-way through my logic another tick comes in. Will another OnBarUpdate() event be triggered or will NT7 wait for my logic to completely and only then trigger the next event? Thank you!

      Comment


        #4
        ramb2000, welcome to our forums here - the new tick seen would retrigger OnBarUpdate() (in case you were processing the study with CalculateOnBarClose = false, as everything is done event based in NT.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Thanks for the welcome! I didn't notice it was my first post . I've been here fora while...

          So in other words OnBarUpdate() events are not serialized?

          Assuming that's the case, would it be a problem to use Monitor.Enter() / Monitor.Exit() to lock on certain objects in my logic inside OnBarUpdate()?

          Thanks!

          Comment


            #6
            Hello,

            Your script will be running on a single thread. As OnBarUpdate() is triggered by a tick it will need to complete before the next incoming tick is processed.

            You likely will not want to lock objects (or need to) as everything for a specific script is processed on the single thread in sequential order.

            Let me know if I can further assist.
            LanceNinjaTrader Customer Service

            Comment


              #7
              I am not sure if that answers my question and I am probably no asking it right. I'm going to describe a portion of the logic of my strategy and tell you the problem I am having -- should have done that right away -- to see if I can explain myself better.

              In my OnBarUpdate() logic, I have two sections: one for in-train and the other for the flat position. When the flat section enters into a trade, it sets the in-trade flag. When the trade exits and we hit the flat section, I use the in-trade flag to determine if it's the first time we hit the flat section after the in-trade section and only on the first run of the flat section, I reset some things and some of them is removing some chart objects are setup when you enter the trade. This works about 70% of the time but occasionally, I will see the strategy enter the trade, set the in-trade flag and then it hits the flat section once and removes all chart objects and then continues and only runs the in trade section.

              Now, I realized something while I was writing this explanation. I use Position.MarketDirection == MarketDirection.Flat condition to run the flat section and otherwise run the in-trade section. Could it be that it just takes enough time for Position.MarketDirection to be set to Long / Short after calling EnterLong() / EnterShort() for the flat section to run again on the next tick? I think that's the case if all OnBarUpdate() events are serialized.

              If that indeed is the flaw, could you recommend on how to fix that?

              Thanks again!

              Comment


                #8
                I'd need to see the actual script to know for sure but it sounds like you're close to handling the situation correctly.

                Consider ordering your code such that the if not flat section happens first.
                Example:

                if(Position.MarketPosition != MarketPosition.Flat)
                {
                //do something
                }
                else if(Position.MarketPosition == MarketPosition.Flat)
                {
                //do something else like enter trades
                }

                this way you wont be placing trades and before they are filled you calculate over the wrong logic.

                For more debugging tips: http://www.ninjatrader.com/support/f...ead.php?t=3418

                Let me know if I can further assist.
                LanceNinjaTrader Customer Service

                Comment


                  #9
                  That looks almost exacly like my code:

                  // in a trade
                  if (Position.MarketDirection != MarketDirection.Flat)
                  {
                  // do the the in trade logic
                  }

                  // first tick after the trade
                  if (_inTrade)
                  {
                  _inTrade = false;
                  RemoveObjectsFromChart(); // <-- in 3 out of 10 cases this executes even after the
                  // the strategy enters the trade and _inTrade = true
                  // could it be because the next tick arrives before
                  // Position.MarketDirection is set to Long?
                  }

                  // check if we should enter
                  if (BuyConditionsMet())
                  {
                  EnterLong(...);
                  _inTrade = true;
                  }

                  Do you think wrapping the rest of the logic into the else if (...) clause would help?

                  P.S. Advance apologies if the indentation will be off after I hit 'Post Quick Reply'.

                  Thanks again!

                  Comment


                    #10
                    Originally posted by ramb2000 View Post
                    Do you think wrapping the rest of the logic into the else if (...) clause would help?
                    I do think this would help but would need to further debug to know for sure

                    I would strongly suggest using print statements and drawing objects to identify when/where your orders are incorrectly being placed (or not being placed)

                    For strategies add TraceOrders = true to your Initialize() method and you can then view valuable output related to strategy submitted orders through Tools > Output window - http://www.ninjatrader.com/support/h...raceorders.htm

                    // first tick after the trade
                    if (_inTrade)
                    {
                    _inTrade = false;
                    RemoveObjectsFromChart(); // <-- in 3 out of 10 cases this executes even after the
                    // the strategy enters the trade and _inTrade = true
                    // could it be because the next tick arrives before
                    // Position.MarketDirection is set to Long?
                    }
                    if this is getting triggered when you don't want it to you'll need to identify how _inTrade isn't set to false before running it. Orders are not guaranteed instant fills, so yes it is possible for your bool to be set to true while the order has not yet been filled.

                    Let me know if I can further assist.
                    LanceNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by helpwanted, Today, 03:06 AM
                    0 responses
                    3 views
                    0 likes
                    Last Post helpwanted  
                    Started by Brevo, Today, 01:45 AM
                    0 responses
                    6 views
                    0 likes
                    Last Post Brevo
                    by Brevo
                     
                    Started by aussugardefender, Today, 01:07 AM
                    0 responses
                    5 views
                    0 likes
                    Last Post aussugardefender  
                    Started by pvincent, 06-23-2022, 12:53 PM
                    14 responses
                    242 views
                    0 likes
                    Last Post Nyman
                    by Nyman
                     
                    Started by TraderG23, 12-08-2023, 07:56 AM
                    9 responses
                    384 views
                    1 like
                    Last Post Gavini
                    by Gavini
                     
                    Working...
                    X