mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-23 01:08:06 +00:00
refactor: Handles current timeframe correctly.
This commit is contained in:
@@ -4,9 +4,9 @@
|
|||||||
//| |
|
//| |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
#property copyright "Copyright 2025, xxxxxxxx"
|
#property copyright "Copyright 2025, xxxxxxxx"
|
||||||
#property version "2.00" // REFACTORED: Self-contained calculation, no iCustom dependency
|
#property version "2.10" // REFACTORED: Handles current timeframe correctly.
|
||||||
#property description "Multi-Timeframe (MTF) version of John Ehlers' MAMA and FAMA."
|
#property description "Multi-Timeframe (MTF) version of John Ehlers' MAMA and FAMA."
|
||||||
#property description "Displays MAMA/FAMA from a higher timeframe on the current chart."
|
#property description "Displays MAMA/FAMA from a higher or the current timeframe on the chart."
|
||||||
|
|
||||||
#property indicator_chart_window
|
#property indicator_chart_window
|
||||||
#property indicator_buffers 2
|
#property indicator_buffers 2
|
||||||
@@ -29,7 +29,7 @@
|
|||||||
#include <MyIncludes\MAMA_Calculator.mqh>
|
#include <MyIncludes\MAMA_Calculator.mqh>
|
||||||
|
|
||||||
//--- Input Parameters ---
|
//--- Input Parameters ---
|
||||||
input ENUM_TIMEFRAMES InpUpperTimeframe = PERIOD_M30; // Timeframe for MAMA calculation
|
input ENUM_TIMEFRAMES InpUpperTimeframe = PERIOD_CURRENT; // Default to current timeframe
|
||||||
input double InpFastLimit = 0.5; // Fast Limit for Alpha
|
input double InpFastLimit = 0.5; // Fast Limit for Alpha
|
||||||
input double InpSlowLimit = 0.05; // Slow Limit for Alpha
|
input double InpSlowLimit = 0.05; // Slow Limit for Alpha
|
||||||
input ENUM_APPLIED_PRICE_HA_ALL InpSourcePrice = PRICE_CLOSE_STD;
|
input ENUM_APPLIED_PRICE_HA_ALL InpSourcePrice = PRICE_CLOSE_STD;
|
||||||
@@ -38,19 +38,30 @@ input ENUM_APPLIED_PRICE_HA_ALL InpSourcePrice = PRICE_CLOSE_STD;
|
|||||||
double BufferMAMA_MTF[];
|
double BufferMAMA_MTF[];
|
||||||
double BufferFAMA_MTF[];
|
double BufferFAMA_MTF[];
|
||||||
|
|
||||||
//--- Global calculator object ---
|
//--- Global variables ---
|
||||||
CMAMACalculator *g_calculator;
|
CMAMACalculator *g_calculator;
|
||||||
|
bool g_is_mtf_mode = false;
|
||||||
|
ENUM_TIMEFRAMES g_calc_timeframe;
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
int OnInit()
|
int OnInit()
|
||||||
{
|
{
|
||||||
//--- Ensure the selected timeframe is higher than the current one
|
// --- Determine calculation mode (MTF or Current) ---
|
||||||
if(InpUpperTimeframe <= Period())
|
g_calc_timeframe = InpUpperTimeframe;
|
||||||
|
if(g_calc_timeframe == PERIOD_CURRENT)
|
||||||
{
|
{
|
||||||
Print("Error: The selected timeframe must be higher than the current chart timeframe.");
|
g_calc_timeframe = (ENUM_TIMEFRAMES)Period();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(g_calc_timeframe < Period())
|
||||||
|
{
|
||||||
|
Print("Error: The selected timeframe must be lower than the current chart timeframe.");
|
||||||
return(INIT_FAILED);
|
return(INIT_FAILED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
g_is_mtf_mode = (g_calc_timeframe > Period());
|
||||||
|
|
||||||
|
// --- Standard buffer and calculator setup ---
|
||||||
SetIndexBuffer(0, BufferMAMA_MTF, INDICATOR_DATA);
|
SetIndexBuffer(0, BufferMAMA_MTF, INDICATOR_DATA);
|
||||||
SetIndexBuffer(1, BufferFAMA_MTF, INDICATOR_DATA);
|
SetIndexBuffer(1, BufferFAMA_MTF, INDICATOR_DATA);
|
||||||
ArraySetAsSeries(BufferMAMA_MTF, false);
|
ArraySetAsSeries(BufferMAMA_MTF, false);
|
||||||
@@ -58,7 +69,6 @@ int OnInit()
|
|||||||
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
|
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
|
||||||
PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
|
PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
|
||||||
|
|
||||||
// --- Create an instance of our own calculator ---
|
|
||||||
if(InpSourcePrice <= PRICE_HA_CLOSE)
|
if(InpSourcePrice <= PRICE_HA_CLOSE)
|
||||||
{
|
{
|
||||||
g_calculator = new CMAMACalculator_HA();
|
g_calculator = new CMAMACalculator_HA();
|
||||||
@@ -74,7 +84,11 @@ int OnInit()
|
|||||||
return(INIT_FAILED);
|
return(INIT_FAILED);
|
||||||
}
|
}
|
||||||
|
|
||||||
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("MAMA MTF(%s)", EnumToString(InpUpperTimeframe)));
|
if(g_is_mtf_mode)
|
||||||
|
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("MAMA MTF(%s)", EnumToString(g_calc_timeframe)));
|
||||||
|
else
|
||||||
|
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("MAMA(%.2f,%.2f)", InpFastLimit, InpSlowLimit));
|
||||||
|
|
||||||
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, 50);
|
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, 50);
|
||||||
PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, 50);
|
PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, 50);
|
||||||
IndicatorSetInteger(INDICATOR_DIGITS, _Digits);
|
IndicatorSetInteger(INDICATOR_DIGITS, _Digits);
|
||||||
@@ -95,65 +109,68 @@ int OnCalculate(const int rates_total, const int, const datetime &time[], const
|
|||||||
if(rates_total < 2 || CheckPointer(g_calculator) == POINTER_INVALID)
|
if(rates_total < 2 || CheckPointer(g_calculator) == POINTER_INVALID)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
// 1. Get the number of bars on the higher timeframe
|
|
||||||
int htf_rates_total = (int)SeriesInfoInteger(_Symbol, InpUpperTimeframe, SERIES_BARS_COUNT);
|
|
||||||
if(htf_rates_total < 50) // MAMA warmup period
|
|
||||||
{
|
|
||||||
return 0; // Not enough data on HTF yet
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. Copy all necessary data from the higher timeframe
|
|
||||||
datetime htf_time[];
|
|
||||||
double htf_open[], htf_high[], htf_low[], htf_close[];
|
|
||||||
if(CopyTime(_Symbol, InpUpperTimeframe, 0, htf_rates_total, htf_time) <= 0 ||
|
|
||||||
CopyOpen(_Symbol, InpUpperTimeframe, 0, htf_rates_total, htf_open) <= 0 ||
|
|
||||||
CopyHigh(_Symbol, InpUpperTimeframe, 0, htf_rates_total, htf_high) <= 0 ||
|
|
||||||
CopyLow(_Symbol, InpUpperTimeframe, 0, htf_rates_total, htf_low) <= 0 ||
|
|
||||||
CopyClose(_Symbol, InpUpperTimeframe, 0, htf_rates_total, htf_close) <= 0)
|
|
||||||
{
|
|
||||||
return 0; // Data not fully ready
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. Calculate MAMA/FAMA on the higher timeframe data
|
|
||||||
double htf_mama_buffer[], htf_fama_buffer[];
|
|
||||||
ArrayResize(htf_mama_buffer, htf_rates_total);
|
|
||||||
ArrayResize(htf_fama_buffer, htf_rates_total);
|
|
||||||
|
|
||||||
ENUM_APPLIED_PRICE price_type;
|
ENUM_APPLIED_PRICE price_type;
|
||||||
if(InpSourcePrice <= PRICE_HA_CLOSE)
|
if(InpSourcePrice <= PRICE_HA_CLOSE)
|
||||||
price_type = (ENUM_APPLIED_PRICE)(-(int)InpSourcePrice);
|
price_type = (ENUM_APPLIED_PRICE)(-(int)InpSourcePrice);
|
||||||
else
|
else
|
||||||
price_type = (ENUM_APPLIED_PRICE)InpSourcePrice;
|
price_type = (ENUM_APPLIED_PRICE)InpSourcePrice;
|
||||||
|
|
||||||
g_calculator.Calculate(htf_rates_total, price_type, htf_open, htf_high, htf_low, htf_close, htf_mama_buffer, htf_fama_buffer);
|
// --- Branching logic based on mode ---
|
||||||
|
if(g_is_mtf_mode)
|
||||||
// 4. Map the higher timeframe data to the current chart's bars
|
|
||||||
ArraySetAsSeries(htf_mama_buffer, true);
|
|
||||||
ArraySetAsSeries(htf_fama_buffer, true);
|
|
||||||
ArraySetAsSeries(htf_time, true);
|
|
||||||
ArraySetAsSeries(time, true);
|
|
||||||
ArraySetAsSeries(BufferMAMA_MTF, true);
|
|
||||||
ArraySetAsSeries(BufferFAMA_MTF, true);
|
|
||||||
|
|
||||||
for(int i = 0; i < rates_total; i++)
|
|
||||||
{
|
{
|
||||||
int htf_bar_shift = iBarShift(_Symbol, InpUpperTimeframe, time[i]);
|
// --- MTF Mode ---
|
||||||
if(htf_bar_shift < htf_rates_total && htf_bar_shift >= 0)
|
int htf_rates_total = (int)SeriesInfoInteger(_Symbol, g_calc_timeframe, SERIES_BARS_COUNT);
|
||||||
{
|
if(htf_rates_total < 50)
|
||||||
BufferMAMA_MTF[i] = htf_mama_buffer[htf_bar_shift];
|
return 0; // MAMA warmup period
|
||||||
BufferFAMA_MTF[i] = htf_fama_buffer[htf_bar_shift];
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
BufferMAMA_MTF[i] = EMPTY_VALUE;
|
|
||||||
BufferFAMA_MTF[i] = EMPTY_VALUE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Restore arrays to non-timeseries for stability
|
datetime htf_time[];
|
||||||
ArraySetAsSeries(BufferMAMA_MTF, false);
|
double htf_open[], htf_high[], htf_low[], htf_close[];
|
||||||
ArraySetAsSeries(BufferFAMA_MTF, false);
|
if(CopyTime(_Symbol, g_calc_timeframe, 0, htf_rates_total, htf_time) <= 0 ||
|
||||||
ArraySetAsSeries(time, false);
|
CopyOpen(_Symbol, g_calc_timeframe, 0, htf_rates_total, htf_open) <= 0 ||
|
||||||
|
CopyHigh(_Symbol, g_calc_timeframe, 0, htf_rates_total, htf_high) <= 0 ||
|
||||||
|
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; // Data not fully ready
|
||||||
|
}
|
||||||
|
|
||||||
|
double htf_mama_buffer[], htf_fama_buffer[];
|
||||||
|
ArrayResize(htf_mama_buffer, htf_rates_total);
|
||||||
|
ArrayResize(htf_fama_buffer, htf_rates_total);
|
||||||
|
|
||||||
|
g_calculator.Calculate(htf_rates_total, price_type, htf_open, htf_high, htf_low, htf_close, htf_mama_buffer, htf_fama_buffer);
|
||||||
|
|
||||||
|
ArraySetAsSeries(htf_mama_buffer, true);
|
||||||
|
ArraySetAsSeries(htf_fama_buffer, true);
|
||||||
|
ArraySetAsSeries(htf_time, true);
|
||||||
|
ArraySetAsSeries(time, true);
|
||||||
|
ArraySetAsSeries(BufferMAMA_MTF, true);
|
||||||
|
ArraySetAsSeries(BufferFAMA_MTF, true);
|
||||||
|
|
||||||
|
for(int i = 0; i < rates_total; i++)
|
||||||
|
{
|
||||||
|
int htf_bar_shift = iBarShift(_Symbol, g_calc_timeframe, time[i]);
|
||||||
|
if(htf_bar_shift < htf_rates_total && htf_bar_shift >= 0)
|
||||||
|
{
|
||||||
|
BufferMAMA_MTF[i] = htf_mama_buffer[htf_bar_shift];
|
||||||
|
BufferFAMA_MTF[i] = htf_fama_buffer[htf_bar_shift];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
BufferMAMA_MTF[i] = EMPTY_VALUE;
|
||||||
|
BufferFAMA_MTF[i] = EMPTY_VALUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ArraySetAsSeries(BufferMAMA_MTF, false);
|
||||||
|
ArraySetAsSeries(BufferFAMA_MTF, false);
|
||||||
|
ArraySetAsSeries(time, false);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// --- Current Timeframe Mode ---
|
||||||
|
g_calculator.Calculate(rates_total, price_type, open, high, low, close, BufferMAMA_MTF, BufferFAMA_MTF);
|
||||||
|
}
|
||||||
|
|
||||||
return(rates_total);
|
return(rates_total);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user