Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Bar Ending Alarm

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

    Bar Ending Alarm

    Hello,

    I am attempting to create an indicator that sounds an alarm 30 seconds before the current bar closes; however, I can't seem to figure out how to get my code to work. It compiles correctly, but doesn't play the alarm.

    Thank you in advance.


    Code:
        public class BarEndingAlarm : Indicator
        {
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Indicator here.";
                    Name                                        = "BarEndingAlarm";
                    Calculate                                    = Calculate.OnEachTick;
                    IsOverlay                                    = false;
                    DisplayInDataBox                            = true;
                    DrawOnPricePanel                            = true;
                    DrawHorizontalGridLines                        = true;
                    DrawVerticalGridLines                        = true;
                    PaintPriceMarkers                            = true;
                    ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                    //See Help Guide for additional information.
                    IsSuspendedWhileInactive                    = true;
    
                }
                else if (State == State.Configure)
                {
                }
            }
    private TimeSpan GetBarDuration()
    {
        switch (BarsPeriod.BarsPeriodType)
        {
            case BarsPeriodType.Day:
                return new TimeSpan(24, 0, 0);
            case BarsPeriodType.Minute:
                return new TimeSpan(0, BarsPeriod.Value, 0);
            case BarsPeriodType.Month:
                return new TimeSpan(30, 0, 0, 0);
            case BarsPeriodType.Second:
                return new TimeSpan(0, 0, BarsPeriod.Value);
            case BarsPeriodType.Tick:
                return new TimeSpan(0, 0, 0, 0, BarsPeriod.Value);
            case BarsPeriodType.Volume:
                return new TimeSpan(0, 0, 0, 0, (int)BarsPeriod.Value);
            case BarsPeriodType.Week:
                return new TimeSpan(7, 0, 0, 0);
            case BarsPeriodType.Year:
                return new TimeSpan(365, 0, 0, 0);
            default:
                return TimeSpan.Zero;
        }
    }
            protected override void OnBarUpdate()
            {
               if (CurrentBar < 0)
            return;
    
            TimeSpan barDuration = GetBarDuration();
                TimeSpan timeUntilBarClose = Time[0].Add(barDuration) - DateTime.Now;
            if (timeUntilBarClose <= TimeSpan.FromSeconds(30))
                {
    
            PlaySound(NinjaTrader.Core.Globals.InstallDir + @"\sounds\Alert1.wav");
            }
            }
        }​

    #2
    Hello, thanks for writing in. I found some reference samples that you can use to make this kind of indicaotor:

    https://ninjatraderecosystem.com/use...ume-bar-timer/
    https://ninjatraderecosystem.com/use...ncybartimer-2/

    There is also the default BarTimer indicator in the platform that sets up a timer in a separate thread so it does not have to rely on OnBarUdate events.

    Kind regards,
    -ChrisL​

    The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.

    Comment


      #3
      I was able to figure this out utilizing the links you provided. Thank you!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      633 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      364 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      105 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      567 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      568 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X