Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Plot highand low for every 5 minutes

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

    Plot highand low for every 5 minutes

    Good afternoon;
    I am trying to plot the high and low for every 5 minutes. Here is my code:
    #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>
    ///
    /// </summary>
    [Description("")]
    public class HighLowByTimeRange : Indicator
    {
    #region Variables
    // Wizard generated variables
    private int startHour = 9; // Default setting for StartHour
    private int startMinute = 30; // Default setting for StartMinute
    private int endHour = 10; // Default setting for EndHour
    private int endMinute = 15; // Default setting for EndMinute
    // 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(Color.FromKnownColor(KnownColor.Green), PlotStyle.Line, "HighestHigh"));
    Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Line, "LowestLow"));
    CalculateOnBarClose = true;
    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);
    }

    // Calculate the number of bars ago for the start and end bars of the specified time range
    int fiveMinutesAgo = GetBar(Time[0].AddMinutes(-5));
    int endBarsAgo = GetBar(endDateTime);

    /* Now that we have the start and end bars ago values for the specified time range we can calculate the highest high for this range

    Note: We add 1 to the period range for MAX and MIN to compensate for the difference between "period" logic and "bars ago" logic.
    "Period" logic means exactly how many bars you want to check including the current bar.
    "Bars ago" logic means how many bars we are going to go backwards. The current bar is not counted because on that bar we aren't going back any bars so it would be "bars ago = 0" */
    double highestHigh = MAX(High, fiveMinutesAgo)[0];

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

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

    But it does this( see picture)

    How do I get it to display the lines for each 5 minute interval seperately as opposed to what is currently showing

    thank you for your help in anticipation
    Attached Files

    #2
    Hello aafwintb,
    Thanks for your note

    Are you trying to draw a line for each 5 minute period?

    Can you please upload a screeshot demonstrating the exact scenario you want your code to do.

    I look forward to assisting you further.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Thank you for your relpy.
      I am trying to display the high and low for each 5 minutes on a 1 minute chart such as the image attached.
      Attached Files

      Comment


        #4
        Hello aafwintb,
        Thanks for the sceenshot.

        The indicator (on the other platform) is not calculating the high and low on each bar, rather it is calculating the high / low every 5 bars or repainting the values.

        If you modify your code as below then are you able to get the correct values.

        Code:
        //in Variable
        double high = 0;
        double low = 0;
        
        //in OnBarUpdate
        if (CurrentBar < 5) return;
        if (CurrentBar % 5 == 0)
        {
        	high = MAX(High, 5)[0];
        	low = MIN(Low, 5)[0];
        }
        
        HighestHigh.Set(high);
        LowestLow.Set(low);
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          Thanks joydeep, I will try that

          Comment


            #6
            hi joydeep, I tried that and it gave me the equivalent of a donchain channel. I am looking for the high and low of the last 5 minutes, not the highest or lowest 5 minutes.

            Comment


              #7
              not to worry I will do it manually.
              thanks

              Comment


                #8
                Hello aafwintb,
                Thanks for the clarification.

                In such scenario simply create a multi sereis indicator. A sample code will b e like

                Code:
                //in Initialze
                protected override void Initialize()
                {
                CalculateOnBarClose = false;
                Add(PeriodType.Minute, 5);
                Overlay				= true;
                }
                
                //in OnBarUpdate
                protected override void OnBarUpdate()
                {
                	
                	if (this.BarsInProgress == 0)
                	{
                		HighestHigh.Set(Highs[1][0]);
                		LowestLow.Set(Lows[1][0]);
                	}
                }


                JoydeepNinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by aafwintb View Post
                  Thank you for your relpy.
                  I am trying to display the high and low for each 5 minutes on a 1 minute chart such as the image attached.
                  Use PlotStyle.Square for the Plot and Reset() the previous bar Plot when the Plot changes value.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                  0 responses
                  673 views
                  0 likes
                  Last Post Geovanny Suaza  
                  Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                  0 responses
                  379 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by Mindset, 02-09-2026, 11:44 AM
                  0 responses
                  111 views
                  0 likes
                  Last Post Mindset
                  by Mindset
                   
                  Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                  0 responses
                  577 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by RFrosty, 01-28-2026, 06:49 PM
                  0 responses
                  582 views
                  1 like
                  Last Post RFrosty
                  by RFrosty
                   
                  Working...
                  X