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

Reference to paste bar

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

    Reference to paste bar

    I'm new in programming in NinjaScript and I'm facing some problem to make reference to paste bars in chart.
    I read on the Help Guide that if I want to reference to the close one bar ago I must write Close[1].

    I made a simple Indicator where I print on the output window the last close or the close one bar ago.This is the last portion of my code:


    protected override void OnBarUpdate()
    {
    // Use this method for calculating your indicator values. Assign a value to each
    // plot below by replacing 'Close[0]' with your own formula.


    Print(Close[0].ToString());

    }


    With the code above I regularly see the last close on the output window.

    If, I write the code below instead I don't see anything on the output window:

    protected override void OnBarUpdate()
    {
    // Use this method for calculating your indicator values. Assign a value to each
    // plot below by replacing 'Close[0]' with your own formula.


    Print(Close[1].ToString());

    }

    Am I missing something?

    Thanks for any help.


    #2
    imported post

    The problem you are facing is a simple one yet sometimes overlooked.

    When you write Close[0], you"0" is the index reference to the current bar. Indicators process data from left to right on the chart (or oldest bars to newest bars) and therefore, when on the very first bar of a data series (the left most bar on the chart) there is no prior bar. So, Close[1], is invalid since there is no prior bar. If you look in your Control Center log window, you will likely see an error. To get around this, you need to add the following line of code:

    if (CurrentBar < 1)
    return;

    This ensures there is at least one bar in the data series so that Close[1] is valid. If you wanted to access Close[5] then you needto have:

    if (CurrentBar < 5)
    return;

    Ray
    RayNinjaTrader Customer Service

    Comment


      #3
      imported post

      Thanks very much now is working fine.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by kempotrader, Today, 08:56 AM
      0 responses
      6 views
      0 likes
      Last Post kempotrader  
      Started by kempotrader, Today, 08:54 AM
      0 responses
      4 views
      0 likes
      Last Post kempotrader  
      Started by mmenigma, Today, 08:54 AM
      0 responses
      2 views
      0 likes
      Last Post mmenigma  
      Started by halgo_boulder, Today, 08:44 AM
      0 responses
      1 view
      0 likes
      Last Post halgo_boulder  
      Started by drewski1980, Today, 08:24 AM
      0 responses
      3 views
      0 likes
      Last Post drewski1980  
      Working...
      X