433 lines
8.3 KiB
Plaintext
433 lines
8.3 KiB
Plaintext
///////////////////////////////////////////////////////
|
|
//
|
|
// SaherElm IT Center XTest MQL5 Expert Advisor
|
|
// -------------------------------------------------
|
|
// Name: XRRToolsEA
|
|
// Description: a Template For Exper Advisors
|
|
//
|
|
// Maintainer:
|
|
// ------------
|
|
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
|
|
//
|
|
//////////////////////////////////////////////////////x
|
|
//
|
|
// Global Properties ...
|
|
#property copyright "Copyright 2023, SaherElm IT Center"
|
|
#property link "https://saherelm.ir"
|
|
#property version "1.00"
|
|
#property icon "../Images/SaherElm_Logo_Color.ico"
|
|
#property description "SaherElm XRRToolsEA"
|
|
#property strict
|
|
|
|
//
|
|
#define ShortName "XRRTools"
|
|
|
|
//
|
|
// Imports ...
|
|
#include "../Classes/x-saherelm.x-alert.class.mq5"
|
|
#include "../Classes/x-saherelm.x-rm.panel.class.mq5"
|
|
#include "../Classes/x-saherelm.x-trade.class.mq5"
|
|
#include "../Helpers/x-saherelm.x121.xcc.helper.mq5"
|
|
#include "../Helpers/x-saherelm.x121.xct.helper.mq5"
|
|
|
|
//
|
|
// Inputs ...
|
|
|
|
//
|
|
// Common ...
|
|
input group "Common";
|
|
input long eaMagicNumber = 78692110; // Magic Number
|
|
input int eaSlippage = 10; // Slippgae
|
|
input string eaLogSuffix = ""; // Log Suffix
|
|
|
|
//
|
|
// Alert ...
|
|
// Here we Provider All Requirements for
|
|
// Configuring Alert Provider ...
|
|
input group "Alert";
|
|
input bool eAEnableAlerts = true; // Enable Alerts
|
|
input bool eALogAlerts = true; // Log Alerts
|
|
input bool eAMailAlerts = false; // Mail Alerts
|
|
input bool eAPushAlerts = false; // Push Alerts
|
|
input bool eATerminalAlerts = false; // Terminal Alerts
|
|
|
|
//
|
|
// Define Local Variables ...
|
|
|
|
//
|
|
string eaLogTag; // EA Log Prefix ...
|
|
XCAlert *eaAlert; // Alert Provider for Expert Adviser ...
|
|
XCTrade *eaTrader; // Trader of Expert Adviser ...
|
|
XCRMPanel eaMainWindow; // Main Window ...
|
|
XCX121XCTHelper *eaCTHelper; // Bar Timer Indicator Helper class ...
|
|
XCX121XCCHelper *eaCCHelper; // Chart Styler Indicator Helper Class ...
|
|
|
|
//
|
|
// Event Handlers ...
|
|
|
|
//
|
|
// Initialization ...
|
|
int OnInit()
|
|
{
|
|
//
|
|
// Validate Inputs and Required Parameters
|
|
// for EA to Works fine ...
|
|
if (!ValidateInputs())
|
|
{
|
|
return INIT_PARAMETERS_INCORRECT;
|
|
}
|
|
|
|
//
|
|
// Initialize Timer ...
|
|
if (!InitTimer())
|
|
{
|
|
return INIT_FAILED;
|
|
}
|
|
|
|
//
|
|
// Initialize all Requirements ...
|
|
if (!InitEA())
|
|
{
|
|
return INIT_FAILED;
|
|
}
|
|
|
|
//
|
|
// Initialize all GUI Objects and Classes ...
|
|
if (!InitGUI())
|
|
{
|
|
return INIT_FAILED;
|
|
}
|
|
|
|
//
|
|
// Alert Initialization ...
|
|
string msg = "Initialized Successfully ...";
|
|
eaAlert.SendAlert(msg);
|
|
|
|
//
|
|
// 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
|
|
|
|
//
|
|
// Destroy Timer ...
|
|
DestroyTimer();
|
|
|
|
//
|
|
// Destroy all GUI Requirements ...
|
|
DestroyGUI();
|
|
|
|
//
|
|
// Alert De Initialization Succeeded ...
|
|
string msg = "De Initialized Successfully ...";
|
|
eaAlert.SendAlert(msg);
|
|
|
|
//
|
|
// Destroy all EA Requirements ...
|
|
DestroyEA();
|
|
}
|
|
|
|
//
|
|
// On Tick Handler ...
|
|
void OnTick()
|
|
{
|
|
//
|
|
UpdateGUI();
|
|
}
|
|
|
|
//
|
|
// Handle On Trade Event ...
|
|
void OnTrade()
|
|
{
|
|
eaTrader.HandleOnTrade();
|
|
}
|
|
|
|
//
|
|
// On Timer ...
|
|
void OnTimer()
|
|
{
|
|
//
|
|
// TODO: Implement Time Based Functionalities ...
|
|
}
|
|
|
|
//
|
|
// Chart Event Handler ...
|
|
void OnChartEvent(
|
|
const int id,
|
|
const long &lparam,
|
|
const double &dparam,
|
|
const string &sparam //
|
|
)
|
|
{
|
|
//
|
|
// Apply Chart Event on Main Window ...
|
|
eaMainWindow.ChartEvent(id, lparam, dparam, sparam);
|
|
}
|
|
|
|
//
|
|
// Custom Event Handlers ...
|
|
|
|
/**
|
|
* Handle StopLoss Triggered Positions ...
|
|
*
|
|
* @param deal: XDeal ...
|
|
*/
|
|
void OnStopLossTriggered(const XDeal &deal)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* Handle Take Profit Triggered Positions ...
|
|
*
|
|
* @param deal: XDeal ...
|
|
*/
|
|
void OnTakeProfitTriggered(const XDeal &deal)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* Handle Force Close a Position ...
|
|
*
|
|
* @param ticket: Position Ticket ...
|
|
* @param position: XPosition ...
|
|
* @param comment: Closing Comment ...
|
|
*/
|
|
void OnPositionForceClosed(
|
|
const ulong ticket,
|
|
const XPosition &position,
|
|
const string comment //
|
|
)
|
|
{
|
|
}
|
|
|
|
//
|
|
// Custom Functions ...
|
|
|
|
/**
|
|
* Validate all Inputs of Expert Adviser ...
|
|
*
|
|
* @return ( bool )
|
|
*/
|
|
bool ValidateInputs()
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
string errMessage = "";
|
|
|
|
//
|
|
bool isCommonValid = (
|
|
//
|
|
eaSlippage > 0 &&
|
|
eaMagicNumber > 0
|
|
//
|
|
);
|
|
if (!isCommonValid)
|
|
{
|
|
//
|
|
errMessage += "common configurations error;" + "\n";
|
|
}
|
|
|
|
//
|
|
result =
|
|
//
|
|
// Common ...
|
|
isCommonValid &&
|
|
//
|
|
// Otere ...
|
|
true
|
|
//
|
|
;
|
|
|
|
//
|
|
if (!result)
|
|
{
|
|
//
|
|
// Since here Logger not Initiallized, we Use Raw Print Command ...
|
|
Print(ShortName + " Errors: \n", errMessage);
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Initialize Timer ...
|
|
*
|
|
* @return ( bool )
|
|
*/
|
|
bool InitTimer()
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
// TODO: Initialize Timer if necessary ...
|
|
// result = EventSetTimer(1);
|
|
// result = EventSetMillisecondTimer(100);
|
|
result = true;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Destroy Timer ...
|
|
*/
|
|
void DestroyTimer()
|
|
{
|
|
//
|
|
// TODO: Use This ...
|
|
// EventKillTimer();
|
|
}
|
|
|
|
/**
|
|
* Initialize all Requirements for Working Fine ...
|
|
*
|
|
* @return ( bool )
|
|
*/
|
|
bool InitEA()
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
// Locally Apply Changes on Prefix ...
|
|
eaLogTag = ShortName + ">" + eaLogSuffix;
|
|
|
|
//
|
|
// Bar Timer ...
|
|
X121XCTInputs ctInputs;
|
|
ctInputs.Default(); // Default Configurations ...
|
|
eaCTHelper = new XCX121XCTHelper();
|
|
result = eaCTHelper.Init(
|
|
_Symbol,
|
|
_Period,
|
|
ctInputs //
|
|
);
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Bar Styles ...
|
|
X121XCCInputs ccInputs;
|
|
ccInputs.Default(); // Default Configurations ...
|
|
eaCCHelper = new XCX121XCCHelper();
|
|
result = eaCCHelper.Init(
|
|
_Symbol,
|
|
_Period,
|
|
ccInputs //
|
|
);
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Create Instance of Alert Class and Configure it ...
|
|
eaAlert = new XCAlert();
|
|
eaAlert.SetPrefix(eaLogTag);
|
|
eaAlert.SetLogAlerts(eALogAlerts);
|
|
eaAlert.SetMailAlerts(eAMailAlerts);
|
|
eaAlert.SetPushAlerts(eAPushAlerts);
|
|
eaAlert.SetEnableAlerts(eAEnableAlerts);
|
|
eaAlert.SetTerminalAlerts(eATerminalAlerts);
|
|
|
|
//
|
|
// Create Trader Instance and Configure it ...
|
|
eaTrader = new XCTrade(
|
|
eaSlippage,
|
|
eaMagicNumber //
|
|
);
|
|
eaTrader.AddOnStopLossEventHandler(OnStopLossTriggered);
|
|
eaTrader.AddOnTakeProfitEventHandler(OnTakeProfitTriggered);
|
|
eaTrader.AddOnForceClosePositionEventHandler(OnPositionForceClosed);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Destroy all Initialized EA Requirements ...
|
|
*/
|
|
void DestroyEA()
|
|
{
|
|
//
|
|
delete eaCTHelper;
|
|
delete eaCCHelper;
|
|
|
|
//
|
|
delete eaAlert;
|
|
}
|
|
|
|
/**
|
|
* Initialize all GUI Requirements for EA ...
|
|
*
|
|
* @return ( bool )
|
|
*/
|
|
bool InitGUI()
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
// Initialize Application Main Window ...
|
|
result = eaMainWindow.Create(
|
|
ChartID(),
|
|
ShortName, // Dialog Name ...
|
|
0, // SubWindow ...
|
|
10, // X1 ...
|
|
35, // Y1 ...
|
|
300, // X2 ...
|
|
200 // Y2 ...
|
|
);
|
|
|
|
//
|
|
// Try To Run Main Window ...
|
|
if (result)
|
|
{
|
|
eaMainWindow.Run();
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Update GUI based on Each Ticks ...
|
|
*/
|
|
void UpdateGUI()
|
|
{
|
|
//
|
|
eaMainWindow.UpdateState();
|
|
}
|
|
|
|
/**
|
|
* Destroy all Initialized GUi Requirements ...
|
|
*/
|
|
void DestroyGUI()
|
|
{
|
|
//
|
|
eaMainWindow.Destroy();
|
|
}
|
|
|
|
//
|