Any thoughts?
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Why does my code keep breaking?
Collapse
X
-
Why does my code keep breaking?
I am working on developing an indicator, starting with a base indicator that works perfectly fine, and the second I use an additional declaration or try to use an array it completely breaks and when debugging in Visual Studio it won't get to OnBarUpdate().
Any thoughts?Tags: None
-
Sounds like you are accessing a barSeries that you have not added. Unfortunately, the only real way to tell what is happening is by seeing some code.Originally posted by cfp462 View PostError on calling 'OnBarUpdate' method for indicator 'nameofindicator' on bar 1: Index was outside the bounds of the array.
Comment
-
//
// Copyright (C) 2006, NinjaTrader LLC <www.ninjatrader.com>.
// NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
//
#region Using declarations
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
using System.Xml.Serialization;
using NinjaTrader.Data;
using NinjaTrader.Gui.Chart;
using System.Linq;
#endregion
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
/// <summary>
/// The Test4 (Simple Moving Average) is an indicator that shows the average value of a security's price over a period of time.
/// </summary>
[Description("")]
public class Test4 : Indicator
{
#region Variables
private int period = 14;
private bool draw = true;
private double x = 0;
private int i = 10;
private int[] signals = new int[10];
private int sum = 0;
#endregion
/// <summary>
/// This method is used to configure the indicator and is called once before any bar data is loaded.
/// </summary>
protected override void Initialize()
{
Add(new Plot(Color.Orange, "Test4"));
Overlay = false;
}
/// <summary>
/// Called on each bar update event (incoming tick).
/// </summary>
protected override void OnBarUpdate()
{
if (CurrentBar == 0)
Value.Set(0);
else
{
if (FirstTickOfBar)
draw = false;
if ('xyz happens; not calling anything crazy here just Close, Open, High etc.')
{
draw = true;
DrawDot("tag1"+CurrentBar, true, 0, Close[0], Color.Lime);
for(int n=0; n< 10; n++)
{
signals[n] = signals[n + 1];
}
signals[0] = 1;
}
if ('xyz happens; not calling anything crazy here just Close, Open, High etc.')
{
draw = true;
DrawDot("tag1" + CurrentBar, true, 0, Close[0], Color.Red);
for(int n=0; n< 10; n++)
signals[n] = signals[n + 1];
signals[0] = 0;
}
sum = signals.Sum();
Value.Set(sum);
}
}
Comment
-
See what I have emphasized in red. When n==9, which is less than 10, signals[n+1] will translate to signals[10], which is outside the array bounds, which will go from index 0 to 9, not 10, as the array was declared to have 10 members.Originally posted by cfp462 View Post//
// Copyright (C) 2006, NinjaTrader LLC <www.ninjatrader.com>.
// NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
//
#region Using declarations
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
using System.Xml.Serialization;
using NinjaTrader.Data;
using NinjaTrader.Gui.Chart;
using System.Linq;
#endregion
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
/// <summary>
/// The Test4 (Simple Moving Average) is an indicator that shows the average value of a security's price over a period of time.
/// </summary>
[Description("")]
public class Test4 : Indicator
{
#region Variables
private int period = 14;
private bool draw = true;
private double x = 0;
private int i = 10;
private int[] signals = new int[10];
private int sum = 0;
#endregion
/// <summary>
/// This method is used to configure the indicator and is called once before any bar data is loaded.
/// </summary>
protected override void Initialize()
{
Add(new Plot(Color.Orange, "Test4"));
Overlay = false;
}
/// <summary>
/// Called on each bar update event (incoming tick).
/// </summary>
protected override void OnBarUpdate()
{
if (CurrentBar == 0)
Value.Set(0);
else
{
if (FirstTickOfBar)
draw = false;
if ('xyz happens; not calling anything crazy here just Close, Open, High etc.')
{
draw = true;
DrawDot("tag1"+CurrentBar, true, 0, Close[0], Color.Lime);
for(int n=0; n< 10; n++)
{
signals[n] = signals[n + 1];
}
signals[0] = 1;
}
if ('xyz happens; not calling anything crazy here just Close, Open, High etc.')
{
draw = true;
DrawDot("tag1" + CurrentBar, true, 0, Close[0], Color.Red);
for(int n=0; n< 10; n++)
signals[n] = signals[n + 1];
signals[0] = 0;
}
sum = signals.Sum();
Value.Set(sum);
}
}
Comment
-
Hello cfp462,
Thanks for your note.
What does your for loop look like now?
It appears you were attempting to shift the values for signal back 1.
This should happen with
for (int i=1; i<9; i++)
signal[n] = signal[n+1];
Let me know if this does not resolve your inquiry.Chelsea B.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
559 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
324 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
101 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
546 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
547 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment