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

OnPositionUpdate() --> Interrupt OnBarUpdate()?

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

    OnPositionUpdate() --> Interrupt OnBarUpdate()?

    Hello-

    I came across some behavior which at first glance was peculiar, but upon inspection might not be.

    In this case, my strategy was in OnBarUpdate(). Within this the code to update stops (UpdateExits())comes before the code that initializes new positions or increases existing positions.

    In LIVE TESTING (that is, live data with an IB paper account), the following happened which I can't re-create in BACKTESTING:

    1.)OnBarUpdate() is called.
    2.)Call to UpdateExit() within OnBarUpdate().
    3.)Stop updated and triggered within UpdateExit().
    4.)Position flattened by UpdateExit();

    now here's the tricky part.

    5.)OnBarUpdate() resumes on the same bar that UpdateExits() was called on above.
    6.)Because position is now flat, it was possible to initialize a new position.
    7.)New position initialized.

    Now, this wasn't actually a huge problem - technically, I did want to get stopped out and, technically, the conditions were correct to initiate a new position. That wasn't the problem.

    The problem is that I can't seem to replicate this behavior in backtesting.

    While I suspect it might have to do something with Sim 101 account settings (specifically, the communications and exchange delay settings), I want to make sure that it isn't something else.

    So to sum this, here's my question: Can OnPositionUpdate() interrupt OnBarUpdate()?

    #2
    Hello cgeorgan,

    Would it be possible for you to provide us with a stripped down copy of your strategy for testing? You can send it to platformsupport[at]ninjatrader[dot]com if you'd prefer. Please include this unique ID number in the body of your e-mail : 1553713
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by cgeorgan View Post

      So to sum this, here's my question: Can OnPositionUpdate() interrupt OnBarUpdate()?
      Hmmm. ..

      the only major difference I've seen that I had to ajust for going from market replay to live with real account is that OnPositionUpdate isn't called directly after OnBarUpdate. .because there is delay.

      In replay it's

      OnBarUpdate (enter position)
      OnPositionUpdate
      OnBarUpdate
      OnBarUpdate

      In live it's

      OnBarUpdate (enter position)
      OnBarUpdate (I had to change logic to let OnBarUpdate know OnPositionUpdate was called for further action later on in the day, else it thought I had done other logic and shutdown)
      OnBarUpdate (more than a few)
      OnPositionUpdate
      OnBarUpdate

      Comment


        #4
        Addition

        I should probably have also noted that the UpdateExits() routine uses the ClosePosition() to stop positions out @market.

        This came to mind because when I use the other SubmitOrder() methods, I typically include a block on the current bar (i.e. barNumberOfOrder) to prevent conflicting orders. It wasn't something I was doing when I used ClosePosition() within my exit routine. I've fixed that, which I think will fix my "problem" (that is, restore operation to what I'd expect it to do).

        Comment


          #5
          OnPositionUpdate is a bit of a nomad, showing up all over the place.

          My experience is that OnPositionUpdate is the least disciplined, as it
          pertains to sequencing.

          For example:
          Support for the development of custom automated trading strategies using NinjaScript.

          Comment


            #6
            Hello cgeorgan,

            OnBarUpdate and OnPositionUpdate are asynchronous, truly event driven calls. As such it would be inappropriate to say that one interrupts the other, as this would imply that one would affect the other's resources and state. However, one can be called while the other is running.

            Beyond that, if you could modify the attached script so that it shows the same discrepancy between backtesting and live testing, or simply let us know if it does show this difference unmodified, it would be appreciated, and it would allow us to have a more complete picture of what you are seeing and provide better answers.
            Attached Files
            Jessica P.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by ETFVoyageur, Today, 07:05 PM
            0 responses
            4 views
            0 likes
            Last Post ETFVoyageur  
            Started by Orion815, 05-02-2024, 08:39 AM
            2 responses
            17 views
            0 likes
            Last Post Orion815  
            Started by suroot, 02-25-2017, 04:43 AM
            11 responses
            2,549 views
            0 likes
            Last Post Zilvercat  
            Started by Rogers101, 05-05-2024, 11:30 AM
            16 responses
            50 views
            0 likes
            Last Post Rogers101  
            Started by ninza33, Today, 12:31 PM
            2 responses
            12 views
            0 likes
            Last Post ninza33
            by ninza33
             
            Working...
            X