I am trying to complete an indicator that plots the closing price of an instrument for a specified time. I have the following code but for some reason it resorts to plotting the close at the end of the 24hour day as opposed to 16:00 which is the defualt setting.
Just hoping someone could have a look at the code and see where the error may lie.
Many Thanks for any help
4blue
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
/// <summary>
/// an indicator to retreive the value of a stock at a particular time.
/// </summary>
[Description("an indicator to retreive the value of a stock at a particular time.")]
public class APreviousValueAtTime : Indicator
{
#region Variables
// Wizard generated variables
private int setHour = 16; // Default setting for SetHour
private int setMinute = 0; // Default setting for SetMinute
private int setSeconds = 0; // Default setting for SetSeconds
private string openClose = @"Close"; // Default setting for OpenClose
// User defined variables (add any user defined variables below)
private DateTime timeOfInterest; // DateTime object to hold the time we're interested in.
private int barsAgo = 0; // Variable to hold a bars ago value.
#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.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "RequestesTime"));
CalculateOnBarClose = true;
Overlay = false;
PriceTypeSupported = false;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
/* This sample assumes instrument sessions that go from Monday-Friday and not the weekends.
If you are using an instrument that has weekend trading sessions like the ES futures, please be aware that the logic presented will need to be
modified to reflect the trading sessions for your particular instrument.
Here, timeOfInterest is reset once per day at session break. */
if (Bars.FirstBarOfSession && FirstTickOfBar)
{
/* If the day is Monday, and we want the value from Friday, we must subtract 3 days from the current date.
If the day is Tuesday-Friday, just subtract one day. */
if (Time[0].DayOfWeek == DayOfWeek.Monday)
timeOfInterest = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day - 3, setHour, setMinute, setSeconds);
else
timeOfInterest = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day - 1, setHour, setMinute, setSeconds);
}
/* Determine the number of bars ago it would take to access the 9:30AM bar for the previous trading day.
GetBar() returns either the bars ago number for the 9:30AM bar if it exists or the number for the first bar after 9:30AM
if there isn’t a specific bar with the exact 9:30AM timestamp (e.g. tick-based bars). With tick bars, it is possible
for many bars to have the same exact time stamp. In this case, the first bar of all the bars with the same timestamp is returned.*/
barsAgo = GetBar(timeOfInterest);
// Use this method for calculating your indicator values. Assign a value to each
// plot below by replacing 'Close[0]' with your own formula.
RequestesTime.Set(Close[barsAgo]);
}
#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 RequestesTime
{
get { return Values[0]; }
}
[Description("Sets the hour time")]
[Category("Parameters")]
public int SetHour
{
get { return setHour; }
set { setHour = Math.Max(0, value); }
}
[Description("Sets the hour time")]
[Category("Parameters")]
public int SetMinute
{
get { return setMinute; }
set { setMinute = Math.Max(0, value); }
}
[Description("Sets the hour time")]
[Category("Parameters")]
public int SetSeconds
{
get { return setSeconds; }
set { setSeconds = Math.Max(0, value); }
}
[Description("Ascertains whether to request the open or close price of the bar")]
[Category("Parameters")]
public string OpenClose
{
get { return openClose; }
set { openClose = value; }
}
#endregion
}
}
Comment