Files
xMQL5/Projects/XAU5/Experts/x-saherelm.xau5.ea.mq5
T
2024-02-08 18:27:33 +03:30

400 lines
8.0 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Expert Advisor
// ----------------------------------------
// Name: XAUUSD
// Description: this is an expert controller
// advisors which provides a mechanism for use them ...
//
//
// Maintainer:
// ------------
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
//
//////////////////////////////////////////////////////
//
// Global Properties ...
#property copyright "Copyright 2023, SaherElm IT Center"
#property link "https://saherelm.ir"
#property version "1.00"
#property description "SaherElm XAU5 EA"
#property strict
//
// START Inputs ...
//
input group "Commons";
//
// XCTIMER inputs ...
input bool xAU5ShowCandleTimer = true; // Show Condle Remaining Time
input color xAU5CandleTimerColot = clrYellow; // Candle Timer Color
input ENUM_BASE_CORNER xAU5CandleTimerCorner = CORNER_RIGHT_LOWER; // Where to Show Candle Timer
//
// END Inputs ...
//
//
// START Including Providers ...
//
//
// Logger Library ...
#include "../Libraries/x-saherelm.log.lib.mq5"
//
// Common Library ...
#include "../Libraries/x-saherelm.common.lib.mq5"
//
// Alert Library ...
#include "../Libraries/x-saherelm.alert.lib.mq5"
//
// Draw Library ...
#include "../Libraries/x-saherelm.draw.lib.mq5"
//
// XAU5 Signal Provider ...
#include "../Providers/x-saherelm.xau5.provider.lib.mq5"
//
// END Including Providers ...
//
//
// START Global Definitions: Variables, Properties and etc ...
//
bool isXAU5NewHour;
bool isXAU5NewDay;
bool isXAU5NewWeek;
bool isXAU5NewMonth;
//
int xAU5CTimerHandler = INVALID_HANDLE;
//
// END Global Definitions: Variables, Properties and etc ...
//
//
// START Event Handlers ...
//
//
// Initialization ...
int OnInit()
{
//
logTag = "XAU5EA";
drawPrefix = logTag;
alertPrefix = logTag;
//
// Apply default Chart Style for EA ...
ApplyChartStyle();
//
// Reset Timings ...
ResetTiming();
//
// Validate Inputs ...
if (!ValidateInputs())
{
return INIT_PARAMETERS_INCORRECT;
}
//
// Initialize XAU5 Provider ...
if (!XAU5InitSignalProviderLibrary())
{
return INIT_PARAMETERS_INCORRECT;
}
//
// Init XCTimer ...
if (xAU5ShowCandleTimer)
{
if (!XAU5CTimerInit())
{
return INIT_FAILED;
}
}
//
// Init Succeed ...
return INIT_SUCCEEDED;
}
//
// DeInitialization ...
void OnDeinit(const int reason)
{
//
// REASON_PROGRAM 0 The EA has stopped working calling the ExpertRemove() function
// REASON_REMOVE 1 Program removed from a chart
// REASON_RECOMPILE 2 Program recompiled
// REASON_CHARTCHANGE 3 A symbol or a chart period is changed
// REASON_CHARTCLOSE 4 Chart closed
// REASON_PARAMETERS 5 Inputs changed by a user
// REASON_ACCOUNT 6 Another account has been activated or reconnection to the trade server has occurred due to changes in the account settings
// REASON_TEMPLATE 7 Another chart template applied
// REASON_INITFAILED 8 The OnInit() handler returned a non-zero value
// REASON_CLOSE 9 Terminal closed
//
// Here we can handle De Initialization Reasons ...
//
// De Initialize XAU5 Provider ...
XAU5DeinitSignalProviderLibrary(reason);
//
// De Init XDraw Library ...
// OnDeinitDrawLibrary();
//
IndicatorRelease(xAU5CTimerHandler);
}
//
// On Tick Handler ...
void OnTick()
{
//
// Handle Monthly Alert ...
bool isFirstMonth = xMonth == -1;
isXAU5NewMonth = IsNewMonth();
bool canMonthlyAlert =
isXAU5NewMonth &&
xAU5EnableAlerts &&
xAU5EnableMonthlyAlerts;
bool canMonthlyReport =
!isFirstMonth &&
canMonthlyAlert &&
xAU5EnableMonthlyReports;
if (canMonthlyAlert)
{
//
string message = "New Month ...";
//
// Prepare Monthly Report ...
if (canMonthlyReport)
{
//
string report = xAU5Reporter.GenerateReportString(XREPORT_MONTHLY);
//
message = "Last Month Report:\n" + report;
}
//
// Reset Report ...
xAU5Reporter.Reset(XREPORT_MONTHLY);
//
SendAlert(message);
}
//
// Handle Weekly Alert ...
bool isFirstWeek = xDayOfWeek == -1;
isXAU5NewWeek = IsNewWeek();
bool canWeeklyAlert =
isXAU5NewWeek &&
xAU5EnableAlerts &&
xAU5EnableWeeklyAlerts;
bool canWeeklyReport =
!isFirstWeek &&
canWeeklyAlert &&
xAU5EnableWeeklyReports;
if (canWeeklyAlert)
{
//
string message = "New Week ...";
//
// Prepare Weekly Report ...
if (canWeeklyReport)
{
//
string report = xAU5Reporter.GenerateReportString(XREPORT_WEEKLY);
//
message = "Last Week Report:\n" + report;
}
//
// Reset Report ...
xAU5Reporter.Reset(XREPORT_WEEKLY);
//
SendAlert(message);
}
//
// Handle Daily Alert ...
bool isFirstDay = xDay == -1;
isXAU5NewDay = IsNewDay();
bool canDailyAlert =
isXAU5NewDay &&
xAU5EnableAlerts &&
xAU5EnableDailyAlerts;
bool canDailyReport =
!isFirstDay &&
canDailyAlert &&
xAU5EnableDailyReports;
if (canDailyAlert)
{
//
string message = "New Day ...";
//
// Prepare Daily Report ...
if (canDailyReport)
{
//
string report = xAU5Reporter.GenerateReportString(XREPORT_DAILY);
//
message = "Last Week Report:\n" + report;
}
//
// Reset Report ...
xAU5Reporter.Reset(XREPORT_DAILY);
//
SendAlert(message);
}
//
// Handle Hourly Alert ...
bool isFirstHour = xHour == -1;
isXAU5NewHour = IsNewHour();
bool canHourlyAlert =
isXAU5NewHour &&
xAU5EnableAlerts &&
xAU5EnableHourlyAlerts;
if (canHourlyAlert)
{
//
string message = "New Hour ...";
//
SendAlert(message);
}
//
// Prevent Going Forward if user Disable it ...
if (xAU5EnableProvider)
{
//
// Handle Processing on XAU5 Signal Provider ...
XAU5SignalProviderDoControlState();
}
//
// Check Can Ignore Tick or not ..
bool canIgnoreTick = xAU5IsTestMode
? !IsNewCandle()
: !IsNewCandle() && xAU5WaitUntilNextCandle;
if (canIgnoreTick)
{
return;
}
//
xAU5WaitUntilNextCandle = false;
//
// Prevent Going Forward if user Disable it ...
if (xAU5EnableProvider)
{
//
// Handle Processing on XAU5 Signal Provider ...
XAU5SignalProviderHandleTick();
}
}
//
// Handle Trade Events ...
void OnTrade()
{
//
// Prevent Going Forward if user Disable it ...
if (xAU5EnableProvider)
{
//
// Handle Processing Trades on XAU5 Signal Provider ...
XAU5SignalProviderHandleTrade();
}
}
//
// END Event Handlers ...
//
//
// START Functions ...
//
//
// Validate Input Args for Initialization ...
bool ValidateInputs()
{
//
bool result = false;
//
// Validate Args ...
//
// Validate XAU5 Provider Inputs ...
result = XAU5ValidateInputs();
if (!result)
{
return false;
}
//
// Logging State ...
string message = "Validation of (" + logTag + ") Inputs " + (result ? "Succeeded" : "Failed") + " ...";
LogMessage(message);
//
return result;
}
bool XAU5CTimerInit()
{
//
bool result = true;
//
xAU5CTimerHandler = iCustom(
_Symbol,
_Period,
"x-saherelm.xctimer.indicator",
//
// Inputs ...
xAU5CandleTimerColot,
xAU5CandleTimerCorner);
//
if (xAU5CTimerHandler == INVALID_HANDLE)
{
result = false;
}
//
return result;
}
//
// END Functions ...
//