/////////////////////////////////////////////////////// // // SaherElm IT Center XSAMPLE MQL5 Expert Advisor // ------------------------------------------------- // Name: XSAMPLE // Description: an Exper Advisor which used RSI and MA // to Analyse Market ... // // 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 description "SaherElm XSAMPLE" #property strict // #define ShortName "XSAMPLE" // #include "../Libraries/x-saherelm.log.lib.mq5" #include "../Libraries/x-saherelm.draw.lib.mq5" #include "../Libraries/x-saherelm.common.lib.mq5" #include "../Classes/x-saherelm.xtrade.class.mq5" // // Inputs ... long xSampleEAMagicNumber = 78692110; // Magic Number int xSampleEASlippage = 10; // Slippgae // double xSampleEAVolume = 0.01; // Volume Per Trade // ENUM_TIMEFRAMES xSCPeriod = PERIOD_H4; ENUM_TIMEFRAMES xMCPeriod = PERIOD_H12; ENUM_TIMEFRAMES xLCPeriod = PERIOD_D1; ENUM_TIMEFRAMES xHCPeriod = PERIOD_W1; // // Vars ... string xSampleEASymbol; ENUM_TIMEFRAMES xSampleEAPeriod; // int maxBuys = 1; int maxSells = 1; int buyCount = 0; int sellCount = 0; // XOHCL candle0; datetime candle0Time; XOHCLDrawSpecification candle0Style; // XOHCL candle1; datetime candle1Time; XOHCLDrawSpecification candle1Style; // XOHCL scCandle0; datetime scCandle0Time; XOHCLDrawSpecification scCandle0Style; // XOHCL scCandle1; datetime scCandle1Time; XOHCLDrawSpecification scCandle1Style; // XOHCL mcCandle0; datetime mcCandle0Time; XOHCLDrawSpecification mcCandle0Style; // XOHCL mcCandle1; datetime mcCandle1Time; XOHCLDrawSpecification mcCandle1Style; // XOHCL lcCandle0; datetime lcCandle0Time; XOHCLDrawSpecification lcCandle0Style; // XOHCL lcCandle1; datetime lcCandle1Time; XOHCLDrawSpecification lcCandle1Style; // XOHCL hcCandle0; datetime hcCandle0Time; XOHCLDrawSpecification hcCandle0Style; // XOHCL hcCandle1; datetime hcCandle1Time; XOHCLDrawSpecification hcCandle1Style; // string c0TagPrefix = "CC0"; string c1TagPrefix = "CC1"; // string sc0TagPrefix = "SC0"; string sc1TagPrefix = "SC1"; // string mc0TagPrefix = "MC0"; string mc1TagPrefix = "MC1"; // string lc0TagPrefix = "L0"; string lc1TagPrefix = "L1"; // string hc0TagPrefix = "H0"; string hc1TagPrefix = "H1"; // double _tp = 0; double _sl = 0; double _entry = 0; ENUM_POSITION_TYPE _type = NULL; // MqlTick xSampleEATick; XSCTrade *xSampleEATrader; // // Event Handlers ... // // Initialization ... int OnInit() { // if (xSampleEASymbol == NULL || StringLen(xSampleEASymbol) <= 0) { xSampleEASymbol = _Symbol; } // if (xSampleEAPeriod == NULL) { xSampleEAPeriod = _Period; } // // Validate Inputs ... if (!XSampleEAValidateInputs()) { return INIT_PARAMETERS_INCORRECT; } // logTag = ShortName; drawPrefix = ShortName; // XSampleEAConfigureCandleStyles(); // // Initialize XTrader ... xSampleEATrader = new XSCTrade( xSampleEASlippage, xSampleEAMagicNumber); // // 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 // // De Initialize XSampleEA Providers ... delete xSampleEATrader; } // // On Tick Handler ... void OnTick() { // if (!IsNewCandle( xSampleEASymbol, xSampleEAPeriod)) { return; } // // Reading Tick ... if (!SymbolInfoTick(xSampleEASymbol, xSampleEATick)) { return; } // // Counting Currently Open Positions ... buyCount = xSampleEATrader.CountPositions(xSampleEASymbol, POSITION_TYPE_BUY); sellCount = xSampleEATrader.CountPositions(xSampleEASymbol, POSITION_TYPE_SELL); // HandleCalculateTick(); // if (buyCount < maxBuys) { // // Check For Buy Signals ... } // if (sellCount < maxSells) { // // Check for Sell Signals ... } } // // Custom Functions ... // // Validate Variables nd Inputs ... bool XSampleEAValidateInputs() { // bool result = false; // result = // xSampleEASlippage > 0 && xSampleEAMagicNumber > 0 // ; // return result; } // void PrepareCandleStyle( XOHCLDrawSpecification &source, int width = 1, color clr = CLR_NONE, ENUM_LINE_STYLE style = STYLE_SOLID, bool ray = false, bool fill = false, bool back = false // ) { // source.Default(); // source.Width(width); // if (clr != CLR_NONE) { source.Color(clr); } // source.Style(style); source.RayRight(ray); source.Fill(fill); source.Back(back); } // // Configure Default Styles for Candle Drawing ... void XSampleEAConfigureCandleStyles() { // PrepareCandleStyle( candle0Style, 1, CLR_NONE, STYLE_DOT, false, false, true // ); candle0Style.SetLabelPrefix(c0TagPrefix); // PrepareCandleStyle( candle1Style, 1, CLR_NONE, STYLE_DOT, false, false, true // ); candle1Style.SetLabelPrefix(c1TagPrefix); // PrepareCandleStyle( scCandle0Style, 1, CLR_NONE, STYLE_DASH, false, false, true // ); scCandle0Style.SetLabelPrefix(sc0TagPrefix); // PrepareCandleStyle( scCandle1Style, 1, CLR_NONE, STYLE_DASH, false, false, true // ); scCandle1Style.SetLabelPrefix(sc1TagPrefix); // PrepareCandleStyle( mcCandle0Style, 1, CLR_NONE, STYLE_DASHDOT, false, false, true // ); mcCandle0Style.SetLabelPrefix(mc0TagPrefix); // PrepareCandleStyle( mcCandle1Style, 1, CLR_NONE, STYLE_DASHDOT, false, false, true // ); mcCandle1Style.SetLabelPrefix(mc1TagPrefix); // PrepareCandleStyle( lcCandle0Style, 1, CLR_NONE, STYLE_DASHDOTDOT, false, false, true // ); lcCandle0Style.SetLabelPrefix(lc0TagPrefix); // PrepareCandleStyle( lcCandle1Style, 1, CLR_NONE, STYLE_DASHDOTDOT, false, false, true // ); lcCandle1Style.SetLabelPrefix(lc1TagPrefix); // PrepareCandleStyle( hcCandle0Style, 1, CLR_NONE, STYLE_DOT, false, false, true // ); hcCandle0Style.highStyle.clr = clrLightSkyBlue; hcCandle0Style.openStyle.clr = clrDarkOliveGreen; hcCandle0Style.closeStyle.clr = clrCoral; hcCandle0Style.lowStyle.clr = clrPink; hcCandle0Style.SetLabelPrefix(hc0TagPrefix); // PrepareCandleStyle( hcCandle1Style, 2, CLR_NONE, STYLE_DOT, false, false, true // ); hcCandle1Style.SetLabelPrefix(hc1TagPrefix); } // void HandleCalculateTick() { // // Calculate Candles ... // // SC ... scCandle0 = GetCandle( xSampleEASymbol, xSCPeriod, 0 // Current Candle ); scCandle1 = GetCandle( xSampleEASymbol, xSCPeriod, 1 // Before Candle ); // scCandle0Time = GetCandleTime( xSampleEASymbol, xSCPeriod, 0); scCandle1Time = GetCandleTime( xSampleEASymbol, xSCPeriod, 1); // // MC ... mcCandle0 = GetCandle( xSampleEASymbol, xMCPeriod, 0 // Current Candle ); mcCandle1 = GetCandle( xSampleEASymbol, xMCPeriod, 1 // Before Candle ); // mcCandle0Time = GetCandleTime( xSampleEASymbol, xMCPeriod, 0); mcCandle1Time = GetCandleTime( xSampleEASymbol, xMCPeriod, 1); // // LC ... lcCandle0 = GetCandle( xSampleEASymbol, xLCPeriod, 0 // Current Candle ); lcCandle1 = GetCandle( xSampleEASymbol, xLCPeriod, 1 // Before Candle ); // lcCandle0Time = GetCandleTime( xSampleEASymbol, xLCPeriod, 0); lcCandle1Time = GetCandleTime( xSampleEASymbol, xLCPeriod, 1); // // HC ... hcCandle0 = GetCandle( xSampleEASymbol, xHCPeriod, 0 // Current Candle ); hcCandle1 = GetCandle( xSampleEASymbol, xHCPeriod, 1 // Before Candle ); // hcCandle0Time = GetCandleTime( xSampleEASymbol, xHCPeriod, 0); hcCandle1Time = GetCandleTime( xSampleEASymbol, xHCPeriod, 1); // // Now Reading Current Candle ... candle0 = GetCandle( xSampleEASymbol, xSampleEAPeriod, 0 // Current Candle ); candle1 = GetCandle( xSampleEASymbol, xSampleEAPeriod, 1 // Before Candle ); // candle0Time = GetCandleTime( xSampleEASymbol, xSampleEAPeriod, 0); candle1Time = GetCandleTime( xSampleEASymbol, xSampleEAPeriod, 1); // // Drawing Candles ... XSampleEADrawCandles(); } // // DRAW Functions ... void XSampleEADrawCandles( // bool drawAsBox = false, // Draw Candles As Box ... // bool update = true, // Update Candles based On Time ... // bool drawCandle0 = false, // Draw 0 Time Candles ... bool drawCandle1 = true, // Draw 1 Time Candles ... // bool drawBody = true, // Show Candle Bodies ... bool drawShadows = true, // Show Candle Shadows ... // // Candles ... bool drawCurrent = false, // Draw Current Candle ... bool drawSC = true, // Draw Short Candle ... bool drawMC = true, // Draw Medium Candle ... bool drawLC = true, // Draw Long Candle ... bool drawHC = true, // Draw Hind Candle ... // // MidLines ... bool draw0MidLine = false, bool draw1MidLine = false, bool drawSC0MidLine = true, bool drawSC1MidLine = true, bool drawMC0MidLine = true, bool drawMC1MidLine = true, bool drawLC0MidLine = true, bool drawLC1MidLine = true, bool drawHC0MidLine = true, bool drawHC1MidLine = true // ) { // int subWindow = 0; long chartId = ChartID(); // // Configure Styles for Draw Shadows and Bodies of Candles // based On Configurations ... // candle0Style.openStyle.draw = drawCandle0 && drawBody; candle0Style.closeStyle.draw = drawCandle0 && drawBody; // candle1Style.openStyle.draw = drawCandle1 && drawBody; candle1Style.closeStyle.draw = drawCandle1 && drawBody; // scCandle0Style.openStyle.draw = drawCandle0 && drawBody; scCandle0Style.closeStyle.draw = drawCandle0 && drawBody; // scCandle1Style.openStyle.draw = drawCandle1 && drawBody; scCandle1Style.closeStyle.draw = drawCandle1 && drawBody; // mcCandle0Style.openStyle.draw = drawCandle0 && drawBody; mcCandle0Style.closeStyle.draw = drawCandle0 && drawBody; // mcCandle1Style.openStyle.draw = drawCandle1 && drawBody; mcCandle1Style.closeStyle.draw = drawCandle1 && drawBody; // lcCandle0Style.openStyle.draw = drawCandle0 && drawBody; lcCandle0Style.closeStyle.draw = drawCandle0 && drawBody; // lcCandle1Style.openStyle.draw = drawCandle1 && drawBody; lcCandle1Style.closeStyle.draw = drawCandle1 && drawBody; // hcCandle0Style.openStyle.draw = drawCandle0 && drawBody; hcCandle0Style.closeStyle.draw = drawCandle0 && drawBody; // hcCandle1Style.openStyle.draw = drawCandle1 && drawBody; hcCandle1Style.closeStyle.draw = drawCandle1 && drawBody; // candle0Style.highStyle.draw = drawCandle0 && drawShadows; candle0Style.lowStyle.draw = drawCandle0 && drawShadows; // candle1Style.highStyle.draw = drawCandle1 && drawShadows; candle1Style.lowStyle.draw = drawCandle1 && drawShadows; // scCandle0Style.highStyle.draw = drawCandle0 && drawShadows; scCandle0Style.lowStyle.draw = drawCandle0 && drawShadows; // scCandle1Style.highStyle.draw = drawCandle1 && drawShadows; scCandle1Style.lowStyle.draw = drawCandle1 && drawShadows; // mcCandle0Style.highStyle.draw = drawCandle0 && drawShadows; mcCandle0Style.lowStyle.draw = drawCandle0 && drawShadows; // mcCandle1Style.highStyle.draw = drawCandle1 && drawShadows; mcCandle1Style.lowStyle.draw = drawCandle1 && drawShadows; // lcCandle0Style.highStyle.draw = drawCandle0 && drawShadows; lcCandle0Style.lowStyle.draw = drawCandle0 && drawShadows; // lcCandle1Style.highStyle.draw = drawCandle1 && drawShadows; lcCandle1Style.lowStyle.draw = drawCandle1 && drawShadows; // hcCandle0Style.highStyle.draw = drawCandle0 && drawShadows; hcCandle0Style.lowStyle.draw = drawCandle0 && drawShadows; // hcCandle1Style.highStyle.draw = drawCandle1 && drawShadows; hcCandle1Style.lowStyle.draw = drawCandle1 && drawShadows; // // Default Mid Line Specs ... XDrawSpecifications specs; specs.width = 2; specs.rayRight = false; specs.clr = clrYellow; specs.style = STYLE_DOT; // if (drawCurrent) { // if (!drawAsBox) { // if (drawCandle0) { // DrawCandleAsLine( chartId, subWindow, candle0, candle0Time, candle0Style, c0TagPrefix, update // ); } // if (drawCandle1) { // if (drawCandle1) { // DrawCandleAsLine( chartId, subWindow, candle1, candle1Time, candle1Style, c1TagPrefix, update // ); } } } else { // if (drawCandle0) { // DrawCandleAsBox( chartId, subWindow, candle0, candle0Time, candle0Style, c0TagPrefix, drawBody, drawShadows, update // ); } // if (drawCandle1) { // DrawCandleAsBox( chartId, subWindow, candle1, candle1Time, candle1Style, c1TagPrefix, drawBody, drawShadows, update // ); } } } // if (drawSC) { // if (!drawAsBox) { // if (drawCandle0) { // DrawCandleAsLine( chartId, subWindow, scCandle0, scCandle0Time, scCandle0Style, sc0TagPrefix, update // ); } // if (drawCandle1) { // DrawCandleAsLine( chartId, subWindow, scCandle1, scCandle1Time, scCandle1Style, sc1TagPrefix, update // ); } } else { // if (drawCandle0) { // DrawCandleAsBox( chartId, subWindow, scCandle0, scCandle0Time, scCandle0Style, sc0TagPrefix, drawBody, drawShadows, update // ); } // if (drawCandle1) { // DrawCandleAsBox( chartId, subWindow, scCandle1, scCandle1Time, scCandle1Style, sc1TagPrefix, drawBody, drawShadows, update // ); } } } // if (drawMC) { // if (!drawAsBox) { // if (drawCandle0) { // DrawCandleAsLine( chartId, subWindow, mcCandle0, mcCandle0Time, mcCandle0Style, mc0TagPrefix, update // ); } // if (drawCandle1) { // DrawCandleAsLine( chartId, subWindow, mcCandle1, mcCandle1Time, mcCandle1Style, mc1TagPrefix, update // ); } } else { // if (drawCandle0) { // DrawCandleAsBox( chartId, subWindow, mcCandle0, mcCandle0Time, mcCandle0Style, mc0TagPrefix, drawBody, drawShadows, update // ); } // if (drawCandle1) { // DrawCandleAsBox( chartId, subWindow, mcCandle1, mcCandle1Time, mcCandle1Style, mc1TagPrefix, drawBody, drawShadows, update // ); } } } // if (drawLC) { // if (!drawAsBox) { // if (drawCandle0) { // DrawCandleAsLine( chartId, subWindow, lcCandle0, lcCandle0Time, lcCandle0Style, lc0TagPrefix, update // ); } // if (drawCandle1) { // DrawCandleAsLine( chartId, subWindow, lcCandle1, lcCandle1Time, lcCandle1Style, lc1TagPrefix, update // ); } } else { // if (drawCandle0) { // DrawCandleAsBox( chartId, subWindow, lcCandle0, lcCandle0Time, lcCandle0Style, lc0TagPrefix, drawBody, drawShadows, update // ); } // if (drawCandle1) { // DrawCandleAsBox( chartId, subWindow, lcCandle1, lcCandle1Time, lcCandle1Style, lc1TagPrefix, drawBody, drawShadows, update // ); } } } // if (drawHC) { // if (!drawAsBox) { // if (drawCandle0) { // DrawCandleAsLine( chartId, subWindow, hcCandle0, hcCandle0Time, hcCandle0Style, hc0TagPrefix, update // ); } // if (drawCandle1) { // DrawCandleAsLine( chartId, subWindow, hcCandle1, hcCandle1Time, hcCandle1Style, hc1TagPrefix, update // ); } } else { // if (drawCandle0) { // DrawCandleAsBox( chartId, subWindow, hcCandle0, hcCandle0Time, hcCandle0Style, hc0TagPrefix, drawBody, drawShadows, update // ); } // if (drawCandle1) { // DrawCandleAsBox( chartId, subWindow, hcCandle1, hcCandle1Time, hcCandle1Style, hc1TagPrefix, drawBody, drawShadows, update // ); } } } // // Draw MidLines ... // string candel0MidLineTag = GenerateCandleMidLineTag( candle0, c0TagPrefix // ); string candel1MidLineTag = GenerateCandleMidLineTag( candle1, c1TagPrefix // ); string scCandel0MidLineTag = GenerateCandleMidLineTag( scCandle0, sc0TagPrefix // ); string scCandel1MidLineTag = GenerateCandleMidLineTag( scCandle1, sc1TagPrefix // ); string mcCandel0MidLineTag = GenerateCandleMidLineTag( mcCandle0, mc0TagPrefix // ); string mcCandel1MidLineTag = GenerateCandleMidLineTag( mcCandle1, mc1TagPrefix // ); string lcCandel0MidLineTag = GenerateCandleMidLineTag( lcCandle0, lc0TagPrefix // ); string lcCandel1MidLineTag = GenerateCandleMidLineTag( lcCandle1, lc1TagPrefix // ); string hcCandel0MidLineTag = GenerateCandleMidLineTag( hcCandle0, hc0TagPrefix // ); string hcCandel1MidLineTag = GenerateCandleMidLineTag( hcCandle1, hc1TagPrefix // ); // // Current ... // // Candle 0 ... specs.style = STYLE_DOT; specs.clr = clrTurquoise; DrawCandleMidLine( candle0, candle0Time, specs, candel0MidLineTag, c0TagPrefix, draw0MidLine // ); // // Candle 1 ... specs.width = 2; specs.style = STYLE_SOLID; DrawCandleMidLine( candle1, candle1Time, specs, candel1MidLineTag, c1TagPrefix, draw1MidLine // ); // // Short ... // // Candle 0 ... specs.width = 1; specs.style = STYLE_DOT; specs.clr = clrSpringGreen; DrawCandleMidLine( scCandle0, scCandle0Time, specs, scCandel0MidLineTag, sc0TagPrefix, drawSC0MidLine // ); // // Candle 1 ... specs.width = 2; specs.style = STYLE_SOLID; DrawCandleMidLine( scCandle1, scCandle1Time, specs, scCandel1MidLineTag, sc1TagPrefix, drawSC1MidLine // ); // // Medium ... // // Candle 0 ... specs.width = 1; specs.clr = clrCornflowerBlue; specs.style = STYLE_DOT; DrawCandleMidLine( mcCandle0, mcCandle0Time, specs, mcCandel0MidLineTag, mc0TagPrefix, drawMC0MidLine // ); // // Candle 1 ... specs.width = 2; specs.style = STYLE_SOLID; DrawCandleMidLine( mcCandle1, mcCandle1Time, specs, mcCandel1MidLineTag, mc1TagPrefix, drawMC1MidLine // ); // // Long ... // // Candle 0 ... specs.width = 1; specs.style = STYLE_DOT; specs.clr = clrYellow; DrawCandleMidLine( lcCandle0, lcCandle0Time, specs, lcCandel0MidLineTag, lc0TagPrefix, drawLC0MidLine // ); // // Candle 1 ... specs.width = 2; specs.style = STYLE_SOLID; DrawCandleMidLine( lcCandle1, lcCandle1Time, specs, lcCandel1MidLineTag, lc1TagPrefix, drawLC1MidLine // ); // // Hind ... // // Candle 0 ... specs.clr = clrDarkOrange; specs.width = 1; specs.style = STYLE_DOT; DrawCandleMidLine( hcCandle0, hcCandle0Time, specs, hcCandel0MidLineTag, hc0TagPrefix, drawHC0MidLine // ); // // Candle 1 ... specs.width = 2; specs.style = STYLE_SOLID; DrawCandleMidLine( hcCandle1, hcCandle1Time, specs, hcCandel1MidLineTag, hc1TagPrefix, drawHC1MidLine // ); } // void DrawCandleMidLine( XOHCL &c, // Candle ... const datetime time, // Candle Time ... XDrawSpecifications &specs, // Draw Specifications ... string tag = "", // Tag ... string label = "", // Label ... bool draw = false // Draw MidLine ... ) { // if (!draw) { return; } // long chartId = ChartID(); int subWindow = 0; // double value = (c.open + c.close) / 2; // datetime cTime = TimeCurrent(); // string lblTag = tag + "_lbl"; DrawText( chartId, lblTag, subWindow, cTime, value, label, "Arial", 20, specs.clr, 90, ANCHOR_LEFT_UPPER, specs.back, specs.selection, specs.hidden, specs.zOrder // ); // // Draw ... DrawTrendLine( chartId, tag, subWindow, time, value, cTime, value, specs.clr, specs.style, specs.width, specs.back, specs.selection, specs.rayRight, specs.hidden, specs.zOrder // ); } // string GenerateCandleMidLineTag( XOHCL &c, // Candle ... string tag = "" // Tag ... ) { // double value = (c.open + c.close) / 2; // string result = tag + "_" + "|MID|"; // return result; }