Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Indicator for previous close at a specified time.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Indicator for previous close at a specified time.

    Hi all,

    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

    PHP Code:
    // 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].YearTime[0].MonthTime[0].Day 3setHoursetMinutesetSeconds);
                    else
                        
    timeOfInterest = new DateTime(Time[0].YearTime[0].MonthTime[0].Day 1setHoursetMinutesetSeconds);
                }
                
                
    /* 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(0value); }
            }

            [
    Description("Sets the hour time")]
            [
    Category("Parameters")]
            public 
    int SetMinute
            
    {
                
    get { return setMinute; }
                
    set setMinute Math.Max(0value); }
            }

            [
    Description("Sets the hour time")]
            [
    Category("Parameters")]
            public 
    int SetSeconds
            
    {
                
    get { return setSeconds; }
                
    set setSeconds Math.Max(0value); }
            }

            [
    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
        
    }

    Last edited by 4blue; 01-01-2010, 08:08 AM.

    #2
    4blue, try printing out timeOfInterest and barsAgo value to doublecheck those calcs hit home as you would expect - also have you tried setting session begin and end times to the regular US cash session for this?
    BertrandNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by RookieTrader, Today, 09:37 AM
    2 responses
    10 views
    0 likes
    Last Post RookieTrader  
    Started by alifarahani, Today, 09:40 AM
    1 response
    6 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by Gerik, Today, 09:40 AM
    1 response
    6 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by KennyK, 05-29-2017, 02:02 AM
    3 responses
    1,285 views
    0 likes
    Last Post NinjaTrader_Clayton  
    Started by AttiM, 02-14-2024, 05:20 PM
    11 responses
    186 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Working...
    X