Alerter
Download scriptThis script is designed to automate the creation and configuration of price alerts within the MetaTrader platform. Instead of requiring a user to manually place an alert, set its parameters, and confirm the settings, this tool performs those actions automatically by interacting directly with the trading software's interface.
Core Functionality
The logic follows a structured sequence to ensure that an alert is perfectly synced with current market conditions:
1. Price Detection and Stabilization When the script starts, it first identifies the current market price (the "Bid"). It draws a visual horizontal line on your chart to mark this specific price level. To ensure accuracy, the script monitors the market for a short duration; if the price fluctuates rapidly, it waits until the movement settles or confirms a specific change before proceeding. This prevents the script from creating an alert on a volatile, "jumping" price.
2. Interface Navigation Rather than using standard trading commands, the script communicates with the software's internal window elements. It searches for the specific tab and menus responsible for alerts. It effectively "navigates" the software's background interface to find where the alert settings are located.
3. Automated Configuration Once it locates the correct settings area, the script automatically populates the required information:
- Alert Type: It determines whether the price is above or below the current market level and selects the appropriate condition accordingly.
- Symbol Selection: It identifies the specific currency pair or asset you are currently viewing.
- Price Entry: It injects the exact numerical value of the price into the alert's text field.
- Notification Parameters: It resets and confirms standard settings such as the alert sound, the timeout duration, and whether the alert should repeat.
4. Final Execution After all parameters (Sound, Timeout, Repetition, and Price) have been automatically filled in, the script sends a final "command" to the software to confirm and activate the alert.
Once the alert is successfully created and active, the script removes the temporary horizontal line from your chart, leaving the automated alert active in the background.
//+------------------------------------------------------------------+
//| Alerter.mq4 |
//| Copyright © 2009, ÏàâåëÈâàíîâè÷ (api) |
//| p231970@hotmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, ÏàâåëÈâàíîâè÷ (api)"
#property link "p231970@hotmail.com"
#import "user32.dll"
int GetAncestor (int hWnd, int gaFlags);
int GetParent (int hWnd);
int GetDlgItem (int hDlg, int nIDDlgItem);
int GetWindowRect(int hDlg, int& ltrb[]);
int SendMessageA (int hWnd, int Msg, int wParam, int lParam);
int PostMessageA (int hWnd, int Msg, int wParam, int lParam);
int GetLastActivePopup (int hWnd);
int GetFocus();
int SetDlgItemTextA(int hWnd, int nIDDlgItem, string text);
#import
#import "kernel32.dll"
int lstrcat(string dst, string src);
#import
#define WM_COMMAND 0x0111
#define WM_KEYDOWN 0x0100
#define WM_LBUTTONDOWN 0x0201
#define BM_SETCHECK 0x00F1
#define VK_TAB 0x09
#define VK_INSERT 0x2D
#define BST_CHECKED 0x0001
#define CB_SETCURSEL 0x014E
#define CB_SELECTSTRING 0x014D
#define CBN_SELCHANGE 1
int hMain=0;
double Value;
string oName = "line-handler";
color LineColor=Red;
int LineWidth=2;
int Counter;
int start()
{
Value = Bid;
double StartVal = Bid;
ObjectCreate(oName,OBJ_HLINE,0,0,Value);
ObjectSetText(oName,"Price alert level",0);
ObjectSet(oName,OBJPROP_COLOR,LineColor);
ObjectSet(oName,OBJPROP_WIDTH,LineWidth);
WindowRedraw();
while(!IsStopped())
{
RefreshRates();
double newval = ObjectGet(oName,OBJPROP_PRICE1);
if(newval!=Value)
{
Counter=0;
Value=newval;
}
Counter++;
if(Counter>=10 && newval!=StartVal) break;
Sleep(100);
}
if(IsStopped())
{
ObjectDelete(oName);
return(0);
}
hMain=GetAncestor(WindowHandle(Symbol(),Period()),2);
int hWnd = FindAlertTab();
if(hWnd!=0)
{
int hCtrl = GetLastActivePopup(hMain);
PostMessageA(hWnd,WM_KEYDOWN,VK_INSERT,0);
while(!IsStopped() && GetLastActivePopup(hMain)==hCtrl) Sleep(100);
hWnd = GetLastActivePopup(hMain);
hCtrl = GetDlgItem(hWnd,0x461);
if(hCtrl!=0)
{
// Óñòàíîâêà ðàçðåøåíèÿ ñèãíàëà
PostMessageA(hCtrl,BM_SETCHECK,BST_CHECKED,0);
}
hCtrl = GetDlgItem(hWnd,0x3EA);
if(hCtrl!=0)
{
// Óñòàíîâêà âûáðàííîãî äåéñòâèÿ
PostMessageA(hCtrl,CB_SETCURSEL,0,0);
Sleep(200);
PostMessageA(hWnd,WM_COMMAND,(CBN_SELCHANGE<<16)+0x3EA,hCtrl);
}
hCtrl = GetDlgItem(hWnd,0x546);
if(hCtrl!=0)
{
SendMessageA(hCtrl,CB_SELECTSTRING,-1,lstrcat(Symbol(),""));
}
hCtrl = GetDlgItem(hWnd,0x427);
if(hCtrl!=0)
{
// Óñòàíîâêà óñëîâèÿ
if(Value<Bid)
PostMessageA(hCtrl,CB_SETCURSEL,0,0);
else
PostMessageA(hCtrl,CB_SETCURSEL,1,0);
Sleep(200);
PostMessageA(hWnd,WM_COMMAND,(CBN_SELCHANGE<<16)+0x427,hCtrl);
}
// Óñòàíîâêà çíà÷åíèÿ
SetDlgItemTextA(hWnd, 0x4DA, DoubleToStr(Value,Digits));
hCtrl = GetDlgItem(hWnd,0x520);
if(hCtrl!=0)
{
// Óñòàíîâêà çâóêà
PostMessageA(hCtrl,CB_SETCURSEL,0,0);
}
hCtrl = GetDlgItem(hWnd,0x555);
if(hCtrl!=0)
{
// Óñòàíîâêà òàéìàóòà
PostMessageA(hCtrl,CB_SETCURSEL,0,0);
}
hCtrl = GetDlgItem(hWnd,0x49A);
if(hCtrl!=0)
{
// Óñòàíîâêà ïîâòîðîâ
PostMessageA(hCtrl,CB_SETCURSEL,0,0);
}
Sleep(200);
PostMessageA(hWnd,WM_COMMAND,1,0);
}
ObjectDelete(oName);
return(0);
}
int FindAlertTab()
{
int hWnd=GetDlgItem(hMain,0xE81E);
int hRet = hWnd;
if(hWnd!=0)
{
hWnd=GetDlgItem(hWnd,0x51);
hRet = hWnd;
if(hWnd!=0)
{
hRet = GetDlgItem(hRet,0x81b6);
}
}
return(hRet);
}
//+------------------------------------------------------------------+
Comments