Files
mql5/Indicators/MyIndicators/RSI_HeikenAshi.mq5
T
2025-08-12 18:08:11 +02:00

204 lines
16 KiB
Plaintext

//+------------------------------------------------------------------+
//| RSI_HeikenAshi.mq5 |
//| Copyright 2025, xxxxxxxx (Based on MetaQuotes RSI) |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx"
#property link ""
#property version "3.00" // Final, stable, non-optimized version
#property description "RSI on Heiken Ashi prices, with a Moving Average."
// --- Standard and Custom Includes ---
#include <MovingAverages.mqh>
#include <MyIncludes\HA_Tools.mqh>
//--- Indicator Window and Level Properties ---
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_level1 30.0
#property indicator_level2 50.0
#property indicator_level3 70.0
//--- Buffers and Plots ---
#property indicator_buffers 4 // 2 for plotting, 2 for RSI calculations
#property indicator_plots 2
//--- Plot 1: RSI MA line (smoothed)
#property indicator_label1 "HA_RSIMA"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- Plot 2: RSI line (raw)
#property indicator_label2 "HA_RSI"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrGreen
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//--- Input Parameters ---
input int InpPeriodRSI = 14; // Period for RSI calculation
input int InpPeriodMA = 14; // Period for Moving Average smoothing
input ENUM_MA_METHOD InpMethodMA = MODE_SMA; // Method for Moving Average smoothing
//--- Indicator Buffers ---
double BufferHARSI_MA[]; // Plotted buffer for the smoothed RSI line
double BufferHARSI[]; // Plotted buffer for the raw Heiken Ashi RSI line
double BufferPos[]; // Calculation buffer for RSI's average gain
double BufferNeg[]; // Calculation buffer for RSI's average loss
//--- Global Objects and Variables ---
int ExtPeriodRSI;
int ExtPeriodMA;
CHA_Calculator g_ha_calculator; // Global instance of our Heiken Ashi calculator
//+------------------------------------------------------------------+
//| Custom indicator initialization function. |
//| Called once when the indicator is first loaded. |
//+------------------------------------------------------------------+
void OnInit()
{
//--- Validate and store input periods
ExtPeriodRSI = (InpPeriodRSI < 1) ? 1 : InpPeriodRSI;
ExtPeriodMA = (InpPeriodMA < 1) ? 1 : InpPeriodMA;
//--- Map the buffers to the indicator's internal memory
SetIndexBuffer(0, BufferHARSI_MA, INDICATOR_DATA);
SetIndexBuffer(1, BufferHARSI, INDICATOR_DATA);
SetIndexBuffer(2, BufferPos, INDICATOR_CALCULATIONS);
SetIndexBuffer(3, BufferNeg, INDICATOR_CALCULATIONS);
//--- Set all buffers to work as regular arrays (non-timeseries)
// This is the key for a stable calculation from past (index 0) to present
ArraySetAsSeries(BufferHARSI_MA, false);
ArraySetAsSeries(BufferHARSI, false);
ArraySetAsSeries(BufferPos, false);
ArraySetAsSeries(BufferNeg, false);
//--- Set indicator display properties
IndicatorSetInteger(INDICATOR_DIGITS, 2);
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, ExtPeriodRSI + ExtPeriodMA - 1);
PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, ExtPeriodRSI);
PlotIndexSetString(0, PLOT_LABEL, "HA_RSIMA");
PlotIndexSetString(1, PLOT_LABEL, "HA_RSI");
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("HA_RSI(%d, %d)", ExtPeriodRSI, ExtPeriodMA));
}
//+------------------------------------------------------------------+
//| Custom indicator calculation function. |
//| This version performs a full recalculation on every call for |
//| maximum stability with complex, multi-stage calculations. |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
//--- Check if there is enough historical data for RSI calculation
if(rates_total < ExtPeriodRSI + 1)
return(0);
//--- STEP 1: Calculate Heiken Ashi bars for the entire history
if(!g_ha_calculator.Calculate(rates_total, 0, open, high, low, close))
return(0);
//--- STEP 2: Calculate RSI on HA Close, iterating from past to present
for(int i = 1; i < rates_total; i++)
{
double diff = g_ha_calculator.ha_close[i] - g_ha_calculator.ha_close[i-1];
double positive_change = (diff > 0) ? diff : 0;
double negative_change = (diff < 0) ? -diff : 0;
// Apply Wilder's Smoothing for Pos and Neg buffers
if(i > ExtPeriodRSI)
{
BufferPos[i] = (BufferPos[i-1] * (ExtPeriodRSI - 1) + positive_change) / ExtPeriodRSI;
BufferNeg[i] = (BufferNeg[i-1] * (ExtPeriodRSI - 1) + negative_change) / ExtPeriodRSI;
}
else
{
BufferPos[i] = BufferPos[i-1] + positive_change;
BufferNeg[i] = BufferNeg[i-1] + negative_change;
if(i == ExtPeriodRSI)
{
BufferPos[i] /= ExtPeriodRSI;
BufferNeg[i] /= ExtPeriodRSI;
}
}
// Calculate the final RSI value
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 (with EMA/SMMA fix)
if(rates_total < ExtPeriodRSI + ExtPeriodMA)
return(rates_total);
for(int i = 1; i < rates_total; i++)
{
// Skip bars until we have enough data for the first MA value
if(i < ExtPeriodRSI + ExtPeriodMA - 2)
{
BufferHARSI_MA[i] = EMPTY_VALUE;
continue;
}
// Calculate the MA value for the current bar 'i'
switch(InpMethodMA)
{
case MODE_EMA:
// --- Special handling for EMA ---
if(i == ExtPeriodRSI + ExtPeriodMA - 2) // First EMA value is an SMA
{
BufferHARSI_MA[i] = SimpleMA(i, ExtPeriodMA, BufferHARSI);
}
else // Subsequent EMA values are calculated recursively
{
double pr = 2.0 / (ExtPeriodMA + 1.0);
BufferHARSI_MA[i] = BufferHARSI[i] * pr + BufferHARSI_MA[i-1] * (1.0 - pr);
}
break;
case MODE_SMMA:
// Special handling for SMMA
if(i == ExtPeriodRSI + ExtPeriodMA - 2) // First SMMA value is an SMA
{
BufferHARSI_MA[i] = SimpleMA(i, ExtPeriodMA, BufferHARSI);
}
else // Subsequent SMMA values are calculated recursively
{
BufferHARSI_MA[i] = (BufferHARSI_MA[i-1] * (ExtPeriodMA - 1) + BufferHARSI[i]) / ExtPeriodMA;
}
break;
case MODE_LWMA:
BufferHARSI_MA[i] = LinearWeightedMA(i, ExtPeriodMA, BufferHARSI);
break;
default: // MODE_SMA
BufferHARSI_MA[i] = SimpleMA(i, ExtPeriodMA, BufferHARSI);
break;
}
}
return(rates_total);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+