Skip to content

PlaySound Method

PlaySound

Summary

Plays a notification sound.

Remarks

This method doesn't work during backtesting and optimization.In indicators, use it with IsRealTime/IsLastBar, for real-time values.

Signature

1
public abstract void PlaySound(string fileName)

Parameters

Name Type Description
fileName string The sound file path

Return Value

void

Declaring Type

cAlgo.API.Internals.INotifications

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 using cAlgo.API;
 namespace cAlgo
 {
     // This sample indicator shows how to use API notifications to play sound or send an email
     [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
     public class NotificationsSample : Indicator
     {
         private int _lastNotifiedBarIndex;
         [Parameter("Sound File Path", DefaultValue = "C:\\Windows\\Media\\notify.wav")]
         public string SoundFilePath { get; set; }
         [Parameter("Sender Email")]
         public string SenderEmail { get; set; }
         [Parameter("Receiver Email")]
         public string ReceiverEmail { get; set; }
         protected override void Initialize()
         {
         }
         public override void Calculate(int index)
         {
             if (!IsLastBar || _lastNotifiedBarIndex == index) return;
             _lastNotifiedBarIndex = index;
             if (Bars.Last(1).Close > Bars.Last(1).Open)
             {
                 Notify("Up Bar Closed");
             }
             else if (Bars.Last(1).Close < Bars.Last(1).Open)
             {
                 Notify("Down Bar Closed");
             }
         }
         private void Notify(string message)
         {
             if (!string.IsNullOrWhiteSpace(SoundFilePath))
             {
                 Notifications.PlaySound(SoundFilePath);
             }
             if (!string.IsNullOrWhiteSpace(SenderEmail) && !string.IsNullOrWhiteSpace(ReceiverEmail))
             {
                 Notifications.SendEmail(SenderEmail, ReceiverEmail, "Notification", message);
             }
         }
     }
 }

Last update: March 23, 2023