From 714a905a2a570bd2e17ddf17a8dfec3de673ac89 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Sun, 9 Feb 2025 22:51:56 +0330 Subject: [PATCH] add support for xtrend indicator in x121 setup helper ... --- Helpers/x-saherelm.x121.helper.mq5 | 214 ++- Helpers/x-saherelm.x121.xtrend.helper.mq5 | 1212 +++++++++++++++++ Indicators/x-saherelm.x121.xtrend.mq5 | 44 +- X121SMCEA/Experts/x-121.smc.expert.class.mq5 | 6 +- .../Strategy/x-121.smc.strategy.class.mq5 | 11 +- 5 files changed, 1416 insertions(+), 71 deletions(-) create mode 100644 Helpers/x-saherelm.x121.xtrend.helper.mq5 diff --git a/Helpers/x-saherelm.x121.helper.mq5 b/Helpers/x-saherelm.x121.helper.mq5 index 8b1651b9..7f2d2007 100644 --- a/Helpers/x-saherelm.x121.helper.mq5 +++ b/Helpers/x-saherelm.x121.helper.mq5 @@ -23,13 +23,12 @@ // // Imports ... #include "../Helpers/x-saherelm.x121.xatr.helper.mq5" -#include "../Helpers/x-saherelm.x121.xcc.helper.mq5" #include "../Helpers/x-saherelm.x121.xche.helper.mq5" -#include "../Helpers/x-saherelm.x121.xct.helper.mq5" #include "../Helpers/x-saherelm.x121.xdon.helper.mq5" #include "../Helpers/x-saherelm.x121.xhk.helper.mq5" #include "../Helpers/x-saherelm.x121.xpv.helper.mq5" #include "../Helpers/x-saherelm.x121.xstr.helper.mq5" +#include "../Helpers/x-saherelm.x121.xtrend.helper.mq5" #include "../Helpers/x-saherelm.x121.xvwap.helper.mq5" // @@ -39,8 +38,6 @@ struct X121Inputs // // Props ... bool showRSI; - X121XCCInputs xccInputs; - X121XCTInputs xctInputs; X121XPVInputs xpvInputs; X121XHKInputs xhkInputs; X121XATRInputs xatrInputs; @@ -48,6 +45,7 @@ struct X121Inputs X121XCHEInputs xcheInputs; X121XDONInputs xdonInputs; X121XVWAPInputs xvwapInputs; + X121XTRENDInputs xtrendInputs; // // Constructor ... @@ -65,8 +63,6 @@ struct X121Inputs void Clean() { // - xccInputs.Clean(); - xctInputs.Clean(); xpvInputs.Clean(); xhkInputs.Clean(); xatrInputs.Clean(); @@ -74,6 +70,7 @@ struct X121Inputs xcheInputs.Clean(); xdonInputs.Clean(); xvwapInputs.Clean(); + xtrendInputs.Clean(); // ZeroMemory(this); @@ -85,8 +82,6 @@ struct X121Inputs void Default() { // - xccInputs.Default(); - xctInputs.Default(); xpvInputs.Default(); xhkInputs.Default(); xatrInputs.Default(); @@ -94,6 +89,7 @@ struct X121Inputs xcheInputs.Default(); xdonInputs.Default(); xvwapInputs.Default(); + xtrendInputs.Default(); } /** @@ -109,15 +105,14 @@ struct X121Inputs // result = // - xccInputs.IsValid() && - xctInputs.IsValid() && xpvInputs.IsValid() && xhkInputs.IsValid() && xatrInputs.IsValid() && xstrInputs.IsValid() && xcheInputs.IsValid() && xdonInputs.IsValid() && - xvwapInputs.IsValid() + xvwapInputs.IsValid() && + xtrendInputs.IsValid() // ; @@ -136,8 +131,6 @@ struct X121Inputs int result = 0; // - int xccMax = xccInputs.Max(); - int xctMax = xctInputs.Max(); int xpvMax = xpvInputs.Max(); int xhkMax = xpvInputs.Max(); int xatrMax = xatrInputs.Max(); @@ -145,15 +138,16 @@ struct X121Inputs int xcheMax = xcheInputs.Max(); int xdonMax = xdonInputs.Max(); int xvwapMax = xvwapInputs.Max(); + int xtrendMax = xtrendInputs.Max(); // - result = MathMax(xccMax, xctMax); - result = MathMax(result, xpvMax); + result = MathMax(xpvMax, xhkMax); result = MathMax(result, xatrMax); result = MathMax(result, xstrMax); result = MathMax(result, xcheMax); result = MathMax(result, xdonMax); result = MathMax(result, xvwapMax); + result = MathMax(result, xtrendMax); // return result; @@ -172,6 +166,7 @@ struct X121Conditions // // Buffers ... + double maBuffer[]; double rsiBuffer[]; double atrBuffer[]; double strBuffer[]; @@ -190,12 +185,18 @@ struct X121Conditions double lLLBuffer[]; double hHHBuffer[]; double hLLBuffer[]; + double upTBuffer[]; + double adxBuffer[]; + double adxPBuffer[]; + double adxMBuffer[]; long volumeBuffer[]; double peaksBuffer[]; double valesBuffer[]; double strUpBuffer[]; + double downTBuffer[]; double strDownBuffer[]; double vwapMidBuffer[]; + double upTrendBuffer[]; double le1StartBuffer[]; double se1StartBuffer[]; double le2StartBuffer[]; @@ -207,6 +208,7 @@ struct X121Conditions double vwapFastBuffer[]; double vwapSlowBuffer[]; double xhkSMLowBuffer[]; + double downTrendBuffer[]; double vwapPriceBuffer[]; double xhkRawLowBuffer[]; double xhkSMOpenBuffer[]; @@ -242,6 +244,15 @@ struct X121Conditions // // Conditions ... + // + // XTrend ... + + // + bool isTrendBullish; + bool isTrendBearish; + bool isTrendSwitchedToBullish; + bool isTrendSwitchedToBearish; + // bool isRsiSlopeBullish; bool isStrSlopeBullish; @@ -601,6 +612,7 @@ struct X121Conditions // Buffers ... // + Clean(maBuffer); Clean(rsiBuffer); Clean(atrBuffer); Clean(strBuffer); @@ -619,12 +631,18 @@ struct X121Conditions Clean(lLLBuffer); Clean(hHHBuffer); Clean(hLLBuffer); + Clean(upTBuffer); + Clean(adxBuffer); + Clean(adxPBuffer); + Clean(adxMBuffer); Clean(volumeBuffer); Clean(peaksBuffer); Clean(valesBuffer); Clean(strUpBuffer); + Clean(downTBuffer); Clean(strDownBuffer); Clean(vwapMidBuffer); + Clean(upTrendBuffer); Clean(le1StartBuffer); Clean(se1StartBuffer); Clean(le2StartBuffer); @@ -636,6 +654,7 @@ struct X121Conditions Clean(vwapFastBuffer); Clean(vwapSlowBuffer); Clean(xhkSMLowBuffer); + Clean(downTrendBuffer); Clean(vwapPriceBuffer); Clean(xhkRawLowBuffer); Clean(xhkSMOpenBuffer); @@ -660,6 +679,7 @@ struct X121Conditions Clean(donCloseLowerBuffer); // + ArraySetAsSeries(maBuffer, true); ArraySetAsSeries(rsiBuffer, true); ArraySetAsSeries(atrBuffer, true); ArraySetAsSeries(strBuffer, true); @@ -678,12 +698,18 @@ struct X121Conditions ArraySetAsSeries(lLLBuffer, true); ArraySetAsSeries(hHHBuffer, true); ArraySetAsSeries(hLLBuffer, true); + ArraySetAsSeries(upTBuffer, true); + ArraySetAsSeries(adxBuffer, true); + ArraySetAsSeries(adxPBuffer, true); + ArraySetAsSeries(adxMBuffer, true); ArraySetAsSeries(volumeBuffer, true); ArraySetAsSeries(peaksBuffer, true); ArraySetAsSeries(valesBuffer, true); ArraySetAsSeries(strUpBuffer, true); + ArraySetAsSeries(downTBuffer, true); ArraySetAsSeries(strDownBuffer, true); ArraySetAsSeries(vwapMidBuffer, true); + ArraySetAsSeries(upTrendBuffer, true); ArraySetAsSeries(le1StartBuffer, true); ArraySetAsSeries(se1StartBuffer, true); ArraySetAsSeries(le2StartBuffer, true); @@ -695,6 +721,7 @@ struct X121Conditions ArraySetAsSeries(vwapFastBuffer, true); ArraySetAsSeries(vwapSlowBuffer, true); ArraySetAsSeries(xhkSMLowBuffer, true); + ArraySetAsSeries(downTrendBuffer, true); ArraySetAsSeries(vwapPriceBuffer, true); ArraySetAsSeries(xhkRawLowBuffer, true); ArraySetAsSeries(xhkSMOpenBuffer, true); @@ -1205,6 +1232,14 @@ struct X121Conditions ToString("isSarSwitchedToBearish", isSarSwitchedToBearish, ignoreFalseConditions, separator) + // "-----------------------" + separator + + "Trend ..." + separator + + "-----------------------" + separator + + ToString("isTrendBullish", isTrendBullish, ignoreFalseConditions, separator) + + ToString("isTrendBearish", isTrendBearish, ignoreFalseConditions, separator) + + ToString("isTrendSwitchedToBullish", isTrendSwitchedToBullish, ignoreFalseConditions, separator) + + ToString("isTrendSwitchedToBearish", isTrendSwitchedToBearish, ignoreFalseConditions, separator) + + // + "-----------------------" + separator + "STR ..." + separator + "-----------------------" + separator + ToString("isStrBullish", isStrBullish, ignoreFalseConditions, separator) + @@ -1502,8 +1537,6 @@ class XCX121Helper : public XCBase // // Helpers ... - XCX121XCCHelper *xccHelper; - XCX121XCTHelper *xctHelper; XCX121XPVHelper *xpvHelper; XCX121XHKHelper *xhkHelper; XCX121XATRHelper *xatrHelper; @@ -1511,6 +1544,7 @@ class XCX121Helper : public XCBase XCX121XCHEHelper *xcheHelper; XCX121XDONHelper *xdonHelper; XCX121XVWAPHelper *xvwapHelper; + XCX121XTRENDHelper *xtrendHelper; // // Constructors ... @@ -1523,8 +1557,6 @@ class XCX121Helper : public XCBase ~XCX121Helper() { // - delete xccHelper; - delete xctHelper; delete xpvHelper; delete xhkHelper; delete xatrHelper; @@ -1532,10 +1564,9 @@ class XCX121Helper : public XCBase delete xcheHelper; delete xdonHelper; delete xvwapHelper; + delete xtrendHelper; // - ZeroMemory(xccHelper); - ZeroMemory(xctHelper); ZeroMemory(xpvHelper); ZeroMemory(xhkHelper); ZeroMemory(xatrHelper); @@ -1543,6 +1574,7 @@ class XCX121Helper : public XCBase ZeroMemory(xcheHelper); ZeroMemory(xdonHelper); ZeroMemory(xvwapHelper); + ZeroMemory(xtrendHelper); } // @@ -1597,8 +1629,6 @@ class XCX121Helper : public XCBase // mInputs = inputs; - xccInputs = inputs.xccInputs; - xctInputs = inputs.xctInputs; xpvInputs = inputs.xpvInputs; xhkInputs = inputs.xhkInputs; xatrInputs = inputs.xatrInputs; @@ -1606,36 +1636,11 @@ class XCX121Helper : public XCBase xcheInputs = inputs.xcheInputs; xdonInputs = inputs.xdonInputs; xvwapInputs = inputs.xvwapInputs; + xtrendInputs = inputs.xtrendInputs; // // Initial Helpers ... - // - // XCC ... - xccHelper = new XCX121XCCHelper(); - result = xccHelper.Init( - symbol, - period, - xccInputs // - ); - if (!result) - { - return result; - } - - // - // XCT ... - xctHelper = new XCX121XCTHelper(); - result = xctHelper.Init( - symbol, - period, - xctInputs // - ); - if (!result) - { - return result; - } - // // XPV ... xpvHelper = new XCX121XPVHelper(); @@ -1727,6 +1732,19 @@ class XCX121Helper : public XCBase return result; } + // + // XTREND ... + xtrendHelper = new XCX121XTRENDHelper(); + result = xtrendHelper.Init( + symbol, + period, + xtrendInputs // + ); + if (!result) + { + return result; + } + // if (inputs.showRSI) { @@ -1907,6 +1925,28 @@ class XCX121Helper : public XCBase return result; } + // + // XTREND ... + bool GetTrendConditions( + X121XTRENDConditions &conditions, // + int barIndex = 0, // + int loopback = 5 // + ) + { + // + bool result = false; + + // + result = xtrendHelper.GetConditions( + conditions, + barIndex, + loopback // + ); + + // + return result; + } + // bool GetConditions( X121Conditions &conditions, // @@ -2064,6 +2104,19 @@ class XCX121Helper : public XCBase return result; } + // + // XTREND ... + X121XTRENDConditions xtrendConditions; + result = GetTrendConditions( + xtrendConditions, + barIndex, + loopback // + ); + if (!result) + { + return result; + } + // // Fill Volume Buffer ... for (int i = barIndex; i < barIndex + loopback; i++) @@ -2275,6 +2328,66 @@ class XCX121Helper : public XCBase conditions.isSMHKSwitchedToBullish = xhkConditions.isSMHKSwitchedToBullish; conditions.isRawHKSwitchedToBullish = xhkConditions.isRawHKSwitchedToBullish; + // + // XTREND ... + + // + // Buffers ... + + // + Copy( + xtrendConditions.upTrendBuffer, + conditions.upTrendBuffer // + ); + + // + Copy( + xtrendConditions.downTrendBuffer, + conditions.downTrendBuffer // + ); + + // + Copy( + xtrendConditions.maBuffer, + conditions.maBuffer // + ); + + // + Copy( + xtrendConditions.adxBuffer, + conditions.adxBuffer // + ); + + // + Copy( + xtrendConditions.adxPBuffer, + conditions.adxPBuffer // + ); + + // + Copy( + xtrendConditions.adxMBuffer, + conditions.adxMBuffer // + ); + + // + Copy( + xtrendConditions.upTBuffer, + conditions.upTBuffer // + ); + + // + Copy( + xtrendConditions.downTBuffer, + conditions.downTBuffer // + ); + + // + conditions.isTrendBullish = xtrendConditions.isTrendBullish; + conditions.isTrendBearish = xtrendConditions.isTrendBearish; + conditions.isTrendSwitchedToBullish = xtrendConditions.isTrendSwitchedToBullish; + conditions.isTrendSwitchedToBearish = xtrendConditions.isTrendSwitchedToBearish; + // // XATR ... @@ -3224,8 +3337,6 @@ class XCX121Helper : public XCBase // // Inputs ... X121Inputs mInputs; - X121XCCInputs xccInputs; - X121XCTInputs xctInputs; X121XPVInputs xpvInputs; X121XHKInputs xhkInputs; X121XATRInputs xatrInputs; @@ -3233,6 +3344,7 @@ class XCX121Helper : public XCBase X121XSTRInputs xstrInputs; X121XCHEInputs xcheInputs; X121XVWAPInputs xvwapInputs; + X121XTRENDInputs xtrendInputs; // int mRSIHandler; diff --git a/Helpers/x-saherelm.x121.xtrend.helper.mq5 b/Helpers/x-saherelm.x121.xtrend.helper.mq5 new file mode 100644 index 00000000..0eac9dfa --- /dev/null +++ b/Helpers/x-saherelm.x121.xtrend.helper.mq5 @@ -0,0 +1,1212 @@ +/////////////////////////////////////////////////////// +// +// SaherElm IT Center MQL5 Helper Class Library +// ---------------------------------------------- +// Name: XCX121XTRENDHelper +// Description: provides all Indicator +// Helper requirements ... +// +// +// Maintainer: +// ------------ +// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com) +// +////////////////////////////////////////////////////// +// +// Global Properties ... +#property library +#property copyright "Copyright 2023, SaherElm IT Center" +#property link "https://www.saherelm.ir" +#property version "1.00" +#property strict + +// +// Imports ... +#include "../Classes/x-saherelm.x-helper.class.mq5" + +// +// Definitions ... + +// +// Definitions ... +enum ENUM_X121_XTREND_BUFFERS +{ + // + X121_XTREND_UP_LINE = 0, + X121_XTREND_DOWN_LINE = 1, + X121_XTREND_MA_LINE = 2, + X121_XTREND_ADX_LINE = 3, + X121_XTREND_ADX_P_LINE = 4, + X121_XTREND_ADX_M_LINE = 5, + X121_XTREND_UP_T_LINE = 6, + X121_XTREND_DOWN_T_LINE = 7, +}; + +// +// Input Models ... +struct X121XTRENDInputs +{ + // + // Props ... + // + // Market ... + ENUM_APPLIED_PRICE trendPriceType; // Detect Trend Price Type + + // + // MA Detection ... + int maLength; // MA Length + ENUM_MA_METHOD maMethod; // MA Method + ENUM_APPLIED_PRICE maAppliedTo; // MA Applied To + + // + // ADX Detection ... + int adxLength; // ADX Length + double adxThreshold; // ADX Threshold for Strong Trends + + // + // Presentation ... + + // + int startCalculationForLastBars; // Calculate Last n Bars + int upTrendArrowCode; // Up Trend Arrow Code + int downTrendArrowCode; // Down Trend Arrow Code + + // + bool showOnlyTrendStart; // Show Only Trend Start + bool showUpTrend; // Show Up Trend + bool showDownTrend; // Show Down Trend + + // + // Constructor(s) ... + X121XTRENDInputs() + { + Clean(); + } + + // + // Tools ... + + // + // Clean ... + void Clean() + { + // + trendPriceType = PRICE_CLOSE; + maLength = 50; + maMethod = MODE_EMA; + maAppliedTo = PRICE_CLOSE; + adxLength = 14; + adxThreshold = 25; + startCalculationForLastBars = 1000; + upTrendArrowCode = 233; + downTrendArrowCode = 234; + showOnlyTrendStart = false; + showUpTrend = false; + showDownTrend = false; + + // + ZeroMemory(this); + } + + // + // Default ... + void Default() + { + // + trendPriceType = PRICE_CLOSE; + maLength = 50; + maMethod = MODE_EMA; + maAppliedTo = PRICE_CLOSE; + adxLength = 14; + adxThreshold = 25; + startCalculationForLastBars = 1000; + upTrendArrowCode = 233; + downTrendArrowCode = 234; + showOnlyTrendStart = true; + showUpTrend = true; + showDownTrend = true; + } + + // + // Validate ... + bool IsValid() + { + // + bool result = false; + + // + result = + // + // MA ... + maLength > 0 && + // + // ADX ... + adxLength > 0 && + adxThreshold > 0 + // + ; + + // + return result; + } + + // + // Retrieve MAx Length ... + int Max() + { + // + int result = 0; + + // + result = MathMax(maLength, adxLength); + + // + return result; + } +}; + +// +// Conditions ... +struct X121XTRENDConditions +{ + // + // Common ... + string symbol; + ENUM_TIMEFRAMES period; + datetime time; + + // + // Buffers ... + double upTrendBuffer[]; + double downTrendBuffer[]; + double maBuffer[]; + double adxBuffer[]; + double adxPBuffer[]; + double adxMBuffer[]; + double upTBuffer[]; + double downTBuffer[]; + + // + // Conditions ... + + // + bool isTrendBullish; + bool isTrendBearish; + + // + bool isTrendSwitchedToBullish; + bool isTrendSwitchedToBearish; + + // + // bool isCloseLower + + // + // Constructor ... + X121XTRENDConditions() + { + Clean(); + } + + // + // Tools ... + + /** + * Cleaning Up ... + */ + void Clean() + { + // + // Commons ... + symbol = NULL; + period = NULL; + time = NULL; + + // + // Buffers ... + + // + Clean(upTrendBuffer); + Clean(downTrendBuffer); + Clean(maBuffer); + Clean(adxBuffer); + Clean(adxPBuffer); + Clean(adxMBuffer); + Clean(upTBuffer); + Clean(downTBuffer); + + // + ArraySetAsSeries(upTrendBuffer, true); + ArraySetAsSeries(downTrendBuffer, true); + ArraySetAsSeries(maBuffer, true); + ArraySetAsSeries(adxBuffer, true); + ArraySetAsSeries(adxPBuffer, true); + ArraySetAsSeries(adxMBuffer, true); + ArraySetAsSeries(upTBuffer, true); + ArraySetAsSeries(downTBuffer, true); + + // + // Conditions ... + + // + isTrendBullish = false; + isTrendBearish = false; + + // + isTrendSwitchedToBullish = false; + isTrendSwitchedToBearish = false; + + // + ZeroMemory(this); + } + + /** + * Generate Conditions Scores ... + * + * @param bullishScore: Double, Directional Scores Reference ... + * @param bearishScore: Double, Directional Scores Reference ... + */ + void GenerateScore( + double &bullishScore, + double &bearishScore // + ) + { + // + bullishScore = 0; + bearishScore = 0; + + // + // TODO: Implement if Required ... + } + + /** + * Generate Summary String for Represent Conditions State ... + * + * @param onlyCommons: Boolean, Just Generate Only Commons Conditions ... + * @param onlyConditions: Boolean, Just Generate Only Conditions ... + * @param includeScores: Boolean, Attach Scores Representations on Result ... + * @param ignoreFalseConditions: Boolean, Ignore False Conditions on Result ... + * @param separator: String, Separate Lines ... + * + * @return ( string ) + */ + string GenerateSummary( + bool onlyCommons = false, + bool onlyConditions = false, + bool includeScores = true, + bool ignoreFalseConditions = true, + string separator = "\n" // + ) + { + // + string result = NULL; + + // + double bullishScore = 0; + double bearishScore = 0; + GenerateScore( + bullishScore, + bearishScore // + ); + + // + string scoresStr = + // + "Scores: " + separator + + "---------------" + separator + + "Bullish: " + ToString(bullishScore) + separator + + "Bearish: " + ToString(bearishScore) + separator + + "" + // + ; + + // + string commonStr = GenerateSpecifiedCommonSummary( + this, + separator, + includeScores // + ); + + // + string conditionsStr = + // + ToString("isTrendBullish", isTrendBullish, ignoreFalseConditions, separator) + + ToString("isTrendBearish", isTrendBearish, ignoreFalseConditions, separator) + + ToString("isTrendSwitchedToBullish", isTrendSwitchedToBullish, ignoreFalseConditions, separator) + + ToString("isTrendSwitchedToBearish", isTrendSwitchedToBearish, ignoreFalseConditions, separator) + + // + "" + // + ; + + // + result = + // + "[" + GetTag() + "]" + separator + + (onlyConditions + ? "" + : commonStr) + + (!includeScores + ? "" + : scoresStr) + + " " + separator + + (onlyCommons + ? "" + : conditionsStr) + + "" + // + ; + + // + return result; + } + + /** + * Retrieve nique Tag Identifier ... + * + * @return ( string ) + */ + string GetTag() + { + return GetTypeName(this); + } + + // +}; + +// +// Class ... +class XCX121XTRENDHelper : public XCBaseHelper +{ + // + // Public ... + public: + // + // Props ... + + // + // Constructors ... + XCX121XTRENDHelper() + : XCBaseHelper(_Symbol, _Period) + { + } + + // + // Deconstructor ... + ~XCX121XTRENDHelper() + { + // + mInputs.Clean(); + + // + Clean(upTrendBuffer); + Clean(downTrendBuffer); + Clean(maBuffer); + Clean(adxBuffer); + Clean(adxPBuffer); + Clean(adxMBuffer); + Clean(upTBuffer); + Clean(downTBuffer); + } + + // + // Tools ... + bool Init( + string symbol, // Trading Symbol + ENUM_TIMEFRAMES period, // Trading Period + X121XTRENDInputs &inputs // Inputs + ) + { + // + bool result = false; + + // + mSymbol = symbol; + mPeriod = period; + + // + result = inputs.IsValid(); + if (!result) + { + return result; + } + + // + ArraySetAsSeries(upTrendBuffer, true); + ArraySetAsSeries(downTrendBuffer, true); + ArraySetAsSeries(maBuffer, true); + ArraySetAsSeries(adxBuffer, true); + ArraySetAsSeries(adxPBuffer, true); + ArraySetAsSeries(adxMBuffer, true); + ArraySetAsSeries(upTBuffer, true); + ArraySetAsSeries(downTBuffer, true); + + // + mInputs = inputs; + + // + mHandler = iCustom( + mSymbol, + mPeriod, + "x-saherelm.x121.xtrend", + // + // Inputs ... + // + // Market ... + "", + mInputs.trendPriceType, + // + // MA Detection ... + "", + mInputs.maLength, + mInputs.maMethod, + mInputs.maAppliedTo, + // + // ADX Detection ... + "", + mInputs.adxLength, + mInputs.adxThreshold, + // + // Presentation ... + "", + mInputs.startCalculationForLastBars, + mInputs.upTrendArrowCode, + mInputs.downTrendArrowCode, + mInputs.showOnlyTrendStart, + mInputs.showUpTrend, + mInputs.showDownTrend + // + ); + result = mHandler != INVALID_HANDLE; + if (!result) + { + return result; + } + + // + return result; + } + + // + // Inputs ... + + // + X121XTRENDInputs GetInputs() + { + return mInputs; + } + + // + bool SetInputs( + X121XTRENDInputs &inputs // Configs + ) + { + // + return Init( + mSymbol, + mPeriod, + inputs + // + ); + } + + // + // Readers ... + + // + // Up Trend ... + + // + double GetUpTrend( + int barIndex // Bar Index + ) + { + // + if (barIndex < 0) + { + barIndex = 0; + } + + // + Calculate(); + + // + int count = ArraySize(upTrendBuffer); + if (barIndex >= count) + { + barIndex = count - 1; + } + + // + return upTrendBuffer[barIndex]; + } + + // + int CopyUpTrend( + int start, // Start + int count, // Number of Items for read + double &buffer[], // Dest Buffer + bool forceClean = true // Force To Clean buffer + ) + { + // + // Update Calculations ... + Calculate(); + + // + // Copy Items ... + return Copy( + start, + count, + upTrendBuffer, + buffer, + forceClean + // + ); + } + + // + // Down Trend ... + + // + double GetDownTrend( + int barIndex // Bar Index + ) + { + // + if (barIndex < 0) + { + barIndex = 0; + } + + // + Calculate(); + + // + int count = ArraySize(downTrendBuffer); + if (barIndex >= count) + { + barIndex = count - 1; + } + + // + return downTrendBuffer[barIndex]; + } + + // + int CopyDownTrend( + int start, // Start + int count, // Number of Items for read + double &buffer[], // Dest Buffer + bool forceClean = true // Force To Clean buffer + ) + { + // + // Update Calculations ... + Calculate(); + + // + // Copy Items ... + return Copy( + start, + count, + downTrendBuffer, + buffer, + forceClean + // + ); + } + + // + // Ma ... + + // + double GetMA( + int barIndex // Bar Index + ) + { + // + if (barIndex < 0) + { + barIndex = 0; + } + + // + Calculate(); + + // + int count = ArraySize(downTrendBuffer); + if (barIndex >= count) + { + barIndex = count - 1; + } + + // + return downTrendBuffer[barIndex]; + } + + // + int CopyMA( + int start, // Start + int count, // Number of Items for read + double &buffer[], // Dest Buffer + bool forceClean = true // Force To Clean buffer + ) + { + // + // Update Calculations ... + Calculate(); + + // + // Copy Items ... + return Copy( + start, + count, + downTrendBuffer, + buffer, + forceClean + // + ); + } + + // + // ADX ... + + // + double GetADX( + int barIndex // Bar Index + ) + { + // + if (barIndex < 0) + { + barIndex = 0; + } + + // + Calculate(); + + // + int count = ArraySize(adxBuffer); + if (barIndex >= count) + { + barIndex = count - 1; + } + + // + return adxBuffer[barIndex]; + } + + // + int CopyADX( + int start, // Start + int count, // Number of Items for read + double &buffer[], // Dest Buffer + bool forceClean = true // Force To Clean buffer + ) + { + // + // Update Calculations ... + Calculate(); + + // + // Copy Items ... + return Copy( + start, + count, + adxBuffer, + buffer, + forceClean + // + ); + } + + // + // ADX P... + + // + double GetADXP( + int barIndex // Bar Index + ) + { + // + if (barIndex < 0) + { + barIndex = 0; + } + + // + Calculate(); + + // + int count = ArraySize(adxPBuffer); + if (barIndex >= count) + { + barIndex = count - 1; + } + + // + return adxPBuffer[barIndex]; + } + + // + int CopyADXP( + int start, // Start + int count, // Number of Items for read + double &buffer[], // Dest Buffer + bool forceClean = true // Force To Clean buffer + ) + { + // + // Update Calculations ... + Calculate(); + + // + // Copy Items ... + return Copy( + start, + count, + adxPBuffer, + buffer, + forceClean + // + ); + } + + // + // ADX M ... + + // + double GetADXM( + int barIndex // Bar Index + ) + { + // + if (barIndex < 0) + { + barIndex = 0; + } + + // + Calculate(); + + // + int count = ArraySize(adxMBuffer); + if (barIndex >= count) + { + barIndex = count - 1; + } + + // + return adxMBuffer[barIndex]; + } + + // + int CopyADXM( + int start, // Start + int count, // Number of Items for read + double &buffer[], // Dest Buffer + bool forceClean = true // Force To Clean buffer + ) + { + // + // Update Calculations ... + Calculate(); + + // + // Copy Items ... + return Copy( + start, + count, + adxMBuffer, + buffer, + forceClean + // + ); + } + + // + // Up Trend ... + + // + double GetUpT( + int barIndex // Bar Index + ) + { + // + if (barIndex < 0) + { + barIndex = 0; + } + + // + Calculate(); + + // + int count = ArraySize(upTBuffer); + if (barIndex >= count) + { + barIndex = count - 1; + } + + // + return upTBuffer[barIndex]; + } + + // + int CopyUpT( + int start, // Start + int count, // Number of Items for read + double &buffer[], // Dest Buffer + bool forceClean = true // Force To Clean buffer + ) + { + // + // Update Calculations ... + Calculate(); + + // + // Copy Items ... + return Copy( + start, + count, + upTBuffer, + buffer, + forceClean + // + ); + } + + // + // Down Trend ... + + // + double GetDownT( + int barIndex // Bar Index + ) + { + // + if (barIndex < 0) + { + barIndex = 0; + } + + // + Calculate(); + + // + int count = ArraySize(downTBuffer); + if (barIndex >= count) + { + barIndex = count - 1; + } + + // + return downTBuffer[barIndex]; + } + + // + int CopyDownT( + int start, // Start + int count, // Number of Items for read + double &buffer[], // Dest Buffer + bool forceClean = true // Force To Clean buffer + ) + { + // + // Update Calculations ... + Calculate(); + + // + // Copy Items ... + return Copy( + start, + count, + downTBuffer, + buffer, + forceClean + // + ); + } + + // + bool GetConditions( + X121XTRENDConditions &conditions, // + int barIndex = 0, // + int loopback = 5 // + ) + { + // + bool result = true; + + // + if (loopback < 5) + { + loopback = 5; + } + + // + conditions.Clean(); + + // + conditions.symbol = mSymbol; + conditions.period = mPeriod; + conditions.time = TimeCurrent(); + + // + int zIndex = barIndex; + int cIndex = zIndex + 1; + int pIndex = cIndex + 1; + int ppIndex = pIndex + 1; + + // + XOHCL zBar; + result = zBar.Init( + mSymbol, + mPeriod, + zIndex // + ); + if (!result) + { + return result; + } + + // + XOHCL cBar; + result = cBar.Init( + mSymbol, + mPeriod, + cIndex // + ); + if (!result) + { + return result; + } + + // + XOHCL pBar; + result = pBar.Init( + mSymbol, + mPeriod, + pIndex // + ); + if (!result) + { + return result; + } + + // + // Buffers ... + + // + CopyUpTrend( + zIndex, + loopback, + conditions.upTrendBuffer // + ); + + // + CopyDownTrend( + zIndex, + loopback, + conditions.downTrendBuffer // + ); + + // + CopyMA( + zIndex, + loopback, + conditions.maBuffer // + ); + + // + CopyADX( + zIndex, + loopback, + conditions.adxBuffer // + ); + + // + CopyADXP( + zIndex, + loopback, + conditions.adxPBuffer // + ); + + // + CopyADXM( + zIndex, + loopback, + conditions.adxMBuffer // + ); + + // + CopyUpT( + zIndex, + loopback, + conditions.upTBuffer // + ); + + // + CopyDownT( + zIndex, + loopback, + conditions.downTBuffer // + ); + + // + // Conditions ... + + // + int cIDX = 1; + int pIDX = cIDX + 1; + + // + bool isTrendBullish = upTBuffer[cIDX] != 0; + bool isTrendBullishPrev = upTBuffer[pIDX] != 0; + + // + bool isTrendBearish = downTBuffer[cIDX] != 0; + bool isTrendBearishPrev = downTBuffer[pIDX] != 0; + + // + bool isTrendSwitchedToBullish = isTrendBullish && + !isTrendBullishPrev; + bool isTrendSwitchedToBearish = isTrendBearish && + !isTrendBearishPrev; + + // + conditions.isTrendBullish = isTrendBullish; + conditions.isTrendBearish = isTrendBearish; + conditions.isTrendSwitchedToBullish = isTrendSwitchedToBullish; + conditions.isTrendSwitchedToBearish = isTrendSwitchedToBearish; + + // + return result; + } + + // + // Protected ... + protected: + // + // Private ... + private: + // + // Props ... + X121XTRENDInputs mInputs; // Inputs ... + + // + // Buffers ... + double upTrendBuffer[]; + double downTrendBuffer[]; + double maBuffer[]; + double adxBuffer[]; + double adxPBuffer[]; + double adxMBuffer[]; + double upTBuffer[]; + double downTBuffer[]; + + // + void Calculate() + { + // + int totalBars = CountBars(); + if (totalBars > 1000) + { + totalBars = 1000; + } + + // + // Buffers ... + + // + // Up Trend ... + CopyBuffer( + mHandler, + X121_XTREND_UP_LINE, + 0, + totalBars, + upTrendBuffer + // + ); + + // + // Down Trend ... + CopyBuffer( + mHandler, + X121_XTREND_DOWN_LINE, + 0, + totalBars, + downTrendBuffer + // + ); + + // + // MA ... + CopyBuffer( + mHandler, + X121_XTREND_MA_LINE, + 0, + totalBars, + maBuffer + // + ); + + // + // ADX ... + CopyBuffer( + mHandler, + X121_XTREND_ADX_LINE, + 0, + totalBars, + adxBuffer + // + ); + + // + // ADX P ... + CopyBuffer( + mHandler, + X121_XTREND_ADX_P_LINE, + 0, + totalBars, + adxPBuffer + // + ); + + // + // ADX M ... + CopyBuffer( + mHandler, + X121_XTREND_ADX_M_LINE, + 0, + totalBars, + adxMBuffer + // + ); + + // + // Up T ... + CopyBuffer( + mHandler, + X121_XTREND_UP_T_LINE, + 0, + totalBars, + upTBuffer + // + ); + + // + // Down T ... + CopyBuffer( + mHandler, + X121_XTREND_DOWN_T_LINE, + 0, + totalBars, + downTBuffer + // + ); + } +}; + +// +// Tools ... \ No newline at end of file diff --git a/Indicators/x-saherelm.x121.xtrend.mq5 b/Indicators/x-saherelm.x121.xtrend.mq5 index 7d923f3a..a9925c40 100644 --- a/Indicators/x-saherelm.x121.xtrend.mq5 +++ b/Indicators/x-saherelm.x121.xtrend.mq5 @@ -39,9 +39,9 @@ input group "Market"; input ENUM_APPLIED_PRICE trendPriceType = PRICE_CLOSE; // Detect Trend Price Type input group "MA Detection"; -input int maLength = 50; // MA Length -input ENUM_MA_METHOD maMethod = MODE_EMA; // MA Method -input ENUM_APPLIED_PRICE maAppliedTo; // MA Applied To +input int maLength = 50; // MA Length +input ENUM_MA_METHOD maMethod = MODE_EMA; // MA Method +input ENUM_APPLIED_PRICE maAppliedTo = PRICE_CLOSE; // MA Applied To input group "ADX Detection"; input int adxLength = 14; // ADX Length @@ -65,7 +65,7 @@ input bool showDownTrend = true; // Show Down Trend #property indicator_chart_window // -#property indicator_buffers 6 +#property indicator_buffers 8 #property indicator_plots 2 // @@ -113,6 +113,14 @@ double adxPBuffer[]; #define adxMBufferIndex mLastBufferIndex + 4 double adxMBuffer[]; +// +#define upTBufferIndex mLastBufferIndex + 5 +double upTBuffer[]; + +// +#define downTBufferIndex mLastBufferIndex + 6 +double downTBuffer[]; + // // Variables, Properties and etc ... @@ -469,6 +477,14 @@ void DefineBuffers() // ArraySetAsSeries(adxMBuffer, true); SetIndexBuffer(adxMBufferIndex, adxMBuffer, INDICATOR_CALCULATIONS); + + // + ArraySetAsSeries(upTBuffer, true); + SetIndexBuffer(upTBufferIndex, upTBuffer, INDICATOR_CALCULATIONS); + + // + ArraySetAsSeries(downTBuffer, true); + SetIndexBuffer(downTBufferIndex, downTBuffer, INDICATOR_CALCULATIONS); } /** @@ -602,18 +618,21 @@ void CalculateTrend( // iValue = iLastValue; // } - // // - // double iValue = low[bar_index] - points10; + // + double selectedValue = low[bar_index] - points10; // double iValue = - !IsBullish(lastTrendDir) ? low[bar_index] - points10 + !IsBullish(lastTrendDir) ? selectedValue : !showOnlyTrendStart ? lastValue : 0; // upTrendBuffer[bar_index] = iValue; + upTBuffer[bar_index] = selectedValue; + + // downTrendBuffer[bar_index] = 0; // @@ -633,12 +652,12 @@ void CalculateTrend( // iValue = iLastValue; // } - // // - // double iValue = high[bar_index] + points10; + // + double selectedValue = high[bar_index] + points10; // double iValue = - !IsBearish(lastTrendDir) ? high[bar_index] + points10 + !IsBearish(lastTrendDir) ? selectedValue : !showOnlyTrendStart ? lastValue : 0; @@ -646,6 +665,7 @@ void CalculateTrend( // upTrendBuffer[bar_index] = 0; downTrendBuffer[bar_index] = iValue; + downTBuffer[bar_index] = selectedValue; // lastValue = iValue; @@ -669,6 +689,10 @@ void CalculateTrend( upTrendBuffer[bar_index] = 0; downTrendBuffer[bar_index] = 0; + // + upTBuffer[bar_index] = 0; + downTBuffer[bar_index] = 0; + // lastValue = 0; lastTrendDir = X_DIRECTION_NONE; diff --git a/X121SMCEA/Experts/x-121.smc.expert.class.mq5 b/X121SMCEA/Experts/x-121.smc.expert.class.mq5 index fa94300e..15c3bfa3 100644 --- a/X121SMCEA/Experts/x-121.smc.expert.class.mq5 +++ b/X121SMCEA/Experts/x-121.smc.expert.class.mq5 @@ -22,7 +22,7 @@ // // Imports ... #include "../../Classes/x-saherelm.x-expert.class.mq5" -#include "../../Helpers/x-saherelm.x121.xatr.helper.mq5" +#include "../../Helpers/x-saherelm.x121.xct.helper.mq5" #include "../../Helpers/x-saherelm.x121.xcc.helper.mq5" #include "../Classes/x-121.smc.x-tradehandler.class.mq5" #include "../Libraries/x-121.smc.lib.mq5" @@ -1341,8 +1341,8 @@ class XC121SMCExpert : public XCBaseExpert ZeroMemory(mTradeHandler); // - delet xccHelper; - delet xctHelper; + delete xccHelper; + delete xctHelper; delete mTradeHandler; // diff --git a/X121SMCEA/Strategy/x-121.smc.strategy.class.mq5 b/X121SMCEA/Strategy/x-121.smc.strategy.class.mq5 index 1d4d1fe9..8b1c4138 100644 --- a/X121SMCEA/Strategy/x-121.smc.strategy.class.mq5 +++ b/X121SMCEA/Strategy/x-121.smc.strategy.class.mq5 @@ -758,13 +758,10 @@ class XCX121SMCStrategy : public XCX121SMCBaseStrategy x121Inputs.xatrInputs.atrSmoothingMode = X_MA_MODE_EMA; // - // XCC ... - x121Inputs.xccInputs.showCandles = false; // false; - - // - // XCT ... - x121Inputs.xctInputs.clr = clrBlueViolet; - x121Inputs.xctInputs.showCandleTime = false; // false; + // XTREND ... + x121Inputs.xtrendInputs.showUpTrend = showAll || false; + x121Inputs.xtrendInputs.showDownTrend = showAll || false; + x121Inputs.xtrendInputs.showOnlyTrendStart = showAll || false; // // XPV ...