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 frankthearm, Yesterday, 09:08 AM
      12 responses
      43 views
      0 likes
      Last Post NinjaTrader_Clayton  
      Started by junkone, Today, 11:37 AM
      1 response
      12 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by quantismo, 04-17-2024, 05:13 PM
      5 responses
      35 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by proptrade13, Today, 11:06 AM
      1 response
      6 views
      0 likes
      Last Post NinjaTrader_Clayton  
      Started by love2code2trade, 04-17-2024, 01:45 PM
      4 responses
      35 views
      0 likes
      Last Post love2code2trade  
      Working...
      X