/////////////////////////////////////////////////////// // // 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 XTEST EA" #property strict // #include // // START Inputs ... // // input group "Commons"; // input double staticVolume = 0.01; // Static Volume // // FAST ... input int fastLength = 9; // Fast Ma Length input ENUM_MA_METHOD fastMethod = MODE_SMA; // fast Ma Method input ENUM_APPLIED_PRICE fastAppliedTo = PRICE_CLOSE; // fast Ma Applied To // // SLOW ... input int slowLength = 18; // Slow Ma Length input ENUM_MA_METHOD slowMethod = MODE_SMA; // slow Ma Method input ENUM_APPLIED_PRICE slowAppliedTo = PRICE_CLOSE; // slow Ma Applied To // // 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" // // END Including Providers ... // // // START Global Definitions: Variables, Properties and etc ... // // ulong magicNumber = 169405612; // int xMAFastHandler = INVALID_HANDLE; double xMAFastBuffer[]; // int xMASlowHandler = INVALID_HANDLE; double xMASlowBuffer[]; // int ticksCount; int signalTicksCount; // CPositionInfo mPositionInfo; // // END Global Definitions: Variables, Properties and etc ... // // // START Event Handlers ... // // // Initialization ... int OnInit() { // logTag = "XTESTEA"; drawPrefix = logTag; alertPrefix = logTag; // // Apply default Chart Style for EA ... ApplyChartStyle(); // // Reset Timings ... ResetTiming(); // // Validate Inputs ... if (!ValidateInputs()) { return INIT_PARAMETERS_INCORRECT; } // // Define Handlers ... if (!DefineHandlers()) { 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 ... ReleaseHandlers(); } // // On Tick Handler ... void OnTick() { // if (!IsNewCandle()) { ticksCount++; } // HandleTick(); } // // Handle Trade Events ... void OnTrade() { } // // END Event Handlers ... // // // START Functions ... // // // Validate Input Args for Initialization ... bool ValidateInputs() { // bool result = false; // // Validate Args ... // // Validate XTEST Provider Inputs ... result = true; if (!result) { return false; } // // Logging State ... string message = "Validation of (" + logTag + ") Inputs " + (result ? "Succeeded" : "Failed") + " ..."; LogMessage(message); // return result; } bool DefineHandlers() { // bool result = true; // // FAST ... xMAFastHandler = iMA( _Symbol, _Period, fastLength, 0, fastMethod, fastAppliedTo); // if (xMAFastHandler == INVALID_HANDLE) { // result = false; return result; } // // SLOW ... xMASlowHandler = iMA( _Symbol, _Period, slowLength, 0, fastMethod, fastAppliedTo); // if (xMASlowHandler == INVALID_HANDLE) { // result = false; return result; } // return result; } void ReadBuffers(int bar_index) { // // Reading Fast Buffer ... int readedFastItems = CopyBuffer( xMAFastHandler, 0, bar_index, 5, xMAFastBuffer); // // Reading Slow Buffer ... int readedSlowItems = CopyBuffer( xMASlowHandler, 0, bar_index, 5, xMASlowBuffer); } void ReleaseHandlers() { // IndicatorRelease(xMAFastHandler); IndicatorRelease(xMASlowHandler); } void HandleTick() { // HandleControlState(); // ReadBuffers(1); // XOHCL cl0 = GetCandle(0); XOHCL cl1 = GetCandle(1); XOHCL cl2 = GetCandle(2); // bool hasLong = // // Base Condition ... ( // xMAFastBuffer[0] > xMASlowBuffer[0] // && // xMAFastBuffer[1] > xMASlowBuffer[1] // && // xMAFastBuffer[2] > xMASlowBuffer[2] // ) // && // cl1.close > cl2.close // && // cl0.close > cl1.close // ; if (hasLong && signalTicksCount < 3) { // signalTicksCount++; // if (signalTicksCount == 3) { // signalTicksCount = 0; // double entry = GetEntry(X_SIGNAL_LONG); double sl = GetLowestLow(3, 0); double tp = entry + (150 * _Point); double volume = staticVolume; // // declare and initialize the trade request and result of trade request MqlTradeRequest request = {}; MqlTradeResult result = {}; // // parameters of request request.tp = tp; request.sl = sl; request.deviation = 5; request.price = entry; request.volume = volume; request.symbol = _Symbol; request.magic = magicNumber; request.type = ORDER_TYPE_BUY; request.action = TRADE_ACTION_DEAL; // ResetLastError(); string signalType = EnumToString(request.type); // bool isExecuted = OrderSend( request, result); if (!isExecuted) { // string errMsg = GetErrorDescription(GetLastError()); LogMessage("Signal " + signalType + " Execution Failed: " + errMsg); } else { LogMessage("Signal " + signalType + " Executed Successfully ..."); } } } // bool hasShort = // // Base Condition ... ( // xMAFastBuffer[0] < xMASlowBuffer[0] // && // xMAFastBuffer[1] < xMASlowBuffer[1] // && // xMAFastBuffer[2] < xMASlowBuffer[2] // ) // && // cl1.close < cl2.close // && // cl0.close < cl1.close // ; if (hasShort && signalTicksCount < 3) { // signalTicksCount++; // if (signalTicksCount == 3) { // signalTicksCount = 0; // double entry = GetEntry(X_SIGNAL_SHORT); double sl = GetHighestHigh(3, 0); double tp = entry - (150 * _Point); double volume = staticVolume; // // declare and initialize the trade request and result of trade request MqlTradeRequest request = {}; MqlTradeResult result = {}; // // parameters of request request.tp = tp; request.sl = sl; request.deviation = 5; request.price = entry; request.volume = volume; request.symbol = _Symbol; request.magic = magicNumber; request.type = ORDER_TYPE_SELL; request.action = TRADE_ACTION_DEAL; // ResetLastError(); string signalType = EnumToString(request.type); // bool isExecuted = OrderSend( request, result); if (!isExecuted) { // string errMsg = GetErrorDescription(GetLastError()); LogMessage("Signal " + signalType + " Execution Failed: " + errMsg); } else { LogMessage("Signal " + signalType + " Executed Successfully ..."); } } } } void HandleControlState() { // int count = PositionsTotal(); if (count <= 0) { return; } // double totalProfit = 0; // // Loop through Positions ... for (int index = count - 1; index >= 0; index--) { // int isSelected = mPositionInfo.SelectByIndex(index); if (!isSelected) { continue; } // string symbol = mPositionInfo.Symbol(); if (symbol != _Symbol) { continue; } // ulong pMagic = mPositionInfo.Magic(); if (pMagic != magicNumber) { continue; } // double pProfit = mPositionInfo.Profit(); totalProfit += pProfit; } // // Handle Hedging Positions ... if (count > 1 && totalProfit > 0) { // // Loop through Positions and Close All of Them ... for (int index = count - 1; index >= 0; index--) { // int isSelected = mPositionInfo.SelectByIndex(index); if (!isSelected) { continue; } // string symbol = mPositionInfo.Symbol(); if (symbol != _Symbol) { continue; } // ulong pMagic = mPositionInfo.Magic(); if (pMagic != magicNumber) { continue; } // ulong ticket = mPositionInfo.Ticket(); double volume = mPositionInfo.Volume(); ENUM_POSITION_TYPE pType = (ENUM_POSITION_TYPE)mPositionInfo.PositionType(); // double entry = GetEntry(pType == POSITION_TYPE_BUY ? X_SIGNAL_SHORT : X_SIGNAL_LONG); // // // declare and initialize the trade request and result of trade request MqlTradeRequest request = {}; MqlTradeResult result = {}; // // parameters of request request.deviation = 5; request.price = entry; request.volume = volume; request.symbol = _Symbol; request.magic = magicNumber; request.type = pType == POSITION_TYPE_BUY ? ORDER_TYPE_SELL : ORDER_TYPE_BUY; request.action = TRADE_ACTION_DEAL; // ResetLastError(); // bool isExecuted = OrderSend( request, result); } } } // // END Functions ... //