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

About CurrentBars value that I need to start calculations

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

    About CurrentBars value that I need to start calculations

    Hello.
    I am creating an strategy, step by step.
    In this strategy I use primary Bars : 5 minutes, and add secondary Bars object : 60 minutes.
    I try to find ( calculate ) supports and resistances in each dimension ( or Bars series).
    Each support and resistance found will be added on Lists of supports and resistances.
    Next step will be calculate ranges between supports and resistances...,
    for example, using 3-4 supports and 3-4 resistances to calculate impulses and retracements between this supports and resistances.

    The question is: How can I know the number of historical bars to begin calculations?
    In the sample code ( see attached file), I write:

    If ( CurrentBars[1] < 11 || CurrentBars[0]<11)
    return;
    // then , begin calculations for each BarsInProgress...

    In the attached code, ( full of explanations to understand it), there is no problem when I Print,
    but when I add into this code methods to calculate impulses and retracements, using at least 4 supports and 4 resistances,
    and I try to Print the values for this impulses and retracements,
    appears the error:
    " error on calling 'OnBarUpdate' method on bar 1079. You are accessing an index with a value that is invalid
    since it is out of range...."
    I solved the problem changing CurrentBars[1] < 11 , increasing the value, to CurrentBars[1]<111)
    With that change I can Print the values for impulses and retracements..... But I just only increased this number, I don't really know
    how big it must be.....

    Then, How can I know the number of CurrentBars that I need , at least, to begin calculations correctly, and Print all the values?


    Thanks for read this.



    Attached Files

    #2
    Hello Parmenides48,

    The CurrentBar check to ensure that enough bars have processed that you can call a barsAgo index without causing an index error.

    As one example, calling Close[1] when CurrentBar is 0 (the very first bar) will cause this error.

    The reason for this is because there will not be a bar ago, essentially there is not enough data at bar 0 to calculate to look a bar ago.

    The script will start from the far left of the chart at the very beginning where the CurrentBar will equal 0 and then start working to the right, calling each bar for OnBarUpdate();

    In this specific example, it would be needed to wait for the second bar to be built (from all series if there are multiple series added), to ensure that you have enough data to call a index 1 bar ago.

    This is especially important when multiple series are added to a single script as the data for all series may not start at the same time.

    Below are public links to the help guide on CurrentBar and CurrentBars.




    If you were to call Close[5], CurrentBar should be greater than 5.
    If you were to call Close[2], CurrentBar should be greater than 20. etc.

    What is the largest index used and what series is this being accessed with?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks for your fast answer.
      I can´t know what is the largest index, for each series.
      I explain:
      I begin calculating supports and resistances.
      To calculate a support , I need 11 bars, because I consider a support if it have 5 bars on the left and 5 on the right
      with Low values equal or higher that the low of this checked bar, five bars on the left from CurrentBar.
      So , If Current bar is 11, I can look back this 5+1+5 bars, ( I can access 11 bars on the left side from CurrentBar, and it won't be out of range...)
      The same for resistances ( in the code are detailed explanations)

      But next calculations,( new code to add ) will be connecting supports and resistances to calculate ranges for impulses and retracements.
      If I want to calculate the 3 last impulses and 3 last retracements, I will need to find at least 3-4 supports and 3-4 resistances,
      and to find all them I have to look back an indetermined numbers of bars!!!
      I don't know how far will be the last support or resistance.!!!

      The method that I will use need all this supports and resistances, and to do it , must go looking back...
      and if CurrentBar's number is not big enough to get all supports and resistances needed
      to calculate 3 impulses and 3 retracements, appears the error of accessing index out of range ( current bar is not enough...)

      How can I solve that problem?
      Is there any way to code to solve the problem for the indetermined number of bars that I need to begin calculations?

      Comment


        #4
        Hello Parmenides48,

        You need to find the index that is causing the error.

        Are you using prints to see the indexes being used and comparing this to CurrentBar?

        Below is a link to a forum post that demonstrates how to use prints to understand behavior.


        For that bar (by also printing the time) when the error occurs, what is the index used and what is the value of CurrentBar?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thanks for your answer,
          tomorrow I will see the you tube video "NinjaScript Editor 401", that is in the link you have sent,
          and I will work on it again.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Jimmyk, 01-26-2018, 05:19 AM
          6 responses
          834 views
          0 likes
          Last Post emuns
          by emuns
           
          Started by jxs_xrj, 01-12-2020, 09:49 AM
          6 responses
          3,290 views
          1 like
          Last Post jgualdronc  
          Started by Touch-Ups, Today, 10:36 AM
          0 responses
          9 views
          0 likes
          Last Post Touch-Ups  
          Started by geddyisodin, 04-25-2024, 05:20 AM
          11 responses
          62 views
          0 likes
          Last Post halgo_boulder  
          Started by Option Whisperer, Today, 09:55 AM
          0 responses
          8 views
          0 likes
          Last Post Option Whisperer  
          Working...
          X