Files
MQL5Data/Experts/x-saherelm.xom1.ea.mq5
T
2025-09-10 17:37:58 +03:30

510 lines
12 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Expert Advisor
// -------------------------------------------------
// Name: XOM1
// Description: XOM1 base Expert Advisor ...
//
// 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 XOM1 based Expert Advisor"
#property strict
//
#define ShortName "XOM1EA"
//
// Importts ...
#include "../XOM1EA/Classes/xom1ea.expert.class.mq5"
//
// Inputs ...
//
// Common ...
input group "Common";
input long eaMagicNumber = 1694056; // Magic Number
input int eaSlippage = 10; // Slippgae
input string eaLogSuffix = ""; // Log Suffix
//
// Symbols
// [
// Symbol|
// period|
// (ST_04:00_08:30),(TL_10:30_12:00),(NY_16:00_20:00)|
// maxAllowedSpread|
// maxAllowedPosition|
// (
// restrictionsPeriod,
// restrictionsMultiplier,
// maxAllowedTPs,
// maxAllowedSLs,
// maxAllowedTrades,
// closeAllOpenTradesAt
// )
// ]
//
//
// [XAUUSDb|M5|(ST_04:00_08:30),(TL_10:30_12:00),(NY_16:00_20:00)|25.0|5|(XTRADE_RESTRICATION_DAILY,1,0,2,0)]
input string eaSymbolConfiguration = "[XAUUSDb|M5|(ST_04:00_08:30),(TL_10:30_12:00),(NY_16:00_20:00)|25.0|5|(XTRADE_RESTRICATION_DAILY,3,0,2,0)]"; // Symbol Configurations
//
// Management ...
input group "Management";
input bool eaAllowGuards = false; // Allow Guards
input bool eaAllowTrade = true; // Allow Trade on Signals
input bool eaAllowLongs = true; // Allow Long Trades
input bool eaAllowShorts = true; // Allow Short Trades
input double eaMaxAllowedDrawdownFactor = 0.3; // Max Allowed DrawDown for Trade
//
// Volume ...
input group "Volume";
input ENUM_X_VOLUME_SELECT_TYPE eaVolumeSelect = X_VOLUME_CONSTANT; // Volume Applying Type
input double eaStaticVoluem = 0.0; // Static Volume
input double eaDynamicVolumeStepBalance = 0.0; // Step of Balance for Increase Volume
input double eaDynamicVolumeStepVolume = X_MIN_VOLUME; // Step of Volume Increasing
input double eaConstantRiskBalance = 0.0; // Constant Risk Balance per Trade
input double eaConstantPercent = 1; // Constant Percent of Balance Per Trade
input double eaConstantBalance = 1000.0; // Constant Balance for Calculations
//
// Alert ...
input group "Alert";
input bool eaEnableAlerts = true; // Enable Alerts
input bool eaSaveAlerts = true; // Save Alerts
input bool eaLogAlerts = true; // Log Alerts
input bool eaMailAlerts = false; // Mail Alerts
input bool eaPushAlerts = true; // Push Alerts
input bool eaTerminalAlerts = false; // Terminal Alerts
//
// Reports ...
input group "Reports";
input bool eaReportNewMonths = false; // Report New Month
input bool eaReportNewWeeks = false; // Report New Weeks
input bool eaReportNewDays = true; // Report New Days
input bool eaReportNewHours = false; // Report New Hours
input bool eaReportTrades = true; // Report Trades
input bool eaReportSignals = true; // Report Signals
input bool eaReportRestrictions = true; // Report Restrictions
input bool eaReportProtector = true; // Report Protector Actions
input bool eaReportAfterTradesBalance = true; // Report Balance after a Trade Finished
//
// Collector ...
input group "Collector";
input bool eaSaveTrades = true; // Save Trades
input bool eaSaveSignals = true; // Save Signals
input bool eaSaveWins = true; // Save Winning Conditions
input bool eaSaveLosts = true; // Save Lost Conditions
input bool eaSaveRestrictions = true; // Save Restriction Reports
//
// Variables ...
//
XCXOM1EAExpert *eaExpert;
//
// Event Handlers ...
//
// Initialization ...
int OnInit()
{
//
// Preparing Tags ...
string eaTag =
(IsValid(eaLogSuffix)
? eaLogSuffix + "_"
: "") +
ShortName;
//
// Validate Inputs and Required Parameters ...
// for EA to Works fine ...
if (!ValidateInputs())
{
return INIT_PARAMETERS_INCORRECT;
}
//
// Initialize Timer ...
if (!InitTimer())
{
return INIT_FAILED;
}
//
// Configure EA Expert Class ...
eaExpert = new XCXOM1EAExpert();
//
// Setting All Inputs to Expert Adviser Class ...
//
// Symbols ...
eaExpert.eaSymbolConfiguration = eaSymbolConfiguration;
//
// Management ...
eaExpert.eaAllowGuards = eaAllowGuards;
eaExpert.eaAllowTrade = eaAllowTrade;
eaExpert.eaAllowLongs = eaAllowLongs;
eaExpert.eaAllowShorts = eaAllowShorts;
eaExpert.eaMaxAllowedDrawdownFactor = eaMaxAllowedDrawdownFactor;
//
// Volume ...
eaExpert.eaVolumeSelect = eaVolumeSelect;
eaExpert.eaStaticVoluem = eaStaticVoluem;
eaExpert.eaDynamicVolumeStepBalance = eaDynamicVolumeStepBalance;
eaExpert.eaDynamicVolumeStepVolume = eaDynamicVolumeStepVolume;
eaExpert.eaConstantRiskBalance = eaConstantRiskBalance;
eaExpert.eaConstantPercent = eaConstantPercent;
eaExpert.eaConstantBalance = eaConstantBalance;
//
// Alert ...
eaExpert.eaEnableAlerts = eaEnableAlerts;
eaExpert.eaSaveAlerts = eaSaveAlerts;
eaExpert.eaLogAlerts = eaLogAlerts;
eaExpert.eaMailAlerts = eaMailAlerts;
eaExpert.eaPushAlerts = eaPushAlerts;
eaExpert.eaTerminalAlerts = eaTerminalAlerts;
//
// Reports ...
eaExpert.eaReportNewMonths = eaReportNewMonths;
eaExpert.eaReportNewWeeks = eaReportNewWeeks;
eaExpert.eaReportNewDays = eaReportNewDays;
eaExpert.eaReportNewHours = eaReportNewHours;
eaExpert.eaReportTrades = eaReportTrades;
eaExpert.eaReportSignals = eaReportSignals;
eaExpert.eaReportRestrictions = eaReportRestrictions;
eaExpert.eaReportProtector = eaReportProtector;
eaExpert.eaReportAfterTradesBalance = eaReportAfterTradesBalance;
//
// Collector ...
eaExpert.eaSaveTrades = eaSaveTrades;
eaExpert.eaSaveSignals = eaSaveSignals;
eaExpert.eaSaveWins = eaSaveWins;
eaExpert.eaSaveLosts = eaSaveLosts;
eaExpert.eaSaveRestrictions = eaSaveRestrictions;
//
// Attach Event Handlers ...
eaExpert.AddOnSignalEventHandler(OnSignalTriggered);
eaExpert.AddOnDealsChangedEventHandler(OnDealsChanged);
eaExpert.AddOnStopLossEventHandler(OnStopLossTriggered);
eaExpert.AddOnOrdersChangedEventHandler(OnOrdersChanged);
eaExpert.AddOnTakeProfitEventHandler(OnTakeProfitTriggered);
eaExpert.AddOnModifyPositionEventHandler(OnPositionModified);
eaExpert.AddOnPositionsChangedEventHandler(OnPositionsChanged);
eaExpert.AddOnTradeStateChangedEventHandler(OnTradeStateChanged);
eaExpert.AddOnForceClosePositionEventHandler(OnPositionForceClosed);
eaExpert.AddOnPartialClosePositionEventHandler(OnPositionPartialClosed);
//
// Try to Initialize eaExpert ...
bool isInited = eaExpert.HandleOnInit();
if (!isInited)
{
//
Print(ShortName, ": Initialization Failed ...");
return INIT_FAILED;
}
//
Print(ShortName, ": Initialized Successfully ...");
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 ...
eaExpert.HandleOnDeInit();
//
delete eaExpert;
ZeroMemory(eaExpert);
}
//
// On Tick Handler ...
void OnTick()
{
eaExpert.HandleOnTick();
}
//
// Handle On Trade Event ...
void OnTrade()
{
eaExpert.HandleOnTrade();
}
//
// On Timer ...
void OnTimer()
{
eaExpert.HandleOnTimer();
}
//
// Chart Event Handler ...
void OnChartEvent(
const int id,
const long &lparam,
const double &dparam,
const string &sparam //
)
{
eaExpert.HandleOnChartEvent(id, lparam, dparam, sparam);
}
//
// Event Listeners ...
/**
* Trigger OnSignal Event ...
*
* @param signal: XSignal
*/
void OnSignalTriggered(XSignal &signal)
{
eaExpert.HandleOnSignalTriggered(signal);
}
/**
* Handle StopLoss Triggered Positions ...
*
* @param deal: XDeal instance ...
*/
void OnStopLossTriggered(const XDeal &deal)
{
eaExpert.HandleOnStopLossTriggered(deal);
}
/**
* Handle Take Profit Triggered Positions ...
*
* @param deal: XDeal instance ...
*/
void OnTakeProfitTriggered(const XDeal &deal)
{
eaExpert.HandleOnTakeProfitTriggered(deal);
}
/**
* Handle Force Close a Position ...
*
* @param ticket: ULONG ...
* @param position: XPosition instance ...
* @param comment: String ...
*/
void OnPositionForceClosed(
const ulong ticket,
const XPosition &position,
const string comment //
)
{
//
eaExpert.HandleOnPositionForceClosed(
ticket,
position,
comment //
);
}
/**
* Handle Deals Changes ...
*
* @param count: Integer, Number of Changes ...
*/
void OnDealsChanged(int count)
{
eaExpert.HandleOnDealsChanged(count);
}
/**
* Handle Orders Changes ...
*
* @param count: Integer, Number of Changes ...
*/
void OnOrdersChanged(int count)
{
eaExpert.HandleOnOrdersChanged(count);
}
/**
* Handle Positions Changes ...
*
* @param count: Integer, Number of Changes ...
*/
void OnPositionsChanged(int count)
{
eaExpert.HandleOnPositionsChanged(count);
}
/**
* Handle Position Modified ...
*
* @param ticket: ulong, Position Ticket ...
* @param profit: double, Position Profit ...
* @param comment: string, Comment ...
*/
void OnPositionModified(
const ulong ticket,
const double profit,
const string comment //
)
{
//
eaExpert.HandleOnPositionModified(
ticket,
profit,
comment //
);
}
/**
* Handle Position Partial Closed ...
*
* @param ticket: ulong, Position Ticket ...
* @param profit: double, Position Profit ...
* @param comment: string, Comment ...
*/
void OnPositionPartialClosed(
const ulong ticket,
const double profit,
const string comment //
)
{
//
eaExpert.HandleOnPositionPartialClosed(
ticket,
profit,
comment //
);
}
/**
* Handle Trade State Changed ...
*
* @param state: XOnTradeHandlerState instance ...
*/
void OnTradeStateChanged(const XOnTradeHandlerState &state)
{
eaExpert.HandleOnTradeStateChanged(state);
}
//
// 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();
}