Home System Trading Post

Sound Alerts for Connection Status in MetaTrader 5

Attachments
47846.zip (328.25 KB, Download 0 times)

If you're looking to enhance your trading experience, adding sound alerts for connection status in MetaTrader 5 is a game-changer. This simple utility will notify you with sound cues whenever your connection status changes, ensuring you never miss a beat.

To get started, you’ll need to add your sound files to the MQL5\Files\Sounds directory. Once that’s done, you can copy the provided code into your EA Utility and compile it. Just a heads-up: the use of #resource can complicate uploads, so be mindful of that.

How to Implement the Sound Alert

Here's a quick rundown of the code you'll need:

//+------------------------------------------------------------------+
//| Connect_Disconnect_Sound_Alert.mq5 |
//| Copyright 2024, Rajesh Kumar Nait |
//| https://www.mql5.com/en/users/rajeshnait/seller |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, Rajesh Kumar Nait"
#property link "https://www.mql5.com/en/users/rajeshnait/seller"
#property version "1.00"
#include

bool first = true;
bool Now_IsConnected = false;
bool Pre_IsConnected = true;
datetime Connect_Start = 0, Connect_Stop = 0;

CTerminalInfo terminalInfo;
//--- Sound files
#resource "\Files\Sounds\CONNECTED.wav"
#resource "\Files\Sounds\DISCONNECTED.wav"
//+------------------------------------------------------------------+
int OnInit()
{
ResetLastError();
while (!IsStopped()) {
Pre_IsConnected = Now_IsConnected;
Now_IsConnected = terminalInfo.IsConnected();

if (first) {
Pre_IsConnected = !Now_IsConnected;
}
if (Now_IsConnected != Pre_IsConnected) {
if (Now_IsConnected) {
Connect_Start = TimeLocal();
if (!first) {
if (!PlaySound("::Files\Sounds\DISCONNECTED.wav"))
Print("Error: ", GetLastError());
}
if (IsStopped()) break;
if (!PlaySound("::Files\Sounds\CONNECTED.wav"))
Print("Error: ", GetLastError());
} else {
Connect_Stop = TimeLocal();
if (!first) {
if (!PlaySound("::Files\Sounds\CONNECTED.wav"))
Print("Error: ", GetLastError());
}
if (IsStopped()) break;
if (!PlaySound("::Files\Sounds\DISCONNECTED.wav"))
Print("Error: ", GetLastError());
}
}
first = false;
Sleep(1000);
}
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+

If you want to see this in action, check out the video below:


Related Posts

Comments (0)