#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Gui.Chart;
#endregion
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
/// <summary>
/// Hi of Bars
/// </summary>
[Description("Hi of Bars")]
public class HiRay : Indicator
{
#region Variables
// Wizard generated variables
private int periods = 10; // Default setting for Periods
// User defined variables (add any user defined variables below)
#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()
{
CalculateOnBarClose = true;
Overlay = true;
PriceTypeSupported = false;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Use this method for calculating your indicator values. Assign a value to each
DrawRay("MyHiRay", periods, MAX(High,periods)[0], 0, MAX(High,periods)[0], Color.Blue, DashStyle.DashDot, 2);
}
#region Properties
[Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
[XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
public DataSeries Plot0
{
get { return Values[0]; }
}
[Description("")]
[Category("Parameters")]
public int Periods
{
get { return periods; }
set { periods = Math.Max(1, value); }
}
#endregion
}
}
#region NinjaScript generated code. Neither change nor remove.
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
public partial class Indicator : IndicatorBase
{
private HiRay[] cacheHiRay = null;
private static HiRay checkHiRay = new HiRay();
/// <summary>
/// Hi of Bars
/// </summary>
/// <returns></returns>
public HiRay HiRay(int periods)
{
return HiRay(Input, periods);
}
/// <summary>
/// Hi of Bars
/// </summary>
/// <returns></returns>
public HiRay HiRay(Data.IDataSeries input, int periods)
{
checkHiRay.Periods = periods;
periods = checkHiRay.Periods;
if (cacheHiRay != null)
for (int idx = 0; idx < cacheHiRay.Length; idx++)
if (cacheHiRay[idx].Periods == periods && cacheHiRay[idx].EqualsInput(input))
return cacheHiRay[idx];
HiRay indicator = new HiRay();
indicator.BarsRequired = BarsRequired;
indicator.CalculateOnBarClose = CalculateOnBarClose;
indicator.Input = input;
indicator.Periods = periods;
indicator.SetUp();
HiRay[] tmp = new HiRay[cacheHiRay == null ? 1 : cacheHiRay.Length + 1];
if (cacheHiRay != null)
cacheHiRay.CopyTo(tmp, 0);
tmp[tmp.Length - 1] = indicator;
cacheHiRay = tmp;
Indicators.Add(indicator);
return indicator;
}
}
}
// This namespace holds all market analyzer column definitions and is required. Do not change it.
namespace NinjaTrader.MarketAnalyzer
{
public partial class Column : ColumnBase
{
/// <summary>
/// Hi of Bars
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.HiRay HiRay(int periods)
{
return _indicator.HiRay(Input, periods);
}
/// <summary>
/// Hi of Bars
/// </summary>
/// <returns></returns>
public Indicator.HiRay HiRay(Data.IDataSeries input, int periods)
{
return _indicator.HiRay(input, periods);
}
}
}
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
public partial class Strategy : StrategyBase
{
/// <summary>
/// Hi of Bars
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.HiRay HiRay(int periods)
{
return _indicator.HiRay(Input, periods);
}
/// <summary>
/// Hi of Bars
/// </summary>
/// <returns></returns>
public Indicator.HiRay HiRay(Data.IDataSeries input, int periods)
{
if (InInitialize && input == null)
throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");
return _indicator.HiRay(input, periods);
}
}
}
#endregion
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Simple DrawRay Issue
Collapse
X
-
Simple DrawRay Issue
My first attempt at NinjaScript. Trying to get a ray from the highest High of N bars. Compiles but nothing showing on the chart. Why?
Code:Tags: None
-
Tip 1: When developing your indicators ALWAYS get in the habit of leaving Ninja on the "Log" Tab. That way you will see the error messages when you try to place your indicators on the screen.
You will see an error message:
Error on calling 'OnBarUpdate' methods for indicator 'HiRay' on bar 0: HiRay.DrawRay: startBarsAgo out of valid range 0 through 0, was 10
what that is saying is "When your chart only has 1 bar 'Bar 0' you are trying to draw a line 10 bars long." so your indicator crashed & was not used on any subsequent bar.
It takes a while to get your head around the fact that your indicator is used when the chart only has 1 bar, then 2 bars, then 3 bars etc.
Tip 2: Tell your indicator not to do anything untill it has sufficient bars to actually function correctly. In this case it needs "Period" number of bar in the chart or it will die.
So in almost every indicator you should add code similar to "If (CurrentBar < 4) return;" to fix your specific issue see the 2 new lines of code below.
protectedoverridevoid OnBarUpdate()
{
// Use this method for calculating your indicator values. Assign a value to each
if (CurrentBar < periods)
return;
I trust this will get you started. Have fun, Ninja is extremely powerful once you get you head around it.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
599 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
344 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
103 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
558 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
557 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment