refactor:

This commit is contained in:
Toh4iem9
2025-08-19 15:36:56 +02:00
parent b4906f0b78
commit b0002f6d36
+66 -62
View File
@@ -1,11 +1,11 @@
//+------------------------------------------------------------------+
//| RSIMA.mq5 |
//| Copyright 2018, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//| RSIMA.mq5 |
//| Copyright 2025, xxxxxxxx |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.10" // Added robust data availability check
#property copyright "Copyright 2025, xxxxxxxx"
#property link ""
#property version "2.00" // Refactored for full recalculation and stability
#property description "Oscillator based on the Moving Average of a standard RSI."
// --- Standard Includes ---
@@ -36,45 +36,48 @@
#property indicator_width2 1
//--- Input Parameters ---
input uint InpPeriodRSI = 14; // Period for RSI
input ENUM_APPLIED_PRICE InpAppliedPrice = PRICE_CLOSE; // Applied price for RSI
input uint InpPeriodMA = 14; // Period for Moving Average
input ENUM_MA_METHOD InpMethod = MODE_SMA; // Method for Moving Average
input int InpPeriodRSI = 14; // Period for RSI
input ENUM_APPLIED_PRICE InpAppliedPrice = PRICE_CLOSE; // Applied price for RSI
input int InpPeriodMA = 14; // Period for Moving Average
input ENUM_MA_METHOD InpMethod = MODE_SMA; // Method for Moving Average
//--- Indicator Buffers ---
double BufferRSIMA[]; // Buffer for the smoothed RSI line (Plot 1)
double BufferRawRSI[]; // Buffer for the raw RSI values (Plot 2)
//--- Global Variables ---
int ExtPeriodRSI;
int ExtPeriodMA;
int handle_rsi; // Handle for the standard RSI indicator
int g_ExtPeriodRSI;
int g_ExtPeriodMA;
int g_handle_rsi; // Handle for the standard RSI indicator
//+------------------------------------------------------------------+
//| Custom indicator initialization function. |
//| Called once when the indicator is first loaded. |
//+------------------------------------------------------------------+
int OnInit()
{
//--- Validate and store input periods
ExtPeriodRSI = (int)(InpPeriodRSI < 1 ? 1 : InpPeriodRSI);
ExtPeriodMA = (int)(InpPeriodMA < 1 ? 1 : InpPeriodMA);
g_ExtPeriodRSI = (InpPeriodRSI < 1) ? 1 : InpPeriodRSI;
g_ExtPeriodMA = (InpPeriodMA < 1) ? 1 : InpPeriodMA;
//--- Map the buffers to the indicator's internal memory
//--- Map the buffers
SetIndexBuffer(0, BufferRSIMA, INDICATOR_DATA);
SetIndexBuffer(1, BufferRawRSI, INDICATOR_DATA);
//--- Set buffers as non-timeseries for stable calculation
ArraySetAsSeries(BufferRSIMA, false);
ArraySetAsSeries(BufferRawRSI, false);
//--- Set indicator display properties
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("RSIMA(%d, %d)", ExtPeriodRSI, ExtPeriodMA));
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("RSIMA(%d, %d)", g_ExtPeriodRSI, g_ExtPeriodMA));
IndicatorSetInteger(INDICATOR_DIGITS, 2);
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, ExtPeriodRSI + ExtPeriodMA - 1);
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, g_ExtPeriodRSI + g_ExtPeriodMA - 1);
PlotIndexSetString(0, PLOT_LABEL, "RSIMA");
PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, ExtPeriodRSI - 1);
PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, g_ExtPeriodRSI - 1);
PlotIndexSetString(1, PLOT_LABEL, "RSI");
//--- Create a handle to the standard iRSI indicator
handle_rsi = iRSI(_Symbol, _Period, ExtPeriodRSI, InpAppliedPrice);
if(handle_rsi == INVALID_HANDLE)
g_handle_rsi = iRSI(_Symbol, _Period, g_ExtPeriodRSI, InpAppliedPrice);
if(g_handle_rsi == INVALID_HANDLE)
{
PrintFormat("Failed to create iRSI handle. Error %d", GetLastError());
return(INIT_FAILED);
@@ -83,9 +86,17 @@ int OnInit()
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function. |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- Release the indicator handle
IndicatorRelease(g_handle_rsi);
}
//+------------------------------------------------------------------+
//| Custom indicator calculation function. |
//| Called on every new tick or new bar. |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
@@ -98,65 +109,58 @@ int OnCalculate(const int rates_total,
const long &volume[],
const int &spread[])
{
//--- Check if there is enough data for the initial calculation
if(rates_total < ExtPeriodRSI)
//--- Check if there is enough data for the calculation
int start_pos = g_ExtPeriodRSI + g_ExtPeriodMA - 1;
if(rates_total <= start_pos)
return(0);
//--- FIX: Check if the source indicator (iRSI) has calculated its data ---
// This prevents "Error copying buffer" when changing timeframes or on first load.
int calculated_rsi = BarsCalculated(handle_rsi);
if(calculated_rsi < rates_total)
//--- STEP 1: Get all available RSI values into our buffer
if(CopyBuffer(g_handle_rsi, 0, 0, rates_total, BufferRawRSI) < rates_total)
{
// Not all data is ready yet, wait for the next OnCalculate call
return(0);
Print("Error copying RSI buffer data.");
}
//--- Get all available RSI values into our buffer ---
if(CopyBuffer(handle_rsi, 0, 0, rates_total, BufferRawRSI) <= 0)
{
// This might still happen occasionally, but the check above reduces it.
Print("Error copying RSI buffer. LastError: ", GetLastError());
return(0);
}
//--- Calculate the Moving Average on the RSI buffer ---
// The MA functions need non-timeseries arrays
ArraySetAsSeries(BufferRawRSI, false);
ArraySetAsSeries(BufferRSIMA, false); // Also set the target buffer
int start_pos;
if(prev_calculated > 1)
start_pos = prev_calculated - 1;
else
start_pos = ExtPeriodRSI + ExtPeriodMA - 2; // Start from the first valid bar
// Loop through the bars that need calculation
//--- STEP 2: Calculate the Moving Average on the RSI buffer
for(int i = start_pos; i < rates_total; i++)
{
if(i < ExtPeriodRSI + ExtPeriodMA - 2)
continue; // Skip bars with insufficient data for MA
switch(InpMethod)
{
case MODE_EMA:
BufferRSIMA[i] = ExponentialMA(i, ExtPeriodMA, BufferRSIMA[i-1], BufferRawRSI);
if(i == start_pos) // Initialization with manual SMA
{
double sum = 0;
for(int j = 0; j < g_ExtPeriodMA; j++)
sum += BufferRawRSI[i - j];
BufferRSIMA[i] = sum / g_ExtPeriodMA;
}
else // Recursive calculation
{
double pr = 2.0 / (g_ExtPeriodMA + 1.0);
BufferRSIMA[i] = BufferRawRSI[i] * pr + BufferRSIMA[i-1] * (1.0 - pr);
}
break;
case MODE_SMMA:
BufferRSIMA[i] = SmoothedMA(i, ExtPeriodMA, BufferRSIMA[i-1], BufferRawRSI);
if(i == start_pos) // Initialization with manual SMA
{
double sum = 0;
for(int j = 0; j < g_ExtPeriodMA; j++)
sum += BufferRawRSI[i - j];
BufferRSIMA[i] = sum / g_ExtPeriodMA;
}
else // Recursive calculation
{
BufferRSIMA[i] = (BufferRSIMA[i-1] * (g_ExtPeriodMA - 1) + BufferRawRSI[i]) / g_ExtPeriodMA;
}
break;
case MODE_LWMA:
BufferRSIMA[i] = LinearWeightedMA(i, ExtPeriodMA, BufferRawRSI);
BufferRSIMA[i] = LinearWeightedMA(i, g_ExtPeriodMA, BufferRawRSI);
break;
default: // MODE_SMA
BufferRSIMA[i] = SimpleMA(i, ExtPeriodMA, BufferRawRSI);
BufferRSIMA[i] = SimpleMA(i, g_ExtPeriodMA, BufferRawRSI);
break;
}
}
// It's good practice to restore the series state if other parts of the code might expect it
ArraySetAsSeries(BufferRawRSI, true);
ArraySetAsSeries(BufferRSIMA, true);
return(rates_total);
}
//+------------------------------------------------------------------+