/////////////////////////////////////////////////////// // // SaherElm IT Center MQL5 Expert Advisor // ------------------------------------------------- // Name: X121XCATB // Description: X121XCATB 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 icon "../Images/SaherElm_Logo_Color.ico" #property description "SaherElm X121XCATB based Expert Advisor" #property strict // #define ShortName "X121XCATBEA" // // Importts ... #include "../XCATBEA/Classes/xcatbea.expert.class.mq5" // // Inputs ... // // Common ... input group "Common"; input long eaMagicNumber = 1694056; // Magic Number input int eaSlippage = 10; // Slippgae input string eaLogSuffix = ""; // Log Suffix // // Detector ... input group "Detector"; // // Validating ... input group "Validations"; input bool eaForceHasSwing = false; // Force Block to Has Propper Swing input bool eaForceOBBarType = false; // Force Order Block to Has InDirectional Bars input int eaMinRequiredOBBar = 0; // Min Required OB Bar input bool eaForceFVGBarType = false; // Force Fair Value Gaps Bars Type input int eaLiquidityLoopback = 0; // Liquidity Detection Loopback input double eaMaxAllowedRange = 0.0; // Max Allowed Order Block Range input double eaMinAllowedRange = 0.0; // Min Allowed Order Block Range input bool eaForceHasFLiquidity = false; // Force Order Block To Has Reversal Liquidity input bool eaForceHasRLiquidity = false; // Force Order Block to Has Following Liquidity input bool eaValidateGapSequence = false; // Validate Fair Value Gaps Bar Sequences input int eaMinAllowedBlockLength = 0; // Min Allowed Block Length input bool eaValidateBlockEdgeBreakout = false; // Validate Order Block Edge Breakeout // // Filtering ... input group "Filters"; input bool eaFilterBasedOnKI = false; // Filter Based On KI Flats input bool eaFilterBasedOnPV = false; // Filter Based On PV input int eaBosLoopback = 0; // PV Based Filtering BOS Lookup input bool eaFilterBasedOnSar = false; // Filter Based On SAR input bool eaForceSarSwitched = false; // SAR Switched on Sides for Filtering input bool eaForceSarOnBothSide = false; // SAR Check on Both Sides for Filtering input bool eaForceSarHasBreakout = false; // SAR Must Breakout input bool eaForceSarHasReversalSide = false; // SAR Must Has Reversal Direction input bool eaFilterBasedOnRSI = false; // Filter Based On RSI input bool eaForceHasRSITrending = false; // RSI Must Has Trending input bool eaForceHasRSIVPattern = false; // RSI Must Has V Pattern input bool eaForceHasRSICrossing = false; // RSI Must Has Crossing Levels input bool eaFilterBasedOnADX = false; // Filter Based On ADX input bool eaForceHasADXTrending = false; // ADX Must Has Trending input bool eaForceHasADXCrossing = false; // ADX Must Has Crossing +DI/-DI input bool eaFilterBasedOnATR = false; // Filter Based On ATR input bool eaForceHasATRTrending = false; // ATR Must Has Trending input bool eaForceHasATRBreakout = false; // ATR Must Has Breakout input bool eaFilterBasedOnTrend = false; // Filter Based On Trend input bool eaForceHasTrendDir = false; // TREND Must Has Following Direction input bool eaForceHasTrendPlace = false; // TREND Place Based Filtering input bool eaForceHasTrendChange = false; // TREND Change Filtering input bool eaForceHasTrendTrending = false; // TREND Must Has Trending input bool eaForceHasTrendRejection = false; // TREND Line Must Rejected input bool eaForceHasTrendLineInside = false; // TREND Line has Inside input bool eaForceHasTrendDirOnBothSide = false; // TREND Must Has Following Direction in Both Side input bool eaFilterBasedOnDelta = false; // Filter Based On Delta input bool eaFilterBasedOnVolume = false; // Filter Based On Volume input bool eaFilterBasedOnSignalBar = false; // Filter Based on Signal Bar input bool eaFilterBasedOnHKSignalBar = false; // Filter Based On HK Signal Bar // // Signal ... input group "Signal"; input int eaSignalR2R = 2; // Risk Reward Ratio input double eaSLATRMultiplier = 0; // ATR Multiplier for SL input bool eaAllowSignallingBasedOnConditions = false; // Signalling Based on Market Conditions input bool eaAllowSignallingBasedOnTriggerBlocks = false; // Signalling Based on Trigger Blocks input bool eaAllowSignallingBasedOnTriggerBlocksInsideOther = false; // Signalling Based on Trigger Blocks Seccond inputs input bool eaSecondsTriggersSLUsingFirstTriggerBlock = true; // Use First Trigger Block for SL of Seconds Trigger Block Signalss input bool eaApplyValidateAndFilterOnSelectInsideOthers = false; // Select Trigger Blocks by Applying Validations and Filterss input bool eaAllowSignallingConditions1 = true; // Allow Market Conditions Signaller to Using Sar Signals input bool eaAllowSignallingConditions2 = true; // Allow Market Conditions Signaller to Using Trend Signals input bool eaAllowSignallingConditions3 = true; // Allow Market Conditions Signaller to Using Delta Signals input bool eaAllowSignallingConditions4 = true; // Allow Market Conditions Signaller to Using Volume Signals input bool eaAllowSignallingConditions5 = true; // Allow Market Conditions Signaller to Using Signal Bar Signals input bool eaAllowSignallingConditions6 = true; // Allow Market Conditions Signaller to Using HK Signal Bar Signals // // Volume ... input group "Volume"; input ENUM_X_VOLUME_SELECT_TYPE eaVolumeSelect = X_VOLUME_EQUITY; // 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 = 0.5; // Constant Percent of Balance Per Trade input double eaConstantBalance = 0.0; // Constant Balance for Calculations // // Management ... // eaSymbols => comma separated Symbols for Trading, use Default if not Provided ... // eaSessions => (ST_04:00_08:30),(TL_10:30_12:00),(NY_16:00_20:00) input group "Management"; input string eaSymbols = "XAUUSDb,XAGUSDb,EURUSDb,GBPUSDb,USDCHFb,USDJPYb,USDCADb,NZDUSDb,DowJones30"; // Allowed Trading Symbols 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 string eaSessions = ""; // Active Sessions input ENUM_XCATBEA_TRADE_RESTRICTION_PERIOD eaRestrictionsPeriod = XCATBEA_TRADE_RESTRICATION_NONE; // Restrictions Period input int eaMaxAllowedTPs = 0; // Max Allowed TP(s) in Restrictions Period input int eaMaxAllowedSLs = 0; // Max Allowed SL(s) in Restrictions Period input int eaMaxAllowedTrades = 0; // Max Allowed Trades in Restrictions Period input int eaMaxAllowedPositions = 5; // Max Allowed Same Time Trades input double eaMaxAllowedSpread = 25; // Max Allowed Spread For Trade input double eaMaxAllowedDrawdownFactor = 0.3; // Max Allowed DrawDown for Trade input string eaCloseAllOpenTradesAt = ""; // Close All Open Trades per Day At Specified Time // // Alert ... // Here we Provider All Requirements for // Configuring Alert Provider ... 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 // // Time Report ... 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 ... // XCXCATBEAExpert *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 XCXCATBEAExpert(); // // Setting All Inputs to Expert Adviser Class ... // // Common ... eaExpert.eaLogSuffix = eaTag; eaExpert.eaSlippage = eaSlippage; eaExpert.eaMagicNumber = eaMagicNumber; // // Detector ... // // Validations ... eaExpert.eaForceHasSwing = eaForceHasSwing; eaExpert.eaForceOBBarType = eaForceOBBarType; eaExpert.eaForceFVGBarType = eaForceFVGBarType; eaExpert.eaMaxAllowedRange = eaMaxAllowedRange; eaExpert.eaMinAllowedRange = eaMinAllowedRange; eaExpert.eaMinRequiredOBBar = eaMinRequiredOBBar; eaExpert.eaLiquidityLoopback = eaLiquidityLoopback; eaExpert.eaForceHasFLiquidity = eaForceHasFLiquidity; eaExpert.eaForceHasRLiquidity = eaForceHasRLiquidity; eaExpert.eaValidateGapSequence = eaValidateGapSequence; eaExpert.eaMinAllowedBlockLength = eaMinAllowedBlockLength; eaExpert.eaValidateBlockEdgeBreakout = eaValidateBlockEdgeBreakout; // // Filters ... eaExpert.eaBosLoopback = eaBosLoopback; eaExpert.eaFilterBasedOnKI = eaFilterBasedOnKI; eaExpert.eaFilterBasedOnPV = eaFilterBasedOnPV; eaExpert.eaFilterBasedOnSar = eaFilterBasedOnSar; eaExpert.eaFilterBasedOnRSI = eaFilterBasedOnRSI; eaExpert.eaForceSarSwitched = eaForceSarSwitched; eaExpert.eaFilterBasedOnADX = eaFilterBasedOnADX; eaExpert.eaFilterBasedOnATR = eaFilterBasedOnATR; eaExpert.eaForceHasTrendDir = eaForceHasTrendDir; eaExpert.eaForceSarOnBothSide = eaForceSarOnBothSide; eaExpert.eaFilterBasedOnTrend = eaFilterBasedOnTrend; eaExpert.eaForceHasTrendPlace = eaForceHasTrendPlace; eaExpert.eaFilterBasedOnDelta = eaFilterBasedOnDelta; eaExpert.eaForceSarHasBreakout = eaForceSarHasBreakout; eaExpert.eaForceHasRSITrending = eaForceHasRSITrending; eaExpert.eaForceHasRSIVPattern = eaForceHasRSIVPattern; eaExpert.eaForceHasRSICrossing = eaForceHasRSICrossing; eaExpert.eaForceHasADXTrending = eaForceHasADXTrending; eaExpert.eaForceHasADXCrossing = eaForceHasADXCrossing; eaExpert.eaForceHasATRTrending = eaForceHasATRTrending; eaExpert.eaForceHasATRBreakout = eaForceHasATRBreakout; eaExpert.eaForceHasTrendChange = eaForceHasTrendChange; eaExpert.eaFilterBasedOnVolume = eaFilterBasedOnVolume; eaExpert.eaForceHasTrendTrending = eaForceHasTrendTrending; eaExpert.eaForceHasTrendRejection = eaForceHasTrendRejection; eaExpert.eaFilterBasedOnSignalBar = eaFilterBasedOnSignalBar; eaExpert.eaForceSarHasReversalSide = eaForceSarHasReversalSide; eaExpert.eaForceHasTrendLineInside = eaForceHasTrendLineInside; eaExpert.eaFilterBasedOnHKSignalBar = eaFilterBasedOnHKSignalBar; eaExpert.eaForceHasTrendDirOnBothSide = eaForceHasTrendDirOnBothSide; // // Signal ... eaExpert.eaSignalR2R = eaSignalR2R; eaExpert.eaSLATRMultiplier = eaSLATRMultiplier; eaExpert.eaAllowSignallingBasedOnConditions = eaAllowSignallingBasedOnConditions; eaExpert.eaAllowSignallingBasedOnTriggerBlocks = eaAllowSignallingBasedOnTriggerBlocks; eaExpert.eaSecondsTriggersSLUsingFirstTriggerBlock = eaSecondsTriggersSLUsingFirstTriggerBlock; eaExpert.eaApplyValidateAndFilterOnSelectInsideOthers = eaApplyValidateAndFilterOnSelectInsideOthers; eaExpert.eaAllowSignallingBasedOnTriggerBlocksInsideOther = eaAllowSignallingBasedOnTriggerBlocksInsideOther; eaExpert.eaAllowSignallingConditions1 = eaAllowSignallingConditions1; eaExpert.eaAllowSignallingConditions2 = eaAllowSignallingConditions2; eaExpert.eaAllowSignallingConditions3 = eaAllowSignallingConditions3; eaExpert.eaAllowSignallingConditions4 = eaAllowSignallingConditions4; eaExpert.eaAllowSignallingConditions5 = eaAllowSignallingConditions5; eaExpert.eaAllowSignallingConditions6 = eaAllowSignallingConditions6; // // Volume ... eaExpert.eaVolumeSelect = eaVolumeSelect; eaExpert.eaStaticVoluem = eaStaticVoluem; eaExpert.eaConstantPercent = eaConstantPercent; eaExpert.eaConstantBalance = eaConstantBalance; eaExpert.eaConstantRiskBalance = eaConstantRiskBalance; eaExpert.eaDynamicVolumeStepVolume = eaDynamicVolumeStepVolume; eaExpert.eaDynamicVolumeStepBalance = eaDynamicVolumeStepBalance; // // Management ... eaExpert.eaSymbols = eaSymbols; eaExpert.eaSessions = eaSessions; eaExpert.eaAllowTrade = eaAllowTrade; eaExpert.eaAllowLongs = eaAllowLongs; eaExpert.eaAllowGuards = eaAllowGuards; eaExpert.eaAllowShorts = eaAllowShorts; eaExpert.eaMaxAllowedTPs = eaMaxAllowedTPs; eaExpert.eaMaxAllowedSLs = eaMaxAllowedSLs; eaExpert.eaMaxAllowedTrades = eaMaxAllowedTrades; eaExpert.eaMaxAllowedSpread = eaMaxAllowedSpread; eaExpert.eaRestrictionsPeriod = eaRestrictionsPeriod; eaExpert.eaMaxAllowedPositions = eaMaxAllowedPositions; eaExpert.eaCloseAllOpenTradesAt = eaCloseAllOpenTradesAt; eaExpert.eaMaxAllowedDrawdownFactor = eaMaxAllowedDrawdownFactor; // // Alert ... eaExpert.eaLogAlerts = eaLogAlerts; eaExpert.eaMailAlerts = eaMailAlerts; eaExpert.eaPushAlerts = eaPushAlerts; eaExpert.eaSaveAlerts = eaSaveAlerts; eaExpert.eaEnableAlerts = eaEnableAlerts; eaExpert.eaTerminalAlerts = eaTerminalAlerts; // // Reports ... eaExpert.eaReportTrades = eaReportTrades; eaExpert.eaReportNewDays = eaReportNewDays; eaExpert.eaReportSignals = eaReportSignals; eaExpert.eaReportNewWeeks = eaReportNewWeeks; eaExpert.eaReportNewHours = eaReportNewHours; eaExpert.eaReportProtector = eaReportProtector; eaExpert.eaReportNewMonths = eaReportNewMonths; eaExpert.eaReportRestrictions = eaReportRestrictions; eaExpert.eaReportAfterTradesBalance = eaReportAfterTradesBalance; // // Collector ... eaExpert.eaSaveWins = eaSaveWins; eaExpert.eaSaveLosts = eaSaveLosts; eaExpert.eaSaveTrades = eaSaveTrades; eaExpert.eaSaveSignals = eaSaveSignals; eaExpert.eaSaveRestrictions = eaSaveRestrictions; // // Attach Event Handlers ... 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); // bool isInited = eaExpert.HandleOnInit(); if (!isInited) { return INIT_FAILED; } // 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 ... /** * 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(); } //