Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

adding a timer counter to print a green or a red stripes

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

    adding a timer counter to print a green or a red stripes

    I have tried multiple ways to plot a green stripe when the timer counter ends but to no avail. please help.
    Here's the example
    1) we have an opening range indicator that plots the opening range high and low.
    2) price touches the opening range high, a internal timer starts counting. once the user defined timer (example:5mins) goes off, it prints a green stripe.

    #2
    Originally posted by alexkoe View Post
    I have tried multiple ways to plot a green stripe when the timer counter ends but to no avail. please help.
    Here's the example
    1) we have an opening range indicator that plots the opening range high and low.
    2) price touches the opening range high, a internal timer starts counting. once the user defined timer (example:5mins) goes off, it prints a green stripe.
    Vertical or horizontal stripe? What's the definition of opening range high?

    Comment


      #3
      hi,
      vertical stripe
      definition of opening range high/l: would be example, the first 15mins opening of GC which opens at 8:20.

      Comment


        #4
        Originally posted by alexkoe View Post
        hi,
        vertical stripe
        definition of opening range high/l: would be example, the first 15mins opening of GC which opens at 8:20.
        Sounds like you want a region highlight. You could modify this indicator for your needs: https://ninjatraderecosystem.com/use...lightmyregion/

        Comment


          #5
          thanks for the indicator. i am actually looking for a timer countdown style rather than a time define type.

          Comment


            #6
            Originally posted by alexkoe View Post
            thanks for the indicator. i am actually looking for a timer countdown style rather than a time define type.
            If you can describe what you're looking for in detail I may help you modify it, otherwise you can use AI to try to change it to your needs.

            Comment


              #7
              sure. i have attached a picture for your reference.Click image for larger version

