mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-24 17:58:07 +00:00
refactor: Refactored with step-blocking Flat-Force mapping and background OnTimer Guard
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| Ehlers_Smoother_MTF_Pro.mq5 |
|
//| Ehlers_Smoother_MTF_Pro.mq5 |
|
||||||
//| Copyright 2025, xxxxxxxx|
|
//| Copyright 2026, xxxxxxxx|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
#property copyright "Copyright 2025, xxxxxxxx"
|
#property copyright "Copyright 2026, xxxxxxxx"
|
||||||
#property version "1.10" // Optimized for incremental MTF calculation
|
#property version "1.20" // Refactored with step-blocking Flat-Force mapping and background OnTimer Guard
|
||||||
#property description "Multi-Timeframe (MTF) version of John Ehlers' Smoothers."
|
#property description "Multi-Timeframe (MTF) version of John Ehlers' Smoothers."
|
||||||
|
|
||||||
#property indicator_chart_window
|
#property indicator_chart_window
|
||||||
@@ -18,44 +18,75 @@
|
|||||||
#include <MyIncludes\Ehlers_Smoother_Calculator.mqh>
|
#include <MyIncludes\Ehlers_Smoother_Calculator.mqh>
|
||||||
|
|
||||||
//--- Input Parameters ---
|
//--- Input Parameters ---
|
||||||
input ENUM_TIMEFRAMES InpUpperTimeframe = PERIOD_CURRENT;
|
input group "Timeframe Settings"
|
||||||
input ENUM_SMOOTHER_TYPE InpSmootherType = SUPERSMOOTHER;
|
input ENUM_TIMEFRAMES InpUpperTimeframe = PERIOD_H1; // Target Higher Timeframe
|
||||||
input int InpPeriod = 20;
|
|
||||||
input ENUM_APPLIED_PRICE_HA_ALL InpSourcePrice = PRICE_CLOSE_STD;
|
input group "Smoother Settings"
|
||||||
|
input ENUM_SMOOTHER_TYPE InpSmootherType = SUPERSMOOTHER; // Smoother Type
|
||||||
|
input int InpPeriod = 20; // Smoothing Period
|
||||||
|
input ENUM_APPLIED_PRICE_HA_ALL InpSourcePrice = PRICE_CLOSE_STD; // Price Source
|
||||||
|
|
||||||
//--- Indicator Buffers ---
|
//--- Indicator Buffers ---
|
||||||
double BufferFilterMTF[];
|
double BufferFilterMTF[];
|
||||||
|
|
||||||
//--- Internal Buffer for HTF Calculation (Must be global to persist state)
|
//--- Internal HTF Data Caches
|
||||||
double BufferFilter_HTF_Internal[];
|
double h_res_smooth[]; // HTF Results cached
|
||||||
|
datetime h_time[]; // HTF Time index
|
||||||
|
double h_open[], h_high[], h_low[], h_close[]; // HTF Price Data
|
||||||
|
|
||||||
//--- Global variables ---
|
//--- Global variables ---
|
||||||
CEhlersSmootherCalculator *g_calculator;
|
CEhlersSmootherCalculator *g_calculator;
|
||||||
bool g_is_mtf_mode = false;
|
bool g_is_mtf_mode = false;
|
||||||
ENUM_TIMEFRAMES g_calc_timeframe;
|
ENUM_TIMEFRAMES g_calc_timeframe;
|
||||||
|
bool g_data_ready = false;
|
||||||
|
bool g_data_synced = false;
|
||||||
|
int g_htf_count = 0;
|
||||||
|
datetime g_last_htf_time = 0;
|
||||||
|
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| EnsureHTFDataReady |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
bool EnsureHTFDataReady(const string symbol, const ENUM_TIMEFRAMES timeframe, const int required_bars)
|
||||||
|
{
|
||||||
|
ResetLastError();
|
||||||
|
if(!SymbolInfoInteger(symbol, SYMBOL_SELECT))
|
||||||
|
{
|
||||||
|
SymbolSelect(symbol, true);
|
||||||
|
}
|
||||||
|
datetime times[];
|
||||||
|
int copied = CopyTime(symbol, timeframe, 0, required_bars, times);
|
||||||
|
return (copied >= required_bars);
|
||||||
|
}
|
||||||
|
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| OnInit |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
int OnInit()
|
int OnInit()
|
||||||
{
|
{
|
||||||
//--- Resolve Timeframe
|
g_data_ready = false;
|
||||||
|
g_data_synced = false;
|
||||||
|
g_last_htf_time = 0;
|
||||||
|
g_htf_count = 0;
|
||||||
|
|
||||||
|
//--- 1. Resolve Timeframe
|
||||||
g_calc_timeframe = InpUpperTimeframe;
|
g_calc_timeframe = InpUpperTimeframe;
|
||||||
if(g_calc_timeframe == PERIOD_CURRENT)
|
if(g_calc_timeframe == PERIOD_CURRENT)
|
||||||
g_calc_timeframe = (ENUM_TIMEFRAMES)Period();
|
g_calc_timeframe = (ENUM_TIMEFRAMES)Period();
|
||||||
|
|
||||||
//--- Validation
|
//--- 2. Validation
|
||||||
if(g_calc_timeframe < Period())
|
if(g_calc_timeframe < Period())
|
||||||
{
|
{
|
||||||
Print("Error: The selected timeframe must be higher than or equal to the current chart timeframe.");
|
Print("Error: Target timeframe must be >= current timeframe.");
|
||||||
return(INIT_FAILED);
|
return(INIT_FAILED);
|
||||||
}
|
}
|
||||||
g_is_mtf_mode = (g_calc_timeframe > Period());
|
g_is_mtf_mode = (g_calc_timeframe > Period());
|
||||||
|
|
||||||
//--- Buffer Mapping
|
//--- 3. Buffer Mapping
|
||||||
SetIndexBuffer(0, BufferFilterMTF, INDICATOR_DATA);
|
SetIndexBuffer(0, BufferFilterMTF, INDICATOR_DATA);
|
||||||
ArraySetAsSeries(BufferFilterMTF, false);
|
ArraySetAsSeries(BufferFilterMTF, false);
|
||||||
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
|
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
|
||||||
|
|
||||||
//--- Initialize Calculator
|
//--- 4. Initialize Calculator
|
||||||
string name = (InpSmootherType == SUPERSMOOTHER) ? "SuperSmoother" : "UltimateSmoother";
|
string name = (InpSmootherType == SUPERSMOOTHER) ? "SuperSmoother" : "UltimateSmoother";
|
||||||
if(InpSourcePrice <= PRICE_HA_CLOSE)
|
if(InpSourcePrice <= PRICE_HA_CLOSE)
|
||||||
g_calculator = new CEhlersSmootherCalculator_HA();
|
g_calculator = new CEhlersSmootherCalculator_HA();
|
||||||
@@ -73,25 +104,31 @@ int OnInit()
|
|||||||
else
|
else
|
||||||
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("%s(%d)", name, InpPeriod));
|
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("%s(%d)", name, InpPeriod));
|
||||||
|
|
||||||
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, 3);
|
int draw_begin = InpPeriod;
|
||||||
|
if(g_is_mtf_mode)
|
||||||
|
draw_begin = 0;
|
||||||
|
|
||||||
|
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, draw_begin);
|
||||||
IndicatorSetInteger(INDICATOR_DIGITS, _Digits);
|
IndicatorSetInteger(INDICATOR_DIGITS, _Digits);
|
||||||
|
|
||||||
|
//--- Initialize 1-second timer for weekend/async chart refreshes (Only if MTF mode is active)
|
||||||
|
if(g_is_mtf_mode)
|
||||||
|
EventSetTimer(1);
|
||||||
|
|
||||||
return(INIT_SUCCEEDED);
|
return(INIT_SUCCEEDED);
|
||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
void OnDeinit(const int reason)
|
void OnDeinit(const int reason)
|
||||||
{
|
{
|
||||||
|
EventKillTimer();
|
||||||
if(CheckPointer(g_calculator) != POINTER_INVALID)
|
if(CheckPointer(g_calculator) != POINTER_INVALID)
|
||||||
delete g_calculator;
|
delete g_calculator;
|
||||||
|
|
||||||
// Free internal memory
|
|
||||||
ArrayFree(BufferFilter_HTF_Internal);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
int OnCalculate(const int rates_total,
|
int OnCalculate(const int rates_total,
|
||||||
const int prev_calculated, // <--- Now used!
|
const int prev_calculated,
|
||||||
const datetime &time[],
|
const datetime &time[],
|
||||||
const double &open[],
|
const double &open[],
|
||||||
const double &high[],
|
const double &high[],
|
||||||
@@ -101,81 +138,173 @@ int OnCalculate(const int rates_total,
|
|||||||
const long &volume[],
|
const long &volume[],
|
||||||
const int &spread[])
|
const int &spread[])
|
||||||
{
|
{
|
||||||
if(rates_total < 2 || CheckPointer(g_calculator) == POINTER_INVALID)
|
if(rates_total < 4)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
if(CheckPointer(g_calculator) == POINTER_INVALID)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
//--- Force strict chronological indexing for state-safety on input price arrays
|
||||||
|
ArraySetAsSeries(time, false);
|
||||||
|
ArraySetAsSeries(open, false);
|
||||||
|
ArraySetAsSeries(high, false);
|
||||||
|
ArraySetAsSeries(low, false);
|
||||||
|
ArraySetAsSeries(close, false);
|
||||||
|
|
||||||
ENUM_APPLIED_PRICE price_type = (InpSourcePrice <= PRICE_HA_CLOSE) ? (ENUM_APPLIED_PRICE)(-(int)InpSourcePrice) : (ENUM_APPLIED_PRICE)InpSourcePrice;
|
ENUM_APPLIED_PRICE price_type = (InpSourcePrice <= PRICE_HA_CLOSE) ? (ENUM_APPLIED_PRICE)(-(int)InpSourcePrice) : (ENUM_APPLIED_PRICE)InpSourcePrice;
|
||||||
|
|
||||||
//================================================================
|
//================================================================
|
||||||
// MTF MODE
|
// MODE 1: Current Timeframe (Standard)
|
||||||
//================================================================
|
//================================================================
|
||||||
if(g_is_mtf_mode)
|
if(!g_is_mtf_mode)
|
||||||
{
|
{
|
||||||
//--- 1. Get HTF Bars Count
|
g_calculator.Calculate(rates_total, prev_calculated, price_type, open, high, low, close, BufferFilterMTF);
|
||||||
int htf_rates_total = (int)SeriesInfoInteger(_Symbol, g_calc_timeframe, SERIES_BARS_COUNT);
|
return(rates_total);
|
||||||
if(htf_rates_total < InpPeriod + 3)
|
}
|
||||||
return 0;
|
|
||||||
|
|
||||||
//--- 2. Manage HTF State (Incremental Logic)
|
//================================================================
|
||||||
static int htf_prev_calculated = 0;
|
// MODE 2: Multi-Timeframe (MTF Engine)
|
||||||
|
//================================================================
|
||||||
|
|
||||||
// Reset if chart was reset
|
//--- Ensure target timeframe history is ready
|
||||||
if(prev_calculated == 0)
|
int required_bars = InpPeriod + 10;
|
||||||
htf_prev_calculated = 0;
|
if(!EnsureHTFDataReady(_Symbol, g_calc_timeframe, required_bars))
|
||||||
|
{
|
||||||
|
g_data_synced = false;
|
||||||
|
return 0; // Wait for next tick to let history load
|
||||||
|
}
|
||||||
|
|
||||||
//--- 3. Fetch HTF Data
|
g_data_synced = true;
|
||||||
datetime htf_time[];
|
|
||||||
double htf_open[], htf_high[], htf_low[], htf_close[];
|
|
||||||
|
|
||||||
if(CopyTime(_Symbol, g_calc_timeframe, 0, htf_rates_total, htf_time) <= 0 ||
|
//--- 1. Check if a new HTF bar has formed
|
||||||
CopyOpen(_Symbol, g_calc_timeframe, 0, htf_rates_total, htf_open) <= 0 ||
|
datetime htf_time_current = iTime(_Symbol, g_calc_timeframe, 0);
|
||||||
CopyHigh(_Symbol, g_calc_timeframe, 0, htf_rates_total, htf_high) <= 0 ||
|
bool htf_updated = (htf_time_current != g_last_htf_time);
|
||||||
CopyLow(_Symbol, g_calc_timeframe, 0, htf_rates_total, htf_low) <= 0 ||
|
|
||||||
CopyClose(_Symbol, g_calc_timeframe, 0, htf_rates_total, htf_close) <= 0)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
//--- 4. Resize Internal Buffer
|
if(htf_updated || prev_calculated == 0)
|
||||||
if(ArraySize(BufferFilter_HTF_Internal) != htf_rates_total)
|
{
|
||||||
ArrayResize(BufferFilter_HTF_Internal, htf_rates_total);
|
g_last_htf_time = htf_time_current;
|
||||||
|
|
||||||
//--- 5. Calculate on HTF (Optimized)
|
int htf_bars = iBars(_Symbol, g_calc_timeframe);
|
||||||
// Pass htf_prev_calculated so the engine skips already calculated bars!
|
if(htf_bars < required_bars)
|
||||||
g_calculator.Calculate(htf_rates_total, htf_prev_calculated, price_type, htf_open, htf_high, htf_low, htf_close, BufferFilter_HTF_Internal);
|
|
||||||
|
|
||||||
// Update state
|
|
||||||
htf_prev_calculated = htf_rates_total;
|
|
||||||
|
|
||||||
//--- 6. Map to Current Timeframe (Optimized Loop)
|
|
||||||
ArraySetAsSeries(htf_time, true);
|
|
||||||
ArraySetAsSeries(BufferFilter_HTF_Internal, true);
|
|
||||||
ArraySetAsSeries(time, true);
|
|
||||||
ArraySetAsSeries(BufferFilterMTF, true);
|
|
||||||
|
|
||||||
// Determine where to start mapping
|
|
||||||
int limit = (prev_calculated > 0) ? rates_total - prev_calculated : rates_total;
|
|
||||||
|
|
||||||
for(int i = 0; i < limit; i++)
|
|
||||||
{
|
{
|
||||||
int htf_bar_shift = iBarShift(_Symbol, g_calc_timeframe, time[i], false);
|
g_data_ready = false;
|
||||||
if(htf_bar_shift >= 0 && htf_bar_shift < htf_rates_total)
|
return 0;
|
||||||
BufferFilterMTF[i] = BufferFilter_HTF_Internal[htf_bar_shift];
|
|
||||||
else
|
|
||||||
BufferFilterMTF[i] = EMPTY_VALUE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ArraySetAsSeries(BufferFilterMTF, false);
|
g_htf_count = MathMin(htf_bars, 3000);
|
||||||
ArraySetAsSeries(time, false);
|
|
||||||
ArraySetAsSeries(BufferFilter_HTF_Internal, false);
|
ArrayResize(h_time, g_htf_count);
|
||||||
|
ArrayResize(h_open, g_htf_count);
|
||||||
|
ArrayResize(h_high, g_htf_count);
|
||||||
|
ArrayResize(h_low, g_htf_count);
|
||||||
|
ArrayResize(h_close, g_htf_count);
|
||||||
|
ArrayResize(h_res_smooth, g_htf_count);
|
||||||
|
|
||||||
|
// Force chronological array alignment on HTF caches after resize
|
||||||
|
ArraySetAsSeries(h_time, false);
|
||||||
|
ArraySetAsSeries(h_open, false);
|
||||||
|
ArraySetAsSeries(h_high, false);
|
||||||
|
ArraySetAsSeries(h_low, false);
|
||||||
|
ArraySetAsSeries(h_close, false);
|
||||||
|
|
||||||
|
if(CopyTime(_Symbol, g_calc_timeframe, 0, g_htf_count, h_time) != g_htf_count ||
|
||||||
|
CopyOpen(_Symbol, g_calc_timeframe, 0, g_htf_count, h_open) != g_htf_count ||
|
||||||
|
CopyHigh(_Symbol, g_calc_timeframe, 0, g_htf_count, h_high) != g_htf_count ||
|
||||||
|
CopyLow(_Symbol, g_calc_timeframe, 0, g_htf_count, h_low) != g_htf_count ||
|
||||||
|
CopyClose(_Symbol, g_calc_timeframe, 0, g_htf_count, h_close) != g_htf_count)
|
||||||
|
{
|
||||||
|
g_data_ready = false;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//--- Calculate Smoother on HTF (Closed bars and forming bar initialized)
|
||||||
|
g_calculator.Calculate(g_htf_count, 0, price_type, h_open, h_high, h_low, h_close, h_res_smooth);
|
||||||
|
|
||||||
|
g_data_ready = true;
|
||||||
}
|
}
|
||||||
//================================================================
|
|
||||||
// CURRENT TIMEFRAME MODE
|
if(!g_data_ready)
|
||||||
//================================================================
|
return 0;
|
||||||
else
|
|
||||||
|
//--- 2. Live Update for the Current Forming HTF Bar (Index: g_htf_count - 1) on every tick!
|
||||||
|
int live_idx = g_htf_count - 1;
|
||||||
|
if(live_idx >= 4)
|
||||||
{
|
{
|
||||||
// Direct calculation with optimization
|
double o[1], h[1], l[1], c[1];
|
||||||
g_calculator.Calculate(rates_total, prev_calculated, price_type, open, high, low, close, BufferFilterMTF);
|
int shift = iBarShift(_Symbol, g_calc_timeframe, htf_time_current, false);
|
||||||
|
if(shift >= 0 &&
|
||||||
|
CopyOpen(_Symbol, g_calc_timeframe, shift, 1, o) == 1 &&
|
||||||
|
CopyHigh(_Symbol, g_calc_timeframe, shift, 1, h) == 1 &&
|
||||||
|
CopyLow(_Symbol, g_calc_timeframe, shift, 1, l) == 1 &&
|
||||||
|
CopyClose(_Symbol, g_calc_timeframe, shift, 1, c) == 1)
|
||||||
|
{
|
||||||
|
h_open[live_idx] = o[0];
|
||||||
|
h_high[live_idx] = h[0];
|
||||||
|
h_low[live_idx] = l[0];
|
||||||
|
h_close[live_idx] = c[0];
|
||||||
|
|
||||||
|
// Incremental recalculation on the live HTF index in O(1)
|
||||||
|
// Passed g_htf_count as prev_calculated to preserve state safety (IIR coefficient protection)
|
||||||
|
g_calculator.Calculate(g_htf_count, g_htf_count, price_type, h_open, h_high, h_low, h_close, h_res_smooth);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//--- 3. FIXED: Dynamically adjust 'start' to the beginning of the current forming HTF bar
|
||||||
|
//--- This forces the entire forming LTF step block to remain perfectly flat, updating on every tick!
|
||||||
|
int start = (prev_calculated > 0) ? prev_calculated - 1 : 0;
|
||||||
|
|
||||||
|
int first_bar_of_forming_htf = rates_total - 1;
|
||||||
|
while(first_bar_of_forming_htf > 0 &&
|
||||||
|
iBarShift(_Symbol, g_calc_timeframe, time[first_bar_of_forming_htf], false) == 0)
|
||||||
|
{
|
||||||
|
first_bar_of_forming_htf--;
|
||||||
|
}
|
||||||
|
first_bar_of_forming_htf++; // This is the start of the forming step on lower TF chart
|
||||||
|
|
||||||
|
if(start > first_bar_of_forming_htf)
|
||||||
|
start = first_bar_of_forming_htf;
|
||||||
|
|
||||||
|
//--- 4. Incremental Mapping of HTF results to Current Chart Timeframe (O(1) per tick)
|
||||||
|
for(int i = start; i < rates_total; i++)
|
||||||
|
{
|
||||||
|
datetime t = time[i];
|
||||||
|
int shift_htf = iBarShift(_Symbol, g_calc_timeframe, t, false);
|
||||||
|
|
||||||
|
if(shift_htf >= 0)
|
||||||
|
{
|
||||||
|
int idx_htf = g_htf_count - 1 - shift_htf;
|
||||||
|
if(idx_htf >= 0 && idx_htf < g_htf_count)
|
||||||
|
{
|
||||||
|
BufferFilterMTF[i] = h_res_smooth[idx_htf];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
BufferFilterMTF[i] = EMPTY_VALUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
BufferFilterMTF[i] = EMPTY_VALUE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return(rates_total);
|
return(rates_total);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| OnTimer |
|
||||||
|
//| Handles loading checks and force-redraws |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
void OnTimer()
|
||||||
|
{
|
||||||
|
if(!g_data_synced)
|
||||||
|
{
|
||||||
|
int required_bars = InpPeriod + 5;
|
||||||
|
if(EnsureHTFDataReady(_Symbol, g_calc_timeframe, required_bars))
|
||||||
|
{
|
||||||
|
g_data_synced = true;
|
||||||
|
ChartRedraw(); // Force MT5 to invoke OnCalculate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
|
|||||||
Reference in New Issue
Block a user