Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Getting the current bar's open price

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

    Getting the current bar's open price

    Code:
    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
    {
    Print(Open[0]);
    }

    I open a trade in OnBarUpdate(), and then this executes. However, I always seem to get the previous bar. I need to get the current bar's open price. How can I do this?

    #2
    Hello takeaseat,

    Using [0] bars ago will always give you the most recently closed bar values unless you are using OnEachTick or OnPriceChange calculation modes in realtime. If you are backtesting or using OnBarClose in realtime then it would be the most recent closed bar prices.

    Comment


      #3
      If I used on price change to get the current bar, would I then just use [1] to calculate on previous bar? And then how do I only do 1 trade per bar?

      Comment


        #4
        Hello takeaseat,

        Yes if you use OnPriceChange the [1] bars ago is the most recent closed bar. [0] is the building bar.

        To trade only 1 time per bar you would need to add conditions for that, one way is using bars since entry execution. https://ninjatrader.com/support/help...tsub=barssince

        In very fast moving markets it may be better to just use a bool variable to signal your entry logic was executed. As part of the entry condition check for a specific bool value like true. When the entry is submitted toggle the bool to false so it can only happen once. Later when you want another entry you can toggle the bool back so the condition works again.

        Comment


          #5
          This is confusing now
          Trying to find out the slippage on a trade. IE: if bar opened at 15000.00 and my market order executed at 15000.50, I want to adjust the take profit down 2 ticks or something like that

          Comment


            #6
            Hello takeaseat,

            When submitting market orders those won't have any relation to the bar open, market orders fill at the current market price. If you submit a market order it will very likely have some amount of slippage. If you need an order to fill at a specific price you would have to use a limit order. Targets are going to be based off the entry fill average price unless you are calculating a price ahead of time and supplying that as the orders price.

            Comment


              #7
              And that's fine. My goal is to figure out the difference and adjust TP across accounts.

              Comment


                #8
                Hello takeaseat,

                When you submit an entry and it fills you can observe the fill price by using OnExecutionUpdate. That is also where you would usually submit targets so you could calculate a price at that point before the targets are submitted. There is a sample of that type of strategy in the following link: https://ninjatrader.com/support/help...and_onexec.htm

                Comment


                  #9
                  Would IsFirstTickOfBar work with OnEachTick? This should be the open price right?

                  Comment


                    #10
                    Hello takeaseat,

                    Yes in realtime IsFirstTickOfBar can be used while using OnEachTick or OnPriceChange. The Open or Close price could be used at that time as they will be the same price on the first tick of the bar.

                    Comment


                      #11
                      Since I have all this code written in OnBarUpdate, and it's based on "on bar close". I could potentially just wrap all the code in InFirstTickOfBar to keep it the same essentially for OnEachTick?

                      Comment


                        #12
                        Hello takeaseat,

                        If you put the code inside a InFirstTickOfBar while using OnEachTick that would essentially make that code run the same as on bar close.

                        Comment


                          #13
                          So I think swapping it to OnEachTick, wrapping my code with an if statement checking IsFirstTickOfBar, and then using Open[0] is getting me the open price of the current bar now (while keeping it the same logic). Do you see any potential issues?
                          Last edited by takeaseat; 09-12-2023, 12:32 PM.

                          Comment


                            #14
                            Hello takeaseat

                            Open[0] would be the open of the current bar, on the first tick of the bar you can use either the open or close series as they should be the same price at that point. To know if there are any problems you will need to run your script and make sure it works as you expected.

                            Comment


                              #15
                              Looks like Time[0].Minute is printing a minute ahead now. Is this intended? No issue with -1 on it but just making sure.
                              Thanks

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Today, 05:17 AM
                              0 responses
                              50 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              126 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              69 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              42 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              46 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X