I have this code and compiling is okay, print shows all values needed, but using datetime seems to be a problem.
No lines are drawn and in many cases it shows: you are accessing an index with a value that is invalid since it is out-of-range
(When in replace the start and end with barnumber like 0 and -30 it works.)
Hope you can help me out so it drawn simple lines from the candle to the max right of the chart. (later on I can add code so endtime is changed when it broken by price)
// Define a class to represent a line
public class CustomMLine
{
public string Label { get; set; }
public double Price { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public Brush Color { get; set; }
}
//This namespace holds Indicators in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Indicators
{
public class MLine : Indicator
{
// Declare an array to store your lines
private List<CustomMLine> lines = new List<CustomMLine>();
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Every candle";
Name = "MLine";
Calculate = Calculate.OnBarClose;
IsOverlay = true;
DisplayInDataBox = true;
DrawOnPricePanel = true;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
//Disable this property if your indicator requires custom values that cumulate with each new market data event.
//See Help Guide for additional information.
IsSuspendedWhileInactive = true;
AddPlot(Brushes.DarkRed, "MidLineColor");
}
else if (State == State.Configure)
{
}
}
protected override void OnBarUpdate()
{
// Check if there are enough bars on the chart
if (CurrentBar < 50)
return;
if (BarsInProgress != 0)
return;
double currentMidpoint = (High[0] + Low[0]) / 2.0;
Print(currentMidpoint);
var newCustomMLine = new CustomMLine
{
Label = "Mid_"+CurrentBar+currentMidpoint,
Price = currentMidpoint,
StartTime = Time[0],
EndTime = DateTime.MaxValue,
Color = Plots[0].Brush
};
lines.Add(newCustomMLine);
//This print is showing everything right
Print(newCustomMLine.Label +" "+ newCustomMLine.StartTime + " - " + newCustomMLine.EndTime + " Price" + newCustomMLine.Price);
Draw.Line(this, newCustomMLine.Label, false, newCustomMLine.StartTime, newCustomMLine.Price, newCustomMLine.EndTime, newCustomMLine.Price, Plots[0].Brush, DashStyleHelper.Dot, 3);
}
#region Properties
[Browsable(false)]
[XmlIgnore]
public Series<double> MidLineColor
{
get { return Values[0]; }
}
#endregion
}
}

Comment