From 476bcaece8ca824fad8a1cb8d6d291875cb81cd6 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Wed, 26 Feb 2025 19:19:50 +0330 Subject: [PATCH] last ... --- Helpers/x-saherelm.x121.helper.mq5 | 338 ++++- Helpers/x-saherelm.x121.x3ma.helper.mq5 | 1339 +++++++++++++++++ Indicators/x-saherelm.x121.x3ma.mq5 | 658 ++++++++ .../Signals/x-121.smc.xwz.signal.class.mq5 | 8 + .../Signals/x-121.smc.xwz.signal.lib.mq5 | 247 ++- 5 files changed, 2501 insertions(+), 89 deletions(-) create mode 100644 Helpers/x-saherelm.x121.x3ma.helper.mq5 create mode 100644 Indicators/x-saherelm.x121.x3ma.mq5 diff --git a/Helpers/x-saherelm.x121.helper.mq5 b/Helpers/x-saherelm.x121.helper.mq5 index 4b83e11e..8862d4eb 100644 --- a/Helpers/x-saherelm.x121.helper.mq5 +++ b/Helpers/x-saherelm.x121.helper.mq5 @@ -22,6 +22,7 @@ // // Imports ... +#include "../Helpers/x-saherelm.x121.x3ma.helper.mq5" #include "../Helpers/x-saherelm.x121.xatr.helper.mq5" #include "../Helpers/x-saherelm.x121.xche.helper.mq5" #include "../Helpers/x-saherelm.x121.xdon.helper.mq5" @@ -44,6 +45,7 @@ struct X121Inputs X121XSTRInputs xstrInputs; X121XCHEInputs xcheInputs; X121XDONInputs xdonInputs; + X121X3MAInputs x3maInputs; X121XVWAPInputs xvwapInputs; X121XTRENDInputs xtrendInputs; @@ -197,6 +199,7 @@ struct X121Conditions double strDownBuffer[]; double vwapMidBuffer[]; double upTrendBuffer[]; + double x3maMidBuffer[]; double le1StartBuffer[]; double se1StartBuffer[]; double le2StartBuffer[]; @@ -208,6 +211,8 @@ struct X121Conditions double vwapFastBuffer[]; double vwapSlowBuffer[]; double xhkSMLowBuffer[]; + double x3maFastBuffer[]; + double x3maSlowBuffer[]; double downTrendBuffer[]; double vwapPriceBuffer[]; double xhkRawLowBuffer[]; @@ -227,10 +232,13 @@ struct X121Conditions double donOpenLowerBuffer[]; double donHighUpperBuffer[]; double donHighLowerBuffer[]; + double x3maMidStateBuffer[]; double vwapFastStateBuffer[]; double vwapSlowStateBuffer[]; double donCloseUpperBuffer[]; double donCloseLowerBuffer[]; + double x3maFastStateBuffer[]; + double x3maSlowStateBuffer[]; // double rsiSlope; @@ -394,6 +402,31 @@ struct X121Conditions bool isVWapSwitchedToBullishOrdered; bool isVWapSwitchedToBearishOrdered; + // + bool isX3MaMidBullish; + bool isX3MaMidBearish; + bool isX3MaFastBullish; + bool isX3MaFastBearish; + bool isX3MaMidNeutural; + bool isX3MaSlowBullish; + bool isX3MaSlowBearish; + bool isX3MaFastOverMid; + bool isX3MaMidOverSlow; + bool isX3MaFastNeutural; + bool isX3MaSlowNeutural; + bool isX3MaFastUnderMid; + bool isX3MaMidUnderSlow; + bool isX3MaBullishState; + bool isX3MaBearishState; + bool isX3MaNeuturalState; + bool isX3MaBullishOrdered; + bool isX3MaBearishOrdered; + bool isX3MaSwitchedToBullishState; + bool isX3MaSwitchedToBearishState; + bool isX3MaSwitchedToNeuturalState; + bool isX3MaSwitchedToBullishOrdered; + bool isX3MaSwitchedToBearishOrdered; + // bool isSMHKBullish; bool isRawHKBullish; @@ -643,6 +676,7 @@ struct X121Conditions Clean(strDownBuffer); Clean(vwapMidBuffer); Clean(upTrendBuffer); + Clean(x3maMidBuffer); Clean(le1StartBuffer); Clean(se1StartBuffer); Clean(le2StartBuffer); @@ -654,6 +688,8 @@ struct X121Conditions Clean(vwapFastBuffer); Clean(vwapSlowBuffer); Clean(xhkSMLowBuffer); + Clean(x3maFastBuffer); + Clean(x3maSlowBuffer); Clean(downTrendBuffer); Clean(vwapPriceBuffer); Clean(xhkRawLowBuffer); @@ -673,10 +709,13 @@ struct X121Conditions Clean(donOpenLowerBuffer); Clean(donHighUpperBuffer); Clean(donHighLowerBuffer); + Clean(x3maMidStateBuffer); Clean(vwapFastStateBuffer); Clean(vwapSlowStateBuffer); Clean(donCloseUpperBuffer); Clean(donCloseLowerBuffer); + Clean(x3maFastStateBuffer); + Clean(x3maSlowStateBuffer); // ArraySetAsSeries(maBuffer, true); @@ -710,6 +749,7 @@ struct X121Conditions ArraySetAsSeries(strDownBuffer, true); ArraySetAsSeries(vwapMidBuffer, true); ArraySetAsSeries(upTrendBuffer, true); + ArraySetAsSeries(x3maMidBuffer, true); ArraySetAsSeries(le1StartBuffer, true); ArraySetAsSeries(se1StartBuffer, true); ArraySetAsSeries(le2StartBuffer, true); @@ -721,6 +761,8 @@ struct X121Conditions ArraySetAsSeries(vwapFastBuffer, true); ArraySetAsSeries(vwapSlowBuffer, true); ArraySetAsSeries(xhkSMLowBuffer, true); + ArraySetAsSeries(x3maFastBuffer, true); + ArraySetAsSeries(x3maSlowBuffer, true); ArraySetAsSeries(downTrendBuffer, true); ArraySetAsSeries(vwapPriceBuffer, true); ArraySetAsSeries(xhkRawLowBuffer, true); @@ -740,10 +782,13 @@ struct X121Conditions ArraySetAsSeries(donOpenLowerBuffer, true); ArraySetAsSeries(donHighUpperBuffer, true); ArraySetAsSeries(donHighLowerBuffer, true); + ArraySetAsSeries(x3maMidStateBuffer, true); ArraySetAsSeries(vwapFastStateBuffer, true); ArraySetAsSeries(vwapSlowStateBuffer, true); ArraySetAsSeries(donCloseUpperBuffer, true); ArraySetAsSeries(donCloseLowerBuffer, true); + ArraySetAsSeries(x3maFastStateBuffer, true); + ArraySetAsSeries(x3maSlowStateBuffer, true); // cheMin = 0; @@ -891,48 +936,56 @@ struct X121Conditions // // VWAP ... - - // - isVWapFastBullish = false; - isVWapFastBearish = false; - isVWapFastNeutural = false; - - // isVWapMidBullish = false; isVWapMidBearish = false; isVWapMidNeutural = false; - - // isVWapSlowBullish = false; isVWapSlowBearish = false; - isVWapSlowNeutural = false; - - // + isVWapFastBullish = false; + isVWapFastBearish = false; isVWapFastOverMid = false; isVWapMidOverSlow = false; - - // + isVWapFastNeutural = false; + isVWapSlowNeutural = false; isVWapFastUnderMid = false; isVWapMidUnderSlow = false; - - // isVWapBullishState = false; isVWapBearishState = false; isVWapNeuturalState = false; - - // isVWapBullishOrdered = false; isVWapBearishOrdered = false; - - // isVWapSwitchedToBullishState = false; isVWapSwitchedToBearishState = false; - - // isVWapSwitchedToNeuturalState = false; isVWapSwitchedToBullishOrdered = false; isVWapSwitchedToBearishOrdered = false; + // + // X3MA ... + isX3MaMidBullish = false; + isX3MaMidBearish = false; + isX3MaFastBullish = false; + isX3MaFastBearish = false; + isX3MaMidNeutural = false; + isX3MaSlowBullish = false; + isX3MaSlowBearish = false; + isX3MaFastOverMid = false; + isX3MaMidOverSlow = false; + isX3MaFastNeutural = false; + isX3MaSlowNeutural = false; + isX3MaFastUnderMid = false; + isX3MaMidUnderSlow = false; + isX3MaBullishState = false; + isX3MaBearishState = false; + isX3MaNeuturalState = false; + isX3MaBullishOrdered = false; + isX3MaBearishOrdered = false; + isX3MaSwitchedToBullishState = false; + isX3MaSwitchedToBearishState = false; + isX3MaSwitchedToNeuturalState = false; + isX3MaSwitchedToBullishOrdered = false; + isX3MaSwitchedToBearishOrdered = false; + // // CHE ... @@ -1474,6 +1527,97 @@ struct X121Conditions } } + // + // X3MA ... + + // + if (isX3MaBullishState) + { + bullishScore += score; + } + if (isX3MaBullishOrdered) + { + bullishScore += score; + } + if (isX3MaSwitchedToBullishState) + { + bullishScore += highScore; + } + if (isX3MaSwitchedToBullishOrdered) + { + bullishScore += highScore; + } + if (!isX3MaBullishState && + !isX3MaSwitchedToBullishState) + { + // + if (isX3MaFastBullish) + { + bullishScore += minScore; + } + if (isX3MaSlowBullish) + { + bullishScore += minScore; + } + } + if (!isX3MaBullishOrdered && + !isX3MaSwitchedToBullishOrdered) + { + // + if (isX3MaFastOverMid) + { + bullishScore += minScore; + } + if (isX3MaMidOverSlow) + { + bullishScore += minScore; + } + } + + // + if (isX3MaBearishState) + { + bearishScore += score; + } + if (isX3MaBearishOrdered) + { + bearishScore += score; + } + if (isX3MaSwitchedToBearishState) + { + bearishScore += highScore; + } + if (isX3MaSwitchedToBearishOrdered) + { + bearishScore += highScore; + } + if (!isX3MaBearishState && + !isX3MaSwitchedToBearishState) + { + // + if (isX3MaFastBearish) + { + bearishScore += minScore; + } + if (isX3MaSlowBearish) + { + bearishScore += minScore; + } + } + if (!isX3MaBearishOrdered && + !isX3MaSwitchedToBearishOrdered) + { + // + if (isX3MaFastUnderMid) + { + bearishScore += minScore; + } + if (isX3MaMidUnderSlow) + { + bearishScore += minScore; + } + } + // // XSMHK ... @@ -1705,6 +1849,33 @@ struct X121Conditions ToString("isVWapSwitchedToNeuturalState", isVWapSwitchedToNeuturalState, ignoreFalseConditions, separator) + // "-----------------------" + separator + + "X3MA ..." + separator + + "-----------------------" + separator + + ToString("isX3MaMidBullish", isX3MaMidBullish, ignoreFalseConditions, separator) + + ToString("isX3MaMidBearish", isX3MaMidBearish, ignoreFalseConditions, separator) + + ToString("isX3MaFastBullish", isX3MaFastBullish, ignoreFalseConditions, separator) + + ToString("isX3MaFastBearish", isX3MaFastBearish, ignoreFalseConditions, separator) + + ToString("isX3MaMidNeutural", isX3MaMidNeutural, ignoreFalseConditions, separator) + + ToString("isX3MaSlowBullish", isX3MaSlowBullish, ignoreFalseConditions, separator) + + ToString("isX3MaSlowBearish", isX3MaSlowBearish, ignoreFalseConditions, separator) + + ToString("isX3MaFastOverMid", isX3MaFastOverMid, ignoreFalseConditions, separator) + + ToString("isX3MaMidOverSlow", isX3MaMidOverSlow, ignoreFalseConditions, separator) + + ToString("isX3MaFastNeutural", isX3MaFastNeutural, ignoreFalseConditions, separator) + + ToString("isX3MaSlowNeutural", isX3MaSlowNeutural, ignoreFalseConditions, separator) + + ToString("isX3MaFastUnderMid", isX3MaFastUnderMid, ignoreFalseConditions, separator) + + ToString("isX3MaMidUnderSlow", isX3MaMidUnderSlow, ignoreFalseConditions, separator) + + ToString("isX3MaBullishState", isX3MaBullishState, ignoreFalseConditions, separator) + + ToString("isX3MaBearishState", isX3MaBearishState, ignoreFalseConditions, separator) + + ToString("isX3MaNeuturalState", isX3MaNeuturalState, ignoreFalseConditions, separator) + + ToString("isX3MaBullishOrdered", isX3MaBullishOrdered, ignoreFalseConditions, separator) + + ToString("isX3MaBearishOrdered", isX3MaBearishOrdered, ignoreFalseConditions, separator) + + ToString("isX3MaSwitchedToBullishState", isX3MaSwitchedToBullishState, ignoreFalseConditions, separator) + + ToString("isX3MaSwitchedToBearishState", isX3MaSwitchedToBearishState, ignoreFalseConditions, separator) + + ToString("isX3MaSwitchedToNeuturalState", isX3MaSwitchedToNeuturalState, ignoreFalseConditions, separator) + + ToString("isX3MaSwitchedToBullishOrdered", isX3MaSwitchedToBullishOrdered, ignoreFalseConditions, separator) + + ToString("isX3MaSwitchedToBearishOrdered", isX3MaSwitchedToBearishOrdered, ignoreFalseConditions, separator) + + // + "-----------------------" + separator + "CHE ..." + separator + "-----------------------" + separator + ToString("isChe1Bullish", isChe1Bullish, ignoreFalseConditions, separator) + @@ -1913,6 +2084,7 @@ class XCX121Helper : public XCBase XCX121XSTRHelper *xstrHelper; XCX121XCHEHelper *xcheHelper; XCX121XDONHelper *xdonHelper; + XCX121X3MAHelper *x3maHelper; XCX121XVWAPHelper *xvwapHelper; XCX121XTRENDHelper *xtrendHelper; @@ -1933,6 +2105,7 @@ class XCX121Helper : public XCBase delete xstrHelper; delete xcheHelper; delete xdonHelper; + delete x3maHelper; delete xvwapHelper; delete xtrendHelper; @@ -1943,6 +2116,7 @@ class XCX121Helper : public XCBase ZeroMemory(xstrHelper); ZeroMemory(xcheHelper); ZeroMemory(xdonHelper); + ZeroMemory(x3maHelper); ZeroMemory(xvwapHelper); ZeroMemory(xtrendHelper); } @@ -2005,6 +2179,7 @@ class XCX121Helper : public XCBase xstrInputs = inputs.xstrInputs; xcheInputs = inputs.xcheInputs; xdonInputs = inputs.xdonInputs; + x3maInputs = inputs.x3maInputs; xvwapInputs = inputs.xvwapInputs; xtrendInputs = inputs.xtrendInputs; @@ -2089,6 +2264,19 @@ class XCX121Helper : public XCBase return result; } + // + // X3MA ... + x3maHelper = new XCX121X3MAHelper(); + result = x3maHelper.Init( + symbol, + period, + x3maInputs // + ); + if (!result) + { + return result; + } + // // XVWAP ... xvwapHelper = new XCX121XVWAPHelper(); @@ -2273,6 +2461,28 @@ class XCX121Helper : public XCBase return result; } + // + // X3MA ... + bool Get3MAConditions( + X121X3MAConditions &conditions, // + int barIndex = 0, // + int loopback = 5 // + ) + { + // + bool result = false; + + // + result = x3maHelper.GetConditions( + conditions, + barIndex, + loopback // + ); + + // + return result; + } + // // XVWAP ... bool GetVWAPConditions( @@ -2475,6 +2685,19 @@ class XCX121Helper : public XCBase return result; } + // + // X3MA ... + X121X3MAConditions x3maConditions; + result = Get3MAConditions( + x3maConditions, + barIndex, + loopback // + ); + if (!result) + { + return result; + } + // // XVWAP ... X121XVWAPConditions xvwapConditions; @@ -3031,6 +3254,76 @@ class XCX121Helper : public XCBase conditions.isRejectedDonCloseUpper = xdonConditions.isRejectedDonCloseUpper; conditions.isRejectedDonCloseLower = xdonConditions.isRejectedDonCloseLower; + // + // X3MA ... + + // + // Buffers ... + + // + Copy( + x3maConditions.x3maFastBuffer, + conditions.x3maFastBuffer // + ); + + // + Copy( + x3maConditions.x3maMidBuffer, + conditions.x3maMidBuffer // + ); + + // + Copy( + x3maConditions.x3maSlowBuffer, + conditions.x3maSlowBuffer // + ); + + // + Copy( + x3maConditions.x3maFastStateBuffer, + conditions.x3maFastStateBuffer // + ); + + // + Copy( + x3maConditions.x3maMidStateBuffer, + conditions.x3maMidStateBuffer // + ); + + // + Copy( + x3maConditions.x3maSlowStateBuffer, + conditions.x3maSlowStateBuffer // + ); + + // + // Conditions ... + + // + conditions.isX3MaMidBullish = x3maConditions.isX3MaMidBullish; + conditions.isX3MaMidBearish = x3maConditions.isX3MaMidBearish; + conditions.isX3MaFastBullish = x3maConditions.isX3MaFastBullish; + conditions.isX3MaFastBearish = x3maConditions.isX3MaFastBearish; + conditions.isX3MaMidNeutural = x3maConditions.isX3MaMidNeutural; + conditions.isX3MaSlowBullish = x3maConditions.isX3MaSlowBullish; + conditions.isX3MaSlowBearish = x3maConditions.isX3MaSlowBearish; + conditions.isX3MaFastOverMid = x3maConditions.isX3MaFastOverMid; + conditions.isX3MaMidOverSlow = x3maConditions.isX3MaMidOverSlow; + conditions.isX3MaFastNeutural = x3maConditions.isX3MaFastNeutural; + conditions.isX3MaSlowNeutural = x3maConditions.isX3MaSlowNeutural; + conditions.isX3MaFastUnderMid = x3maConditions.isX3MaFastUnderMid; + conditions.isX3MaMidUnderSlow = x3maConditions.isX3MaMidUnderSlow; + conditions.isX3MaBullishState = x3maConditions.isX3MaBullishState; + conditions.isX3MaBearishState = x3maConditions.isX3MaBearishState; + conditions.isX3MaNeuturalState = x3maConditions.isX3MaNeuturalState; + conditions.isX3MaBullishOrdered = x3maConditions.isX3MaBullishOrdered; + conditions.isX3MaBearishOrdered = x3maConditions.isX3MaBearishOrdered; + conditions.isX3MaSwitchedToBullishState = x3maConditions.isX3MaSwitchedToBullishState; + conditions.isX3MaSwitchedToBearishState = x3maConditions.isX3MaSwitchedToBearishState; + conditions.isX3MaSwitchedToNeuturalState = x3maConditions.isX3MaSwitchedToNeuturalState; + conditions.isX3MaSwitchedToBullishOrdered = x3maConditions.isX3MaSwitchedToBullishOrdered; + conditions.isX3MaSwitchedToBearishOrdered = x3maConditions.isX3MaSwitchedToBearishOrdered; + // // XVWAP ... @@ -3740,6 +4033,7 @@ class XCX121Helper : public XCBase X121XDONInputs xdonInputs; X121XSTRInputs xstrInputs; X121XCHEInputs xcheInputs; + X121X3MAInputs x3maInputs; X121XVWAPInputs xvwapInputs; X121XTRENDInputs xtrendInputs; diff --git a/Helpers/x-saherelm.x121.x3ma.helper.mq5 b/Helpers/x-saherelm.x121.x3ma.helper.mq5 new file mode 100644 index 00000000..8337273c --- /dev/null +++ b/Helpers/x-saherelm.x121.x3ma.helper.mq5 @@ -0,0 +1,1339 @@ +/////////////////////////////////////////////////////// +// +// SaherElm IT Center MQL5 Helper Class Library +// ---------------------------------------------- +// Name: XCX121X3MAHelper +// 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_X3MA_BUFFERS +{ + // + X121_X3MA_FAST_LINE = 0, + X121_X3MA_FAST_STATE_LINE = 6, + X121_X3MA_MID_LINE = 2, + X121_X3MA_MID_STATE_LINE = 7, + X121_X3MA_SLOW_LINE = 4, + X121_X3MA_SLOW_STATE_LINE = 8, +}; + +// +enum ENUM_X3MA_STATES +{ + X3MA_STATE_BULLISH = 1, + X3MA_STATE_BEARISH = 2, + X3MA_STATE_NEUTURAL = 3, +}; + +// +// Input Models ... +struct X121X3MAInputs +{ + // + // Props ... + int x3maFastMALength; // Fast MA Length + int x3maMidMALength; // Mid MA Length + int x3maSlowMALength; // Slow MA Length + ENUM_MA_METHOD x3maMaMethod; // MA Method + ENUM_APPLIED_PRICE x3maMaAppliedTo; // Applied To + + // + int startCalculationForLastBars; // Calculate Last n Bars + + // + bool showX3MaFast; // Show X3Ma Fast + bool showX3MaMid; // Show X3Ma Medium + bool showX3MaSlow; // Show X3Ma Slow + + // + // Constructor(s) ... + X121X3MAInputs() + { + Clean(); + } + + // + // Tools ... + + // + // Clean ... + void Clean() + { + // + x3maFastMALength = 0; + x3maMidMALength = 0; + x3maSlowMALength = 0; + x3maMaMethod = MODE_EMA; + x3maMaAppliedTo = PRICE_CLOSE; + startCalculationForLastBars = 0; + showX3MaFast = false; + showX3MaMid = false; + showX3MaSlow = false; + + // + ZeroMemory(this); + } + + // + // Default ... + void Default() + { + // + x3maFastMALength = 20; + x3maMidMALength = 50; + x3maSlowMALength = 200; + x3maMaMethod = MODE_EMA; + x3maMaAppliedTo = PRICE_CLOSE; + startCalculationForLastBars = 1000; + showX3MaFast = true; + showX3MaMid = true; + showX3MaSlow = true; + } + + // + // Validate ... + bool IsValid() + { + // + bool result = false; + + // + result = + // + x3maFastMALength > 2 && + x3maMidMALength > x3maFastMALength && + x3maSlowMALength > x3maMidMALength + // + ; + + // + return result; + } + + // + // Retrieve MAx Length ... + int Max() + { + // + int result = 0; + + // + result = MathMax(x3maFastMALength, x3maMidMALength); + result = MathMax(result, x3maSlowMALength); + + // + return result; + } +}; + +// +// Conditions ... +struct X121X3MAConditions +{ + // + // Common ... + string symbol; + ENUM_TIMEFRAMES period; + datetime time; + + // + // Buffers ... + double x3maFastBuffer[]; + double x3maMidBuffer[]; + double x3maSlowBuffer[]; + double x3maFastStateBuffer[]; + double x3maMidStateBuffer[]; + double x3maSlowStateBuffer[]; + + // + // Conditions ... + + // + bool isX3MaFastBullish; + bool isX3MaFastBearish; + bool isX3MaFastNeutural; + + // + bool isX3MaMidBullish; + bool isX3MaMidBearish; + bool isX3MaMidNeutural; + + // + bool isX3MaSlowBullish; + bool isX3MaSlowBearish; + bool isX3MaSlowNeutural; + + // + bool isX3MaFastOverMid; + bool isX3MaMidOverSlow; + + // + bool isX3MaFastUnderMid; + bool isX3MaMidUnderSlow; + + // + bool isX3MaBullishState; + bool isX3MaBearishState; + bool isX3MaNeuturalState; + + // + bool isX3MaBullishOrdered; + bool isX3MaBearishOrdered; + + // + bool isX3MaSwitchedToBullishOrdered; + bool isX3MaSwitchedToBearishOrdered; + + // + bool isX3MaSwitchedToBullishState; + bool isX3MaSwitchedToBearishState; + bool isX3MaSwitchedToNeuturalState; + + // + // Constructor ... + X121X3MAConditions() + { + Clean(); + } + + // + // Tools ... + + /** + * Cleaning Up ... + */ + void Clean() + { + // + // Commons ... + symbol = NULL; + period = NULL; + time = NULL; + + // + // Buffers ... + + // + Clean(x3maFastBuffer); + Clean(x3maMidBuffer); + Clean(x3maSlowBuffer); + Clean(x3maFastStateBuffer); + Clean(x3maMidStateBuffer); + Clean(x3maSlowStateBuffer); + + // + ArraySetAsSeries(x3maFastBuffer, true); + ArraySetAsSeries(x3maMidBuffer, true); + ArraySetAsSeries(x3maSlowBuffer, true); + ArraySetAsSeries(x3maFastStateBuffer, true); + ArraySetAsSeries(x3maMidStateBuffer, true); + ArraySetAsSeries(x3maSlowStateBuffer, true); + + // + // Conditions ... + + // + isX3MaFastBullish = false; + isX3MaFastBearish = false; + isX3MaFastNeutural = false; + + // + isX3MaMidBullish = false; + isX3MaMidBearish = false; + isX3MaMidNeutural = false; + + // + isX3MaSlowBullish = false; + isX3MaSlowBearish = false; + isX3MaSlowNeutural = false; + + // + isX3MaFastOverMid = false; + isX3MaMidOverSlow = false; + + // + isX3MaFastUnderMid = false; + isX3MaMidUnderSlow = false; + + // + isX3MaBullishState = false; + isX3MaBearishState = false; + isX3MaNeuturalState = false; + + // + isX3MaBullishOrdered = false; + isX3MaBearishOrdered = false; + + // + isX3MaSwitchedToBullishOrdered = false; + isX3MaSwitchedToBearishOrdered = false; + + // + isX3MaSwitchedToBullishState = false; + isX3MaSwitchedToBearishState = false; + isX3MaSwitchedToNeuturalState = 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("isX3MaFastBullish", isX3MaFastBullish, ignoreFalseConditions, separator) + + ToString("isX3MaFastBearish", isX3MaFastBearish, ignoreFalseConditions, separator) + + ToString("isX3MaFastNeutural", isX3MaFastNeutural, ignoreFalseConditions, separator) + + ToString("isX3MaMidBullish", isX3MaMidBullish, ignoreFalseConditions, separator) + + ToString("isX3MaMidBearish", isX3MaMidBearish, ignoreFalseConditions, separator) + + ToString("isX3MaMidNeutural", isX3MaMidNeutural, ignoreFalseConditions, separator) + + ToString("isX3MaSlowBullish", isX3MaSlowBullish, ignoreFalseConditions, separator) + + ToString("isX3MaSlowBearish", isX3MaSlowBearish, ignoreFalseConditions, separator) + + ToString("isX3MaSlowNeutural", isX3MaSlowNeutural, ignoreFalseConditions, separator) + + ToString("isX3MaFastOverMid", isX3MaFastOverMid, ignoreFalseConditions, separator) + + ToString("isX3MaMidOverSlow", isX3MaMidOverSlow, ignoreFalseConditions, separator) + + ToString("isX3MaFastUnderMid", isX3MaFastUnderMid, ignoreFalseConditions, separator) + + ToString("isX3MaMidUnderSlow", isX3MaMidUnderSlow, ignoreFalseConditions, separator) + + ToString("isX3MaBullishState", isX3MaBullishState, ignoreFalseConditions, separator) + + ToString("isX3MaBearishState", isX3MaBearishState, ignoreFalseConditions, separator) + + ToString("isX3MaNeuturalState", isX3MaNeuturalState, ignoreFalseConditions, separator) + + ToString("isX3MaBullishOrdered", isX3MaBullishOrdered, ignoreFalseConditions, separator) + + ToString("isX3MaBearishOrdered", isX3MaBearishOrdered, ignoreFalseConditions, separator) + + ToString("isX3MaSwitchedToBullishOrdered", isX3MaSwitchedToBullishOrdered, ignoreFalseConditions, separator) + + ToString("isX3MaSwitchedToBearishOrdered", isX3MaSwitchedToBearishOrdered, ignoreFalseConditions, separator) + + ToString("isX3MaSwitchedToBullishState", isX3MaSwitchedToBullishState, ignoreFalseConditions, separator) + + ToString("isX3MaSwitchedToBearishState", isX3MaSwitchedToBearishState, ignoreFalseConditions, separator) + + ToString("isX3MaSwitchedToNeuturalState", isX3MaSwitchedToNeuturalState, 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 XCX121X3MAHelper : public XCBaseHelper +{ + // + // Public ... + public: + // + // Props ... + + // + // Constructors ... + XCX121X3MAHelper() + : XCBaseHelper(_Symbol, _Period) + { + } + + // + // Deconstructor ... + ~XCX121X3MAHelper() + { + // + mInputs.Clean(); + + // + Clean(x3maFastBuffer); + Clean(x3maMidBuffer); + Clean(x3maSlowBuffer); + Clean(x3maFastStateBuffer); + Clean(x3maMidStateBuffer); + Clean(x3maSlowStateBuffer); + } + + // + // Tools ... + bool Init( + string symbol, // Trading Symbol + ENUM_TIMEFRAMES period, // Trading Period + X121X3MAInputs &inputs // Inputs + ) + { + // + bool result = false; + + // + mSymbol = symbol; + mPeriod = period; + + // + result = inputs.IsValid(); + if (!result) + { + return result; + } + + // + ArraySetAsSeries(x3maFastBuffer, true); + ArraySetAsSeries(x3maMidBuffer, true); + ArraySetAsSeries(x3maSlowBuffer, true); + ArraySetAsSeries(x3maFastStateBuffer, true); + ArraySetAsSeries(x3maMidStateBuffer, true); + ArraySetAsSeries(x3maSlowStateBuffer, true); + + // + mInputs = inputs; + + // + mHandler = iCustom( + mSymbol, + mPeriod, + "x-saherelm.x121.x3ma", + // + // Inputs ... + // + // Market ... + "", + mInputs.x3maFastMALength, + mInputs.x3maMidMALength, + mInputs.x3maSlowMALength, + mInputs.x3maMaMethod, + mInputs.x3maMaAppliedTo, + // + // Presentation ... + "", + // + mInputs.startCalculationForLastBars, + // + mInputs.showX3MaFast, + mInputs.showX3MaMid, + mInputs.showX3MaSlow + // + ); + result = mHandler != INVALID_HANDLE; + if (!result) + { + return result; + } + + // + return result; + } + + // + // Inputs ... + + // + X121X3MAInputs GetInputs() + { + return mInputs; + } + + // + bool SetInputs( + X121X3MAInputs &inputs // Configs + ) + { + // + return Init( + mSymbol, + mPeriod, + inputs + // + ); + } + + // + // Readers ... + + // + // FAST ... + + // + double GetX3MaFast( + int barIndex // Bar Index + ) + { + // + if (barIndex < 0) + { + barIndex = 0; + } + + // + Calculate(); + + // + int count = ArraySize(x3maFastBuffer); + if (barIndex >= count) + { + barIndex = count - 1; + } + + // + return x3maFastBuffer[barIndex]; + } + + // + int CopyX3MaFast( + 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, + x3maFastBuffer, + buffer, + forceClean + // + ); + } + + // + // STATE FAST ... + + // + double GetX3MaFastState( + int barIndex // Bar Index + ) + { + // + if (barIndex < 0) + { + barIndex = 0; + } + + // + Calculate(); + + // + int count = ArraySize(x3maFastStateBuffer); + if (barIndex >= count) + { + barIndex = count - 1; + } + + // + return x3maFastStateBuffer[barIndex]; + } + + // + int CopyX3MaFastState( + 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, + x3maFastStateBuffer, + buffer, + forceClean + // + ); + } + + // + // MID ... + + // + double GetX3MaMid( + int barIndex // Bar Index + ) + { + // + if (barIndex < 0) + { + barIndex = 0; + } + + // + Calculate(); + + // + int count = ArraySize(x3maMidBuffer); + if (barIndex >= count) + { + barIndex = count - 1; + } + + // + return x3maMidBuffer[barIndex]; + } + + // + int CopyX3MaMid( + 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, + x3maMidBuffer, + buffer, + forceClean + // + ); + } + + // + // STATE MID ... + + // + double GetX3MaMidState( + int barIndex // Bar Index + ) + { + // + if (barIndex < 0) + { + barIndex = 0; + } + + // + Calculate(); + + // + int count = ArraySize(x3maMidStateBuffer); + if (barIndex >= count) + { + barIndex = count - 1; + } + + // + return x3maMidStateBuffer[barIndex]; + } + + // + int CopyX3MaMidState( + 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, + x3maMidStateBuffer, + buffer, + forceClean + // + ); + } + + // + // SLOW ... + + // + double GetX3MaSlowBuffer( + int barIndex // Bar Index + ) + { + // + if (barIndex < 0) + { + barIndex = 0; + } + + // + Calculate(); + + // + int count = ArraySize(x3maSlowBuffer); + if (barIndex >= count) + { + barIndex = count - 1; + } + + // + return x3maSlowBuffer[barIndex]; + } + + // + int CopyX3MaSlow( + 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, + x3maSlowBuffer, + buffer, + forceClean + // + ); + } + + // + // STATE SLOW ... + + // + double GetX3MaSlowState( + int barIndex // Bar Index + ) + { + // + if (barIndex < 0) + { + barIndex = 0; + } + + // + Calculate(); + + // + int count = ArraySize(x3maSlowStateBuffer); + if (barIndex >= count) + { + barIndex = count - 1; + } + + // + return x3maSlowStateBuffer[barIndex]; + } + + // + int CopyX3MaSlowState( + 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, + x3maSlowStateBuffer, + buffer, + forceClean + // + ); + } + + // + // Converts to State ... + ENUM_X3MA_STATES ToX3MAState(double value) + { + // + ENUM_X3MA_STATES result = + value == 1 + ? X3MA_STATE_BULLISH + : value == 2 + ? X3MA_STATE_BEARISH + : X3MA_STATE_NEUTURAL; + + // + return result; + } + + // + bool IsX3MABullish(double value) + { + // + bool result = false; + + // + result = ToX3MAState(value) == X3MA_STATE_BULLISH; + + // + return result; + } + + // + bool IsX3MABearish(double value) + { + // + bool result = false; + + // + result = ToX3MAState(value) == X3MA_STATE_BEARISH; + + // + return result; + } + + // + bool IsX3MANeutural(double value) + { + // + bool result = false; + + // + result = ToX3MAState(value) == X3MA_STATE_NEUTURAL; + + // + return result; + } + + // + void Free() override + { + Cleanup(10); + } + + // + bool GetConditions( + X121X3MAConditions &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 ... + + // + CopyX3MaFast( + zIndex, + loopback, + conditions.x3maFastBuffer // + ); + + // + CopyX3MaFastState( + zIndex, + loopback, + conditions.x3maFastStateBuffer // + ); + + // + CopyX3MaMid( + zIndex, + loopback, + conditions.x3maMidBuffer // + ); + + // + CopyX3MaMidState( + zIndex, + loopback, + conditions.x3maMidStateBuffer // + ); + + // + CopyX3MaSlow( + zIndex, + loopback, + conditions.x3maSlowBuffer // + ); + + // + CopyX3MaSlowState( + zIndex, + loopback, + conditions.x3maSlowStateBuffer // + ); + + // + // Conditions ... + + // + int cIDX = 1; + int pIDX = cIDX + 1; + + // + // SAR ... + + // + bool isX3MaFastBullish = IsX3MABullish(conditions.x3maFastStateBuffer[cIDX]); + bool isX3MaFastBullishPrev = IsX3MABullish(conditions.x3maFastStateBuffer[pIDX]); + + // + bool isX3MaFastBearish = IsX3MABearish(conditions.x3maFastStateBuffer[cIDX]); + bool isX3MaFastBearishPrev = IsX3MABearish(conditions.x3maFastStateBuffer[pIDX]); + + // + bool isX3MaFastNeutural = IsX3MANeutural(conditions.x3maFastStateBuffer[cIDX]); + bool isX3MaFastNeuturalPrev = IsX3MANeutural(conditions.x3maFastStateBuffer[pIDX]); + + // + bool isX3MaMidBullish = IsX3MABullish(conditions.x3maMidStateBuffer[cIDX]); + bool isX3MaMidBullishPrev = IsX3MABullish(conditions.x3maMidStateBuffer[pIDX]); + + // + bool isX3MaMidBearish = IsX3MABearish(conditions.x3maMidStateBuffer[cIDX]); + bool isX3MaMidBearishPrev = IsX3MABearish(conditions.x3maMidStateBuffer[pIDX]); + + // + bool isX3MaMidNeutural = IsX3MANeutural(conditions.x3maMidStateBuffer[cIDX]); + bool isX3MaMidNeuturalPrev = IsX3MANeutural(conditions.x3maMidStateBuffer[pIDX]); + + // + bool isX3MaSlowBullish = IsX3MABullish(conditions.x3maSlowStateBuffer[cIDX]); + bool isX3MaSlowBullishPrev = IsX3MABullish(conditions.x3maSlowStateBuffer[pIDX]); + + // + bool isX3MaSlowBearish = IsX3MABearish(conditions.x3maSlowStateBuffer[cIDX]); + bool isX3MaSlowBearishPrev = IsX3MABearish(conditions.x3maSlowStateBuffer[pIDX]); + + // + bool isX3MaSlowNeutural = IsX3MANeutural(conditions.x3maSlowStateBuffer[cIDX]); + bool isX3MaSlowNeuturalPrev = IsX3MANeutural(conditions.x3maSlowStateBuffer[pIDX]); + + // + bool isX3MaFastOverMid = conditions.x3maFastBuffer[cIDX] > conditions.x3maMidBuffer[cIDX]; + bool isX3MaFastOverMidPrev = conditions.x3maFastBuffer[pIDX] > conditions.x3maMidBuffer[pIDX]; + + // + bool isX3MaMidOverSlow = conditions.x3maMidBuffer[cIDX] > conditions.x3maSlowBuffer[cIDX]; + bool isX3MaMidOverSlowPrev = conditions.x3maMidBuffer[pIDX] > conditions.x3maSlowBuffer[pIDX]; + + // + bool isX3MaFastUnderMid = conditions.x3maFastBuffer[cIDX] < conditions.x3maMidBuffer[cIDX]; + bool isX3MaFastUnderMidPrev = conditions.x3maFastBuffer[pIDX] < conditions.x3maMidBuffer[pIDX]; + + // + bool isX3MaMidUnderSlow = conditions.x3maMidBuffer[cIDX] < conditions.x3maSlowBuffer[cIDX]; + bool isX3MaMidUnderSlowPrev = conditions.x3maMidBuffer[pIDX] < conditions.x3maSlowBuffer[pIDX]; + + // + bool isX3MaBullishState = isX3MaFastBullish && + isX3MaMidBullish && + isX3MaSlowBullish; + bool isX3MaBullishStatePrev = isX3MaFastBullishPrev && + isX3MaMidBullishPrev && + isX3MaSlowBullishPrev; + + // + bool isX3MaBearishState = isX3MaFastBearish && + isX3MaMidBearish && + isX3MaSlowBearish; + bool isX3MaBearishStatePrev = isX3MaFastBearishPrev && + isX3MaMidBearishPrev && + isX3MaSlowBearishPrev; + + // + bool isX3MaNeuturalState = isX3MaFastNeutural && + isX3MaMidNeutural && + isX3MaSlowNeutural; + bool isX3MaNeuturalStatePrev = isX3MaFastNeuturalPrev && + isX3MaMidNeuturalPrev && + isX3MaSlowNeuturalPrev; + + // + bool isX3MaBullishOrdered = isX3MaFastOverMid && + isX3MaMidOverSlow; + bool isX3MaBullishOrderedPrev = isX3MaFastOverMidPrev && + isX3MaMidOverSlowPrev; + + // + bool isX3MaBearishOrdered = isX3MaFastUnderMid && + isX3MaMidUnderSlow; + bool isX3MaBearishOrderedPrev = isX3MaFastUnderMidPrev && + isX3MaMidUnderSlowPrev; + + // + bool isX3MaSwitchedToBullishOrdered = isX3MaBullishOrdered && + !isX3MaBullishOrderedPrev; + bool isX3MaSwitchedToBearishOrdered = isX3MaBearishOrdered && + !isX3MaBearishOrderedPrev; + + // + bool isX3MaSwitchedToBullishState = isX3MaBullishState && + !isX3MaBullishStatePrev; + bool isX3MaSwitchedToBearishState = isX3MaBearishState && + !isX3MaBearishStatePrev; + bool isX3MaSwitchedToNeuturalState = isX3MaNeuturalState && + !isX3MaNeuturalStatePrev; + + // + conditions.isX3MaFastBullish = isX3MaFastBullish; + conditions.isX3MaFastBearish = isX3MaFastBearish; + conditions.isX3MaFastNeutural = isX3MaFastNeutural; + conditions.isX3MaMidBullish = isX3MaMidBullish; + conditions.isX3MaMidBearish = isX3MaMidBearish; + conditions.isX3MaMidNeutural = isX3MaMidNeutural; + conditions.isX3MaSlowBullish = isX3MaSlowBullish; + conditions.isX3MaSlowBearish = isX3MaSlowBearish; + conditions.isX3MaSlowNeutural = isX3MaSlowNeutural; + conditions.isX3MaFastOverMid = isX3MaFastOverMid; + conditions.isX3MaMidOverSlow = isX3MaMidOverSlow; + conditions.isX3MaFastUnderMid = isX3MaFastUnderMid; + conditions.isX3MaMidUnderSlow = isX3MaMidUnderSlow; + conditions.isX3MaBullishState = isX3MaBullishState; + conditions.isX3MaBearishState = isX3MaBearishState; + conditions.isX3MaNeuturalState = isX3MaNeuturalState; + conditions.isX3MaBullishOrdered = isX3MaBullishOrdered; + conditions.isX3MaBearishOrdered = isX3MaBearishOrdered; + conditions.isX3MaSwitchedToBullishOrdered = isX3MaSwitchedToBullishOrdered; + conditions.isX3MaSwitchedToBearishOrdered = isX3MaSwitchedToBearishOrdered; + conditions.isX3MaSwitchedToBullishState = isX3MaSwitchedToBullishState; + conditions.isX3MaSwitchedToBearishState = isX3MaSwitchedToBearishState; + conditions.isX3MaSwitchedToNeuturalState = isX3MaSwitchedToNeuturalState; + + // + Cleanup(); + + // + zBar.Clean(); + cBar.Clean(); + pBar.Clean(); + + // + return result; + } + + // + // Protected ... + protected: + // + // Private ... + private: + // + // Props ... + X121X3MAInputs mInputs; // Inputs ... + + // + // Buffers ... + double x3maFastBuffer[]; + double x3maMidBuffer[]; + double x3maSlowBuffer[]; + double x3maFastStateBuffer[]; + double x3maMidStateBuffer[]; + double x3maSlowStateBuffer[]; + + // + void Calculate( + int maxRequiredBars = 100 // + ) + { + // + // Buffers ... + + // + // FAST ... + CopyBuffer( + mHandler, + X121_X3MA_FAST_LINE, + 0, + maxRequiredBars, + x3maFastBuffer + // + ); + + // + // FAST STATE ... + CopyBuffer( + mHandler, + X121_X3MA_FAST_STATE_LINE, + 0, + maxRequiredBars, + x3maFastStateBuffer + // + ); + + // + // MID ... + CopyBuffer( + mHandler, + X121_X3MA_MID_LINE, + 0, + maxRequiredBars, + x3maMidBuffer + // + ); + + // + // MID STATE ... + CopyBuffer( + mHandler, + X121_X3MA_MID_STATE_LINE, + 0, + maxRequiredBars, + x3maMidStateBuffer + // + ); + + // + // SLOW ... + CopyBuffer( + mHandler, + X121_X3MA_SLOW_LINE, + 0, + maxRequiredBars, + x3maSlowBuffer + // + ); + + // + // SLOW STATE ... + CopyBuffer( + mHandler, + X121_X3MA_SLOW_STATE_LINE, + 0, + maxRequiredBars, + x3maSlowStateBuffer + // + ); + } + + // + void Cleanup( + int maxAllowed = 100 // + ) + { + // + CleanupArray( + x3maFastBuffer, + maxAllowed // + ); + + // + CleanupArray( + x3maMidBuffer, + maxAllowed // + ); + + // + CleanupArray( + x3maSlowBuffer, + maxAllowed // + ); + + // + CleanupArray( + x3maFastStateBuffer, + maxAllowed // + ); + + // + CleanupArray( + x3maMidStateBuffer, + maxAllowed // + ); + + // + CleanupArray( + x3maSlowStateBuffer, + maxAllowed // + ); + + // + } + + // +}; + +// +// Tools ... \ No newline at end of file diff --git a/Indicators/x-saherelm.x121.x3ma.mq5 b/Indicators/x-saherelm.x121.x3ma.mq5 new file mode 100644 index 00000000..a16b3ced --- /dev/null +++ b/Indicators/x-saherelm.x121.x3ma.mq5 @@ -0,0 +1,658 @@ +/////////////////////////////////////////////////////// +// +// SaherElm IT Center MQL5 Indicator +// ------------------------------------------------- +// Name: X121 XWZ +// Description: XWZ ... +// +// +// 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 X121 XWZ Indicator" +#property strict + +// +// Definitions ... + +// +#define ShortName "X121_X3MA" + +// +// Imports ... +#include "../Libraries/x-saherelm.common.lib.mq5" + +// +// Inputs ... + +// +// Market ... +input group "Market"; +input int fastMALength = 20; // Fast MA Length +input int midMALength = 50; // Mid MA Length +input int slowMALength = 200; // Slow MA Length +input ENUM_MA_METHOD maMethod = MODE_EMA; // MA Method +input ENUM_APPLIED_PRICE maAppliedTo = PRICE_CLOSE; // Applied To + +// +// Presentation ... +input group "Presentation"; + +// +input int startCalculationForLastBars = 1000; // Calculate Last n Bars +input bool showMaFast = true; // Show Fast MA +input bool showMaMid = true; // Show Mid MA +input bool showMaSlow = true; // Show Slow MA + +// +// Buffers ... + +// +#define hideColorIDX 0 +#define bullishColorIDX 1 +#define bearishColorIDX 2 +#define neuturalColorIDX 3 + +// +#property indicator_chart_window + +// +#property indicator_buffers 9 +#property indicator_plots 3 + +// +// Plot Buffers ... + +// +// FAST ... +#define maFastBufferIndex 0 +double maFastBuffer[]; + +#define maFastColorBufferIndex 1 +double maFastColorBuffer[]; + +// +#define maFastPlotBufferIndex 0 +#property indicator_label1 "X121 MAF" +#property indicator_type1 DRAW_COLOR_LINE +#property indicator_color1 CLR_NONE, clrAqua, clrMagenta, clrGray +#property indicator_style1 STYLE_SOLID +#property indicator_width1 2 + +// +// MID ... +#define maMidBufferIndex 2 +double maMidBuffer[]; + +#define maMidColorBufferIndex 3 +double maMidColorBuffer[]; + +// +#define maMidPlotBufferIndex 1 +#property indicator_label2 "X121 MAM" +#property indicator_type2 DRAW_COLOR_LINE +#property indicator_color2 CLR_NONE, clrGreen, clrRed, clrGray +#property indicator_style2 STYLE_SOLID +#property indicator_width2 2 + +// +// SLOW ... +#define maSlowBufferIndex 4 +double maSlowBuffer[]; + +#define maSlowColorBufferIndex 5 +double maSlowColorBuffer[]; + +// +#define maSlowPlotBufferIndex 2 +#property indicator_label3 "X121 MAS" +#property indicator_type3 DRAW_COLOR_LINE +#property indicator_color3 CLR_NONE, clrSteelBlue, clrDarkRed, clrGray +#property indicator_style3 STYLE_SOLID +#property indicator_width3 2 + +// +// Data Buffers ... + +// +int mLastBufferIndex = 5; + +// +// Fast State ... +#define maFastStateBufferIndex mLastBufferIndex + 1 +double maFastStateBuffer[]; + +// +// Mid State ... +#define maMidStateBufferIndex mLastBufferIndex + 2 +double maMidStateBuffer[]; + +// +// Slow State ... +#define maSlowStateBufferIndex mLastBufferIndex + 3 +double maSlowStateBuffer[]; + +// +// Variables, Properties and etc ... + +// +// this counts Available Bars ... +int limit; + +// +int maxLength; + +// +int maFastHandler = INVALID_HANDLE; +int maMidHandler = INVALID_HANDLE; +int maSlowHandler = INVALID_HANDLE; + +// +// Event Handlers ... + +/** + * Initialize Indicator ... + * + * @return ( int ) + */ +int OnInit() +{ + // + // Validate Inputs ... + if (!ValidateInputs()) + { + return INIT_PARAMETERS_INCORRECT; + } + + // + // Initialize Indicator Handlers ... + + // + // Fast MA ... + maFastHandler = iMA( + _Symbol, + _Period, + fastMALength, + 0, + maMethod, + maAppliedTo // + ); + bool isInited = maFastHandler != INVALID_HANDLE; + if (!isInited) + { + return INIT_FAILED; + } + + // + // Mid MA ... + maMidHandler = iMA( + _Symbol, + _Period, + midMALength, + 0, + maMethod, + maAppliedTo // + ); + isInited = maMidHandler != INVALID_HANDLE; + if (!isInited) + { + return INIT_FAILED; + } + + // + // Slow MA ... + maSlowHandler = iMA( + _Symbol, + _Period, + slowMALength, + 0, + maMethod, + maAppliedTo // + ); + isInited = maSlowHandler != INVALID_HANDLE; + if (!isInited) + { + return INIT_FAILED; + } + + // + // because in some cases we may have more than one input for + // calculation and we must prevent any calculation + // untill we pass the biggest input length, here we get max Input length + // and then wait until pass it ... + maxLength = ExtractMaxLengthOfInputs(); + + // + // Define Index Buffers ... + DefineBuffers(); + + // + // Set Indicator ShortName ... + SetIndicatorName(); + IndicatorSetInteger(INDICATOR_DIGITS, 2); + + // + // Init Succeed ... + return INIT_SUCCEEDED; +} + +/** + * De Initialize Indicator ... + * + * @param reason: Integer, De Initialization Reason ... + */ +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 + + // + IndicatorRelease(maFastHandler); + IndicatorRelease(maMidHandler); + IndicatorRelease(maSlowHandler); +} + +/** + * Calculate Bars ... + * + * @param rates_total: Integer, Total Bars on Chart ... + * @param prev_calculated: Integer, Total Calculated Bars on Charts ... + * @param time: DateTime Array, History of Open Time ... + * @param open: Double Array, History of Open Prices ... + * @param high: Double Array, History of High Prices ... + * @param low: Double Array, History of Low Prices ... + * @param close: Double Array, History of Close Prices ... + * @param tick_volume: Long, History of Tick Volumes on Bar ... + * @param volume: Long, History of Trade Volumes ... + * @param spread: Double, History of Spread Price ... + * + * @return ( int ) + */ +int OnCalculate( + const int rates_total, + const int prev_calculated, + const datetime &time[], + const double &open[], + const double &high[], + const double &low[], + const double &close[], + const long &tick_volume[], + const long &volume[], + const int &spread[] // +) +{ + // + // Prepare Buffers ... + ArraySetAsSeries(time, true); + ArraySetAsSeries(open, true); + ArraySetAsSeries(high, true); + ArraySetAsSeries(low, true); + ArraySetAsSeries(close, true); + ArraySetAsSeries(tick_volume, true); + ArraySetAsSeries(volume, true); + ArraySetAsSeries(spread, true); + + // + // Validate Calculated Bars ... + + // + // Fast MA ... + int maFastCalculatedBars = BarsCalculated(maFastHandler); + + // + // Mid MA ... + int maMidCalculatedBars = BarsCalculated(maMidHandler); + + // + // Slow MA ... + int maSlowCalculatedBars = BarsCalculated(maSlowHandler); + + // + bool isPassedRequiredCalculatedBars = + // + // Fast MA ... + maFastCalculatedBars >= maxLength && + // + // Slow MA ... + maMidCalculatedBars >= maxLength && + // + // Slow MA ... + maSlowCalculatedBars >= maxLength + // + ; + if (!isPassedRequiredCalculatedBars) + { + return prev_calculated; + } + + // + limit = (prev_calculated > rates_total || prev_calculated <= 0) + ? rates_total + : (rates_total - prev_calculated) + 1; + + // + // Buffers Copy ... + + // + // Fast MA ... + int copiedFastMas = CopyBuffer(maFastHandler, 0, 0, limit, maFastBuffer); + + // + // Mid MA ... + int copiedMidMas = CopyBuffer(maMidHandler, 0, 0, limit, maMidBuffer); + + // + // Slow MA ... + int copiedSlowMas = CopyBuffer(maSlowHandler, 0, 0, limit, maSlowBuffer); + + // + // Validate Copied Items ... + bool isPassedRequiredCopiedItems = + // + // Fast MA ... + copiedFastMas > 0 && + // + // Mid MA ... + copiedMidMas > 0 && + // + // Slow MA ... + copiedSlowMas > 0 + // + ; + if (!isPassedRequiredCopiedItems) + { + return prev_calculated; + } + + // + // Main Loop ... + for (int i = limit - 1; i >= 0 && !IsStopped(); i--) + { + // + CalculateBuffers( + // + i, + prev_calculated, + rates_total, + // + open, + high, + close, + low, + tick_volume // + ); + } + + // + return rates_total; +} + +// +// Functions ... + +/** + * Validate Input Args for Initialization ... + * + * @return ( bool ) + */ +bool ValidateInputs() +{ + // + bool result = false; + + // + result = fastMALength > 0 && + midMALength > 0 && + slowMALength > 0 && + midMALength > fastMALength && + slowMALength > midMALength; + + // + return result; +} + +/** + * Extract Max Length of Inputs ... + * + * @return ( int ) + */ +int ExtractMaxLengthOfInputs() +{ + // + int result = 0; + + // + result = MathMax(fastMALength, midMALength); + result = MathMax(result, slowMALength); + + // + return result; +} + +/** + * Set Indicator Short Name and also we can define Buffers Labels ... + */ +void SetIndicatorName() +{ + IndicatorSetString(INDICATOR_SHORTNAME, ShortName); +} + +/** + * Define Required Buffers ... + */ +void DefineBuffers() +{ + // + // Plot Buffers ... + + // + // Fast ... + ArraySetAsSeries(maFastBuffer, true); + ArraySetAsSeries(maFastColorBuffer, true); + SetIndexBuffer(maFastBufferIndex, maFastBuffer, INDICATOR_DATA); + SetIndexBuffer(maFastColorBufferIndex, maFastColorBuffer, INDICATOR_COLOR_INDEX); + + // + // Mid ... + ArraySetAsSeries(maMidBuffer, true); + ArraySetAsSeries(maMidColorBuffer, true); + SetIndexBuffer(maMidBufferIndex, maMidBuffer, INDICATOR_DATA); + SetIndexBuffer(maMidColorBufferIndex, maMidColorBuffer, INDICATOR_COLOR_INDEX); + + // + // Slow ... + ArraySetAsSeries(maSlowBuffer, true); + ArraySetAsSeries(maSlowColorBuffer, true); + SetIndexBuffer(maSlowBufferIndex, maSlowBuffer, INDICATOR_DATA); + SetIndexBuffer(maSlowColorBufferIndex, maSlowColorBuffer, INDICATOR_COLOR_INDEX); + + // + // Data Buffers ... + + // + // Fast State ... + ArraySetAsSeries(maFastStateBuffer, true); + SetIndexBuffer(maFastStateBufferIndex, maFastStateBuffer, INDICATOR_CALCULATIONS); + + // + // Mid State ... + ArraySetAsSeries(maMidStateBuffer, true); + SetIndexBuffer(maMidStateBufferIndex, maMidStateBuffer, INDICATOR_CALCULATIONS); + + // + // Slow State ... + ArraySetAsSeries(maSlowStateBuffer, true); + SetIndexBuffer(maSlowStateBufferIndex, maSlowStateBuffer, INDICATOR_CALCULATIONS); +} + +/** + * Calculate Custom Buffers ... + * + * @param bar_index: Integer, Represent Current Bar ... + * @param prevCalculated: Integer, Represent Previous Calculated Bars ... + * @param ratesTotal: Integer, Represents All Available Bars ... + * @param open: Double Array, History of Open Prices ... + * @param high: Double Array, History of High Prices ... + * @param close: Double Array, History of Close Prices ... + * @param low: Double Array, History of Low Prices ... + * @param tickVolume: Long, History of Tick Volumes on Bar ... + */ +void CalculateBuffers( + int bar_index, // Selected Bar Index + const int prevCalculated, + const int ratesTotal, + const double &open[], + const double &high[], + const double &close[], + const double &low[], + const long &tickVolume[] // +) +{ + // + int barsLimit = startCalculationForLastBars > 0 + ? startCalculationForLastBars + : 0; + // + // bool canCalculate = true; + bool canCalculate = + barsLimit == 0 || + bar_index <= barsLimit; + if (canCalculate) + { + // + CalculateValues( + bar_index, + prevCalculated, + ratesTotal, + open, + high, + close, + low, + tickVolume // + ); + } + else + { + FillBuffersZero(bar_index); + } +} + +/** + * Fill All Bufers to Zero Vlue for Specified Bar Index ... + * + * @param barIndex: Integer ... + */ +void FillBuffersZero(int barIndex) +{ + // + maMidBuffer[barIndex] = 0; + maFastBuffer[barIndex] = 0; + maSlowBuffer[barIndex] = 0; + + // + maFastColorBuffer[barIndex] = hideColorIDX; + maMidColorBuffer[barIndex] = hideColorIDX; + maSlowColorBuffer[barIndex] = hideColorIDX; + maFastStateBuffer[barIndex] = hideColorIDX; + maMidStateBuffer[barIndex] = hideColorIDX; + maSlowStateBuffer[barIndex] = hideColorIDX; +} + +/** + * Calculate Vales ... + * + * @param bar_index: Integer, Represent Current Bar ... + * @param prevCalculated: Integer, Represent Previous Calculated Bars ... + * @param ratesTotal: Integer, Represents All Available Bars ... + * @param open: Double Array, History of Open Prices ... + * @param high: Double Array, History of High Prices ... + * @param close: Double Array, History of Close Prices ... + * @param low: Double Array, History of Low Prices ... + * @param tickVolume: Long, History of Tick Volumes on Bar ... + */ +void CalculateValues( + int bar_index, // Selected Bar Index + const int prevCalculated, + const int ratesTotal, + const double &open[], + const double &high[], + const double &close[], + const double &low[], + const long &tickVolume[] // +) +{ + // + // Implement Colorify Mechanism ... + + // + // Calculate Applied Price ... + double iPrice = GetAppliedPrice( + maAppliedTo, + open, + high, + low, + close, + bar_index // + ); + + // + double iFast = maFastBuffer[bar_index]; + double iMid = maMidBuffer[bar_index]; + double iSlow = maSlowBuffer[bar_index]; + + // + double iFastState = iPrice > iFast + ? bullishColorIDX + : iPrice < iFast + ? bearishColorIDX + : neuturalColorIDX; + double iMidState = iPrice > iMid + ? bullishColorIDX + : iPrice < iMid + ? bearishColorIDX + : neuturalColorIDX; + double iSlowState = iPrice > iSlow + ? bullishColorIDX + : iPrice < iSlow + ? bearishColorIDX + : neuturalColorIDX; + + // + maFastColorBuffer[bar_index] = + showMaFast + ? iFastState + : hideColorIDX; + maFastStateBuffer[bar_index] = iFastState; + + // + maMidColorBuffer[bar_index] = + showMaMid + ? iMidState + : hideColorIDX; + maMidStateBuffer[bar_index] = iMidState; + + // + maSlowColorBuffer[bar_index] = + showMaSlow + ? iSlowState + : hideColorIDX; + maSlowStateBuffer[bar_index] = iSlowState; +} + +// diff --git a/X121SMCEA/Signals/x-121.smc.xwz.signal.class.mq5 b/X121SMCEA/Signals/x-121.smc.xwz.signal.class.mq5 index 181a0555..60026085 100644 --- a/X121SMCEA/Signals/x-121.smc.xwz.signal.class.mq5 +++ b/X121SMCEA/Signals/x-121.smc.xwz.signal.class.mq5 @@ -361,6 +361,14 @@ class XCX121SMCXWZSignalDetector : public XCBase Print("Has Inside Zones"); } + // + bool hasBreakedZones = hasWorkingZone && + workingZone.HasBreakedZones(); + if (hasBreakedZones) + { + Print("Has Breaked Zones"); + } + // UpdateTo(cTime); diff --git a/X121SMCEA/Signals/x-121.smc.xwz.signal.lib.mq5 b/X121SMCEA/Signals/x-121.smc.xwz.signal.lib.mq5 index b1b8e367..c33d7497 100644 --- a/X121SMCEA/Signals/x-121.smc.xwz.signal.lib.mq5 +++ b/X121SMCEA/Signals/x-121.smc.xwz.signal.lib.mq5 @@ -75,6 +75,7 @@ struct X121SMCStrategyXWZWorkingZone // XOBFVG insideZones[]; + XOBFVG breakedZones[]; XOBFVG waitingZones[]; // @@ -109,6 +110,7 @@ struct X121SMCStrategyXWZWorkingZone // Clean(insideZones); Clean(waitingZones); + Clean(breakedZones); // Clean(supportZones); @@ -432,70 +434,72 @@ struct X121SMCStrategyXWZWorkingZone return; } - // // - // // Supply Zones ... - // count = mtfState.FilterSupplyZones( - // supplyZones, - // NULL, - // XZONE_STATE_PASSED, - // false, - // XZONE_STATE_FAILED, - // false, - // XZONE_STATE_FAILED, - // false, - // XZONE_STATE_IGNORED, - // false // - // ); - - // // - // // Demand Zones ... - // count = mtfState.FilterDemandZones( - // demandZones, - // NULL, - // XZONE_STATE_PASSED, - // false, - // XZONE_STATE_FAILED, - // false, - // XZONE_STATE_FAILED, - // false, - // XZONE_STATE_IGNORED, - // false // - // ); - - // // - // // Support Zones ... - // count = stfState.FilterSupportZones( - // supportZones, - // period, - // XZONE_STATE_PASSED, - // false, - // XZONE_STATE_FAILED, - // false, - // XZONE_STATE_FAILED, - // false, - // XZONE_STATE_IGNORED, - // false // - // ); - - // // - // // Resistance Zones ... - // count = stfState.FilterResistanceZones( - // resistanceZones, - // period, - // XZONE_STATE_PASSED, - // false, - // XZONE_STATE_FAILED, - // false, - // XZONE_STATE_FAILED, - // false, - // XZONE_STATE_IGNORED, - // false // - // ); + // + // Supply Zones ... + count = mtfState.FilterSupplyZones( + supplyZones, + NULL, + XZONE_STATE_PASSED, + false, + XZONE_STATE_FAILED, + false, + XZONE_STATE_FAILED, + false, + XZONE_STATE_IGNORED, + false // + ); // - // Detect price Inside Zones ... + // Demand Zones ... + count = mtfState.FilterDemandZones( + demandZones, + NULL, + XZONE_STATE_PASSED, + false, + XZONE_STATE_FAILED, + false, + XZONE_STATE_FAILED, + false, + XZONE_STATE_IGNORED, + false // + ); // + // Support Zones ... + count = stfState.FilterSupportZones( + supportZones, + period, + XZONE_STATE_PASSED, + false, + XZONE_STATE_FAILED, + false, + XZONE_STATE_FAILED, + false, + XZONE_STATE_IGNORED, + false // + ); + + // + // Resistance Zones ... + count = stfState.FilterResistanceZones( + resistanceZones, + period, + XZONE_STATE_PASSED, + false, + XZONE_STATE_FAILED, + false, + XZONE_STATE_FAILED, + false, + XZONE_STATE_IGNORED, + false // + ); + + // + // + // + + // + // Detect Price Inside Zones ... has = HasWaitingZones(); if (has) { @@ -520,16 +524,25 @@ struct X121SMCStrategyXWZWorkingZone if (isInside) { // - Add( - i, - mustRemove // - ); - - // - AddRef( + idx = FindIndex( iOBFVG, insideZones // ); + has = IsValidSize(idx); + if (!has) + { + // + Add( + i, + mustRemove // + ); + + // + AddRef( + iOBFVG, + insideZones // + ); + } } } @@ -549,9 +562,97 @@ struct X121SMCStrategyXWZWorkingZone ); } } + + // + Clean(mustRemove); } // + // Detect Inside Zones ... + has = HasInsideZones(); + if (has) + { + // + int mustRemove[]; + count = CountInsideZones(); + for (int i = 0; i < count; i++) + { + // + XOBFVG iOBFVG = insideZones[i]; + ENUM_X_DIRECTION iDir = iOBFVG.GetDirection(); + bool isBullish = IsBullish(iDir); + + // + bool isBullishBreaked = + // + zBar.high < iOBFVG.ob.Lower() && + cBar.high < iOBFVG.ob.Lower() + // + ; + + // + bool isBearishBreaked = + // + zBar.low > iOBFVG.ob.Upper() && + cBar.low > iOBFVG.ob.Upper() + // + ; + + // + bool isBreaked = + isBullish + ? isBullishBreaked + : isBearishBreaked; + + // + if (isBreaked) + { + // + idx = FindIndex( + iOBFVG, + breakedZones // + ); + has = IsValidIndex(idx); + if (!has) + { + // + Add( + i, + mustRemove // + ); + + // + AddRef( + iOBFVG, + breakedZones // + ); + } + } + } + + // + has = HasChild(mustRemove); + if (has) + { + // + count = ArraySize(mustRemove); + for (int i = 0; i < count; i++) + { + // + ArrayRemove( + insideZones, + mustRemove[i], + 1 // + ); + } + } + + // + Clean(mustRemove); + } + + // + // Adding New OBFVG to Waiting Zones if not Exists ... XOBFVG tmpWaitingZones[]; count = ExtractOBFVGs( tmpWaitingZones, @@ -682,6 +783,18 @@ struct X121SMCStrategyXWZWorkingZone return ArraySize(insideZones); } + // + bool HasBreakedZones() + { + return HasChild(breakedZones); + } + + // + int CountBreakedZones() + { + return ArraySize(breakedZones); + } + // // //