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

Can OnBarUpdate be called simultaneously?

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

    Can OnBarUpdate be called simultaneously?

    Hello NT Partners,

    I'm testing a strategy and I'm noticing I'm entering market twice when my indicators are triggered.

    I have a control on the code that if I just submitted an order, then OnBarUpdate code won't attempt to submit new orders for the next x minutes. This is to prevent duplicated orders, since to check just MarketPosition could lead to mistakes as orders could take a few seconds to be filled.

    I'm running on tick by tick basis (CalculateOnBarClose = false), that is why I have this concern.

    My question goes: could OnBarUpdate method be triggered at the same time if 2 ticks are received at the same time? Or one OmBarUpdate Thread will always wait for the other to finish before it is executed.

    On a tick by tick basis, this is a kind of concern one has to have while coding.

    What would be typically used to prevent this?

    Appreciate any support.

    Thanks,

    Rodrigo

    #2
    OnBarUpdate called for each tick, and never at the same time. It'll finish before the next tick.

    Have you looked into entriesperdirection setting?

    Comment


      #3
      Hello rgaleote,

      Thanks for your post.

      Thanks to sledge for providing initial feedback that is correctly addressing some of the issues.

      So I will tackle the issues differently to give you further thought.

      When using CalculateOnBarClose (COBC) = false, your strategy will execute in its entirety on every single tick. This also means that your indicators will reevaluate on every single tick and depending on your coding, a condition for entry could be true on one tick and false on another and then true again and etx, etc, as relates to the price movement.

      A great example of this and a way to visualize this is to examine two moving averages (MAs). If for example you were using the crossover as a condition to enter then when they cross you would enter, if they had not crossed you would not enter. If you look closely at the moment of crossover and examine the visualization on the chart you would likely see where in one tick the MAs do cross and then next where they do not and this could be repeated many many times in one bar depending on of course price movement.
      Here is a recording I made showing this with a 13 EMA and a 20 EMA both set COBC = false: http://screencast.com/t/KhCDGOUcgB6

      To prevent multiple entries because of the repeated conditions you can add some logic that says the condition has occurred so don't repeat. You would do this with bool variables that are only true or false. For example:

      if (your entry condition && doitOnce)
      {
      // place your entry order here
      doitOnce = false; // prevent repeated orders!!
      }

      In the above example doitOnce previously is set to true and after entering one order it is set to false so that subsequent attempts will not work. Note that in your code at a later time you would want to reset doitOnce back to true so that you could enter an order when appropriate.

      To assist with resetting variables like doitOnce when COBC=false, you can use "FirstTickOfBar" which is only true on the very first tick of a bar. You could use that bool then to reset doitOnce at the first tick of the next bar, IE:

      if (FirstTickOfBar)
      {
      doitOnce = true; // enable to enter orders on this bar
      }

      FirstTickOfBar: http://www.ninjatrader.com/support/h...ttickofbar.htm

      Other ways to control your entries are using BarsSinceExit() or BarsSinceEntry(), These also can be added to your entry logic to help ensure a set number of bars have passed before allowing an entry. Here are the helpguide links for those:

      Paul H.NinjaTrader Customer Service

      Comment


        #4
        Thanks Sledge and Paul, I've been using CurrentBar and other logics like that DoItOnce example, but it seemed to me that it was being executed more than once. I'll check it better to understand what is going on.

        Thanks for you extensive explanation and examples. Just wanted to make sure OnBarUpdate couldn't be executed at the same time, as two concurrent threads, now I know that is not a possibility

        Best Regards

        Comment


          #5
          Are you using multitimeframe series?

          Any Add() statements?

          Comment


            #6
            Yes, but I'm checking BarsInProgress right at the beginning of OnBarUpdate and then returning right away if not the proper instrument.

            Comment


              #7
              Hello rgaleote,

              Thanks for your clarity (and thanks to sledge for prompting) concerning this being a MultiTimeFrame strategy.

              To be sure you will get multiple OnBarUpdate events in your strategy for each dataseries (base plus each added), however it sounds like you are checking for the BarsInProgress and not executing further code unless it is the correct BarsInProgress.

              Even though you have segregated your code by BarsInProgress, are your entry methods also reflecting the correct BarsInProgress (Are you using the method overload that references Barsinprogress)?
              Paul H.NinjaTrader Customer Service

              Comment


                #8
                Hello Paul,

                Thanks for the reply, I'm not using any overloads that references barinprogress, so I'm entering and exiting makert using the current BarInProgress

                Rgds
                Rodrigo

                Comment


                  #9
                  Hello Rodrigo,

                  Thanks for the reply.

                  At this point without seeing your code it is difficult to further assist. If you would like to, please send your code to Platformsupport[at]Ninjatrader[dot]com with atten: Paul and this thread in the title.
                  Paul H.NinjaTrader Customer Service

                  Comment


                    #10
                    No worries, I'll check further the code and try other scenarios based on what you guys gave me here, and then if needed further assistance I'll let you know. For now , thanks a lot to both of you.

                    Rgds

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by timmbbo, Today, 08:59 AM
                    0 responses
                    1 view
                    0 likes
                    Last Post timmbbo
                    by timmbbo
                     
                    Started by bmartz, 03-12-2024, 06:12 AM
                    5 responses
                    33 views
                    0 likes
                    Last Post NinjaTrader_Zachary  
                    Started by Aviram Y, Today, 05:29 AM
                    4 responses
                    14 views
                    0 likes
                    Last Post Aviram Y  
                    Started by algospoke, 04-17-2024, 06:40 PM
                    3 responses
                    28 views
                    0 likes
                    Last Post NinjaTrader_Jesse  
                    Started by gentlebenthebear, Today, 01:30 AM
                    1 response
                    8 views
                    0 likes
                    Last Post NinjaTrader_Jesse  
                    Working...
                    X