last works on EA all Requirements Prepare as Template and Implement it's Testing Usage as XFIMA EA ...

This commit is contained in:
2025-09-17 22:22:12 +03:30
parent b11e4f7f4b
commit 177d115de6
11 changed files with 4555 additions and 31 deletions
+52 -5
View File
@@ -33,11 +33,30 @@ class XCChartHelper : public XCBase
//
// Constructor ...
XCChartHelper(ulong _chartId = 0)
XCChartHelper(
ulong _chartId = 0,
string _symbol = NULL,
ENUM_TIMEFRAMES _period = NULL)
{
//
// Normalize Chart ID ...
mChartID = NormalizeChartId(_chartId);
bool canSetSymbolPeriod =
IsSpecifiedValid(_symbol) &&
IsSpecifiedValid(_period);
//
// Normalize ...
if (!canSetSymbolPeriod)
{
//
mChartID = NormalizeChartId(_chartId);
mSymbol = NormalizeSymbol(_symbol);
mPeriod = NormalizePeriod(_period);
}
else
{
//
SetSymbolPeriod(mSymbol, mPeriod);
}
}
//
@@ -181,7 +200,16 @@ class XCChartHelper : public XCBase
ENUM_TIMEFRAMES period //
)
{
return (ChartSetSymbolPeriod(mChartID, symbol, period));
bool result = (ChartSetSymbolPeriod(mChartID, symbol, period));
if (result)
{
//
mSymbol = symbol;
mPeriod = period;
}
//
return result;
}
/**
@@ -644,6 +672,23 @@ class XCChartHelper : public XCBase
return has;
}
ulong Open()
{
//
mChartID = ChartOpen(mSymbol, mPeriod);
return (mChartID);
}
void Close()
{
//
if (mChartID != -1 && mChartID != 0)
{
ChartClose(mChartID);
mChartID = 0;
}
}
//
// Properties ...
@@ -819,7 +864,9 @@ class XCChartHelper : public XCBase
// Props ...
//
ulong mChartID; // Chart ID ...
ulong mChartID; // Chart ID ...
string mSymbol; // Chart Symbol ...
ENUM_TIMEFRAMES mPeriod; // Chart Period ...
};
//
File diff suppressed because it is too large Load Diff
+10 -24
View File
@@ -2,7 +2,7 @@
//
// SaherElm IT Center MQL5 Class
// -------------------------------------------------
// Name: XCXBaseSignaller ...
// Name: XCBaseSignaller ...
// Description: Base Signaller Class ...
//
// - Create an Instance based on this ...
@@ -18,7 +18,7 @@
//
// - also Whenever you want you can call PrepareSignal Action to
// provide a Signal based on Current Market State ...
//
//
// Maintainer:
// ------------
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
@@ -41,23 +41,9 @@
//
// Definitions ...
//
// a Event Listener for Checking Can Analyse Market or not ...
// Usually for Time Based Session Handling ...
typedef bool (*TXCSignallerCanAnalyse)(
string _symbol,
ENUM_TIMEFRAMES _period,
datetime _time //
);
//
// a Event Listener for Notifying a Signal ...
// Usually for Time Based Session Handling ...
typedef bool (*TXCSignallerOnSignalEvent)(XSignal &signal);
//
// Implementations ...
class XCXBaseSignaller : public XCBase
class XCBaseSignaller : public XCBase
{
//
// Public ...
@@ -69,11 +55,11 @@ class XCXBaseSignaller : public XCBase
ENUM_TIMEFRAMES period; // Period ...
//
TXCSignallerCanAnalyse canAnalyseEventListener;
TCanAnalyse canAnalyseEventListener;
//
// Constructor ...
XCXBaseSignaller(
XCBaseSignaller(
string _name, // Name ...
string _symbol, // Symbol ...
ENUM_TIMEFRAMES _period, // Period ...
@@ -110,7 +96,7 @@ class XCXBaseSignaller : public XCBase
//
// Deconstructor ...
~XCXBaseSignaller()
~XCBaseSignaller()
{
//
// Cleanup Event Listeners ...
@@ -123,9 +109,9 @@ class XCXBaseSignaller : public XCBase
/**
* Add On Signal Event Listener ...
*
* @param listener: TXCSignallerOnSignalEvent instance ...
* @param listener: TOnSignal instance ...
*/
void AddOnSignalEventListener(TXCSignallerOnSignalEvent listener)
void AddOnSignalEventHandler(TOnSignal listener)
{
//
// Validate ...
@@ -392,7 +378,7 @@ class XCXBaseSignaller : public XCBase
/**
* Preparing Default Signal ...
*
*
* @param signal: XSignal, holding result Signal ...
* @param forDir: ENUM_X_DIRECTION
* @return ( bool )
@@ -473,7 +459,7 @@ class XCXBaseSignaller : public XCBase
//
// Event Listeners ...
TXCSignallerOnSignalEvent onSignalEventListeners[];
TOnSignal onSignalEventListeners[];
};
//
+1
View File
@@ -15,6 +15,7 @@
#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 XBackTester"
#property strict
+1
View File
@@ -15,6 +15,7 @@
#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
+608
View File
@@ -0,0 +1,608 @@
///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Expert Advisor
// -------------------------------------------------
// Name: XBaseEA
// Description: Base Expert Advisor as Template ...
//
// 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 icon "../Images/SaherElm_Logo_Color.ico"
#property description "SaherElm Based Expert Advisor"
#property strict
//
#define ShortName "XBaseEA"
//
// Importts ...
#include "../Classes/x-saherelm.x-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
//
// Management Panel ...
input group "Management Panel";
input bool eaShowPanel = false; // Show Management Panel
double eaPanelAllowedTPSLStep = 10; // Value for Change TP/SL in Each Step
double eaPanelMinAllowedRiskInPoint = 20; // Min Allowed Risk in Point
double eaPanelRiskInPoint = 50; // Risk in Point per Trades
double eaPanelRiskToRewardRatio = 2.0; // Risk To Reward per Trades
double eaPanelVolume = X_MIN_VOLUME; // Default volume Per Trade
//
// Variables ...
//
XCBaseExpert *eaExpert;
//
// Event Handlers ...
/**
* Initialize EA Process ...
*
* @return ( int )
*/
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 XCBaseExpert();
//
// Setting All Inputs to Expert Adviser Class ...
//
// Common ...
eaExpert.eaMagicNumber = eaMagicNumber;
eaExpert.eaSlippage = eaSlippage;
eaExpert.eaLogSuffix = eaTag;
//
// 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;
//
// Management Panel ...
eaExpert.eaShowPanel = eaShowPanel;
eaExpert.eaPanelAllowedTPSLStep = eaPanelAllowedTPSLStep;
eaExpert.eaPanelMinAllowedRiskInPoint = eaPanelMinAllowedRiskInPoint;
eaExpert.eaPanelRiskInPoint = eaPanelRiskInPoint;
eaExpert.eaPanelRiskToRewardRatio = eaPanelRiskToRewardRatio;
eaExpert.eaPanelVolume = eaPanelVolume;
//
// Attach Event Handlers ...
eaExpert.AddOnSignalEventHandler(OnSignalTriggered);
eaExpert.onGuardedEventHandler = OnGuardedTriggered;
eaExpert.AddOnDealsChangedEventHandler(OnDealsChanged);
eaExpert.AddOnStopLossEventHandler(OnStopLossTriggered);
eaExpert.AddOnOrdersChangedEventHandler(OnOrdersChanged);
eaExpert.canAnalyseEventHandler = CanAnalyseEventTriggered;
eaExpert.AddOnTakeProfitEventHandler(OnTakeProfitTriggered);
eaExpert.checkForGuardEventHandler = CheckForGuardTriggered;
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;
}
/**
* De Initialize Expert ...
*
* @param reason: int, DeInitialization 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
*
*/
void OnDeinit(const int reason)
{
//
// Destroy Timer ...
// If Initialized ...
//
// De Init Expert Class Instance ...
eaExpert.HandleOnDeInit();
//
// Cleanup Resources ...
delete eaExpert;
ZeroMemory(eaExpert);
}
/**
* Handle Tick for Processing ...
*/
void OnTick()
{
eaExpert.HandleOnTick();
}
/**
* Handle On Trade Processing ...
*/
void OnTrade()
{
eaExpert.HandleOnTrade();
}
/**
* Handle On Timer Processing ...
*/
void OnTimer()
{
eaExpert.HandleOnTimer();
}
/**
* Handle On Chart Event Processing ...
* @param id: int, Specified Event ID ...
* @param lparam: long, type event parameter
* @param dparam: double, type event parameter
* @param sparam: string, type event parameter
*/
void OnChartEvent(
const int id,
const long &lparam,
const double &dparam,
const string &sparam //
)
{
eaExpert.HandleOnChartEvent(id, lparam, dparam, sparam);
}
//
// Event Listeners ...
/**
* Handle CanAnalyse Event Triggered ...
*
* @param symbol: Symbol ...
* @param period: Period ...
* @param time: Analysing Time ...
*
* @return ( bool )
*/
bool CanAnalyseEventTriggered(
string symbol,
ENUM_TIMEFRAMES period,
datetime time //
)
{
return eaExpert.HandleCanAnalyseEventTriggered(
symbol,
period,
time //
);
}
/**
* Check For Guard Event Handler ...
*
* @param guards: XGuard[]
*
* @return ( int )
*/
int CheckForGuardTriggered(XGuard &guards[])
{
return eaExpert.CheckForGuardTriggered(guards);
}
/**
* Handle Guard Events ...
*
* @param action: ENUM_X_GUARD_ACTIONS
* @param positions: XPosition[]
*/
void OnGuardedTriggered(
ENUM_X_GUARD_ACTIONS action,
XPosition &positions[] //
)
{
eaExpert.OnGuardedTriggered(action, positions);
}
/**
* 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();
}
//
+1
View File
@@ -15,6 +15,7 @@
#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 X121XCATB based Expert Advisor"
#property strict
+613
View File
@@ -0,0 +1,613 @@
///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Expert Advisor
// -------------------------------------------------
// Name: XFIMAEA
// Description: Base Expert Advisor as Template ...
//
// 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 icon "../Images/SaherElm_Logo_Color.ico"
#property description "SaherElm XFIMA Expert Advisor"
#property strict
//
#define ShortName "XFIMAEA"
//
// Importts ...
#include "../XFIMA/Classes/x-saherelm.xfima.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
// )
// ]
//
//
// [EURUSDb|M5||25.0|5|]
// [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)]
// [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)]
input string eaSymbolConfiguration = "[EURUSDb|M5||25.0|5|]"; // 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
//
// Management Panel ...
input group "Management Panel";
input bool eaShowPanel = false; // Show Management Panel
double eaPanelAllowedTPSLStep = 10; // Value for Change TP/SL in Each Step
double eaPanelMinAllowedRiskInPoint = 20; // Min Allowed Risk in Point
double eaPanelRiskInPoint = 50; // Risk in Point per Trades
double eaPanelRiskToRewardRatio = 2.0; // Risk To Reward per Trades
double eaPanelVolume = X_MIN_VOLUME; // Default volume Per Trade
//
// Variables ...
//
XCXFIMAExpret *eaExpert;
//
// Event Handlers ...
/**
* Initialize EA Process ...
*
* @return ( int )
*/
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 XCXFIMAExpret();
//
// Setting All Inputs to Expert Adviser Class ...
//
// Common ...
eaExpert.eaMagicNumber = eaMagicNumber;
eaExpert.eaSlippage = eaSlippage;
eaExpert.eaLogSuffix = eaTag;
//
// 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;
//
// Management Panel ...
eaExpert.eaShowPanel = eaShowPanel;
eaExpert.eaPanelAllowedTPSLStep = eaPanelAllowedTPSLStep;
eaExpert.eaPanelMinAllowedRiskInPoint = eaPanelMinAllowedRiskInPoint;
eaExpert.eaPanelRiskInPoint = eaPanelRiskInPoint;
eaExpert.eaPanelRiskToRewardRatio = eaPanelRiskToRewardRatio;
eaExpert.eaPanelVolume = eaPanelVolume;
//
// Attach Event Handlers ...
eaExpert.AddOnSignalEventHandler(OnSignalTriggered);
eaExpert.onGuardedEventHandler = OnGuardedTriggered;
eaExpert.AddOnDealsChangedEventHandler(OnDealsChanged);
eaExpert.AddOnStopLossEventHandler(OnStopLossTriggered);
eaExpert.AddOnOrdersChangedEventHandler(OnOrdersChanged);
eaExpert.canAnalyseEventHandler = CanAnalyseEventTriggered;
eaExpert.AddOnTakeProfitEventHandler(OnTakeProfitTriggered);
eaExpert.checkForGuardEventHandler = CheckForGuardTriggered;
eaExpert.AddOnModifyPositionEventHandler(OnPositionModified);
eaExpert.AddOnPositionsChangedEventHandler(OnPositionsChanged);
eaExpert.AddOnTradeStateChangedEventHandler(OnTradeStateChanged);
eaExpert.AddOnForceClosePositionEventHandler(OnPositionForceClosed);
eaExpert.AddOnPartialClosePositionEventHandler(OnPositionPartialClosed);
//
// TODO: Attach other Requirements if necessary ...
//
// Try to Initialize eaExpert ...
bool isInited = eaExpert.HandleOnInit();
if (!isInited)
{
//
Print(ShortName, ": Initialization Failed ...");
return INIT_FAILED;
}
//
Print(ShortName, ": Initialized Successfully ...");
return INIT_SUCCEEDED;
}
/**
* De Initialize Expert ...
*
* @param reason: int, DeInitialization 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
*
*/
void OnDeinit(const int reason)
{
//
// Destroy Timer ...
// If Initialized ...
//
// De Init Expert Class Instance ...
eaExpert.HandleOnDeInit();
//
// Cleanup Resources ...
delete eaExpert;
ZeroMemory(eaExpert);
}
/**
* Handle Tick for Processing ...
*/
void OnTick()
{
eaExpert.HandleOnTick();
}
/**
* Handle On Trade Processing ...
*/
void OnTrade()
{
eaExpert.HandleOnTrade();
}
/**
* Handle On Timer Processing ...
*/
void OnTimer()
{
eaExpert.HandleOnTimer();
}
/**
* Handle On Chart Event Processing ...
* @param id: int, Specified Event ID ...
* @param lparam: long, type event parameter
* @param dparam: double, type event parameter
* @param sparam: string, type event parameter
*/
void OnChartEvent(
const int id,
const long &lparam,
const double &dparam,
const string &sparam //
)
{
eaExpert.HandleOnChartEvent(id, lparam, dparam, sparam);
}
//
// Event Listeners ...
/**
* Handle CanAnalyse Event Triggered ...
*
* @param symbol: Symbol ...
* @param period: Period ...
* @param time: Analysing Time ...
*
* @return ( bool )
*/
bool CanAnalyseEventTriggered(
string symbol,
ENUM_TIMEFRAMES period,
datetime time //
)
{
return eaExpert.HandleCanAnalyseEventTriggered(
symbol,
period,
time //
);
}
/**
* Check For Guard Event Handler ...
*
* @param guards: XGuard[]
*
* @return ( int )
*/
int CheckForGuardTriggered(XGuard &guards[])
{
return eaExpert.CheckForGuardTriggered(guards);
}
/**
* Handle Guard Events ...
*
* @param action: ENUM_X_GUARD_ACTIONS
* @param positions: XPosition[]
*/
void OnGuardedTriggered(
ENUM_X_GUARD_ACTIONS action,
XPosition &positions[] //
)
{
eaExpert.OnGuardedTriggered(action, positions);
}
/**
* 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();
}
//
+1
View File
@@ -15,6 +15,7 @@
#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 XOM1 based Expert Advisor"
#property strict
@@ -0,0 +1,117 @@
///////////////////////////////////////////////////////
//
// SaherElm IT Center MQL5 Class
// -------------------------------------------------
// Name: XCXFIMAExpret ...
// Description: XFIMA Expert Class ...
//
//
// Maintainer:
// ------------
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
//
//////////////////////////////////////////////////////
//
// Global Properties ...
#property library
#property copyright "Copyright 2023, SaherElm IT Center"
#property link "https://saherelm.ir"
#property version "1.00"
#property description "SaherElm XFIMA Expert Class"
#property strict
//
// Imports ...
#include "../../Classes/x-saherelm.x-expert.class.mq5"
#include "../Classes/x-saherelm.xfima.signaller.class.mq5"
//
// Definitions ...
//
// Implementation ...
class XCXFIMAExpret : public XCBaseExpert
{
//
// Public ...
public:
//
//
// Overrides ...
/**
* Prepare Signallers per Symbol Config ...
*
* @param symbolConfig: XSymbolTradeConfig, Symbol Trade Configurations ...
*/
void PrepareSignallers(XSymbolTradeConfig &symbolConfig) override
{
//
// Validate ...
if (!symbolConfig.IsValid())
{
return;
}
//
// Instantiate Signaller ...
//
// XFIMA Inputs ...
//
XFIMAInputs fimaInputs;
fimaInputs.Default();
fimaInputs.Hide();
//
// Instantiate Signaller Class ...
XCXFIMAStrategy1Signaller *fima1Signaller = new XCXFIMAStrategy1Signaller(
symbolConfig.symbol,
symbolConfig.period,
fimaInputs,
false // Process All Ticks ...
);
if (!fima1Signaller.IsValid())
{
//
fima1Signaller.Destroy();
delete fima1Signaller;
ZeroMemory(fima1Signaller);
return;
}
//
// Register Signaller ...
RegisterSignaller((XCBaseSignaller *)fima1Signaller);
//
// Cleanup Resources ...
fimaInputs.Clean();
ZeroMemory(fima1Signaller);
}
/**
* Additional On Tick Processing if Required ...
*
* @param barIndex: int, Specified Bar Index ...
*/
void virtual HandleOnTickAdditional(int barIndex = 0)
{
//
// Implement this if required ...
}
//
// Protected ...
protected:
//
//
// Private ...
private:
//
};
//
@@ -5,7 +5,7 @@
// Name: XCXFIMASignaller ...
// Description: XFIMA Signaller Class ...
//
//
//
// Maintainer:
// ------------
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
@@ -17,8 +17,341 @@
#property copyright "Copyright 2023, SaherElm IT Center"
#property link "https://saherelm.ir"
#property version "1.00"
#property description "SaherElm Base Signaller Class"
#property description "SaherElm XFIMA Signaller Class"
#property strict
//
// Imports ...
#include "../../Classes/x-saherelm.x-signaller.class.mq5"
#include "../../Helpers/x-saherelm.xfima.helper.mq5"
//
// Implementation ...
//
// Base XFIMA base Signaller ...
class XCXFIMABaseSignaller : public XCBaseSignaller
{
//
// Public ...
public:
//
// Props ...
//
XFIMAInputs fimaInputs; // XFIMA Inputs ...
XCXFIMAHelper *fimaHelper; // XFIMA Indicator Helper ...
//
// Constructor ...
XCXFIMABaseSignaller(
string _name, // Name ...
string _symbol, // Symbol ...
ENUM_TIMEFRAMES _period, // Period ...
XFIMAInputs &_fimaInputs, // XFIMAInputs ...
bool _processAllTicks = false, // Process All Ticks ...
double _defaultSignalR2R = 1.5, // Default Signal Risk To Reward Ratio ...
double _defaultSignalRiskInPoint = 50, // Default Signal Risk In Point ...
double _defaultSignalVolume = 0.01 // Default Signal Volume ...
) : XCBaseSignaller(_name,
_symbol,
_period,
_processAllTicks,
_defaultSignalR2R,
_defaultSignalRiskInPoint,
_defaultSignalVolume //
)
{
//
// Instantiating XFIMA Indicator Helper Class ...
if (_fimaInputs.IsValid())
{
//
fimaHelper = new XCXFIMAHelper();
bool isInited = fimaHelper.Init(
_symbol,
_period,
_fimaInputs //
);
if (isInited)
{
fimaInputs = _fimaInputs;
}
else
{
//
delete fimaHelper;
ZeroMemory(fimaHelper);
}
}
}
//
// Deconstructor ...
~XCXFIMABaseSignaller()
{
Destroy();
}
//
// Overrides ...
//
// Actions ...
/**
* Destroy ...
*/
void Destroy()
{
//
BeforeDestroy();
//
fimaInputs.Clean();
//
delete fimaHelper;
ZeroMemory(fimaHelper);
}
//
// Virtuals ...
/**
* Before Destroy ...
*/
void virtual BeforeDestroy()
{
}
/**
* Processing Tick ...
*
* @param barIndex: int, Specified Bar Index ...
*/
virtual void ProcessTick(int barIndex = 0)
{
}
/**
* Processing New Bar ...
*
* @param barIndex: int, Specified Bar Index ...
*/
virtual void ProcessNewBar(int barIndex = 0)
{
}
/**
* Check Guards Based on MarketConditions ...
*
* @param guards: XGuard[]
* @param barIndex: int
*
* @return ( int )
*/
int virtual CheckForGuard(
XGuard &guards[],
int barIndex = 0 //
)
{
//
int result = 0;
//
SpecifiedClean(guards);
//
result = ArraySize(guards);
//
return result;
}
/**
* Check Additional Validations on Childs Instances ...
*
* @return ( bool virtual )
*/
bool virtual CheckAdditionslSignallerValidations()
{
//
bool result = false;
//
// Validate Fima Helper Requirements ...
result = fimaInputs.IsValid() &&
fimaHelper != NULL;
//
return result;
}
//
// Protected ...
protected:
//
//
// Private ...
private:
//
};
//
// XFIMA Strategy 1 Signaller Class ...
class XCXFIMAStrategy1Signaller : public XCXFIMABaseSignaller
{
//
// Public ...
public:
//
// Props ...
//
// Constructor ...
XCXFIMAStrategy1Signaller(
string _symbol, // Symbol ...
ENUM_TIMEFRAMES _period, // Period ...
XFIMAInputs &_fimaInputs, // XFIMAInputs ...
bool _processAllTicks = false, // Process All Ticks ...
double _defaultSignalR2R = 1.5, // Default Signal Risk To Reward Ratio ...
double _defaultSignalRiskInPoint = 50, // Default Signal Risk In Point ...
double _defaultSignalVolume = 0.01 // Default Signal Volume ...
) : XCXFIMABaseSignaller("XFIMA1",
_symbol,
_period,
_fimaInputs,
_processAllTicks,
_defaultSignalR2R,
_defaultSignalRiskInPoint,
_defaultSignalVolume //
)
{
}
//
// Overrides ...
/**
* Processing Tick ...
*
* @param barIndex: int, Specified Bar Index ...
*/
virtual void ProcessTick(int barIndex = 0)
{
}
/**
* Processing New Bar ...
*
* @param barIndex: int, Specified Bar Index ...
*/
virtual void ProcessNewBar(int barIndex = 0)
{
//
// Normalize ...
barIndex = NormalizeInt(barIndex, 0);
//
// Update Helper ...
fimaHelper.Update(barIndex);
//
XOHCL bar;
XSignal signal;
bool has = false;
ENUM_X_DIRECTION signalDir = X_DIRECTION_NONE;
//
// Initialize Bar ...
has = bar.Init(
symbol,
period,
barIndex //
);
if (!has)
{
//
bar.Clean();
return;
}
//
// Retrieve Conditions ...
XFIMAConditions fimaConditions;
has = fimaHelper.GetConditions(
fimaConditions,
barIndex,
10 // Loopback ...
);
if (!has)
{
//
bar.Clean();
fimaConditions.Clean();
//
return;
}
//
// Analyse Conditions for Finding Signal ...
//
// Check Signal Founded ...
has = HasDirection(signalDir);
if (has)
{
//
// Prepare Founded Signal ...
has = PrepareSignal(
signal,
signalDir //
);
if (has)
{
//
// Notify On Signal Event ...
NotifySignalEventListeners(signal);
}
}
//
// Cleanup Resources ...
bar.Clean();
signal.Clean();
fimaConditions.Clean();
}
/**
* Check Guards Based on MarketConditions ...
*
* @param guards: XGuard[]
* @param barIndex: int
*
* @return ( int )
*/
int virtual CheckForGuard(
XGuard &guards[],
int barIndex = 0 //
)
{
//
int result = 0;
//
SpecifiedClean(guards);
//
result = ArraySize(guards);
//
return result;
}
//
};
//