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 skipping first bar

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

    Indicator skipping first bar

    Hi friends i want to calculate high and low of first 5 min .The indicator is missing first bar.Is there better method to code high and low of first 5 min and draw lines ?
    regards
    NH



    {
    #region Variables
    // Wizard generated variables
    private int startHour = 9; // Default setting for StartHour
    private int startMinute = 15; // Default setting for StartMinute
    private int endHour = 9; // Default setting for EndHour
    private int endMinute = 20; // Default setting for EndMinute
    private bool runonce = true;
    // 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()
    {
    Add(new Plot(new Pen(Color.Red, 2), PlotStyle.Line, "HighestHigh"));
    Add(new Plot(new Pen(Color.Red, 2), PlotStyle.Line, "LowestLow"));
    CalculateOnBarClose = false;
    Overlay = true;
    PriceTypeSupported = false;
    }
    private DateTime startDateTime;
    private DateTime endDateTime;

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Check to make sure the end time is not earlier than the start time
    if (EndHour < StartHour)
    return;

    //Do not calculate the high or low value when the ending time of the desired range is less than the current time of the bar being processed
    if (ToTime(EndHour, EndMinute, 0) > ToTime(Time[0]))
    return;

    // If the stored date time date is not the same date as the bar time date, create a new DateTime object
    if (startDateTime.Date != Time[0].Date)
    {
    startDateTime = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, StartHour, StartMinute, 0);
    endDateTime = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, EndHour, EndMinute, 0);
    }

    if (//startDateTime.DayOfWeek.ToString() == "Saturday" ||
    startDateTime.DayOfWeek.ToString() == "Sunday") return;

    // Calculate the number of bars ago for the start and end bars of the specified time range
    int startBarsAgo = GetBar(startDateTime);
    int endBarsAgo = GetBar(endDateTime);

    // Now that we have the start end end bars ago values for the specified time range we can calculate the highest high for this range
    double highestHigh = MAX(High, startBarsAgo - endBarsAgo)[endBarsAgo];

    // Now that we have the start end end bars ago values for the specified time range we can calculate the lowest low for this range
    double lowestLow = MIN(Low, startBarsAgo - endBarsAgo)[endBarsAgo];

    // Set the plot values
    HighestHigh.Set(highestHigh);
    LowestLow.Set(lowestLow);
    }

    /// --------------------------------------------------------------------------------
    /// --------------------------------------------------------------------------------
    /// In order to trim the indicator's label we need to override the ToString() method.
    public override string ToString()
    {
    /* We return a label consisting of only the parameters we want to include. If we did not
    override this method we would have a label with all four parameters. In this example we will
    be left with only the parameters "Period" and "Interval". */
    return Name;
    }

    #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 HighestHigh
    {
    get { return Values[0]; }
    }

    [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 LowestLow
    {
    get { return Values[1]; }
    }

    [Description("")]
    [Category("Parameters")]
    [Gui.Design.DisplayNameAttribute("2. Start Hour")]
    public int StartHour
    {
    get { return startHour; }
    set { startHour = Math.Max(1, value); }
    }

    [Description("")]
    [Category("Parameters")]
    [Gui.Design.DisplayNameAttribute("3. Start Minute")]
    public int StartMinute
    {
    get { return startMinute; }
    set { startMinute = Math.Max(1, value); }
    }

    [Description("")]
    [Category("Parameters")]
    [Gui.Design.DisplayNameAttribute("4. End Hour")]
    public int EndHour
    {
    get { return endHour; }
    set { endHour = Math.Max(1, value); }
    }

    [Description("")]
    [Category("Parameters")]
    [Gui.Design.DisplayNameAttribute("5. End Minute")]
    public int EndMinute
    {
    get { return endMinute; }
    set { endMinute = Math.Max(1, value); }
    }
    #endregion
    }
    }
    Last edited by SLASH; 02-18-2011, 12:46 PM.

    #2
    Hello ninja hattori,

    It looks like your start time is after your end time. Do you have better results when swapping these?

    private int startHour = 9; // Default setting for StartHour
    private int startMinute = 15; // Default setting for StartMinute
    private int endHour = 9; // Default setting for EndHour
    private int endMinute = 05; // Default setting for EndMinute
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Opps mistake edited start time is 9:15 and end time is 9:20 .
      still same result skipping first bar
      regards
      NH

      Comment


        #4
        It's likely due to how that sample works. It's designed to get high / low of a time range and then display that value only after end hour has been complete.

        If you wanted to get the high / low from the first bar of the session that, can be done with:
        double myDouble = High[Bars.BarsSinceSession];
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_RyanM View Post
          It's likely due to how that sample works. It's designed to get high / low of a time range and then display that value only after end hour has been complete.

          If you wanted to get the high / low from the first bar of the session that, can be done with:
          double myDouble = High[Bars.BarsSinceSession];
          I am novice please tell me ,where to put this code in the indicator codes.
          regards
          nh

          Comment


            #6
            Hello nh,

            That belongs in OnBarUpdate()

            To get started with custom indicator development in NinjaTrader, please see the following link:
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              still not working someone plz check the code below
              regards
              nh
              HTML Code:
              {
                      #region Variables
                      // Wizard generated variables
                      private double highestHigh    = 0;
                      private double lowestLow = 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(new Pen(Color.Red, 2), PlotStyle.Line, "highestHigh"));
                          Add(new Plot(new Pen(Color.Red, 2), PlotStyle.Line, "lowestLow"));
                          CalculateOnBarClose    = false;
                          Overlay                = true;
                          PriceTypeSupported    = false;
                      }
                      /// <summary>
                      /// Called on each bar update event (incoming tick)
                      /// </summary>
                      protected override void OnBarUpdate()
                      {
                        // Resets the highest high at the start of every new session
                          if (Bars.FirstBarOfSession)
                              highestHigh = High[0];
                          // Resets the lowest low at the start of every new session
                          if (Bars.FirstBarOfSession)
                              lowestLow = Low[0];
                          
                          // Stores the highest high from the first 30 bars
                          if (Bars.BarsSinceSession < 30 && High[0] > highestHigh)
                              highestHigh = High[0];
                          // Stores the lowest low from the first 30 bars
                          if (Bars.BarsSinceSession < 30 && Low[0] < lowestLow)
                              lowestLow = Low[0];
                          
                      }

              Comment


                #8
                nh,

                If you are looking for someone to debug your code or build projects for you, please consider a NinjaScript consultant.


                If you're interested in learning NinjaScript, start with the tutorials and work in the strategy wizard at first to get familiar with expression building in a point and click interface.

                We're happy to guide you in the right direction, but you have to define some type of issue for us to assist.
                Ryan M.NinjaTrader Customer Service

                Comment


                  #9
                  The code looks correct, though it will calculate on the first 30 bars regardless of time frame for the chart. Unfortunately, you have provided code, but not posed a problem. What is the issue ?

                  Comment


                    #10
                    Originally posted by koganam View Post
                    The code looks correct, though it will calculate on the first 30 bars regardless of time frame for the chart. Unfortunately, you have provided code, but not posed a problem. What is the issue ?
                    not able to plot high and low lines
                    regards
                    nh

                    Comment


                      #11
                      HI nh,

                      Your code doesn't plot anything. Please go through the NinjaScript tutorials which walks you through basic items like setting plots. If you set this up through the indicator wizard, then it creates the structure needed to set plots.

                      If you continue to have trouble after going through those tutorials, please share also your Properties region and I'll take a look.
                      Ryan M.NinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by ninja hattori View Post
                        not able to plot high and low lines
                        regards
                        nh
                        You have not set any plots, so none will plot.

                        Code:
                        // Set the plot values
                        HighestHigh.Set(highestHigh);
                        LowestLow.Set(lowestLow);
                        I am assuming that your plots are called HighestHigh and LowestLow (case sensitive) respectively. If they are not, then you must use their correct names as defined in the Properties section.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by josh18955, 03-25-2023, 11:16 AM
                        6 responses
                        435 views
                        0 likes
                        Last Post Delerium  
                        Started by FAQtrader, Today, 03:35 PM
                        0 responses
                        3 views
                        0 likes
                        Last Post FAQtrader  
                        Started by rocketman7, Today, 09:41 AM
                        5 responses
                        18 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Started by frslvr, 04-11-2024, 07:26 AM
                        9 responses
                        126 views
                        1 like
                        Last Post caryc123  
                        Started by selu72, Today, 02:01 PM
                        1 response
                        12 views
                        0 likes
                        Last Post NinjaTrader_Zachary  
                        Working...
                        X