Sounds not playing from indicator alerts
Sounds not playing from indicator alerts
03 Mar 2025, 07:06
Can anyone suggest why I can never get alerts to play sounds from indicators? I am a recent CTrader convert so suspect its something basic ….
Example indicator below:
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Indicators
{
// Updated enum with a "Both" option
public enum NotificationOption
{
Up,
Down,
Both,
None
}
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
public class PriceCrossWMAAlert : Indicator
{
[Parameter("Period", DefaultValue = 21, MinValue = 1)]
public int Period { get; set; }
[Parameter("Source", DefaultValue = MovingAverageType.Weighted)]
public MovingAverageType Source { get; set; }
[Parameter("Alert Sound File", DefaultValue = @"C:\Users\steve\Sounds\Alert.wav")]
public string AlertSoundFile { get; set; }
[Parameter("Play Sound", DefaultValue = true)]
public bool PlaySound { get; set; }
[Parameter("Notification Option", DefaultValue = NotificationOption.Both)]
public NotificationOption NotificationOption { get; set; }
private WeightedMovingAverage wma;
private DataSeries priceSeries;
private int lastAlertedBar = -1;
[Output("WMA", PlotType = PlotType.Line, Thickness = 2, LineColor = "#FFA500")]
public IndicatorDataSeries WMAOutput { get; set; }
protected override void Initialize()
{
wma = Indicators.WeightedMovingAverage(Bars.ClosePrices, Period);
priceSeries = Bars.ClosePrices;
}
public override void Calculate(int index)
{
// Plot the WMA on the chart
WMAOutput[index] = wma.Result[index];
// Ensure we have enough bars for a valid WMA reading
if (index < Period)
return;
double currentPrice = priceSeries[index];
double previousPrice = priceSeries[index - 1];
double currentWMA = wma.Result[index];
double previousWMA = wma.Result[index - 1];
// Only alert once per bar
if (index > 0 && index != lastAlertedBar)
{
bool crossedUp = (previousPrice < previousWMA && currentPrice > currentWMA);
bool crossedDown = (previousPrice > previousWMA && currentPrice < currentWMA);
// If a cross is detected, decide if we should do anything
if (crossedUp || crossedDown)
{
// Cross UP logic
if (crossedUp && (NotificationOption == NotificationOption.Up || NotificationOption == NotificationOption.Both))
{
if (PlaySound)
Notifications.PlaySound(AlertSoundFile);
Print($"Price crossed UP WMA({Period}) at {Bars.OpenTimes[index]}");
// Optionally: Notifications.SendNotification(...);
// Draw UP arrow
double arrowPrice = Bars.LowPrices[index] - (Symbol.TickSize * 30);
Chart.DrawIcon(
"UpArrow" + index,
ChartIconType.UpArrow,
Bars.OpenTimes[index],
arrowPrice,
Color.Green);
}
// Cross DOWN logic
if (crossedDown && (NotificationOption == NotificationOption.Down || NotificationOption == NotificationOption.Both))
{
if (PlaySound)
Notifications.PlaySound(AlertSoundFile);
Print($"Price crossed DOWN WMA({Period}) at {Bars.OpenTimes[index]}");
// Optionally: Notifications.SendNotification(...);
// Draw DOWN arrow
double arrowPrice = Bars.HighPrices[index] + (Symbol.TickSize * 30);
Chart.DrawIcon(
"DownArrow" + index,
ChartIconType.DownArrow,
Bars.OpenTimes[index],
arrowPrice,
Color.Red);
}
// Mark this bar as alerted so we don't repeat on subsequent ticks
lastAlertedBar = index;
}
}
}
}
}
firemyst
13 Mar 2025, 07:20
Instead of Notifications.PlaySound, try:
System.Media.SystemSounds.Exclamation.Play();
@firemyst