2026-03-24 17:34:14 -07:00 | | | //+------------------------------------------------------------------+
|
| | | //| Copyright (C) 2026, boyvlad |
|
| | | //| https://www.mql5.com/en/users/boyvlad |
|
| | | //+------------------------------------------------------------------+
|
2026-04-27 06:10:41 -07:00 | | | #include "SubIndiRegistry.mqh"
|
2026-03-24 17:34:14 -07:00 | | |
|
2026-08-10 21:08:23 -07:00 | | | //+------------------------------------------------------------------+
|
| | | //| Base class for all sub-indicators. Extracts common traits and |
|
| | | //| handles routine boilerplate |
|
| | | //+------------------------------------------------------------------+
|
2026-03-24 17:34:14 -07:00 | | | class CSubIndiBase
|
| | | {
|
| | | private:
|
2026-08-10 21:08:23 -07:00 | | | //--- Subclass hook for clearing all buffers at a given bar index
|
2026-05-18 22:19:08 -07:00 | | | virtual void clearBuffersAt(int barIdx) = 0;
|
| | |
|
2026-08-10 21:08:23 -07:00 | | | //--- Ensures barsRequired is valid
|
2026-03-24 17:34:14 -07:00 | | | static int correctBarsRequired(int barsRequired)
|
| | | {
|
| | | if(barsRequired >= 1)
|
| | | return barsRequired;
|
| | | const int prevValue = barsRequired;
|
| | | barsRequired = 1;
|
| | | PrintFormat(__FUNCTION__": invalid barsRequired value (%i); corrected to %i", prevValue, barsRequired);
|
| | | return barsRequired;
|
| | | }
|
2026-05-19 19:58:57 -07:00 | | |
|
| | | protected:
|
2026-08-18 18:44:50 -07:00 | | | //--- Determines calculation start index; clears buffers on full calculation
|
| | | int prepareCalculation(const int rates_total, const int prev_calculated)
|
2026-05-19 19:58:57 -07:00 | | | {
|
2026-08-18 18:44:50 -07:00 | | | int calcStartIdx;
|
2026-05-19 19:58:57 -07:00 | | | switch(rates_total - prev_calculated)
|
| | | {
|
| | | case 0:
|
| | | case 1:
|
2026-08-18 18:44:50 -07:00 | | | calcStartIdx = prev_calculated - 1;
|
2026-05-19 19:58:57 -07:00 | | | break;
|
| | | default:
|
2026-08-18 18:44:50 -07:00 | | | calcStartIdx = drawBegin;
|
2026-05-19 19:58:57 -07:00 | | | clearLeftFromDrawBegin();
|
| | | break;
|
| | | }
|
2026-08-18 18:44:50 -07:00 | | | return calcStartIdx;
|
2026-05-19 19:58:57 -07:00 | | | }
|
| | |
|
2026-08-18 18:45:45 -07:00 | | | //--- Overload for recurrent/stateful indicators: reports if drawBegin is calculated
|
2026-08-28 12:32:39 -07:00 | | | int prepareCalculation(const int rates_total, const int prev_calculated, bool& isFullCalculation)
|
2026-08-18 18:45:45 -07:00 | | | {
|
| | | const int calcStartIdx = prepareCalculation(rates_total, prev_calculated);
|
| | | isFullCalculation = calcStartIdx <= drawBegin;
|
| | | return calcStartIdx;
|
| | | }
|
| | |
|
2026-08-10 21:08:23 -07:00 | | | //--- Clears all buffers at each bar before drawBegin
|
2026-05-19 19:58:57 -07:00 | | | void clearLeftFromDrawBegin()
|
| | | {
|
| | | for(int i = 0; i < drawBegin; i++)
|
| | | clearBuffersAt(i);
|
| | | }
|
| | |
|
2026-03-24 17:34:14 -07:00 | | | public:
|
2026-08-10 21:08:23 -07:00 | | | const int barsRequired; // minimum bars required for valid calculation
|
| | | const int drawBegin; // index of first valid value (barsRequired - 1)
|
| | | const double emptyValue; // placeholder for bars with no value
|
2026-03-24 17:34:14 -07:00 | | | protected:
|
| | | CSubIndiBase(int a_barsRequired, double a_emptyValue)
|
| | | : barsRequired(correctBarsRequired(a_barsRequired)),
|
| | | drawBegin(barsRequired - 1),
|
2026-04-27 06:10:41 -07:00 | | | emptyValue(a_emptyValue)
|
| | | {
|
| | | CSubIndiRegistry::register(this);
|
| | | }
|
2026-03-24 17:34:14 -07:00 | | | };
|