Announcement

Collapse
No announcement yet.

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 sjsj2732, Yesterday, 04:31 AM
      0 responses
      27 views
      0 likes
      Last Post sjsj2732  
      Started by NullPointStrategies, 03-13-2026, 05:17 AM
      0 responses
      283 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      280 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      131 views
      1 like
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      90 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Working...
      X