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.
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");
}
}
}

Comment