mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-23 09:18:05 +00:00
refactor(indicators): Optimized for incremental calculation
This commit is contained in:
@@ -1,11 +1,11 @@
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| Cyber_Cycle_Pro.mq5 |
|
//| Cyber_Cycle_Pro.mq5 |
|
||||||
//| Copyright 2025, xxxxxxxx|
|
//| Copyright 2026, xxxxxxxx|
|
||||||
//| |
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
#property copyright "Copyright 2025, xxxxxxxx"
|
#property copyright "Copyright 2026, xxxxxxxx"
|
||||||
#property version "1.00"
|
#property version "2.00" // Optimized for incremental calculation
|
||||||
#property description "John Ehlers' Cyber Cycle indicator for identifying market cycles."
|
#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_separate_window
|
||||||
#property indicator_buffers 2
|
#property indicator_buffers 2
|
||||||
@@ -22,20 +22,17 @@
|
|||||||
#property indicator_label2 "Signal"
|
#property indicator_label2 "Signal"
|
||||||
#property indicator_type2 DRAW_LINE
|
#property indicator_type2 DRAW_LINE
|
||||||
#property indicator_color2 clrOrangeRed
|
#property indicator_color2 clrOrangeRed
|
||||||
#property indicator_style2 STYLE_DOT
|
#property indicator_style2 STYLE_SOLID
|
||||||
#property indicator_width2 1
|
#property indicator_width2 1
|
||||||
|
|
||||||
#property indicator_level1 0.0
|
#property indicator_level1 0.0
|
||||||
#property indicator_levelstyle STYLE_SOLID
|
#property indicator_levelstyle STYLE_DOT
|
||||||
#property indicator_levelcolor clrGray
|
|
||||||
|
|
||||||
#include <MyIncludes\Cyber_Cycle_Calculator.mqh>
|
#include <MyIncludes\Cyber_Cycle_Calculator.mqh>
|
||||||
|
|
||||||
enum ENUM_PRICE_SOURCE { SOURCE_STANDARD, SOURCE_HEIKIN_ASHI };
|
|
||||||
|
|
||||||
//--- Input Parameters ---
|
//--- Input Parameters ---
|
||||||
input double InpAlpha = 0.07; // Smoothing factor
|
input double InpAlpha = 0.07; // Smoothing factor
|
||||||
input ENUM_PRICE_SOURCE InpSource = SOURCE_STANDARD;
|
input ENUM_APPLIED_PRICE_HA_ALL InpSourcePrice = PRICE_MEDIAN_STD; // Price Source (Default: Median)
|
||||||
|
|
||||||
//--- Indicator Buffers ---
|
//--- Indicator Buffers ---
|
||||||
double BufferCycle[];
|
double BufferCycle[];
|
||||||
@@ -44,6 +41,8 @@ double BufferSignal[];
|
|||||||
//--- Global calculator object ---
|
//--- Global calculator object ---
|
||||||
CCyberCycleCalculator *g_calculator;
|
CCyberCycleCalculator *g_calculator;
|
||||||
|
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| OnInit |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
int OnInit()
|
int OnInit()
|
||||||
{
|
{
|
||||||
@@ -52,23 +51,23 @@ int OnInit()
|
|||||||
ArraySetAsSeries(BufferCycle, false);
|
ArraySetAsSeries(BufferCycle, false);
|
||||||
ArraySetAsSeries(BufferSignal, false);
|
ArraySetAsSeries(BufferSignal, false);
|
||||||
|
|
||||||
if(InpSource == SOURCE_HEIKIN_ASHI)
|
//--- Factory Logic
|
||||||
{
|
if(InpSourcePrice <= PRICE_HA_CLOSE)
|
||||||
g_calculator = new CCyberCycleCalculator_HA();
|
g_calculator = new CCyberCycleCalculator_HA();
|
||||||
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("Cyber Cycle HA(%.2f)", InpAlpha));
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
g_calculator = new CCyberCycleCalculator();
|
g_calculator = new CCyberCycleCalculator();
|
||||||
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("Cyber Cycle(%.2f)", InpAlpha));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
//--- Initialize
|
||||||
if(CheckPointer(g_calculator) == POINTER_INVALID || !g_calculator.Init(InpAlpha))
|
if(CheckPointer(g_calculator) == POINTER_INVALID || !g_calculator.Init(InpAlpha))
|
||||||
{
|
{
|
||||||
Print("Failed to initialize Cyber Cycle Calculator.");
|
Print("Failed to initialize Cyber Cycle Calculator.");
|
||||||
return(INIT_FAILED);
|
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(0, PLOT_DRAW_BEGIN, 7);
|
||||||
PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, 9);
|
PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, 9);
|
||||||
IndicatorSetInteger(INDICATOR_DIGITS, 2);
|
IndicatorSetInteger(INDICATOR_DIGITS, 2);
|
||||||
@@ -76,6 +75,8 @@ int OnInit()
|
|||||||
return(INIT_SUCCEEDED);
|
return(INIT_SUCCEEDED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| OnDeinit |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
void OnDeinit(const int reason)
|
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)
|
if(rates_total < 7)
|
||||||
return 0;
|
return(0);
|
||||||
g_calculator.Calculate(rates_total, open, high, low, close, BufferCycle, BufferSignal);
|
|
||||||
|
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);
|
return(rates_total);
|
||||||
}
|
}
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//+------------------------------------------------------------------+
|
|
||||||
|
|||||||
Reference in New Issue
Block a user