Name:	Screenshot 2025-05-03 082841.png
Views:	229
Size:	109.0 KB
ID:	1342136

              Comment


                #8
                what's the width of the stripe?

                Comment


                  #9
                  just 1 bar size.
                  from what i understand. ninjatrader is not able to define a timer count.. because, most coding working with either a min bar close at a signal, rather than using time as a factor..
                  hope u got what i mean.

                  Comment


                    #10
                    Originally posted by alexkoe View Post
                    just 1 bar size.
                    from what i understand. ninjatrader is not able to define a timer count.. because, most coding working with either a min bar close at a signal, rather than using time as a factor..
                    hope u got what i mean.

                    Comment


                      #11
                      This indicator draws a vertical line x minutes after cross.
                      PHP Code:
                      #region Using declarations
                      using System;
                      using System.ComponentModel;
                      using System.ComponentModel.DataAnnotations;
                      using System.Xml.Serialization;
                      using System.Collections.Generic;
                      using System.Linq;
                      using NinjaTrader.Cbi;
                      using NinjaTrader.Gui;
                      using NinjaTrader.Gui.Chart;
                      using NinjaTrader.Gui.SuperDom;
                      using NinjaTrader.Gui.Tools;
                      using NinjaTrader.Data;
                      using NinjaTrader.NinjaScript;
                      using NinjaTrader.Core.FloatingPoint;
                      using NinjaTrader.NinjaScript.DrawingTools;
                      using System.Windows.Media;
                      #endregion
                      
                      namespace NinjaTrader.NinjaScript.Indicators
                      {
                          public class TimeRangeHighLow : Indicator
                          {
                              // State machine enum
                              private enum IndicatorState
                              {
                                  WaitingForTimeWindow,
                                  InTimeWindow,
                                  TimeWindowComplete,
                                  WaitingForCross,
                                  HighCrossDetected,
                                  LowCrossDetected,
                                  BothCrossesDetected
                              }
                              
                              // Core state variables
                              private IndicatorState currentState = IndicatorState.WaitingForTimeWindow;
                              private DateTime currentSessionDate = DateTime.MinValue;
                              private DateTime startTime;
                              private DateTime endTime;
                              
                              // High/Low tracking
                              private double highValue = double.MinValue;
                              private double lowValue = double.MaxValue;
                              
                              // Cross time tracking
                              private DateTime highCrossTime = DateTime.MinValue;
                              private DateTime lowCrossTime = DateTime.MinValue;
                              private DateTime highSignalTime = DateTime.MinValue;
                              private DateTime lowSignalTime = DateTime.MinValue;
                              
                              // Signal flags
                              private bool highSignalDrawn = false;
                              private bool lowSignalDrawn = false;
                      
                              protected override void OnStateChange()
                              {
                                  if (State == State.SetDefaults)
                                  {
                                      Description = @"Detects high and low values within a specified time range and draws time-based signals";
                                      Name = "TimeRangeHighLow";
                                      Calculate = Calculate.OnBarClose;
                                      IsOverlay = true;
                                      DisplayInDataBox = true;
                                      DrawOnPricePanel = true;
                                      DrawHorizontalGridLines = true;
                                      DrawVerticalGridLines = true;
                                      PaintPriceMarkers = true;
                                      ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                                      
                                      // Default parameters
                                      StartHour = 8;
                                      StartMinute = 20;
                                      EndHour = 8;
                                      EndMinute = 30;
                                      
                                      // Timer settings
                                      MinutesAfterCross = 8;
                                      DrawVerticalLines = true;
                                      HighCrossSignalEnabled = true;
                                      LowCrossSignalEnabled = true;
                                      
                                      // Default visual settings
                                      VerticalLineColor = Brushes.Blue;
                                      TextColor = Brushes.Black;
                                      
                                      // Reset state variables to defaults
                                      ResetStateVariables();
                                  }
                                  else if (State == State.Historical)
                                  {
                                      // Reset state variables at the start of historical processing
                                      ResetStateVariables();
                                  }
                              }
                              
                              private void ResetStateVariables()
                              {
                                  currentState = IndicatorState.WaitingForTimeWindow;
                                  currentSessionDate = DateTime.MinValue;
                                  highValue = double.MinValue;
                                  lowValue = double.MaxValue;
                                  highCrossTime = DateTime.MinValue;
                                  lowCrossTime = DateTime.MinValue;
                                  highSignalTime = DateTime.MinValue;
                                  lowSignalTime = DateTime.MinValue;
                                  highSignalDrawn = false;
                                  lowSignalDrawn = false;
                              }
                              
                              private void ResetForNewSession(DateTime sessionDate)
                              {
                                  currentSessionDate = sessionDate;
                                  startTime = sessionDate.Date.Add(new TimeSpan(StartHour, StartMinute, 0));
                                  endTime = sessionDate.Date.Add(new TimeSpan(EndHour, EndMinute, 0));
                                  
                                  highValue = double.MinValue;
                                  lowValue = double.MaxValue;
                                  
                                  highCrossTime = DateTime.MinValue;
                                  lowCrossTime = DateTime.MinValue;
                                  highSignalTime = DateTime.MinValue;
                                  lowSignalTime = DateTime.MinValue;
                                  
                                  highSignalDrawn = false;
                                  lowSignalDrawn = false;
                                  
                                  currentState = IndicatorState.WaitingForTimeWindow;
                      
                              }
                      
                              protected override void OnBarUpdate()
                              {
                                  // Check for session change - this handles the first bar as well
                                  if (currentSessionDate.Date != Time[0].Date)
                                  {
                                      ResetForNewSession(Time[0]);
                                  }
                                  
                                  // State machine
                                  switch (currentState)
                                  {
                                      case IndicatorState.WaitingForTimeWindow:
                                          if (Time[0] >= startTime && Time[0] <= endTime)
                                          {
                                              currentState = IndicatorState.InTimeWindow;
                                              
                                              // Start tracking high/low right away
                                              UpdateHighLowValues();
                                          }
                                          break;
                                          
                                      case IndicatorState.InTimeWindow:
                                          // Continue updating high/low values
                                          UpdateHighLowValues();
                                          
                                          // Check if we've reached the end of the time window
                                          if (Time[0] >= endTime)
                                          {
                                              currentState = IndicatorState.TimeWindowComplete;
                                          }
                                          break;
                                          
                                      case IndicatorState.TimeWindowComplete:
                                          // Time window is complete, transition to waiting for crosses
                                          currentState = IndicatorState.WaitingForCross;
                                          
                                          // Continue falling through to check for crosses immediately
                                          goto case IndicatorState.WaitingForCross;
                                          
                                      case IndicatorState.WaitingForCross:
                                          // Check for crosses only if they're enabled and haven't occurred yet
                                          bool highCrossDetected = HighCrossSignalEnabled && !highSignalDrawn && Close[0] >= highValue;
                                          bool lowCrossDetected = LowCrossSignalEnabled && !lowSignalDrawn && Close[0] <= lowValue;
                                          
                                          if (highCrossDetected && lowCrossDetected)
                                          {
                                              // Both crosses happened on same bar
                                              highCrossTime = Time[0];
                                              lowCrossTime = Time[0];
                                              highSignalTime = highCrossTime.AddMinutes(MinutesAfterCross);
                                              lowSignalTime = lowCrossTime.AddMinutes(MinutesAfterCross);
                                              currentState = IndicatorState.BothCrossesDetected;
                                          }
                                          else if (highCrossDetected)
                                          {
                                              // High cross detected
                                              highCrossTime = Time[0];
                                              highSignalTime = highCrossTime.AddMinutes(MinutesAfterCross);
                                              currentState = IndicatorState.HighCrossDetected;
                                          }
                                          else if (lowCrossDetected)
                                          {
                                              // Low cross detected
                                              lowCrossTime = Time[0];
                                              lowSignalTime = lowCrossTime.AddMinutes(MinutesAfterCross);
                                              currentState = IndicatorState.LowCrossDetected;
                                          }
                                          break;
                                          
                                      case IndicatorState.HighCrossDetected:
                                          // Check if it's time to draw the high signal
                                          if (!highSignalDrawn && Time[0] >= highSignalTime)
                                          {
                                              DrawHighCrossSignal();
                                              highSignalDrawn = true;
                                          }
                                          
                                          // Also check for low cross
                                          if (LowCrossSignalEnabled && !lowSignalDrawn && Close[0] <= lowValue)
                                          {
                                              lowCrossTime = Time[0];
                                              lowSignalTime = lowCrossTime.AddMinutes(MinutesAfterCross);
                                              currentState = IndicatorState.BothCrossesDetected;
                                          }
                                          break;
                                          
                                      case IndicatorState.LowCrossDetected:
                                          // Check if it's time to draw the low signal
                                          if (!lowSignalDrawn && Time[0] >= lowSignalTime)
                                          {
                                              DrawLowCrossSignal();
                                              lowSignalDrawn = true;
                                          }
                                          
                                          // Also check for high cross
                                          if (HighCrossSignalEnabled && !highSignalDrawn && Close[0] >= highValue)
                                          {
                                              highCrossTime = Time[0];
                                              highSignalTime = highCrossTime.AddMinutes(MinutesAfterCross);
                                              currentState = IndicatorState.BothCrossesDetected;
                                          }
                                          break;
                                          
                                      case IndicatorState.BothCrossesDetected:
                                          // Check if it's time to draw the high signal
                                          if (!highSignalDrawn && Time[0] >= highSignalTime)
                                          {
                                              DrawHighCrossSignal();
                                              highSignalDrawn = true;
                                          }
                                          
                                          // Check if it's time to draw the low signal
                                          if (!lowSignalDrawn && Time[0] >= lowSignalTime)
                                          {
                                              DrawLowCrossSignal();
                                              lowSignalDrawn = true;
                                          }
                                          break;
                                  }
                                  
                              }
                              
                              private void UpdateHighLowValues()
                              {
                                  // Update high value
                                  if (High[0] > highValue)
                                      highValue = High[0];
                                      
                                  // Update low value
                                  if (Low[0] < lowValue)
                                      lowValue = Low[0];
                              }
                            
                              private void DrawHighCrossSignal()
                              {
                                  if (DrawVerticalLines && HighCrossSignalEnabled)
                                  {
                                      string lineLabel = "HighCross_" + currentSessionDate.ToShortDateString();
                                      Draw.VerticalLine(this, lineLabel, 0, VerticalLineColor);
                                      
                                      string message = "High Cross + " + MinutesAfterCross + " minutes";
                                      Draw.Text(this, lineLabel + "_Text", message, 0, High[0], TextColor);
                                  }
                              }
                              
                              private void DrawLowCrossSignal()
                              {
                                  if (DrawVerticalLines && LowCrossSignalEnabled)
                                  {
                                      string lineLabel = "LowCross_" + currentSessionDate.ToShortDateString();
                                      Draw.VerticalLine(this, lineLabel, 0, VerticalLineColor);
                                      
                                      string message = "Low Cross + " + MinutesAfterCross + " minutes";
                                      Draw.Text(this, lineLabel + "_Text", message, 0, Low[0], TextColor);
                                  }
                              }
                      
                              #region Properties
                              [Range(0, 23)]
                              [Display(Name = "Start Hour", Description = "Start hour (0-23)", GroupName = "Time Range", Order = 0)]
                              public int StartHour { get; set; }
                      
                              [Range(0, 59)]
                              [Display(Name = "Start Minute", Description = "Start minute (0-59)", GroupName = "Time Range", Order = 1)]
                              public int StartMinute { get; set; }
                      
                              [Range(0, 23)]
                              [Display(Name = "End Hour", Description = "End hour (0-23)", GroupName = "Time Range", Order = 2)]
                              public int EndHour { get; set; }
                      
                              [Range(0, 59)]
                              [Display(Name = "End Minute", Description = "End minute (0-59)", GroupName = "Time Range", Order = 3)]
                              public int EndMinute { get; set; }
                              
                              // Timer properties
                              [Range(1, 360)]
                              [Display(Name = "Minutes After Cross", Description = "Number of minutes after a cross to draw vertical line", GroupName = "Timer", Order = 0)]
                              public int MinutesAfterCross { get; set; }
                              
                              [Display(Name = "Draw Vertical Lines", Description = "Enable drawing vertical lines after crosses", GroupName = "Timer", Order = 1)]
                              public bool DrawVerticalLines { get; set; }
                              
                              [Display(Name = "Enable High Cross Signal", Description = "Enable signals when price crosses above the high value", GroupName = "Timer", Order = 2)]
                              public bool HighCrossSignalEnabled { get; set; }
                              
                              [Display(Name = "Enable Low Cross Signal", Description = "Enable signals when price crosses below the low value", GroupName = "Timer", Order = 3)]
                              public bool LowCrossSignalEnabled { get; set; }
                      
                              // Visual properties
                              [XmlIgnore]
                              [Display(Name = "Vertical Line Color", Description = "Color for the vertical signal lines", GroupName = "Visual", Order = 0)]
                              public Brush VerticalLineColor { get; set; }
                      
                              [Browsable(false)]
                              public string VerticalLineColorSerializable
                              {
                                  get { return Serialize.BrushToString(VerticalLineColor); }
                                  set { VerticalLineColor = Serialize.StringToBrush(value); }
                              }
                              
                              [XmlIgnore]
                              [Display(Name = "Text Color", Description = "Color for the text labels", GroupName = "Visual", Order = 1)]
                              public Brush TextColor { get; set; }
                      
                              [Browsable(false)]
                              public string TextColorSerializable
                              {
                                  get { return Serialize.BrushToString(TextColor); }
                                  set { TextColor = Serialize.StringToBrush(value); }
                              }
                              #endregion
                          }
                      }&#8203; 
                      
                      Last edited by MiCe1999; 05-03-2025, 09:13 PM.

                      Comment


                        #12
                        thank u so so much.. i will try it out..

                        Comment


                          #13
                          Originally posted by alexkoe View Post
                          I have tried multiple ways to plot a green stripe when the timer counter ends but to no avail. please help.
                          Here's the example
                          1) we have an opening range indicator that plots the opening range high and low.
                          2) price touches the opening range high, a internal timer starts counting. once the user defined about us timer (example:5mins) goes off, it prints a green stripe.
                          Make sure your script tracks the timer using timenow or timestamp functions. Start counting once price hits the opening range high, then compare elapsed time to your defined duration. When matched, use plotshape or line.new to draw the green stripe.

                          Comment


                            #14
                            thank you so much for your input Fertryd2​ & MiCe1999​.
                            I trying to put both of them to use..

                            Comment


                              #15
                              i manage to let the stripes print.. but i am not sure why sometimes it prints automatically and sometimes i have to click reload ninjascript for it to print.. has it occurs to you guys when u are coding?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              547 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              323 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              99 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              543 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              545 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X