• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

8.0.0.3 Bar Index Off by One in OnRender()?

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

    8.0.0.3 Bar Index Off by One in OnRender()?

    I'm not sure if this is a bug, or if I just misunderstand something very fundamental. I ran across this doing a conversion from NT7 to NT8. At the least, something seems to have changed.
    I've attached a tiny indicator that shows the issue. But here is an attempt to describe the issue.
    OnBarUpdate() behaves as expected -- the index is the same as the CurrentBar
    protected override void OnBarUpdate()
    {
    if (CurrentBar % 2 == 0)
    {
    Signals[0] = CurrentBar;
    }
    var isValid = Signals.IsValidDataPoint(0);
    Print(string.Format("isValid={0} at {1}", isValid, CurrentBar));
    if (isValid)
    {
    var signal = Signals.GetValueAt(CurrentBar);
    Print(string.Format("signal={0} at {1}", signal, CurrentBar));
    }
    }

    But OnRender() has a very different behavior (unless I'm missing something major!).
    var ADJUSTMENT = 1;
    Print(string.Format("Bars.Count={0},ChartBars.Coun t={1}", Bars.Count, ChartBars.Count));
    for (int idx = ChartBars.FromIndex; idx <= ChartBars.ToIndex; idx++)
    {
    var isValidSignal = Signals.IsValidDataPoint(Bars.Count - 1 - idx);
    Print(string.Format("At idx={0} isValidSignal={1}", idx, isValidSignal));

    if (!isValidSignal)
    {
    continue;
    }
    var signal = Signals.GetValueAt(idx - ADJUSTMENT);
    Print(string.Format("At idx={0} signal={1}", idx, signal));
    }

    So you can see that I have to subtract 1 from the idx for Signals.GetValueAt() but NOT for Signals.IsValidDataPoint(), otherwise I get 0 (wrong) values for the Signals setting at idx.

    For example, in the Output window, OnBarUpdate gives me:
    isValid=False at 85
    isValid=True at 86
    signal=86 at 86
    isValid=False at 87

    But OnRender gives me:
    At idx=86 isValidSignal=False
    At idx=87 isValidSignal=True
    At idx=87 signal=86
    At idx=88 isValidSignal=False

    Thanks for your help,
    Dale
    Attached Files

    #2
    Originally posted by dalebru View Post
    I'm not sure if this is a bug, or if I just misunderstand something very fundamental. I ran across this doing a conversion from NT7 to NT8. At the least, something seems to have changed.
    I've attached a tiny indicator that shows the issue. But here is an attempt to describe the issue.
    OnBarUpdate() behaves as expected -- the index is the same as the CurrentBar
    protected override void OnBarUpdate()
    {
    if (CurrentBar % 2 == 0)
    {
    Signals[0] = CurrentBar;
    }
    var isValid = Signals.IsValidDataPoint(0);
    Print(string.Format("isValid={0} at {1}", isValid, CurrentBar));
    if (isValid)
    {
    var signal = Signals.GetValueAt(CurrentBar);
    Print(string.Format("signal={0} at {1}", signal, CurrentBar));
    }
    }

    But OnRender() has a very different behavior (unless I'm missing something major!).
    var ADJUSTMENT = 1;
    Print(string.Format("Bars.Count={0},ChartBars.Coun t={1}", Bars.Count, ChartBars.Count));
    for (int idx = ChartBars.FromIndex; idx <= ChartBars.ToIndex; idx++)
    {
    var isValidSignal = Signals.IsValidDataPoint(Bars.Count - 1 - idx);
    Print(string.Format("At idx={0} isValidSignal={1}", idx, isValidSignal));

    if (!isValidSignal)
    {
    continue;
    }
    var signal = Signals.GetValueAt(idx - ADJUSTMENT);
    Print(string.Format("At idx={0} signal={1}", idx, signal));
    }

    So you can see that I have to subtract 1 from the idx for Signals.GetValueAt() but NOT for Signals.IsValidDataPoint(), otherwise I get 0 (wrong) values for the Signals setting at idx.

    For example, in the Output window, OnBarUpdate gives me:
    isValid=False at 85
    isValid=True at 86
    signal=86 at 86
    isValid=False at 87

    But OnRender gives me:
    At idx=86 isValidSignal=False
    At idx=87 isValidSignal=True
    At idx=87 signal=86
    At idx=88 isValidSignal=False

    Thanks for your help,
    Dale

    Try it again after making the following change.

    From: Calculate = Calculate.OnBarClose;

    To: Calculate = Calculate.OnPriceChange;

    Or To: Calculate = Calculate.OnEachTick;
    RJay
    NinjaTrader Ecosystem Vendor - Innovative Trading Solutions

    Comment


      #3
      Originally posted by RJay View Post
      Try it again after making the following change.

      From: Calculate = Calculate.OnBarClose;

      To: Calculate = Calculate.OnPriceChange;

      Or To: Calculate = Calculate.OnEachTick;
      Thank you RJay. That "explains" it. I'm shocked that I've programmed NinjaScript for so long without understanding this quirk.
      NinjaTrader, I'm ready to declare this "not a bug."
      For anyone interested, attached are two more simple indicators showing that the behavior is the same in NT7 and NT8.
      Amazingly, I don't need to adjust for Calculate.OnBarClose if I use the indexer, i.e. Signals[barsAgo]. I only need to adjust for it if I use the Signal.GetValueAt(index).

      Thanks again,
      Dale
      Attached Files

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by ballboy11, Today, 11:57 AM
      1 response
      5 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by swcooke, Today, 09:41 AM
      1 response
      9 views
      0 likes
      Last Post NinjaTrader_Jim  
      Started by Catalon, Today, 10:02 AM
      1 response
      9 views
      0 likes
      Last Post NinjaTrader_Jim  
      Started by pel11, Yesterday, 06:25 PM
      1 response
      11 views
      0 likes
      Last Post NinjaTrader_ChrisL  
      Started by dmackay41, Today, 09:35 AM
      1 response
      9 views
      0 likes
      Last Post NinjaTrader_SergeyS  
      Working...
      X