diff --git a/Classes/x-saherelm.x-poi.drawer.class.mq5 b/Classes/x-saherelm.x-poi.drawer.class.mq5 index f96c4619..f1185c31 100644 --- a/Classes/x-saherelm.x-poi.drawer.class.mq5 +++ b/Classes/x-saherelm.x-poi.drawer.class.mq5 @@ -239,8 +239,7 @@ class XCPOIDrawer : public XCBase string prefix = Prefix(); ulong chartId = ChartIdentification(); int window = SubWindowIdentification(); - to = - IsSpecifedValid(to) + to = IsSpecifiedValid(to) ? to : ((datetime)((int)bar.time) + (PeriodSeconds(bar.period) - 1)); object = new XCOHCLObject(); @@ -871,7 +870,7 @@ class XCPOIDrawer : public XCBase object != NULL; if (!has) { - return result; + return; } // diff --git a/Documents/BKP/tmp.backtester.codes.mq5 b/Documents/BKP/tmp.backtester.codes.mq5 index 47bd1e6e..be71b017 100644 --- a/Documents/BKP/tmp.backtester.codes.mq5 +++ b/Documents/BKP/tmp.backtester.codes.mq5 @@ -645,94 +645,91 @@ bool CreateMainPanel() // edtPositionId.TextAlign(ALIGN_LEFT); // } - ////////////////////////////////////////////////////////////////// - // CButton btnPauseReplay; bool isVisualTesterPaused = false; - // - // Initial Btn Pause Play ... - int subWindow = 0; - int defaultGap = 5; - int defaultWidth = 100; - int defaultHeight = 30; - ulong chartId = eaExpert.eaChartHelper.ChartId(); - - // - int btnPauseReplayX1 = defaultGap; - int btnPauseReplayX2 = btnPauseReplayX1 + defaultWidth; - int btnPauseReplayY1 = eaExpert.eaChartHelper.Height() - defaultHeight - defaultGap; - int btnPauseReplayY2 = btnPauseReplayY1 + defaultHeight; - - // - string btnPauseReplayName = ShortName + "BTN_PAUSE_REPLAY"; - - // - isInited = btnPauseReplay.Create( - chartId, - btnPauseReplayName, - subWindow, - btnPauseReplayX1, - btnPauseReplayY1, - btnPauseReplayX2, - btnPauseReplayY2 // - ); - if (isInited) - { - // - // Apply Style ... - string btnPauseReplayText = - isVisualTesterPaused - ? "Play" - : "Pause"; - btnPauseReplay.Text(btnPauseReplayText); - btnPauseReplay.Color(clrAzure); - btnPauseReplay.ColorBackground(clrBlack); - } +// +// Initial Btn Pause Play ... +int subWindow = 0; +int defaultGap = 5; +int defaultWidth = 100; +int defaultHeight = 30; +ulong chartId = eaExpert.eaChartHelper.ChartId(); // +int btnPauseReplayX1 = defaultGap; +int btnPauseReplayX2 = btnPauseReplayX1 + defaultWidth; +int btnPauseReplayY1 = eaExpert.eaChartHelper.Height() - defaultHeight - defaultGap; +int btnPauseReplayY2 = btnPauseReplayY1 + defaultHeight; + +// +string btnPauseReplayName = ShortName + "BTN_PAUSE_REPLAY"; + +// +isInited = btnPauseReplay.Create( + chartId, + btnPauseReplayName, + subWindow, + btnPauseReplayX1, + btnPauseReplayY1, + btnPauseReplayX2, + btnPauseReplayY2 // +); +if (isInited) +{ // - // Update btn Pause Replay ... + // Apply Style ... string btnPauseReplayText = isVisualTesterPaused ? "Play" : "Pause"; btnPauseReplay.Text(btnPauseReplayText); - if (IsRunningeOnTestVisualMode()) + btnPauseReplay.Color(clrAzure); + btnPauseReplay.ColorBackground(clrBlack); +} + +// +// +// Update btn Pause Replay ... +string btnPauseReplayText = + isVisualTesterPaused + ? "Play" + : "Pause"; +btnPauseReplay.Text(btnPauseReplayText); +if (IsRunningeOnTestVisualMode()) +{ + // + btnPauseReplay.Show(); + btnPauseReplay.Enable(); +} +else +{ + // + btnPauseReplay.Hide(); + btnPauseReplay.Enable(); +} +if (btnPauseReplay.Pressed()) +{ + // + if (btnPauseReplay.IsEnabled()) { // - btnPauseReplay.Show(); - btnPauseReplay.Enable(); - } - else - { - // - btnPauseReplay.Hide(); - btnPauseReplay.Enable(); - } - if (btnPauseReplay.Pressed()) - { - // - if (btnPauseReplay.IsEnabled()) - { - // - HandlePauseStrategyTester(); - - // - btnPauseReplayText = - isVisualTesterPaused - ? "Play" - : "Pause"; - btnPauseReplay.Text(btnPauseReplayText); - } + HandlePauseStrategyTester(); // - btnPauseReplay.Pressed(false); + btnPauseReplayText = + isVisualTesterPaused + ? "Play" + : "Pause"; + btnPauseReplay.Text(btnPauseReplayText); } + // + btnPauseReplay.Pressed(false); +} void HandlePauseStrategyTester() { @@ -745,3 +742,180 @@ void HandlePauseStrategyTester() keybd_event(VK_SPACE, 0, 0, 0); keybd_event(VK_SPACE, 0, KEYEVENTF_KEYUP, 0); } + +/////////////////////////// +/// XFIMA ... +/////////////////////////// + +// +// ZIGZAG Pattern ... +// if (detectZIGZAGPatterns) +// { +// // +// // Detect Market Analysis ... +// XZigZagAnalysis zAnalysis; +// has = fima2Signaller.fimaPOIDetector.AnalyseMarketUsingZigZag( +// cBar, +// cBar.period, +// fima2Signaller.fimaHelper, +// zAnalysis, +// 15 // +// ); +// if (has) +// { +// // +// Print(ShortName + zAnalysis.GetMessage()); + +// // +// XCZigZagAnalysisObject *iObj; +// has = fima2Signaller.fimaPOIDrawer.DrawZigZagMarketAnalysisByPV( +// iObj, +// zAnalysis // +// ); +// if (has) +// { +// draws.Add(iObj); +// } +// } +// } + +// +// Reading Pivots ... +double iLows[]; +double iHighs[]; +double lowTo = 0; +double highTo = 0; +double lowFrom = 0; +double highFrom = 0; +datetime lowToTime = NULL; +datetime highToTime = NULL; +datetime lowFromTime = NULL; +datetime highFromTime = NULL; +double lowsSlope = EMPTY_VALUE; +double highsSlope = EMPTY_VALUE; +double lowsIntercept = EMPTY_VALUE; +double highsIntercept = EMPTY_VALUE; + +// +int iLoopbackLength = 50; +for (int i = 0; i < iLoopbackLength; i++) +{ + // + double iLowPrice = iLow(_Symbol, _Period, i); + double iHighPrice = iHigh(_Symbol, _Period, i); + + // + Add( + iLowPrice, + iLows // + ); + Add( + iHighPrice, + iHighs // + ); +} +has = CalculateLinearRegression( + lowsSlope, + lowsIntercept, + iLows, + 0, + 0 // +); +if (has) +{ + // + lowToTime = GetBarTime( + _Symbol, + _Period, + 0 // + ); + lowTo = CalculateValueBySlopeIntercept( + lowsSlope, + lowsIntercept, + 0 // + ); + + // + lowFromTime = GetBarTime( + _Symbol, + _Period, + iLoopbackLength // + ); + lowFrom = CalculateValueBySlopeIntercept( + lowsSlope, + lowsIntercept, + iLoopbackLength // + ); +} + +// +has = CalculateLinearRegression( + highsSlope, + highsIntercept, + iHighs, + 0, + 0 // +); +if (has) +{ + // + highToTime = GetBarTime( + _Symbol, + _Period, + 0 // + ); + highTo = CalculateValueBySlopeIntercept( + highsSlope, + highsIntercept, + 0 // + ); + + // + highFromTime = GetBarTime( + _Symbol, + _Period, + iLoopbackLength // + ); + highFrom = CalculateValueBySlopeIntercept( + highsSlope, + highsIntercept, + iLoopbackLength // + ); +} + +// +// Try to Draw ... + +// +// Low ... +CChartObjectTrend *iLowTrend; +has = fima2Signaller.fimaPOIDrawer.DrawTrendLine( + lowFrom, + lowFromTime, + lowTo, + lowToTime, + iLowTrend, + "LOW_TREMD", + true // Ray Right ... +); +if (has) +{ + draws.Add(iLowTrend); +} + +// +// High ... +CChartObjectTrend *iHighTrend; +has = fima2Signaller.fimaPOIDrawer.DrawTrendLine( + highFrom, + highFromTime, + highTo, + highToTime, + iHighTrend, + "HIGH_TREMD", + true // Ray Right ... +); +if (has) +{ + draws.Add(iHighTrend); +} diff --git a/Experts/x-saherelm.xfima.backtester.ea.mq5 b/Experts/x-saherelm.xfima.backtester.ea.mq5 index 1eb521d1..2012001a 100644 --- a/Experts/x-saherelm.xfima.backtester.ea.mq5 +++ b/Experts/x-saherelm.xfima.backtester.ea.mq5 @@ -159,7 +159,6 @@ int eaCTHandler = INVALID_HANDLE; // Candle Timer Handler ... // bool eaIsNewBar; // Check New Bar ... -XZigZagAnalysis zigzagAnalysis; ENUM_X_DIRECTION trendDir = X_DIRECTION_NONE; // @@ -816,7 +815,6 @@ void DetectPatterns() // // XFIMA Patetrns ... bool detectFIMAPatterns = true; - bool detectZIGZAGPatterns = true; // // Checking Momentum Bar ... @@ -1145,178 +1143,6 @@ void DetectPatterns() // Detecting FIMA Patterns ... if (detectFIMAPatterns) { - // - // ZIGZAG Pattern ... - // if (detectZIGZAGPatterns) - // { - // // - // // Detect Market Analysis ... - // XZigZagAnalysis zAnalysis; - // has = fima2Signaller.fimaPOIDetector.AnalyseMarketUsingZigZag( - // cBar, - // cBar.period, - // fima2Signaller.fimaHelper, - // zAnalysis, - // 15 // - // ); - // if (has) - // { - // // - // Print(ShortName + zAnalysis.GetMessage()); - - // // - // XCZigZagAnalysisObject *iObj; - // has = fima2Signaller.fimaPOIDrawer.DrawZigZagMarketAnalysisByPV( - // iObj, - // zAnalysis // - // ); - // if (has) - // { - // draws.Add(iObj); - // } - // } - // } - - // - // Reading Pivots ... - double iLows[]; - double iHighs[]; - double lowTo = 0; - double highTo = 0; - double lowFrom = 0; - double highFrom = 0; - datetime lowToTime = NULL; - datetime highToTime = NULL; - datetime lowFromTime = NULL; - datetime highFromTime = NULL; - double lowsSlope = EMPTY_VALUE; - double highsSlope = EMPTY_VALUE; - double lowsIntercept = EMPTY_VALUE; - double highsIntercept = EMPTY_VALUE; - - // - int iLoopbackLength = 50; - for (int i = 0; i < iLoopbackLength; i++) - { - // - double iLowPrice = iLow(_Symbol, _Period, i); - double iHighPrice = iHigh(_Symbol, _Period, i); - - // - Add( - iLowPrice, - iLows // - ); - Add( - iHighPrice, - iHighs // - ); - } - has = CalculateLinearRegression( - lowsSlope, - lowsIntercept, - iLows, - 0, - 0 // - ); - if (has) - { - // - lowToTime = GetBarTime( - _Symbol, - _Period, - 0 // - ); - lowTo = CalculateValueBySlopeIntercept( - lowsSlope, - lowsIntercept, - 0 // - ); - - // - lowFromTime = GetBarTime( - _Symbol, - _Period, - iLoopbackLength // - ); - lowFrom = CalculateValueBySlopeIntercept( - lowsSlope, - lowsIntercept, - iLoopbackLength // - ); - } - - // - has = CalculateLinearRegression( - highsSlope, - highsIntercept, - iHighs, - 0, - 0 // - ); - if (has) - { - // - highToTime = GetBarTime( - _Symbol, - _Period, - 0 // - ); - highTo = CalculateValueBySlopeIntercept( - highsSlope, - highsIntercept, - 0 // - ); - - // - highFromTime = GetBarTime( - _Symbol, - _Period, - iLoopbackLength // - ); - highFrom = CalculateValueBySlopeIntercept( - highsSlope, - highsIntercept, - iLoopbackLength // - ); - } - - // - // Try to Draw ... - - // - // Low ... - CChartObjectTrend *iLowTrend; - has = fima2Signaller.fimaPOIDrawer.DrawTrendLine( - lowFrom, - lowFromTime, - lowTo, - lowToTime, - iLowTrend, - "LOW_TREMD", - true // Ray Right ... - ); - if (has) - { - draws.Add(iLowTrend); - } - - // - // High ... - CChartObjectTrend *iHighTrend; - has = fima2Signaller.fimaPOIDrawer.DrawTrendLine( - highFrom, - highFromTime, - highTo, - highToTime, - iHighTrend, - "HIGH_TREMD", - true // Ray Right ... - ); - if (has) - { - draws.Add(iHighTrend); - } } // diff --git a/XFIMAEA/Classes/x-saherelm.xfima.x-poi.detector.class.mq5 b/XFIMAEA/Classes/x-saherelm.xfima.x-poi.detector.class.mq5 index 7cdde8cf..89a472a5 100644 --- a/XFIMAEA/Classes/x-saherelm.xfima.x-poi.detector.class.mq5 +++ b/XFIMAEA/Classes/x-saherelm.xfima.x-poi.detector.class.mq5 @@ -56,277 +56,6 @@ class XCXFIMAPOIDetector : public XCMarketPatternDetector // // Actions and Tools ... - /** - * Get Specified Number of Recent ZigZag Points ... - * - * @param bar: XOHCL, Specified Bar for Start ... - * @param fimaHelper: XCXFIMAHelper, instance of XFIMA Helper ... - * @param zigzagPoints: XZigZagPoint, Array of Founded Points ... - * @param numberOfRequiredPivots: int, Specified Number of required ZigZag Points ... - * - * @return ( int ) - */ - int GetZigZagPivot( - XOHCL &bar, - XCXFIMAHelper *&fimaHelper, - XZigZagPivot &zigzagPivots[], - int numberOfRequiredPivots = 5 // - ) - { - // - int result = 0; - - // - // Normalize ... - SpecifiedClean(zigzagPivots); - numberOfRequiredPivots = NormalizeInt(numberOfRequiredPivots, 3); - - // - // Validate ... - result = bar.IsValid() && - bar.symbol == fimaHelper.GetSymbol() && - bar.period == fimaHelper.GetPeriod(); - if (!result) - { - return result; - } - - // - // Update Fima Helper ... - int index = bar.Index(); - fimaHelper.Update(index); - - // - // Start Reading ZigZag Values ... - double iZG = 0; - double iPZG = 0; - int iZGIdx = -1; - int iPZGIdx = -1; - bool has = false; - int start = index; - XZigZagPivot iZGPoint; - datetime iZGTime = NULL; - datetime iPZGTime = NULL; - int lastZGDetected = start; - ENUM_X_DIRECTION iDir = X_DIRECTION_NONE; - ENUM_X_ZIGZAG_POINT_TYPE iType = X_ZIGZAG_PIVOT_TYPE_NONE; - while (ArraySize(zigzagPivots) < numberOfRequiredPivots) - { - // - // Reading Last two ZigZag Value ... - - // - // Detect Last ZG Value ... - has = GetZigZagValue(lastZGDetected, iZG, iZGIdx, iZGTime, fimaHelper); - if (!has) - { - // - iZG = 0; - iPZG = 0; - iZGIdx = -1; - iPZGIdx = -1; - iZGTime = NULL; - iPZGTime = NULL; - iZGPoint.Clean(); - iDir = X_DIRECTION_NONE; - iType = X_ZIGZAG_PIVOT_TYPE_NONE; - - // - start++; - lastZGDetected++; - - // - continue; - } - - // - // Detect Previous ZG Value ... - has = GetZigZagValue(lastZGDetected + 1, iPZG, iPZGIdx, iPZGTime, fimaHelper); - if (!has) - { - // - iZG = 0; - iPZG = 0; - iZGIdx = -1; - iPZGIdx = -1; - iZGTime = NULL; - iPZGTime = NULL; - iZGPoint.Clean(); - iDir = X_DIRECTION_NONE; - iType = X_ZIGZAG_PIVOT_TYPE_NONE; - - // - start++; - lastZGDetected++; - - // - continue; - } - - // - // Check Direction ... - iDir = - (iZG > iPZG) - ? X_DIRECTION_BULLISH - : (iZG < iPZG) - ? X_DIRECTION_BEARISH - : X_DIRECTION_NONE; - - // - // Check Point Type ... - iType = - IsSpecifiedBullish(iDir) - ? X_ZIGZAG_PIVOT_TYPE_PEAK - : IsSpecifiedBearish(iDir) - ? X_ZIGZAG_PIVOT_TYPE_VALE - : X_ZIGZAG_PIVOT_TYPE_NONE; - has = - IsXValid(iType) && - HasDirection(iDir); - if (!has) - { - // - iZG = 0; - iPZG = 0; - iZGIdx = -1; - iPZGIdx = -1; - iZGTime = NULL; - iPZGTime = NULL; - iZGPoint.Clean(); - iDir = X_DIRECTION_NONE; - iType = X_ZIGZAG_PIVOT_TYPE_NONE; - - // - start++; - lastZGDetected++; - - // - continue; - } - - // - // Initialize ZigZag Point ... - has = iZGPoint.Init( - iZG, - iZGTime, - bar.symbol, - iDir, - bar.period, - iType // - ); - if (has) - { - // - iZGPoint.AddIfNotExists(zigzagPivots); - lastZGDetected = iZGIdx + 1; - } - - // - // Reset Resources ... - iZG = 0; - iPZG = 0; - iZGIdx = -1; - iPZGIdx = -1; - iZGTime = NULL; - iPZGTime = NULL; - iZGPoint.Clean(); - iDir = X_DIRECTION_NONE; - iType = X_ZIGZAG_PIVOT_TYPE_NONE; - - // - start++; - } - - // - result = ArraySize(zigzagPivots); - - // - return result; - } - - // - bool AnalyseMarketUsingZigZag( - XOHCL &bar, - ENUM_TIMEFRAMES forPeriod, - XCXFIMAHelper *&fimaHelper, - XZigZagAnalysis &analysis, - int numberOfRequiredPivots = 20 // - ) - { - // - bool result = false; - - // - // Prepare ... - analysis.Clean(); - - // - // Validate ... - result = bar.IsValid(); - if (!result) - { - return result; - } - - // - // Normalize ... - numberOfRequiredPivots = NormalizeInt(numberOfRequiredPivots, 5); - if (!IsSpecifiedValid(forPeriod)) - { - forPeriod = bar.period; - } - - // - // Reading XZigZag Points ... - XOHCL iPeriodBar; - result = iPeriodBar.Init( - bar.symbol, - forPeriod, - bar.Index() // - ); - if (!result) - { - // - iPeriodBar.Clean(); - return result; - } - - // - // Reading Required Pivots ... - XZigZagPivot pivots[]; - int pivotsCount = GetZigZagPivot( - iPeriodBar, - fimaHelper, - pivots, - numberOfRequiredPivots // - ); - result = IsValidSize(pivotsCount); - if (!result) - { - // - analysis.Clean(); - iPeriodBar.Clean(); - SpecifiedClean(pivots); - } - - // - // Initialize Analysis Model ... - result = analysis.Analyse( - bar, - pivots // - ); - if (!result) - { - // - analysis.Clean(); - iPeriodBar.Clean(); - SpecifiedClean(pivots); - } - - // - return result; - } - // // Tools ... @@ -349,102 +78,6 @@ class XCXFIMAPOIDetector : public XCMarketPatternDetector // // Tools / Actions ... - /** - * Read Specified Value of ZigZag ... - * - * @param fromBarIndex: int, Specified Start Bar Index ... - * @param value: double, reference to Hold ZigZag Value ... - * @param index: int, reference to Hold ZigZag Value's Bar Index ... - * @param time: datetime, reference to Hold ZigZag Value's Time ... - * @param maxAllowedLoopback: int, Maximum Allowe Loopback to Detect Specified Point ... - * - * @return ( bool ) - */ - bool GetZigZagValue( - int fromBarIndex, - double &value, - int &index, - datetime &time, - XCXFIMAHelper *&fimaHelper, - int maxAllowedLoopback = 100 // - ) - { - // - bool result = false; - - // - // Normalize ... - fromBarIndex = NormalizeInt(fromBarIndex, 0); - maxAllowedLoopback = NormalizeInt(maxAllowedLoopback, 10, 100); - - // - // Prepare ... - value = 0; - index = -1; - time = NULL; - - // - // Validate ... - result = fimaHelper != NULL; - if (!result) - { - return result; - } - - // - int start = fromBarIndex; - int end = start + maxAllowedLoopback; - while (start < end) - { - // - fimaHelper.Update(start); - - // - value = fimaHelper.GetZigZag(start); - result = value > 0 && value != EMPTY_VALUE; - if (result) - { - // - time = GetBarTime( - fimaHelper.GetSymbol(), - fimaHelper.GetPeriod(), - start // - ); - - // - index = start; - - // - break; - } - - // - value = 0; - index = -1; - time = NULL; - - // - start++; - } - - // - result = - value > 0 && - IsValidIndex(index) && - value != EMPTY_VALUE && - IsSpecifiedValid(time); - if (!result) - { - // - value = 0; - index = -1; - time = NULL; - } - - // - return result; - } - // // Private ... private: diff --git a/XFIMAEA/Classes/x-saherelm.xfima.x-poi.drawer.class.mq5 b/XFIMAEA/Classes/x-saherelm.xfima.x-poi.drawer.class.mq5 index 63c4eb91..5a3479d8 100644 --- a/XFIMAEA/Classes/x-saherelm.xfima.x-poi.drawer.class.mq5 +++ b/XFIMAEA/Classes/x-saherelm.xfima.x-poi.drawer.class.mq5 @@ -33,179 +33,6 @@ class XCXFIMAPOIDrawer : public XCPOIDrawer public: // - /** - * Draw Specified XZigZagMarketStyle on Chart ... - * - * @param object: XCZigZagAnalysisObject, pointer reference to Drawn Object ... - * @param model: XZigZagAnalysis, reference to Model ... - * @param numberOfPivots: int, required number of pivots for Draw Trends ... - * @param rayRight: bool, Ray To Right ... - * @param rayLeft: bool, Ray To Left ... - * - * @return ( bool ) - */ - bool DrawZigZagMarketAnalysisByPV( - XCZigZagAnalysisObject *&object, - XZigZagAnalysis &model, - bool rayRight = false, - bool rayLeft = false, - int numberOfPivots = 2 // - ) - { - // - bool result = false; - - // - // Prepare ... - object = NULL; - - // - // Validate ... - result = model.IsValid(); - if (!result) - { - return result; - } - - // - object = new XCZigZagAnalysisObject(); - result = object.CreateByPV( - ChartIdentification(), - SubWindowIdentification(), - model // - ); - if (result) - { - // - // Apply Style ... - bool isBullish = model.IsBullish(); - - // - XPOIStyle style; - if (isBullish) - { - style = defaultBullishStyle; - } - else - { - style = defaultBearishStyle; - } - - // - ApplyZigZagMarketAnalysisStyle( - object, - style, - true, // Ray Right ... - false // Ray Left ... - ); - } - - // - return result; - } - - /** - * Draw Specified XZigZagMarketStyle on Chart ... - * - * @param object: XCZigZagAnalysisObject, pointer reference to Drawn Object ... - * @param model: XZigZagAnalysis, reference to Model ... - * @param numberOfPivots: int, required number of pivots for Draw Trends ... - * @param rayRight: bool, Ray To Right ... - * @param rayLeft: bool, Ray To Left ... - * - * @return ( bool ) - */ - bool DrawZigZagMarketAnalysisByRecent( - XCZigZagAnalysisObject *&object, - XZigZagAnalysis &model, - bool rayRight = false, - bool rayLeft = false, - int numberOfPivots = 2 // - ) - { - // - bool result = false; - - // - // Prepare ... - object = NULL; - - // - // Validate ... - result = model.IsValid(); - if (!result) - { - return result; - } - - // - object = new XCZigZagAnalysisObject(); - result = object.CreateByRecent( - ChartIdentification(), - SubWindowIdentification(), - model, - numberOfPivots // - ); - if (result) - { - // - // Apply Style ... - bool isBullish = model.IsBullish(); - - // - XPOIStyle style; - if (isBullish) - { - style = defaultBullishStyle; - } - else - { - style = defaultBearishStyle; - } - - // - ApplyZigZagMarketAnalysisStyle( - object, - style, - true, // Ray Right ... - false // Ray Left ... - ); - } - - // - return result; - } - - /** - * Apply Specified Style to Object ... - */ - void ApplyZigZagMarketAnalysisStyle( - XCZigZagAnalysisObject *&object, - XPOIStyle &style, - bool rayRight = false, - bool rayLeft = false // - ) - { - // - bool has = object != NULL; - if (!has) - { - return; - } - - // - object.UpperColor(style.clr); - object.LowerColor(style.clr); - object.UpperWidth(style.width); - object.UpperRayLeft(rayLeft); - object.UpperRayRight(rayRight); - object.LowerWidth(style.width); - object.UpperStyle(style.style); - object.LowerStyle(style.style); - object.LowerRayLeft(rayLeft); - object.LowerRayRight(rayRight); - } - // }; diff --git a/XFIMAEA/Libraries/x-saherelm.xfima.lib.mq5 b/XFIMAEA/Libraries/x-saherelm.xfima.lib.mq5 index 280f1b00..d9ae7862 100644 --- a/XFIMAEA/Libraries/x-saherelm.xfima.lib.mq5 +++ b/XFIMAEA/Libraries/x-saherelm.xfima.lib.mq5 @@ -28,1374 +28,6 @@ // // Definitions ... -// -// Enumerations ... - -// -// Point Type ... - // // Structs ... -/** - * Specified ZigZag based Market Analysis ... - **/ -struct XFIMAAnalysis -{ - // - // Props ... - string symbol; // Symbol ... - datetime time; // Time ... - ENUM_TIMEFRAMES period; // Period ... - XZigZagPivot pivots[]; // ZigZag Pivots ... - - // - // Calculated Values ... - double peakTo; // Recent Peak .. - double valeTo; // Recent Vale ... - double peakFrom; // Previous Peak ... - double valeFrom; // Previous Vale ... - double peakSlope; // Calculated Slope for Peak ... - double valeSlope; // Calculated Slope for Vale ... - datetime peakToTime; // Recent Peak Time ... - datetime valeToTime; // Recent Vale Time ... - double peakIntercept; // Calculated Intercept for Peak ... - double valeIntercept; // Calculated Intercept for Vale ... - ENUM_X_DIRECTION dir; // Direction ... - datetime peakFromTime; // Previous Peak Time ... - datetime valeFromTime; // Previous Vale Time ... - - // - // Constructor ... - XZigZagAnalysis() - { - Clean(); - } - - // - // Initialize ... - - /** - * Analyse Market Structure ... - * - * @param bar: XOHCL, Specified Bar ... - * @param _pivots: XZigZagPivot, Specified ZigZag Points Collection ... - * - * @return ( bool ) - */ - bool Analyse( - XOHCL &bar, - XZigZagPivot &_pivots[] // - ) - { - // - bool result = false; - - // - // Validate ... - result = bar.IsValid() && - HasChild(_pivots) && - bar.symbol == _pivots[0].symbol; - if (!result) - { - // - Clean(); - return result; - } - - // - // Assign Requirements ... - - // - time = bar.time; - symbol = bar.symbol; - period = bar.period; - Copy(_pivots, pivots); - - // - int oldestPeakIdx = GetOldest(pivots, X_PIVOT_TYPE_PEAK); - int oldestValeIdx = GetOldest(pivots, X_ZIGZAG_PIVOT_TYPE_VALE); - int youngestPeakIdx = GetYoungest(pivots, X_PIVOT_TYPE_PEAK); - int youngestValeIdx = GetYoungest(pivots, X_ZIGZAG_PIVOT_TYPE_VALE); - - // - // Calculate Peaks and Vales Regressions ... - - // - // Peak Time ... - peakToTime = pivots[youngestPeakIdx].time; - peakFromTime = pivots[oldestPeakIdx].time; - - // - // Vale Time ... - valeToTime = pivots[youngestValeIdx].time; - valeFromTime = pivots[oldestValeIdx].time; - - // - // Validating Calculated Peaks and Vales Times ... - result = - (IsSpecifiedValid(valeToTime) && - IsSpecifiedValid(valeFromTime)) || - (IsSpecifiedValid(peakToTime) && - IsSpecifiedValid(peakFromTime)); - if (!result) - { - // - Clean(); - return result; - } - - // - // Calculate Slope and Intercepts ... - - // - // Peaks ... - result = CalculateZigZagLinearRegression( - peakSlope, - peakIntercept, - pivots, - X_PIVOT_TYPE_PEAK // - ); - if (!result) - { - // - Clean(); - return result; - } - - // - // Vales ... - result = CalculateZigZagLinearRegression( - valeSlope, - valeIntercept, - pivots, - X_ZIGZAG_PIVOT_TYPE_VALE // - ); - if (!result) - { - // - Clean(); - return result; - } - - // - // Calculate Peaks and Vales ... - - // - // Peaks ... - - // - int iPeakToIDX = GetBarIndex( - symbol, - period, - peakToTime // - ); - if (IsValidIndex(iPeakToIDX)) - { - // - peakTo = CalculateValueBySlopeIntercept( - peakSlope, - peakIntercept, - iPeakToIDX // - ); - } - - // - int iPeakFromIDX = GetBarIndex( - symbol, - period, - peakFromTime // - ); - if (IsValidIndex(iPeakFromIDX)) - { - // - peakFrom = CalculateValueBySlopeIntercept( - peakSlope, - peakIntercept, - iPeakFromIDX // - ); - } - - // - // Vales ... - - // - int iValeToIDX = GetBarIndex( - symbol, - period, - valeToTime // - ); - if (IsValidIndex(iValeToIDX)) - { - // - valeTo = CalculateValueBySlopeIntercept( - valeSlope, - valeIntercept, - iValeToIDX // - ); - } - - // - int iValeFromIDX = GetBarIndex( - symbol, - period, - valeFromTime // - ); - if (IsValidIndex(iValeFromIDX)) - { - // - valeFrom = CalculateValueBySlopeIntercept( - valeSlope, - valeIntercept, - iValeFromIDX // - ); - } - - // - // Validating Calculated Peaks and Vales ... - result = - (NotEmpty(valeTo) && - NotEmpty(valeFrom) > 0) || - (NotEmpty(peakTo) > 0 && - NotEmpty(peakFrom) > 0); - if (!result) - { - // - Clean(); - return result; - } - - // - // Detecting Direction ... - - // - bool isBullish = - valeTo > valeFrom || - (valeTo > valeFrom && - peakTo > peakFrom); - - // - bool isBearish = - peakTo < peakFrom || - (peakTo < peakFrom && - valeTo < valeFrom); - - // - // Validate When Only One Direction Exists ... - result = - (isBullish && !isBearish) || - (isBearish && !isBullish); - if (!result) - { - // - Clean(); - return result; - } - - // - dir = - isBullish - ? X_DIRECTION_BULLISH - : X_DIRECTION_BEARISH; - - // - // Calculate Support and Resistance ... - - // - // Validate ... - result = IsValid(); - if (!result) - { - Clean(); - } - - // - return result; - } - - // - // Tools ... - - /** - * Cleanup Model ... - */ - void Clean() - { - // - time = NULL; - symbol = NULL; - period = NULL; - dir = X_DIRECTION_NONE; - SpecifiedClean(pivots); - - // - peakTo = 0; - valeTo = 0; - peakFrom = 0; - valeFrom = 0; - peakToTime = NULL; - valeToTime = NULL; - peakFromTime = NULL; - valeFromTime = NULL; - - // - ZeroMemory(this); - } - - /** - * Validate Model ... - * - * @return ( bool ) - */ - bool IsValid() - { - // - bool result = false; - - // - result = - // - HasChild(pivots) && - HasDirection(dir) && - IsSpecifiedValid(time) && - IsSpecifiedValid(symbol) && - IsSpecifiedValid(period) - // - && - // - ( - // - // Validate Peak ... - (peakTo > 0 && - peakFrom > 0 && - peakSlope != EMPTY_VALUE && - peakIntercept != EMPTY_VALUE && - IsSpecifiedValid(peakToTime) && - IsSpecifiedValid(peakFromTime)) - // - || - // - // Validate Vale ... - (valeTo > 0 && - valeFrom > 0 && - valeSlope != EMPTY_VALUE && - valeIntercept != EMPTY_VALUE && - IsSpecifiedValid(valeToTime) && - IsSpecifiedValid(valeFromTime)) - // - ) - // - ; - - // - return result; - } - - /** - * Calculate Analysis Age, based on Specified Time Frame ... - * - * @param forPeriod: ENUM_TIMEFRAMES, Specified Period ... - * - * @return ( int ) - */ - int GetAge( - ENUM_TIMEFRAMES forPeriod = NULL // - ) - { - // - int result = -1; - - // - // Validate ... - if (!IsValid()) - { - return result; - } - - // - // Normalize ... - if (!IsSpecifiedValid(forPeriod)) - { - forPeriod = period; - } - - // - result = GetBarIndex( - symbol, - forPeriod, - time // - ); - - // - return result; - } - - /** - * Check Market Direction is Bullish or not ... - * - * @return ( bool ) - */ - bool IsBullish() - { - // - bool result = false; - - // - result = IsValid() && - IsSpecifiedBullish(dir); - - // - return result; - } - - /** - * Check Market Direction is Bearish or not ... - * - * @return ( bool ) - */ - bool IsBearish() - { - // - bool result = false; - - // - result = IsValid() && - IsSpecifiedBearish(dir); - - // - return result; - } - - /** - * Check two item is Same or not ... - * - * @param item: XZigZagAnalysis, dest model for Checking ... - * - * @return ( bool ) - */ - bool IsSameAs(XZigZagAnalysis &item) - { - // - bool result = false; - - // - result = - IsValid() && - item.IsValid() && - time == item.time && - IsSameMarketAs(item); - - // - return result; - } - - /** - * Check to model Has Same Symbol ... - * - * @param item: XZigZagAnalysis, dest model for Checking ... - * - * @return ( bool ) - */ - bool IsSameSymbolAs(XZigZagAnalysis &item) - { - // - bool result = false; - - // - result = - IsValid() && - item.IsValid() && - symbol == item.symbol; - - // - return result; - } - - /** - * Check to model Has Same Period ... - * - * @param item: XZigZagAnalysis, dest model for Checking ... - * - * @return ( bool ) - */ - bool IsSamePeriodAs(XZigZagAnalysis &item) - { - // - bool result = false; - - // - result = - IsValid() && - item.IsValid() && - period == item.period; - - // - return result; - } - - /** - * Check to model Has Same Symbol/Period ... - * - * @param item: XZigZagAnalysis, dest model for Checking ... - * - * @return ( bool ) - */ - bool IsSameMarketAs(XZigZagAnalysis &item) - { - // - bool result = false; - - // - result = - IsValid() && - item.IsValid() && - IsSameSymbolAs(item) && - IsSamePeriodAs(item); - - // - return result; - } - - /** - * Check to model Has Same Direction ... - * - * @param item: XZigZagAnalysis, dest model for Checking ... - * - * @return ( bool ) - */ - bool IsSameDirectionAs(XZigZagAnalysis &item) - { - // - bool result = false; - - // - result = - IsValid() && - item.IsValid() && - dir == item.dir; - - // - return result; - } - - /** - * Check to model Has Analysis Trend ... - * - * @param item: XZigZagAnalysis, dest model for Checking ... - * - * @return ( bool ) - */ - bool IsSameTrendAs(XZigZagAnalysis &item) - { - // - bool result = false; - - // - result = - IsValid() && - item.IsValid() && - IsSameMarketAs(item) && - IsSameDirectionAs(item); - - // - return result; - } - - // - // Pivots Tools ... - - /** - * Get Highest Specified Pivot ... - * - * @param type: ENUM_X_PIVOT_TYPE, Specified Pivot Type ... - * - * @return ( int ) - */ - int GetHighestPivot( - ENUM_X_PIVOT_TYPE type = X_PIVOT_TYPE_NONE // - ) - { - // - int result = -1; - - // - if (!IsValid()) - { - return result; - } - - // - bool has = false; - XZigZagPivot iPivot; - int count = ArraySize(pivots); - for (int i = 0; i < count; i++) - { - // - // Check Type ... - has = - !IsXValid(type) || - (IsXValid(type) && - type == pivots[i].type); - if (!has) - { - continue; - } - - // - // Check Relation based on last Detected Pivot ... - has = !iPivot.IsValid() || - (iPivot.IsValid() && - iPivot.value < pivots[i].value); - if (has) - { - // - result = i; - iPivot = pivots[i]; - } - } - - // - has = - iPivot.IsValid() && - IsValidIndex(result); - iPivot.Clean(); - - // - return result; - } - - /** - * Get Lowest Specified Pivot ... - * - * @param type: ENUM_X_PIVOT_TYPE, Specified Pivot Type ... - * - * @return ( int ) - */ - int GetLowestPivot( - ENUM_X_PIVOT_TYPE type = X_PIVOT_TYPE_NONE // - ) - { - // - int result = -1; - - // - if (!IsValid()) - { - return result; - } - - // - bool has = false; - XZigZagPivot iPivot; - int count = ArraySize(pivots); - for (int i = 0; i < count; i++) - { - // - // Check Type ... - has = - !IsXValid(type) || - (IsXValid(type) && - type == pivots[i].type); - if (!has) - { - continue; - } - - // - // Check Relation based on last Detected Pivot ... - has = !iPivot.IsValid() || - (iPivot.IsValid() && - iPivot.value > pivots[i].value); - if (has) - { - // - result = i; - iPivot = pivots[i]; - } - } - - // - has = - iPivot.IsValid() && - IsValidIndex(result); - iPivot.Clean(); - - // - return result; - } - - /** - * Get Oldest Specified Pivot ... - * - * @param type: ENUM_X_PIVOT_TYPE, Specified Pivot Type ... - * - * @return ( int ) - */ - int GetOldestPivot( - ENUM_X_PIVOT_TYPE type = X_PIVOT_TYPE_NONE // - ) - { - // - int result = -1; - - // - if (!IsValid()) - { - return result; - } - - // - bool has = false; - XZigZagPivot iPivot; - int count = ArraySize(pivots); - for (int i = 0; i < count; i++) - { - // - // Check Type ... - has = - !IsXValid(type) || - (IsXValid(type) && - type == pivots[i].type); - if (!has) - { - continue; - } - - // - // Check Relation based on last Detected Pivot ... - has = !iPivot.IsValid() || - (iPivot.IsValid() && - iPivot.time > pivots[i].time); - if (has) - { - // - result = i; - iPivot = pivots[i]; - } - } - - // - has = - iPivot.IsValid() && - IsValidIndex(result); - iPivot.Clean(); - - // - return result; - } - - /** - * Get Youngest Specified Pivot ... - * - * @param type: ENUM_X_PIVOT_TYPE, Specified Pivot Type ... - * - * @return ( int ) - */ - int GetYoungestPivot( - ENUM_X_PIVOT_TYPE type = X_PIVOT_TYPE_NONE // - ) - { - // - int result = -1; - - // - // Validate ... - if (!IsValid()) - { - return result; - } - - // - bool has = false; - XZigZagPivot iPivot; - int count = ArraySize(pivots); - for (int i = 0; i < count; i++) - { - // - // Check Type ... - has = - !IsXValid(type) || - (IsXValid(type) && - type == pivots[i].type); - if (!has) - { - continue; - } - - // - // Check Relation based on last Detected Pivot ... - has = !iPivot.IsValid() || - (iPivot.IsValid() && - iPivot.time > pivots[i].time); - if (has) - { - // - result = i; - iPivot = pivots[i]; - } - } - - // - has = - iPivot.IsValid() && - IsValidIndex(result); - iPivot.Clean(); - - // - return result; - } - - /** - * Get Recent Number of Specified Type of Pivots ... - * - * @param recent: XZigZagPivot, Collection reference as result ... - * @param numberOfRequiredPivots: int, required number of Pivots ... - * @param type: ENUM_X_PIVOT_TYPE, type of Pivot ... - * - * @return ( int ) - */ - int GetRecentPivots( - XZigZagPivot &recent[], - int numberOfRequiredPivots = 2, - ENUM_X_PIVOT_TYPE type = X_PIVOT_TYPE_NONE // - ) - { - // - int result = -1; - - // - result = GetRecent( - pivots, - recent, - numberOfRequiredPivots, - type // - ); - - // - return result; - } - - // - // Loggint Tools ... - - /** - * Extract Specified Token of Struct ... - * - * @return ( string ) - */ - string GetTag() - { - return GetToken(this); - } - - /** - * Represent an String Unique Identifier for Chart Objects ... - * - * @return ( string ) - */ - string GetObjectID() - { - // - string result = ""; - - // - if (!IsValid()) - { - return result; - } - - // - result = GetTag() + "_" + symbol + "_" + ToXString(period) + "_" + ToXString(dir) + "_" + ToXString(TimeToSeconds(time)); - - // - return result; - } - - /** - * Represent a Point as String ... - * - * @param spacer: string, How to Represent Spaces ... - * - * @return ( string ) - */ - string GetMessage(string spacer = NULL) - { - // - string result = ""; - - // - // Normalize Spacer ... - if (!IsSpecifiedValid(spacer)) - { - spacer = " "; - } - - // - // Validate ... - if (!IsValid()) - { - return result; - } - - // - result = - ToXString(dir) + spacer + GetTag() + ":" + spacer + - "Symbol: " + symbol + spacer + - "Period: " + ToXString(period) + spacer + - "Direction: " + ToXString(dir) + spacer + - "Time: " + ToXString(time) + spacer; - - // - return result; - } - - // -}; - -/** - * XZigZagAnalysis Chart Object ... - **/ -class XCZigZagAnalysisObject : public XCBaseObject -{ - // - public: - // - - /** - * Create Object ... - * - * @param chart_id: Long, Specified Chart ID ... - * @param window: Integer, Specified Chart Window ID ... - * @param model: XZigZagAnalysis, reference model ... - * - * @return ( bool ) - */ - bool CreateByRecent( - long chart_id, - int window, - XZigZagAnalysis &model, - int numberOfPivots = 2 // - ) - { - // - bool result = false; - - // - // Normalize ... - numberOfPivots = NormalizeInt(numberOfPivots, 2); - - // - // Validate ... - result = model.IsValid(); - if (!result) - { - return result; - } - - // - // Generate Object ID ... - string objID = model.GetObjectID(); - string upperName = "Upper_" + objID; - string lowerName = "Lower_" + objID; - - // - // Extract Recent Pivots ... - - // - // Peaks ... - XZigZagPivot rPeaks[]; - int count = model.GetRecentPivots( - rPeaks, - numberOfPivots, - X_PIVOT_TYPE_PEAK // - ); - - // - // Vales ... - XZigZagPivot rVales[]; - count = model.GetRecentPivots( - rVales, - numberOfPivots, - X_ZIGZAG_PIVOT_TYPE_VALE // - ); - - // - // Creating Upper ... - result = mUpper.Create( - chart_id, - upperName, - window, - rPeaks[numberOfPivots - 1].time, - rPeaks[numberOfPivots - 1].value, - rPeaks[0].time, - rPeaks[0].value // - ); - if (!result) - { - return result; - } - - // - // Create Lower ... - result = mLower.Create( - chart_id, - lowerName, - window, - rVales[numberOfPivots - 1].time, - rVales[numberOfPivots - 1].value, - rVales[0].time, - rVales[0].value // - ); - if (!result) - { - // - mUpper.Detach(); - mUpper.Delete(); - - // - return result; - } - - // - // Setting Object Name ... - if (result) - { - ObjName(objID); - } - - // - return result; - } - - /** - * Create Object ... - * - * @param chart_id: Long, Specified Chart ID ... - * @param window: Integer, Specified Chart Window ID ... - * @param model: XZigZagAnalysis, reference model ... - * - * @return ( bool ) - */ - bool CreateByPV( - long chart_id, - int window, - XZigZagAnalysis &model // - ) - { - // - bool result = false; - - // - // Validate ... - result = model.IsValid(); - if (!result) - { - return result; - } - - // - // Generate Object ID ... - string objID = model.GetObjectID(); - string upperName = "Upper_" + objID; - string lowerName = "Lower_" + objID; - - // - // Extract Recent Pivots ... - - // - // Creating Upper ... - result = mUpper.Create( - chart_id, - upperName, - window, - model.peakFromTime, - model.peakFrom, - model.peakToTime, - model.peakTo // - ); - if (!result) - { - return result; - } - - // - // Create Lower ... - result = mLower.Create( - chart_id, - lowerName, - window, - model.valeFromTime, - model.valeFrom, - model.valeToTime, - model.valeTo // - ); - if (!result) - { - // - mUpper.Detach(); - mUpper.Delete(); - - // - return result; - } - - // - // Setting Object Name ... - if (result) - { - ObjName(objID); - } - - // - return result; - } - - // - // Virtuals ... - - /** - * Retrieve Object Type ... - * - * @return ( ENUM_XCHARTOBJECTS ) - */ - virtual int Type() - { - return 9860; - } - - // - // Styling ... - - /** - * Set Width ... - * - * @param value: Integer ... - */ - void UpperWidth(int value) - { - // - if (value < 1) - { - value = 1; - } - - // - mUpper.Width(value); - } - - /** - * Set Color ... - * - * @param value: Color ... - */ - void UpperColor(color value) - { - // - mUpper.Color(value); - } - - /** - * Set Style ... - * - * @param value: ENUM_LINE_STYLE member ... - */ - void UpperStyle(ENUM_LINE_STYLE value) - { - // - mUpper.Style(value); - } - - /** - * Specified Upper Ray to Left or not ... - * - * @param value: bool - */ - void UpperRayLeft(bool value) - { - mUpper.RayLeft(value); - } - - /** - * Specified Upper Ray to Right or not ... - * - * @param value: bool - */ - void UpperRayRight(bool value) - { - mUpper.RayRight(value); - } - - /** - * Set Width ... - * - * @param value: Integer ... - */ - void LowerWidth(int value) - { - // - if (value < 1) - { - value = 1; - } - - // - mLower.Width(value); - } - - /** - * Set Color ... - * - * @param value: Color ... - */ - void LowerColor(color value) - { - // - mLower.Color(value); - } - - /** - * Set Style ... - * - * @param value: ENUM_LINE_STYLE member ... - */ - void LowerStyle(ENUM_LINE_STYLE value) - { - // - mLower.Style(value); - } - - /** - * Specified Lower Ray to Left or not ... - * - * @param value: bool - */ - void LowerRayLeft(bool value) - { - mLower.RayLeft(value); - } - - /** - * Specified Lower Ray to Right or not ... - * - * @param value: bool - */ - void LowerRayRight(bool value) - { - mLower.RayRight(value); - } - - // - private: - // - CChartObjectTrend mUpper; - CChartObjectTrend mLower; -}; - -// -// Extensions ... - - - -bool CalculateZigZagLinearRegression( - double &_slope, - double &_intercept, - XZigZagPivot &_pivots[], - ENUM_X_PIVOT_TYPE _type // -) -{ - // - bool result = false; - - // - // Prepare ... - _slope = EMPTY_VALUE; - _intercept = EMPTY_VALUE; - - // - // Validate ... - result = - IsXValid(_type) && - HasChild(_pivots); - if (!result) - { - return result; - } - - // - // Extracted Type Specific Valus ... - double values[]; - XZigZagPivot tmp[]; - XZigZagPivot iPivot; - Copy( - _pivots, - tmp // - ); - int count = ArraySize(tmp); - while (HasChild(tmp)) - { - // - // Get Model ... - iPivot = tmp[0]; - - // - // Remove Model ... - ArrayRemove( - tmp, - 0, - 1 // - ); - - // - // Validate Type ... - result = _type == iPivot.type; - if (result) - { - // - // Add Value if Type Passed ... - Add( - iPivot.value, - values // - ); - } - - // - iPivot.Clean(); - } - - // - // Validation Valus ... - result = HasChild(values); - if (result) - { - // - // Calculate Slope / Intercept ... - result = CalculateLinearRegression( - _slope, - _intercept, - values // - ); - } - - // - // Validate Result ... - result = - _slope != EMPTY_VALUE && - _intercept != EMPTY_VALUE; - if (!result) - { - // - _slope = EMPTY_VALUE; - _intercept = EMPTY_VALUE; - } - - // - // Cleanup Resources ... - iPivot.Clean(); - SpecifiedClean(tmp); - SpecifiedClean(values); - - // - return result; -} - -// \ No newline at end of file