//+------------------------------------------------------------------+
//| BoxAlert.mq5 |
//| Copyright 2023 [Your Name/Company] |
//| https://www.example.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023 [Your Name/Company]"
#property link "https://www.example.com"
#property version "1.00"
#property description "Draw a box on candles and get alerts when price breaks outside."
//--- Input parameters for the box
input color BoxColor = clrGreen; // Box color
input color BoxBorderColor = clrRed; // Box border color
input int BoxBorderWidth = 1; // Box border width
input bool BoxFill = true; // Fill the box
input bool BoxOutline = true; // Show box outline
input int BoxPadding = 2; // Padding between box and candle
input int BoxTolerance = 0; // Tolerance for price break (in pips)
//--- Input parameters for alerts
input bool AlertOnBreak = true; // Enable alerts on price break
input string AlertName = "Box Break Alert"; // Alert name
input string AlertMessage = "Price broke outside the box."; // Alert message
input EnumsAlertLevel AlertLevel = ALRT_LOW_LEVEL; // Alert level (e.g., ALRT_LOW_LEVEL, ALRT_MEDIUM_LEVEL, ALRT_HIGH_LEVEL)
//--- Global variables
int m_handleBox = -1;
bool m_boxDrawn = false;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- Create the box object
m_handleBox = BoxCreate();
if(m_handleBox == INVALID_HANDLE)
{
Print("Error creating box object! Error code = ", GetLastError());
return(INIT_FAILED);
}
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- Delete the box object
if(m_handleBox != INVALID_HANDLE)
{
BoxDelete(m_handleBox);
m_handleBox = -1;
}
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(m_boxDrawn)
{
//--- Check for price break outside the box
if(CheckPriceBreak())
{
SendAlert(AlertName, AlertMessage, AlertLevel);
}
}
}
//+------------------------------------------------------------------+
//| Function to draw the box on the chart |
//+------------------------------------------------------------------+
bool DrawBox(MqlTradeRequest &request)
{
if(m_handleBox != -1)
{
BoxSet(m_handleBox, request.symbol, request.time[0], request.time[1],
request.open, request.close,
BoxColor, BoxBorderColor, BoxBorderWidth, BoxFill, BoxOutline, BoxPadding);
BoxRepaint(m_handleBox);
return(true);
}
//--- Get the current price
double currentOpen = request.open;
double currentClose = request.close;
//--- Draw the box
m_handleBox = BoxCreate();
if(m_handleBox == INVALID_HANDLE)
{
Print("Error creating box object! Error code = ", GetLastError());
return(false);
}
BoxSet(m_handleBox, request.symbol, request.time[0], request.time[1],
currentOpen, currentClose,
BoxColor, BoxBorderColor, BoxBorderWidth, BoxFill, BoxOutline, BoxPadding);
BoxRepaint(m_handleBox);
m_boxDrawn = true;
return(true);
}
//+------------------------------------------------------------------+
//| Function to check if the price has broken outside the box |
//+------------------------------------------------------------------+
bool CheckPriceBreak()
{
//--- Get the current price
double currentHigh = iHigh(Symbol(), Period(), 0);
double currentLow = iLow(Symbol(), Period(), 0);
double currentOpen = iOpen(Symbol(), Period(), 0);
double currentClose = iClose(Symbol(), Period(), 0);
//--- Get the box boundaries
double boxHigh = iHigh(Symbol(), Period(), 0);
double boxLow = iLow(Symbol(), Period(), 0);
//--- Check if the price broke outside the box
if((currentHigh > boxHigh + BoxTolerance) || (currentLow < boxLow - BoxTolerance))
{
return(true);
}
return(false);
}
//+------------------------------------------------------------------+
//| Function to send an alert |
//+------------------------------------------------------------------+
void SendAlert(string alertName, string alertMessage, EnumsAlertLevel alertLevel)
{
Alert(alertName, alertMessage, alertLevel);
}
//+------------------------------------------------------------------+
//| Function to handle chart events (e.g., mouse click) |
//+------------------------------------------------------------------+
void OnChartEvent(const int id, int time, int price, int shift)
{
if(id == CHART_EVENT_MOUSE_CLICK)
{
//--- Get the click coordinates
double x = GetChartMouseX();
double y = GetChartMouseY();
//--- Get the symbol and time of the click
string symbol = Symbol();
datetime time = Time[0];
//--- Check if the click is within the current candle
if(time >= Time[0] && time <= Time[1])
{
//--- Draw the box
DrawBox(GetChartRequest());
}
}
}
//+------------------------------------------------------------------+
Explanation:
-
Input Parameters:
- Box Color: The color of the box outline and fill.
- Box Border Color: The color of the box border.
- Box Border Width: The thickness of the box border.
- Box Fill: Whether to fill the box with the specified color.
- Box Outline: Whether to show the box outline.
- Box Padding: The distance between the box and the candle's edges.
- Box Tolerance: The number of pips the price needs to move outside the box to trigger an alert.
- Alert On Break: Enables or disables alerts.
- Alert Name: The name of the alert.
- Alert Message: The message to be displayed in the alert.
- Alert Level: The priority level of the alert (e.g., low, medium, high).
-
Global Variables:
m_handleBox: Stores the handle of the drawn box object.
m_boxDrawn: A boolean flag to indicate if a box has been drawn.
-
OnInit():
- Creates a box object using
BoxCreate().
- Handles potential errors during box creation.
-
OnDeinit():
- Deletes the box object using
BoxDelete() when the indicator is removed from the chart.
-
OnTick():
- Checks if a box has been drawn (
m_boxDrawn).
- If a box is drawn, it calls
CheckPriceBreak() to see if the price has broken outside the box.
- If a break is detected and alerts are enabled, it sends an alert using
SendAlert().
-
DrawBox(MqlTradeRequest &request):
- This function is called when the user clicks on the chart.
- It creates a box object if one doesn't exist.
- It sets the box properties (color, border, fill, etc.) using
BoxSet().
- It repaints the chart to draw the box using
BoxRepaint().
- It sets
m_boxDrawn to .