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

Processing (Historical) orders *after* OnBarUpdate

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

    Processing (Historical) orders *after* OnBarUpdate

    Hi,

    Any help/advice on the following would be greatly appreciated.
    I am setting my profit target limit order under certain conditions on a 1min basis.
    My strategy is running on a tick-by-tick basis ("CalculateOnBarClose = false;") and simulating bar closes by using "if (FirstTickOfBar ) {..." .
    This is working as it should in realtime, and on market replay. The problem very occasionally arises when I reload the strategy and the (Historical) reload of the data causes the profit target to be hit due to the 1min bar data (OHLC) being loaded for a particular bar *before* the new profit target for that minute can be set.

    An example:
    At 09:14:00 my profit target for my long position is 134.79. (The high so far on the day was 134.78).
    At 09:15:00 - when running the strategy in realtime - the new target gets moved by the code in the OnBarUpdate() method to 134.89 and the target is not hit. I am still long. (The price goes to 134.79 at 09:15:26, ie 26 seconds after the target is moved higher when running in realtime).
    If, however, I reload the strategy at any point after 09h15 then at the open of the 09h15 bar (before the code has a chance to move the profit target higher) the system checks the OHLC 1min bar data, which has a 1min high of 134.80, and declares that the profit target order is filled at 09:15:00 at 134.79.
    Of course in realtime that high is not really hit until 09:15:26, which allows the code to move the profit target higher to the new target price which it does when running in realtime at 09:15:00.
    I understand that historically the bar data is loaded once for each bar and that therefore the system 'sees' the OHLC close data for the 09h15 bar as a single event and so fills my limit order at 134.79.

    My question is therefore as follows:

    Is there anyway when loading *(Historical)* data to prevent the order processing from occurring *before* my code updates the positions/orders via the OnBarUpdate method?
    Or to put it another way: is it possible to *prioritise* the code within my OnBarUpdate() method over the *historical* order processing, so that order fills on a historical re-load of data do not occur until after the OnBarUpdate() method has run? (This would effectively be like saying to the system to only fill orders at the *end* of each 1min bar when loading historical data, not at the beginning of each bar).

    Many thanks.

    #2
    Pourleau, in attempting to understand your setup better: is this occuring for trades where the target on the 1 min would be historically filled intrabar then?
    BertrandNinjaTrader Customer Service

    Comment


      #3
      do you want to recover your strategy postion only or do you want to backtest your strategy?
      During "Historic" your strategy behaves like in a backtest, so the limitations you describe apply and you cannot reproduce the correct real-time behaviour.

      If you just want to keep track of your positions you can do so by intelligent coding;
      complex but doable. You are on your own (except if you are willing to pay >1k USD for advice)



      regards
      andreas

      Comment


        #4
        Hi Bertrand,

        No - in the example given the target was 134.79 at the close of the 09h14 bar ( at 09:14:59 ) but in realtime this would then be moved to 134.89 at 09:15:00 ( and hence not filled as the price does not go that high until much later, ie 2 hours later).
        On historical re-load however, the On BarUpdate() method does not get the chance to run until after the limit order to sell at 134.79 is filled by the historical order processing mechanism at 09:15:00 (when the price in reality does not get to 134.79 until 09:15:26, ie after the OnBarUpdate method runs in a realtime scenario).
        What I would ideally like to happen is that the OnBarUpdate() method runs at 09:15:00 on the first tick of the new bar at 09h15 (and thus moves the profit target up to the new correct target level for that new 1min bar), and then at some point after that (eg 09:15:01 or 09:15:59) the historical order processing runs on the 1min OHLC data.
        many thanks

        Comment


          #5
          Pourleau, thanks for the clarification - there would no really NinjaScript supported way to control, the historical processing of the OnBarUpdate() is done before any realtime calls are done, this would simply be the event sequence you see on the reload. As Andreas mentioned there would be custom code solutions to this, but unfortunately nothing like a setting or property to channel it.
          BertrandNinjaTrader Customer Service

          Comment


            #6
            OK understood - many thanks.

            Comment


              #7
              Hi Andreas,

              Yes - for the correct recovery of position purposes.
              I will have a think about how it might possibly handle it. 1k a bit rich for now I'm afraid but any tips that would give me a start on that would be great! (Esp how to 'trap' the data load prior to OnBarUpdate running...)

              cheers

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Mongo, Today, 11:05 AM
              0 responses
              1 view
              0 likes
              Last Post Mongo
              by Mongo
               
              Started by Tim-c, Today, 10:58 AM
              0 responses
              1 view
              0 likes
              Last Post Tim-c
              by Tim-c
               
              Started by traderqz, Yesterday, 09:06 AM
              3 responses
              22 views
              0 likes
              Last Post NinjaTrader_ThomasC  
              Started by f.saeidi, Today, 10:19 AM
              1 response
              5 views
              0 likes
              Last Post NinjaTrader_BrandonH  
              Started by kujista, Today, 06:23 AM
              5 responses
              18 views
              0 likes
              Last Post kujista
              by kujista
               
              Working...
              X