mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-21 00:08:07 +00:00
refactor:
This commit is contained in:
@@ -1,11 +1,11 @@
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| RSIMA.mq5 |
|
//| RSIMA.mq5 |
|
||||||
//| Copyright 2018, MetaQuotes Software Corp. |
|
//| Copyright 2025, xxxxxxxx |
|
||||||
//| https://www.mql5.com |
|
//| |
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
#property copyright "Copyright 2018, MetaQuotes Software Corp."
|
#property copyright "Copyright 2025, xxxxxxxx"
|
||||||
#property link "https://www.mql5.com"
|
#property link ""
|
||||||
#property version "1.10" // Added robust data availability check
|
#property version "2.00" // Refactored for full recalculation and stability
|
||||||
#property description "Oscillator based on the Moving Average of a standard RSI."
|
#property description "Oscillator based on the Moving Average of a standard RSI."
|
||||||
|
|
||||||
// --- Standard Includes ---
|
// --- Standard Includes ---
|
||||||
@@ -36,45 +36,48 @@
|
|||||||
#property indicator_width2 1
|
#property indicator_width2 1
|
||||||
|
|
||||||
//--- Input Parameters ---
|
//--- Input Parameters ---
|
||||||
input uint InpPeriodRSI = 14; // Period for RSI
|
input int InpPeriodRSI = 14; // Period for RSI
|
||||||
input ENUM_APPLIED_PRICE InpAppliedPrice = PRICE_CLOSE; // Applied price for RSI
|
input ENUM_APPLIED_PRICE InpAppliedPrice = PRICE_CLOSE; // Applied price for RSI
|
||||||
input uint InpPeriodMA = 14; // Period for Moving Average
|
input int InpPeriodMA = 14; // Period for Moving Average
|
||||||
input ENUM_MA_METHOD InpMethod = MODE_SMA; // Method for Moving Average
|
input ENUM_MA_METHOD InpMethod = MODE_SMA; // Method for Moving Average
|
||||||
|
|
||||||
//--- Indicator Buffers ---
|
//--- Indicator Buffers ---
|
||||||
double BufferRSIMA[]; // Buffer for the smoothed RSI line (Plot 1)
|
double BufferRSIMA[]; // Buffer for the smoothed RSI line (Plot 1)
|
||||||
double BufferRawRSI[]; // Buffer for the raw RSI values (Plot 2)
|
double BufferRawRSI[]; // Buffer for the raw RSI values (Plot 2)
|
||||||
|
|
||||||
//--- Global Variables ---
|
//--- Global Variables ---
|
||||||
int ExtPeriodRSI;
|
int g_ExtPeriodRSI;
|
||||||
int ExtPeriodMA;
|
int g_ExtPeriodMA;
|
||||||
int handle_rsi; // Handle for the standard RSI indicator
|
int g_handle_rsi; // Handle for the standard RSI indicator
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| Custom indicator initialization function. |
|
//| Custom indicator initialization function. |
|
||||||
//| Called once when the indicator is first loaded. |
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
int OnInit()
|
int OnInit()
|
||||||
{
|
{
|
||||||
//--- Validate and store input periods
|
//--- Validate and store input periods
|
||||||
ExtPeriodRSI = (int)(InpPeriodRSI < 1 ? 1 : InpPeriodRSI);
|
g_ExtPeriodRSI = (InpPeriodRSI < 1) ? 1 : InpPeriodRSI;
|
||||||
ExtPeriodMA = (int)(InpPeriodMA < 1 ? 1 : InpPeriodMA);
|
g_ExtPeriodMA = (InpPeriodMA < 1) ? 1 : InpPeriodMA;
|
||||||
|
|
||||||
//--- Map the buffers to the indicator's internal memory
|
//--- Map the buffers
|
||||||
SetIndexBuffer(0, BufferRSIMA, INDICATOR_DATA);
|
SetIndexBuffer(0, BufferRSIMA, INDICATOR_DATA);
|
||||||
SetIndexBuffer(1, BufferRawRSI, 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
|
//--- 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);
|
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");
|
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");
|
PlotIndexSetString(1, PLOT_LABEL, "RSI");
|
||||||
|
|
||||||
//--- Create a handle to the standard iRSI indicator
|
//--- Create a handle to the standard iRSI indicator
|
||||||
handle_rsi = iRSI(_Symbol, _Period, ExtPeriodRSI, InpAppliedPrice);
|
g_handle_rsi = iRSI(_Symbol, _Period, g_ExtPeriodRSI, InpAppliedPrice);
|
||||||
if(handle_rsi == INVALID_HANDLE)
|
if(g_handle_rsi == INVALID_HANDLE)
|
||||||
{
|
{
|
||||||
PrintFormat("Failed to create iRSI handle. Error %d", GetLastError());
|
PrintFormat("Failed to create iRSI handle. Error %d", GetLastError());
|
||||||
return(INIT_FAILED);
|
return(INIT_FAILED);
|
||||||
@@ -83,9 +86,17 @@ int OnInit()
|
|||||||
return(INIT_SUCCEEDED);
|
return(INIT_SUCCEEDED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
//| Custom indicator deinitialization function. |
|
||||||
|
//+------------------------------------------------------------------+
|
||||||
|
void OnDeinit(const int reason)
|
||||||
|
{
|
||||||
|
//--- Release the indicator handle
|
||||||
|
IndicatorRelease(g_handle_rsi);
|
||||||
|
}
|
||||||
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
//| Custom indicator calculation function. |
|
//| Custom indicator calculation function. |
|
||||||
//| Called on every new tick or new bar. |
|
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
int OnCalculate(const int rates_total,
|
int OnCalculate(const int rates_total,
|
||||||
const int prev_calculated,
|
const int prev_calculated,
|
||||||
@@ -98,65 +109,58 @@ int OnCalculate(const int rates_total,
|
|||||||
const long &volume[],
|
const long &volume[],
|
||||||
const int &spread[])
|
const int &spread[])
|
||||||
{
|
{
|
||||||
//--- Check if there is enough data for the initial calculation
|
//--- Check if there is enough data for the calculation
|
||||||
if(rates_total < ExtPeriodRSI)
|
int start_pos = g_ExtPeriodRSI + g_ExtPeriodMA - 1;
|
||||||
|
if(rates_total <= start_pos)
|
||||||
return(0);
|
return(0);
|
||||||
|
|
||||||
//--- FIX: Check if the source indicator (iRSI) has calculated its data ---
|
//--- STEP 1: Get all available RSI values into our buffer
|
||||||
// This prevents "Error copying buffer" when changing timeframes or on first load.
|
if(CopyBuffer(g_handle_rsi, 0, 0, rates_total, BufferRawRSI) < rates_total)
|
||||||
int calculated_rsi = BarsCalculated(handle_rsi);
|
|
||||||
if(calculated_rsi < rates_total)
|
|
||||||
{
|
{
|
||||||
// Not all data is ready yet, wait for the next OnCalculate call
|
Print("Error copying RSI buffer data.");
|
||||||
return(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--- Get all available RSI values into our buffer ---
|
//--- STEP 2: Calculate the Moving Average on the RSI 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
|
|
||||||
for(int i = start_pos; i < rates_total; i++)
|
for(int i = start_pos; i < rates_total; i++)
|
||||||
{
|
{
|
||||||
if(i < ExtPeriodRSI + ExtPeriodMA - 2)
|
|
||||||
continue; // Skip bars with insufficient data for MA
|
|
||||||
|
|
||||||
switch(InpMethod)
|
switch(InpMethod)
|
||||||
{
|
{
|
||||||
case MODE_EMA:
|
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;
|
break;
|
||||||
case MODE_SMMA:
|
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;
|
break;
|
||||||
case MODE_LWMA:
|
case MODE_LWMA:
|
||||||
BufferRSIMA[i] = LinearWeightedMA(i, ExtPeriodMA, BufferRawRSI);
|
BufferRSIMA[i] = LinearWeightedMA(i, g_ExtPeriodMA, BufferRawRSI);
|
||||||
break;
|
break;
|
||||||
default: // MODE_SMA
|
default: // MODE_SMA
|
||||||
BufferRSIMA[i] = SimpleMA(i, ExtPeriodMA, BufferRawRSI);
|
BufferRSIMA[i] = SimpleMA(i, g_ExtPeriodMA, BufferRawRSI);
|
||||||
break;
|
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);
|
return(rates_total);
|
||||||
}
|
}
|
||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
|
|||||||
Reference in New Issue
Block a user