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

Close[1] not working for me

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

    Close[1] not working for me

    For some reason when I try to get the previous bar's close value in my indicator, using Close[1], it doesn't work.

    To test this I created a new indicator, with one line, and set it to just plot Close[1]. When I do that, nothing is plotted. If I change it to Close[0] it plots as expected.

    Here is my code if that helps:

    ///<summary>
    /// This method is used to configure the indicator and is called once before any bar data is loaded.
    ///</summary>
    protectedoverridevoid Initialize()
    {
    Add(
    new Plot(Color.FromKnownColor(KnownColor.Sienna), PlotStyle.Line, "PChange"));
    Overlay =
    false;
    }
    ///<summary>
    /// Called on each bar update event (incoming tick)
    ///</summary>
    protectedoverridevoid OnBarUpdate()
    {
    // Use this method for calculating your indicator values. Assign a value to each
    // plot below by replacing 'Close[0]' with your own formula.
    PChange.Set(Close[1]);
    }


    #2
    Originally posted by bflotrader3 View Post
    For some reason when I try to get the previous bar's close value in my indicator, using Close[1], it doesn't work.

    To test this I created a new indicator, with one line, and set it to just plot Close[1]. When I do that, nothing is plotted. If I change it to Close[0] it plots as expected.

    Here is my code if that helps:

    ///<summary>
    /// This method is used to configure the indicator and is called once before any bar data is loaded.
    ///</summary>
    protectedoverridevoid Initialize()
    {
    Add(
    new Plot(Color.FromKnownColor(KnownColor.Sienna), PlotStyle.Line, "PChange"));
    Overlay =
    false;
    }
    ///<summary>
    /// Called on each bar update event (incoming tick)
    ///</summary>
    protectedoverridevoid OnBarUpdate()
    {
    // Use this method for calculating your indicator values. Assign a value to each
    // plot below by replacing 'Close[0]' with your own formula.
    PChange.Set(Close[1]);
    }

    Look in your log and you should see an error about an index being out of bounds.

    You need to escape your first bar, as it does not exist when the code first runs. Try the suggestion in this thread (and numerous others in the forum).

    ref: http://www.ninjatrader.com/support/f...41212#poststop

    Comment


      #3
      Hello bflotrader3,

      Thank you for your post.

      The bar data is being referenced but does not exist, you will need to ensure enough bars are loaded before accessing that data. Something like if(CurrentBar <= BarsRequired) return;

      You can find more information on this item at the following link: http://www.ninjatrader.com/support/f...ead.php?t=3170

      Please let me know if I may be of further assistance,

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by swestendorf, Today, 11:14 AM
      1 response
      1 view
      0 likes
      Last Post swestendorf  
      Started by Sparkyboy, Today, 10:57 AM
      0 responses
      3 views
      0 likes
      Last Post Sparkyboy  
      Started by TheMarlin801, 10-13-2020, 01:40 AM
      21 responses
      3,917 views
      0 likes
      Last Post Bidder
      by Bidder
       
      Started by timmbbo, 07-05-2023, 10:21 PM
      3 responses
      155 views
      0 likes
      Last Post grayfrog  
      Started by Lumbeezl, 01-11-2022, 06:50 PM
      30 responses
      812 views
      1 like
      Last Post grayfrog  
      Working...
      X