From 103c4b7847775d5bdc3dd9119c3711661c27234e Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Sat, 1 Jun 2024 16:22:45 +0330 Subject: [PATCH] last works on XInd and XMid ... --- Classes/Indicators/x-saherelm.mid.class.mq5 | 254 +++++++++++++++++++- Classes/x-saherelm.xind.class.mq5 | 207 +++++++--------- Indicators/x-saherelm.xmidtest.mq5 | 40 +-- Libraries/x-saherelm.common.lib.mq5 | 9 + 4 files changed, 362 insertions(+), 148 deletions(-) diff --git a/Classes/Indicators/x-saherelm.mid.class.mq5 b/Classes/Indicators/x-saherelm.mid.class.mq5 index 27500a06..3082f710 100644 --- a/Classes/Indicators/x-saherelm.mid.class.mq5 +++ b/Classes/Indicators/x-saherelm.mid.class.mq5 @@ -22,7 +22,7 @@ // // Imports ... -#include +#include "../../Classes/x-saherelm.xind.class.mq5" // // XSCMid Indicator Buffers ... @@ -136,8 +136,258 @@ struct XSCMidInputs // // a Simple Indicator ... -class XSCMid : public CIndicator +class XSCMid : public XSCBaseInd { + // + // Public ... +public: + // + // Props ... + + // + // Constructor(s) ... + void XSCMid( + string symbol, // Trading Symbol + ENUM_TIMEFRAMES period // Trading TimeFrame + ) : XSCBaseInd(symbol, period) + { + // + mSymbol = symbol; + mPeriod = period; + + // + mCalculatedBars = 0; + } + + // + // Deconstructor ... + void ~XSCMid() {} + // + // Initialize Indicator ... + bool Init( + XSCMidInputs &inputs // Indicator Inputs ... + ) + { + // + bool result = false; + + // + result = inputs.IsValid(); + if (!result) + { + return result; + } + + // + // Set Inputs ... + this.mInputs = inputs; + + // + // Validate Indicator State ... + result = this.IsValid(); + if (!result) + { + return result; + } + + // + // Register Required Buffers ... + XBuffer main; + string mTitle = GetTitle(XSCMID_MAIN_LINE); + result = main.Init( + mTitle, + mSymbol, + mPeriod // + ); + if (!result) + { + return result; + } + + // + result = SetBuffer(main); + if (!result) + { + return result; + } + + // + // Calculate First Time ... + Calculate(); + + // + return result; + } + + // + // Property Getter(s) / Setter(s) ... + + // + XSCMidInputs GetInputs() + { + return mInputs; + } + + // + bool SetInputs( + XSCMidInputs &inputs // Indicator Inputs + ) + { + return Init(inputs); + } + + // + // Override(s) ... + + // + // Get Tag ... + string GetTag() override + { + // + string result = NULL; + + // + result = GetToken(); + + // + return result; + } + + // + // Get Token ... + string GetToken() override + { + // + string result = NULL; + + // + result = GetSpecificToken(this); + + // + return result; + } + + // + // Validate Inputs ... + bool IsValidInputs() override + { + return mInputs.IsValid(); + } + + // + // Calculate Buffers ... + void Calculate() override + { + // + int max = mInputs.Max(); + int limit = CountLimit(); + int totalBars = CountBars(); + int prevCalculated = GetCalculatedBars(); + + // + // Prepare Buffers Index ... + int mainIDX = GetBufferIndexByLine(XSCMID_MAIN_LINE); + + // + // Print("Total: ", totalBars, ", prevCalculated: ", prevCalculated, ", Limit: ", limit); + + // + // Only Calculate Last Item ... + bool onlyLast = false; + if (prevCalculated == totalBars - 1) + { + onlyLast = true; + } + + // + int start = totalBars - prevCalculated - 1; + for (int i = start; i >= 0; i--) + { + // + int barIndex = i - 1; + // Print("barIndex: ", barIndex, ", onlyLast: ", onlyLast); + if (barIndex < 0 && !onlyLast) + { + continue; + } + + // + if (onlyLast) + { + barIndex = 0; + } + + // + datetime iBarTime = iTime( + mSymbol, + mPeriod, + barIndex - 1 + // + ); + int iBarIndex = iBarShift( + mSymbol, + mPeriod, + iBarTime // + ); + + // + double iValue = iClose(mSymbol, mPeriod, iBarIndex); + + // + Print("Bar: ", barIndex, ", Bar Time: ", iBarTime, ", Bar Index: ", iBarIndex, ", iValue: ", iValue); + if (iValue != 0.0) + { + // + // Print("Bar: ", barIndex, ", Bar Time: ", iBarTime, ", Bar Index: ", iBarIndex, ", iValue: ", iValue); + mBuffers[mainIDX].Add(barIndex, iValue); + } + } + + // + // Prevent Double Calculation Same Bars ... + mCalculatedBars = totalBars - 1; + } + + // + // De Initialize Class ... + void DeInit(int reason) + { + } + + // + // Tools ... + + // + int GetBufferIndexByLine( + ENUM_XSCMID_BUFFERS bufferLine // Specified Buffer Line + ) + { + // + string mTitle = GetTitle(bufferLine); + return GetBufferIndex(mTitle); + } + + // + bool GetBufferByLine( + ENUM_XSCMID_BUFFERS bufferLine, // Specified Buffer Line + XBuffer &buffer // Hold Result + ) + { + // + string mTitle = GetTitle(bufferLine); + return GetBuffer(mTitle, buffer); + } + + // // + // double GetBufferValueByLine( + // ENUM_XSCMID_BUFFERS bufferLine, // Specified Buffer Line + // datetime time = NULL // Time + // ) + // { + // // + // string mTitle = GetTitle(bufferLine); + // return GetBufferValue(mTitle, time); + // } // // Protected ... diff --git a/Classes/x-saherelm.xind.class.mq5 b/Classes/x-saherelm.xind.class.mq5 index d3b28eca..6927335a 100644 --- a/Classes/x-saherelm.xind.class.mq5 +++ b/Classes/x-saherelm.xind.class.mq5 @@ -48,14 +48,18 @@ struct XBuffer // // Tools ... bool Init( - string mTitle // Title + string mTitle, // Title + string mSymbol, // Symbol + ENUM_TIMEFRAMES mPeriod // TimeFrame ) { // bool result = false; // - result = IsValid(mTitle); + result = IsValid(mTitle) && + IsValid(mSymbol) && + IsValid(mPeriod); if (!result) { return result; @@ -64,6 +68,10 @@ struct XBuffer // Clean(); + // + this.symbol = mSymbol; + this.period = mPeriod; + // result = StringToLower(mTitle); if (!result) @@ -72,8 +80,7 @@ struct XBuffer } // - title = mTitle; - // ArraySetAsSeries(values, false); + this.title = mTitle; // result = IsValid(); @@ -155,7 +162,10 @@ struct XBuffer // // Add Value to Buffer ... - int Add(double value) + int Add( + int index, + double value // + ) { // int result = -1; @@ -167,45 +177,13 @@ struct XBuffer } // - datetime time = GetPeriodStartTime( - this.symbol, - this.period); - - // - // Check Date Exists before ... - int timeIDX = FindIndex( - time, - times // - ); - if (timeIDX >= 0) + if (index > 0) { - // - // Item Exists ... - // Update it ... - - // - values[timeIDX] = value; + Add(value, values); } else { - // - // New Item ... - // Add It ... - - // - int count = Count(); - ArrayResize( - values, - count + 1 // - ); - ArrayResize( - times, - count + 1 // - ); - - // - times[count] = time; - values[count] = value; + values[Count() - 1] = value; } // @@ -269,76 +247,6 @@ struct XBuffer // return result; } - - // - double Get( - datetime time = NULL // - ) - { - // - double result = EMPTY_VALUE; - - // - NormalizeTime(time); - - // - datetime iTime = GetPeriodStartTime( - this.symbol, - this.period, - time // - ); - - // - int timeIDX = FindIndex( - iTime, - this.times // - ); - if (timeIDX < 0) - { - return result; - } - - // - result = Get(timeIDX); - - // - return result; - } - - // - bool Remove( - datetime time = NULL // - ) - { - // - bool result = false; - - // - NormalizeTime(time); - - // - datetime iTime = GetPeriodStartTime( - this.symbol, - this.period, - time // - ); - - // - int timeIDX = FindIndex( - iTime, - this.times // - ); - if (timeIDX < 0) - { - return result; - } - - // - result = Remove(timeIDX); - - // - return result; - } }; // @@ -415,6 +323,44 @@ public: // // Tools ... + // + datetime GetBarTime( + datetime time = NULL // + ) + { + // + NormalizeTime(time); + + // + datetime result = GetPeriodStartTime( + mSymbol, + mPeriod, + time // + ); + + // + return result; + } + + // + int GetBarIndex( + datetime time = NULL // + ) + { + // + datetime iBarTime = GetBarTime(time); + + // + int result = iBarShift( + mSymbol, + mPeriod, + iBarTime // + ); + + // + return result; + } + // // Retrieve Calculate Bars ... int GetCalculatedBars() @@ -459,6 +405,23 @@ public: return result; } + // + // Calculate Limit ... + // this means Required Bars for Calculation ... + int CountLimit() + { + // + int result = 0; + + // + int totalBars = CountBars(); + int calculatedBars = GetCalculatedBars(); + result = MathAbs(totalBars - calculatedBars); + + // + return result; + } + // // Buffer Registrations ... @@ -626,25 +589,25 @@ public: } // - double GetBufferValue( - string mTitle, // Required Buffer Title - datetime time = NULL // Time + int CopyBufferValue( + string mTitle, + double &buffer[], + int start, + int count // ) { // - NormalizeTime(time); - double result = EMPTY_VALUE; + int result = 0; // - // Validate State ... if (!IsValid()) { return result; } // - int bufferIDX = GetBufferIndex(mTitle); - if (bufferIDX < 0) + int sourceIDX = GetBufferIndex(mTitle); + if (sourceIDX < 0) { return result; } @@ -653,7 +616,13 @@ public: Calculate(); // - result = mBuffers[bufferIDX].Get(time); + result = ArrayCopy( + buffer, + mBuffers[sourceIDX].values, + start, + start, + count // + ); // return result; diff --git a/Indicators/x-saherelm.xmidtest.mq5 b/Indicators/x-saherelm.xmidtest.mq5 index 15dae2e9..eee4d84b 100644 --- a/Indicators/x-saherelm.xmidtest.mq5 +++ b/Indicators/x-saherelm.xmidtest.mq5 @@ -122,17 +122,17 @@ int OnCalculate( // limit = (prev_calculated > rates_total || prev_calculated <= 0) ? rates_total : (rates_total - prev_calculated) + 1; - // // - // int midMainCopiedItems = mMid.CopyBufferValueByLine( - // XSCMID_MAIN_LINE, - // mainBuffer, - // 0, - // limit // - // ); - // if (midMainCopiedItems <= 0) - // { - // return prev_calculated; - // } + // + int midMainCopiedItems = mMid.CopyBufferValue( + GetTitle(XSCMID_MAIN_LINE), + mainBuffer, + 0, + limit // + ); + if (midMainCopiedItems <= 0) + { + return prev_calculated; + } // // Main Loop ... @@ -211,6 +211,7 @@ void DefineBuffers() XBufferPlotStyle mainBufferStyle; mainBufferStyle.clr = clrAqua; mainBufferStyle.type = DRAW_LINE; + mainBufferStyle.width = 3; // SetIndexBuffer( @@ -220,8 +221,7 @@ void DefineBuffers() mainColorBuffer, bufferLabel, true, - 0, - max // + EMPTY_VALUE // ); } @@ -235,18 +235,4 @@ void SetIndicatorName() // void CalculateBuffers(int barIndex) { - // - double iValue = mMid.GetBufferValueByLine( - XSCMID_MAIN_LINE, - barIndex // - ); - - // - // double iValue = iClose( - // _Symbol, - // _Period, - // barIndex // - // ); - - mainBuffer[barIndex] = iValue; } \ No newline at end of file diff --git a/Libraries/x-saherelm.common.lib.mq5 b/Libraries/x-saherelm.common.lib.mq5 index 10aa1db4..3c76e9d9 100644 --- a/Libraries/x-saherelm.common.lib.mq5 +++ b/Libraries/x-saherelm.common.lib.mq5 @@ -4055,6 +4055,15 @@ void CalculateTPSL( : mEntry + reward; } +// +bool GetTick( + string mSymbol, + MqlTick &tick // +) +{ + return SymbolInfoTick(mSymbol, tick); +} + // // Symbols ...