usingcAlgo.API;namespacecAlgo{// 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)]publicclassNotificationsSample:Indicator{privateint_lastNotifiedBarIndex;[Parameter("Sound File Path", DefaultValue = "C:\\Windows\\Media\\notify.wav")]publicstringSoundFilePath{get;set;}[Parameter("Sender Email")]publicstringSenderEmail{get;set;}[Parameter("Receiver Email")]publicstringReceiverEmail{get;set;}protectedoverridevoidInitialize(){}publicoverridevoidCalculate(intindex){if(!IsLastBar||_lastNotifiedBarIndex==index)return;_lastNotifiedBarIndex=index;if(Bars.Last(1).Close>Bars.Last(1).Open){Notify("Up Bar Closed");}elseif(Bars.Last(1).Close<Bars.Last(1).Open){Notify("Down Bar Closed");}}privatevoidNotify(stringmessage){if(!string.IsNullOrWhiteSpace(SoundFilePath)){Notifications.PlaySound(SoundFilePath);}if(!string.IsNullOrWhiteSpace(SenderEmail)&&!string.IsNullOrWhiteSpace(ReceiverEmail)){Notifications.SendEmail(SenderEmail,ReceiverEmail,"Notification",message);}}}}
1 2 3 4 5 6 7 8 9101112131415161718192021222324
importclrclr.AddReference("cAlgo.API")fromcAlgo.APIimport*classNotificationsSample():definitialize(self):self.lastNotifiedBarIndex=0defcalculate(self,index):ifapi.IsLastBar==Falseorself.lastNotifiedBarIndex==index:returnself.lastNotifiedBarIndex=indexifapi.Bars.Last(1).Close>api.Bars.Last(1).Open:self.notify("Up Bar Closed")elifapi.Bars.Last(1).Close<api.Bars.Last(1).Open:self.notify("Down Bar Closed")defnotify(self,message):# SoundFilePath is a parameter defined in C# file of indicatorifapi.SoundFilePathisnotNoneandlen(api.SoundFilePath)>0:api.Notifications.PlaySound(api.SoundFilePath)# SenderEmail and ReceiverEmail are parameters defined in C# file of indicatorifapi.SenderEmailisnotNoneandlen(api.SenderEmail)>0andapi.ReceiverEmailisnotNoneandlen(api.ReceiverEmail)>0:api.Notifications.SendEmail(api.SenderEmail,api.ReceiverEmail,"Notification",message)# ShowPopup is a parameter defined in C# file of indicatorifapi.ShowPopup:api.Notifications.ShowPopup("Notification",message,PopupNotificationState.Success)
Methods
PlaySound (2)
PlaySound (1 of 2)
Summary
Plays a notification sound.
Remarks
This method doesn't work during backtesting and optimization.In indicators, use it with IsLastBar/IsLastBar, for real-time values.
This method doesn't work during backtesting and optimization.Use correct settings before trying to send an email notification.You can do that in Preferences -> Email Settings