refactor: harmonized with fully manual MA calculations

This commit is contained in:
Toh4iem9
2025-08-29 10:01:01 +02:00
parent d9bb631dc6
commit b1207b73b2
+60 -123
View File
@@ -5,11 +5,9 @@
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx" #property copyright "Copyright 2025, xxxxxxxx"
#property link "" #property link ""
#property version "4.00" // Refactored for full recalculation and stability #property version "4.01" // Harmonized with fully manual MA calculations
#property description "RSI on Heikin Ashi prices, with a Moving Average." #property description "RSI on Heikin Ashi prices, with a Moving Average."
// --- Standard and Custom Includes ---
#include <MovingAverages.mqh>
#include <MyIncludes\HeikinAshi_Tools.mqh> #include <MyIncludes\HeikinAshi_Tools.mqh>
//--- Indicator Window and Level Properties --- //--- Indicator Window and Level Properties ---
@@ -21,82 +19,62 @@
#property indicator_level3 70.0 #property indicator_level3 70.0
//--- Buffers and Plots --- //--- Buffers and Plots ---
#property indicator_buffers 4 // 2 for plotting, 2 for RSI calculations #property indicator_buffers 2 // RSI and its MA
#property indicator_plots 2 #property indicator_plots 2
//--- Plot 1: RSI MA line (smoothed) //--- Plot 1: RSI MA line (smoothed)
#property indicator_label1 "HA_RSIMA" #property indicator_label1 "HA_RSIMA"
#property indicator_type1 DRAW_LINE #property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue #property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID #property indicator_style1 STYLE_DOT
#property indicator_width1 1 #property indicator_width1 1
//--- Plot 2: RSI line (raw) //--- Plot 2: RSI line (raw)
#property indicator_label2 "HA_RSI" #property indicator_label2 "HA_RSI"
#property indicator_type2 DRAW_LINE #property indicator_type2 DRAW_LINE
#property indicator_color2 clrGreen #property indicator_color2 clrDodgerBlue
#property indicator_style2 STYLE_SOLID #property indicator_style2 STYLE_SOLID
#property indicator_width2 1 #property indicator_width2 1
//--- Input Parameters --- //--- Input Parameters ---
input int InpPeriodRSI = 14; // Period for RSI calculation input int InpPeriodRSI = 14;
input int InpPeriodMA = 14; // Period for Moving Average smoothing input group "Signal Line Settings"
input ENUM_MA_METHOD InpMethodMA = MODE_SMA; // Method for Moving Average smoothing input int InpPeriodMA = 14;
input ENUM_MA_METHOD InpMethodMA = MODE_SMA;
//--- Indicator Buffers --- //--- Indicator Buffers ---
double BufferHARSI_MA[]; // Plotted buffer for the smoothed RSI line double BufferHARSI_MA[];
double BufferHARSI[]; // Plotted buffer for the raw Heikin Ashi RSI line double BufferHARSI[];
double BufferPos[]; // Calculation buffer for RSI's average gain
double BufferNeg[]; // Calculation buffer for RSI's average loss
//--- Intermediate Heikin Ashi Buffers ---
double ExtHaOpenBuffer[];
double ExtHaHighBuffer[];
double ExtHaLowBuffer[];
double ExtHaCloseBuffer[];
//--- Global Objects and Variables --- //--- Global Objects and Variables ---
int g_ExtPeriodRSI; int g_ExtPeriodRSI, g_ExtPeriodMA;
int g_ExtPeriodMA; CHeikinAshi_RSI_Calculator *g_ha_rsi_calculator;
CHeikinAshi_Calculator *g_ha_calculator; // Pointer to our Heikin Ashi calculator
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Custom indicator initialization function. | //| Custom indicator initialization function. |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
int OnInit() int OnInit()
{ {
//--- Validate and store input periods
g_ExtPeriodRSI = (InpPeriodRSI < 1) ? 1 : InpPeriodRSI; g_ExtPeriodRSI = (InpPeriodRSI < 1) ? 1 : InpPeriodRSI;
g_ExtPeriodMA = (InpPeriodMA < 1) ? 1 : InpPeriodMA; g_ExtPeriodMA = (InpPeriodMA < 1) ? 1 : InpPeriodMA;
//--- Map the buffers
SetIndexBuffer(0, BufferHARSI_MA, INDICATOR_DATA); SetIndexBuffer(0, BufferHARSI_MA, INDICATOR_DATA);
SetIndexBuffer(1, BufferHARSI, INDICATOR_DATA); SetIndexBuffer(1, BufferHARSI, INDICATOR_DATA);
SetIndexBuffer(2, BufferPos, INDICATOR_CALCULATIONS);
SetIndexBuffer(3, BufferNeg, INDICATOR_CALCULATIONS);
//--- Set all buffers as non-timeseries
ArraySetAsSeries(BufferHARSI_MA, false); ArraySetAsSeries(BufferHARSI_MA, false);
ArraySetAsSeries(BufferHARSI, false); ArraySetAsSeries(BufferHARSI, false);
ArraySetAsSeries(BufferPos, false);
ArraySetAsSeries(BufferNeg, false);
//--- Set indicator display properties
IndicatorSetInteger(INDICATOR_DIGITS, 2); IndicatorSetInteger(INDICATOR_DIGITS, 2);
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, g_ExtPeriodRSI + g_ExtPeriodMA - 1); PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, g_ExtPeriodRSI + g_ExtPeriodMA - 1);
PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, g_ExtPeriodRSI); PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, g_ExtPeriodRSI);
PlotIndexSetString(0, PLOT_LABEL, "HA_RSIMA");
PlotIndexSetString(1, PLOT_LABEL, "HA_RSI");
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("HA_RSI(%d, %d)", g_ExtPeriodRSI, g_ExtPeriodMA)); IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("HA_RSI(%d, %d)", g_ExtPeriodRSI, g_ExtPeriodMA));
//--- Create the calculator instance g_ha_rsi_calculator = new CHeikinAshi_RSI_Calculator();
g_ha_calculator = new CHeikinAshi_Calculator(); if(CheckPointer(g_ha_rsi_calculator) == POINTER_INVALID)
if(CheckPointer(g_ha_calculator) == POINTER_INVALID)
{ {
Print("Error creating CHeikinAshi_Calculator object"); Print("Error creating CHeikinAshi_RSI_Calculator object");
return(INIT_FAILED); return(INIT_FAILED);
} }
return(INIT_SUCCEEDED); return(INIT_SUCCEEDED);
} }
@@ -105,16 +83,15 @@ int OnInit()
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
void OnDeinit(const int reason) void OnDeinit(const int reason)
{ {
//--- Free the calculator object if(CheckPointer(g_ha_rsi_calculator) != POINTER_INVALID)
if(CheckPointer(g_ha_calculator) != POINTER_INVALID)
{ {
delete g_ha_calculator; delete g_ha_rsi_calculator;
g_ha_calculator = NULL; g_ha_rsi_calculator = NULL;
} }
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
//| Custom indicator calculation function. | //| RSI on Heikin Ashi calculation function. |
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+
int OnCalculate(const int rates_total, int OnCalculate(const int rates_total,
const int prev_calculated, const int prev_calculated,
@@ -127,108 +104,68 @@ int OnCalculate(const int rates_total,
const long &volume[], const long &volume[],
const int &spread[]) const int &spread[])
{ {
if(rates_total <= g_ExtPeriodRSI) int start_pos = g_ExtPeriodRSI + g_ExtPeriodMA;
if(rates_total <= start_pos)
return(0); return(0);
//--- Resize intermediate buffers //--- STEP 1: Calculate Heikin Ashi RSI values using our toolkit
ArrayResize(ExtHaOpenBuffer, rates_total); if(!g_ha_rsi_calculator.Calculate(rates_total, g_ExtPeriodRSI, open, high, low, close, BufferHARSI))
ArrayResize(ExtHaHighBuffer, rates_total);
ArrayResize(ExtHaLowBuffer, rates_total);
ArrayResize(ExtHaCloseBuffer, rates_total);
//--- STEP 1: Calculate Heikin Ashi bars
g_ha_calculator.Calculate(rates_total, open, high, low, close,
ExtHaOpenBuffer, ExtHaHighBuffer, ExtHaLowBuffer, ExtHaCloseBuffer);
//--- STEP 2: Calculate RSI on HA Close in a single, robust loop
for(int i = 1; i < rates_total; i++)
{ {
double diff = ExtHaCloseBuffer[i] - ExtHaCloseBuffer[i-1]; Print("Heikin Ashi RSI calculation failed.");
double positive_change = (diff > 0) ? diff : 0; return(0);
double negative_change = (diff < 0) ? -diff : 0;
if(i == g_ExtPeriodRSI)
{
double sum_pos=0, sum_neg=0;
for(int j=1; j<=g_ExtPeriodRSI; j++)
{
double p_diff = ExtHaCloseBuffer[j] - ExtHaCloseBuffer[j-1];
sum_pos += (p_diff > 0) ? p_diff : 0;
sum_neg += (p_diff < 0) ? -p_diff : 0;
}
BufferPos[i] = sum_pos / g_ExtPeriodRSI;
BufferNeg[i] = sum_neg / g_ExtPeriodRSI;
}
else
if(i > g_ExtPeriodRSI)
{
BufferPos[i] = (BufferPos[i-1] * (g_ExtPeriodRSI - 1) + positive_change) / g_ExtPeriodRSI;
BufferNeg[i] = (BufferNeg[i-1] * (g_ExtPeriodRSI - 1) + negative_change) / g_ExtPeriodRSI;
}
if(i >= g_ExtPeriodRSI)
{
if(BufferNeg[i] > 0)
{
double rs = BufferPos[i] / BufferNeg[i];
BufferHARSI[i] = 100.0 - (100.0 / (1.0 + rs));
}
else
{
BufferHARSI[i] = 100.0;
}
}
} }
//--- STEP 3: Calculate Moving Average on the HA RSI buffer //--- STEP 2: Calculate the Signal Line (MA of HA RSI)
// --- FIX: Correct starting position for the MA calculation ---
int ma_start_pos = g_ExtPeriodRSI + g_ExtPeriodMA - 1; int ma_start_pos = g_ExtPeriodRSI + g_ExtPeriodMA - 1;
for(int i = ma_start_pos; i < rates_total; i++) for(int i = ma_start_pos; i < rates_total; i++)
{ {
switch(InpMethodMA) switch(InpMethodMA)
{ {
case MODE_EMA: case MODE_EMA:
if(i == ma_start_pos)
{
// Manual SMA for initialization on non-timeseries array
double sum = 0;
for(int j = 0; j < g_ExtPeriodMA; j++)
{
sum += BufferHARSI[i - j];
}
BufferHARSI_MA[i] = sum / g_ExtPeriodMA;
}
else
{
double pr = 2.0 / (g_ExtPeriodMA + 1.0);
BufferHARSI_MA[i] = BufferHARSI[i] * pr + BufferHARSI_MA[i-1] * (1.0 - pr);
}
break;
case MODE_SMMA: case MODE_SMMA:
if(i == ma_start_pos) if(i == ma_start_pos)
{ {
// Manual SMA for initialization on non-timeseries array double sum=0;
double sum = 0; for(int j=0; j<g_ExtPeriodMA; j++)
for(int j = 0; j < g_ExtPeriodMA; j++) sum+=BufferHARSI[i-j];
{ BufferHARSI_MA[i] = sum/g_ExtPeriodMA;
sum += BufferHARSI[i - j];
}
BufferHARSI_MA[i] = sum / g_ExtPeriodMA;
} }
else else
BufferHARSI_MA[i] = (BufferHARSI_MA[i-1] * (g_ExtPeriodMA - 1) + BufferHARSI[i]) / g_ExtPeriodMA; {
if(InpMethodMA == MODE_EMA)
{
double pr=2.0/(g_ExtPeriodMA+1.0);
BufferHARSI_MA[i] = BufferHARSI[i]*pr + BufferHARSI_MA[i-1]*(1.0-pr);
}
else
BufferHARSI_MA[i] = (BufferHARSI_MA[i-1]*(g_ExtPeriodMA-1)+BufferHARSI[i])/g_ExtPeriodMA;
}
break; break;
case MODE_LWMA: case MODE_LWMA:
BufferHARSI_MA[i] = LinearWeightedMA(i, g_ExtPeriodMA, BufferHARSI); {
break; double lwma_sum=0, weight_sum=0;
for(int j=0; j<g_ExtPeriodMA; j++)
{
int weight=g_ExtPeriodMA-j;
lwma_sum+=BufferHARSI[i-j]*weight;
weight_sum+=weight;
}
if(weight_sum>0)
BufferHARSI_MA[i]=lwma_sum/weight_sum;
}
break;
default: // MODE_SMA default: // MODE_SMA
BufferHARSI_MA[i] = SimpleMA(i, g_ExtPeriodMA, BufferHARSI); {
break; double sum=0;
for(int j=0; j<g_ExtPeriodMA; j++)
sum+=BufferHARSI[i-j];
BufferHARSI_MA[i] = sum/g_ExtPeriodMA;
}
break;
} }
} }
return(rates_total); return(rates_total);
} }
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+```
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+