refactor(indicators): Optimized for incremental calculation

This commit is contained in:
Toh4iem9
2026-01-18 14:02:45 +01:00
parent 3a588f5abe
commit bbe16f56a1
@@ -1,11 +1,11 @@
//+------------------------------------------------------------------+
//| Cyber_Cycle_Pro.mq5 |
//| Copyright 2025, xxxxxxxx|
//| |
//| Copyright 2026, xxxxxxxx|
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx"
#property version "1.00"
#property copyright "Copyright 2026, xxxxxxxx"
#property version "2.00" // Optimized for incremental calculation
#property description "John Ehlers' Cyber Cycle indicator for identifying market cycles."
#property description "Features O(1) calculation and full Heikin Ashi support."
#property indicator_separate_window
#property indicator_buffers 2
@@ -22,20 +22,17 @@
#property indicator_label2 "Signal"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrOrangeRed
#property indicator_style2 STYLE_DOT
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
#property indicator_level1 0.0
#property indicator_levelstyle STYLE_SOLID
#property indicator_levelcolor clrGray
#property indicator_levelstyle STYLE_DOT
#include <MyIncludes\Cyber_Cycle_Calculator.mqh>
enum ENUM_PRICE_SOURCE { SOURCE_STANDARD, SOURCE_HEIKIN_ASHI };
//--- Input Parameters ---
input double InpAlpha = 0.07; // Smoothing factor
input ENUM_PRICE_SOURCE InpSource = SOURCE_STANDARD;
input double InpAlpha = 0.07; // Smoothing factor
input ENUM_APPLIED_PRICE_HA_ALL InpSourcePrice = PRICE_MEDIAN_STD; // Price Source (Default: Median)
//--- Indicator Buffers ---
double BufferCycle[];
@@ -44,6 +41,8 @@ double BufferSignal[];
//--- Global calculator object ---
CCyberCycleCalculator *g_calculator;
//+------------------------------------------------------------------+
//| OnInit |
//+------------------------------------------------------------------+
int OnInit()
{
@@ -52,23 +51,23 @@ int OnInit()
ArraySetAsSeries(BufferCycle, false);
ArraySetAsSeries(BufferSignal, false);
if(InpSource == SOURCE_HEIKIN_ASHI)
{
//--- Factory Logic
if(InpSourcePrice <= PRICE_HA_CLOSE)
g_calculator = new CCyberCycleCalculator_HA();
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("Cyber Cycle HA(%.2f)", InpAlpha));
}
else
{
g_calculator = new CCyberCycleCalculator();
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("Cyber Cycle(%.2f)", InpAlpha));
}
//--- Initialize
if(CheckPointer(g_calculator) == POINTER_INVALID || !g_calculator.Init(InpAlpha))
{
Print("Failed to initialize Cyber Cycle Calculator.");
return(INIT_FAILED);
}
//--- Shortname
string type = (InpSourcePrice <= PRICE_HA_CLOSE) ? " HA" : "";
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("Cyber Cycle%s(%.2f)", type, InpAlpha));
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, 7);
PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, 9);
IndicatorSetInteger(INDICATOR_DIGITS, 2);
@@ -76,6 +75,8 @@ int OnInit()
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| OnDeinit |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
@@ -84,12 +85,29 @@ void OnDeinit(const int reason)
}
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total, const int, const datetime&[], const double &open[], const double &high[], const double &low[], const double &close[], const long&[], const long&[], const int&[])
//| OnCalculate |
//+------------------------------------------------------------------+
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[])
{
if(CheckPointer(g_calculator) == POINTER_INVALID)
return 0;
g_calculator.Calculate(rates_total, open, high, low, close, BufferCycle, BufferSignal);
if(rates_total < 7)
return(0);
ENUM_APPLIED_PRICE price_type = (InpSourcePrice <= PRICE_HA_CLOSE) ?
(ENUM_APPLIED_PRICE)(-(int)InpSourcePrice) :
(ENUM_APPLIED_PRICE)InpSourcePrice;
g_calculator.Calculate(rates_total, prev_calculated, price_type, open, high, low, close,
BufferCycle, BufferSignal);
return(rates_total);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+