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

Is it possible to get the current forming candles OHLC

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

    Is it possible to get the current forming candles OHLC

    From every script I've come across so far I do not see any way of accessing the current FORMING candle's OHLC & this is a MAJOR part of what I am trying to program,

    Very simple no further explanation

    Thank you in advance.

    #2
    You need to study the Calculate property.

    It is this setting that allows you to get the OHLC values of the
    current forming candle while that candle is forming.

    Calculate can be set by the programmer or by the user in the
    property grid.

    If set to Calculate.OnEachBar then OnBarUpdate is only called
    when the bar closes, and High[0] represents the high of the most
    recently closed bar.

    If set to Calculate.OnEachTick, then OnBarUpdate is called for
    each tick that is processed, and High[1] represents the high of
    the most recently closed bar while High[0] is the high of the active
    bar currently being built. Obviously, OnEachTick can be highly
    CPU-intensive.

    The index values I've highlighted in red are not array indexes,
    but instead are referred to as a 'BarsAgo' index.

    This 0-vs-1 shift in the BarsAgo index occurs based upon the
    Calculate setting. However, most indicators are specifically
    coded using BarsAgo index '0' and still produce correct results
    regardless of the Calculate setting.
    Last edited by bltdavid; 02-23-2021, 04:53 PM. Reason: fix typos

    Comment


      #3
      Hello AlexsOptions,

      Thanks for your post and welcome to the NinjaTrader forums!

      Member bltdavid has provided a good explanation of the Calculate setting along with a link to that section of the Help Guide.

      Using Calculate.OnEachTick does provide you with the prices of the currently forming bar so keep in mind that the High, Low and Close prices can and will change until the bar closes. The Open price does not change. The Close price of Close[0] on live data is the current price. Close[1] is the close price of the just-closed bar.

      Running your strategy with Calculate.OnEachTick does mean that the OnBarUpdate() is called on every incoming tick regardless of the price of the tick. It often occurs that you will receive several ticks in a row that do not change the price. If your strategy and indicators it calls are working on price change, you may want to instead use the calculate setting of Calculate.OnPriceChange so that the OnBarUpdate() is only called when there is a change in price. This reduces the cycling of the script.

      It is possible to run parts of your code OnEachTick (Or OnPriceChange) and other parts once per bar by using the system bool IsFirstTickOfBar to segment the code. Here is a link to a reference example: https://ninjatrader.com/support/help...either_cal.htm

      Finally, please note that Calculate.OnEachTick or Calculate.OnPriceChange, are only functional on live (or playback with market replay) data. If you apply a script with Calculate.OnEachTick or Calculate.OnPriceChange) to historical data, the script will only process once per bar. You can get the functionality with historical data if you enable/use Tick Replay which requires tick data for the duration of the historical data. Reference: https://ninjatrader.com/support/help...ick_replay.htm

      Last edited by NinjaTrader_PaulH; 02-23-2021, 08:01 AM.
      Paul H.NinjaTrader Customer Service

      Comment


        #4
        Thanks to both of you! Didn't knows that changing the trigger would change the list of bars received. I think I understand it clearly now though.

        Comment


          #5
          Just to follow up on OP's question. Is there any way to get the current forming candle's OHLC in ninjascript without calculating on each tick or on each price change? For example, there may be cases where these OHLC are only needed once when a user clicks a button. Calculating them once only when a button is clicked will consume less CPU, as compared to calculating on each tick or on each price change.

          Comment


            #6
            Hello muchacha,

            Thanks for your reply.

            From post #3: "It is possible to run parts of your code OnEachTick (Or OnPriceChange) and other parts once per bar by using the system bool IsFirstTickOfBar to segment the code. Here is a link to a reference example: https://ninjatrader.com/support/help...either_cal.htm"

            Paul H.NinjaTrader Customer Service

            Comment


              #7
              IsFirstTickOfBar can only get the open price of the forming bar. Is there any way to get the High, Low, and Last price of the forming bar without calculating on each tick or on each price change? The idea is that the calculation should be performed once only when a user clicks a button.

              Originally posted by NinjaTrader_PaulH View Post
              Hello muchacha,

              Thanks for your reply.

              From post #3: "It is possible to run parts of your code OnEachTick (Or OnPriceChange) and other parts once per bar by using the system bool IsFirstTickOfBar to segment the code. Here is a link to a reference example: https://ninjatrader.com/support/help...either_cal.htm"

              Comment


                #8
                Hello muchacha,

                Thanks for your reply.

                Sorry, I was not clear enough. You would run your script with Calculate.OnPriceChange or Calculate.OnEachtick so that your bar reference of [0] can point to the currently forming bar where you can get the current Open, High, Low and Close (close being the current price). IsFirstTickOFBar would be used for your code that needs to run only once per bar just like Calculate.OnBarClose (except it is the first tick of the new bar).

                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  Please disregard the Open price. I should not have included it in my original question. I was aware that IsFirstTickOFBar can get the Open price of the forming bar.

                  I was also aware that Calculate.OnPriceChange or Calculate.OnEachtick can get the HLC prices of the forming bar. But my question was: is there any way to get the HLC prices of the forming bar without calculating on each tick or on each price change? Let me reiterate my idea: the calculation should only be performed once when it's needed, but not constantly on each tick or price change. In the example of my previous post, the calculation is only needed when a button is manually clicked by a user.

                  Please don't repeat the previous solutions discussed in this thread. I am aware of them. I just want to explore a different solution that would consume less CPU compared to the existing solutions discussed in this thread.


                  Originally posted by NinjaTrader_PaulH View Post
                  Hello muchacha,

                  Thanks for your reply.

                  Sorry, I was not clear enough. You would run your script with Calculate.OnPriceChange or Calculate.OnEachtick so that your bar reference of [0] can point to the currently forming bar where you can get the current Open, High, Low and Close (close being the current price). IsFirstTickOFBar would be used for your code that needs to run only once per bar just like Calculate.OnBarClose (except it is the first tick of the new bar).

                  Comment


                    #10
                    Originally posted by muchacha View Post
                    is there any way to get the HLC prices of the forming bar without calculating on each tick or on each price change?
                    Strictly speaking, no.

                    There are other ways to get the tick-by-tick price, but these are
                    raw values and not organized/categorized into the neat HLC values
                    you seek.

                    If you want the raw tick values in these other methods, you
                    can get that (OnMarketData, 1-Tick secondary series) but
                    you'll have to write your own code to keep track of the
                    Highest, Lowest, and Close values yourself.

                    Pick your poison.

                    Comment


                      #11
                      Not knowing what exactly what you are doing you could try this:
                      Set indicator to Calculate.OnPriceChange.
                      Do your testing something like this;
                      if(<test your button click>)
                      {
                      Get your OHLC info on the current bar
                      }
                      else if(IsFirstTiskOfBar)
                      {
                      Do other stuff once per bar
                      }
                      else return;
                      eDanny
                      NinjaTrader Ecosystem Vendor - Integrity Traders

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by algospoke, Today, 06:40 PM
                      0 responses
                      8 views
                      0 likes
                      Last Post algospoke  
                      Started by maybeimnotrader, Today, 05:46 PM
                      0 responses
                      7 views
                      0 likes
                      Last Post maybeimnotrader  
                      Started by quantismo, Today, 05:13 PM
                      0 responses
                      6 views
                      0 likes
                      Last Post quantismo  
                      Started by AttiM, 02-14-2024, 05:20 PM
                      8 responses
                      168 views
                      0 likes
                      Last Post jeronymite  
                      Started by cre8able, Today, 04:22 PM
                      0 responses
                      9 views
                      0 likes
                      Last Post cre8able  
                      Working...
                      X