Files
mql5/Indicators/MyIndicators/RSI_Oscillator.mq5
T

112 lines
4.3 KiB
Plaintext
Raw Normal View History

2025-08-29 10:01:26 +02:00
//+------------------------------------------------------------------+
//| RSI_Oscillator.mq5 |
2025-09-24 12:39:46 +02:00
//| Copyright 2025, xxxxxxxx|
2025-08-29 10:01:26 +02:00
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx"
2025-09-24 12:39:46 +02:00
#property version "3.00"
#property description "RSI Oscillator (Histogram of RSI vs Signal Line) with selectable price source."
2025-08-29 10:01:26 +02:00
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_type1 DRAW_HISTOGRAM
#property indicator_color1 clrSilver
2025-09-24 12:39:46 +02:00
#property indicator_width1 2
2025-08-29 10:01:26 +02:00
#property indicator_label1 "RSI Oscillator"
#property indicator_level1 0.0
#property indicator_levelstyle STYLE_DOT
2025-09-24 12:39:46 +02:00
#include <MyIncludes\RSI_Pro_Calculator.mqh>
2025-08-29 10:01:26 +02:00
//--- Input Parameters ---
2025-09-24 12:39:46 +02:00
input int InpPeriodRSI = 14;
input ENUM_APPLIED_PRICE_HA_ALL InpSourcePrice = PRICE_CLOSE_STD;
input group "Signal Line Settings"
input int InpPeriodMA = 14;
input ENUM_MA_METHOD InpMethodMA = MODE_SMA;
2025-08-29 10:01:26 +02:00
//--- Indicator Buffers ---
double BufferOscillator[];
2025-09-24 12:39:46 +02:00
//--- Global calculator object ---
CRSIProCalculator *g_calculator;
2025-08-29 10:01:26 +02:00
//+------------------------------------------------------------------+
//| Custom indicator initialization function. |
//+------------------------------------------------------------------+
int OnInit()
{
SetIndexBuffer(0, BufferOscillator, INDICATOR_DATA);
ArraySetAsSeries(BufferOscillator, false);
2025-09-24 12:39:46 +02:00
if(InpSourcePrice <= PRICE_HA_CLOSE)
2025-08-29 10:01:26 +02:00
{
2025-09-24 12:39:46 +02:00
g_calculator = new CRSIProCalculator_HA();
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("RSI Osc HA(%d,%d)", InpPeriodRSI, InpPeriodMA));
}
else
{
g_calculator = new CRSIProCalculator();
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("RSI Osc(%d,%d)", InpPeriodRSI, InpPeriodMA));
}
//--- We pass a dummy deviation value (0.0) as it's not used for the oscillator
if(CheckPointer(g_calculator) == POINTER_INVALID ||
!g_calculator.Init(InpPeriodRSI, InpPeriodMA, InpMethodMA, 0.0))
{
Print("Failed to initialize RSI Pro Calculator for Oscillator.");
2025-08-29 10:01:26 +02:00
return(INIT_FAILED);
}
2025-09-24 12:39:46 +02:00
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, InpPeriodRSI + InpPeriodMA - 1);
IndicatorSetInteger(INDICATOR_DIGITS, 2);
2025-08-29 10:01:26 +02:00
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function. |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
2025-09-24 12:39:46 +02:00
if(CheckPointer(g_calculator) != POINTER_INVALID)
delete g_calculator;
2025-08-29 10:01:26 +02:00
}
//+------------------------------------------------------------------+
2025-09-24 12:39:46 +02:00
//| Custom indicator iteration function. |
2025-08-29 10:01:26 +02:00
//+------------------------------------------------------------------+
2025-09-24 12:39:46 +02:00
int OnCalculate(const int rates_total, const int, const datetime&[], const double &open[], const double &high[], const double &low[], const double &close[], const long&[], const long&[], const int&[])
2025-08-29 10:01:26 +02:00
{
2025-09-24 12:39:46 +02:00
if(CheckPointer(g_calculator) == POINTER_INVALID)
return 0;
2025-08-29 10:01:26 +02:00
2025-09-24 12:39:46 +02:00
//--- Step 1: Use the Pro calculator to get the core RSI and MA values
double rsi_buffer[], ma_buffer[], dummy_upper[], dummy_lower[];
ArrayResize(rsi_buffer, rates_total);
ArrayResize(ma_buffer, rates_total);
ArrayResize(dummy_upper, rates_total);
ArrayResize(dummy_lower, rates_total);
2025-08-29 10:01:26 +02:00
2025-09-24 12:39:46 +02:00
ENUM_APPLIED_PRICE price_type;
if(InpSourcePrice <= PRICE_HA_CLOSE)
price_type = (ENUM_APPLIED_PRICE)(-(int)InpSourcePrice);
else
price_type = (ENUM_APPLIED_PRICE)InpSourcePrice;
2025-08-29 10:01:26 +02:00
2025-09-24 12:39:46 +02:00
g_calculator.Calculate(rates_total, price_type, open, high, low, close,
rsi_buffer, ma_buffer, dummy_upper, dummy_lower);
//--- Step 2: Calculate the final Oscillator value (RSI - MA)
int start_pos = InpPeriodRSI + InpPeriodMA - 1;
2025-08-29 10:01:26 +02:00
for(int i = start_pos; i < rates_total; i++)
{
2025-09-24 12:39:46 +02:00
BufferOscillator[i] = rsi_buffer[i] - ma_buffer[i];
2025-08-29 10:01:26 +02:00
}
return(rates_total);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+