mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-08 18:17:44 +00:00
refactor: add oscillators
This commit is contained in:
@@ -6,6 +6,8 @@ Cutler's RSI is a variation of the classic Relative Strength Index (RSI) develop
|
||||
|
||||
This modification results in an oscillator that can react slightly differently to price movements compared to the standard RSI. It is still a momentum oscillator used to identify overbought and oversold conditions, but its SMA-based calculation gives it a unique character.
|
||||
|
||||
The **Cutler's RSI Oscillator** is a supplementary indicator that displays the difference between the main RSI line and its signal line as a histogram, providing a clearer visual of accelerating and decelerating momentum.
|
||||
|
||||
## 2. Mathematical Foundations and Calculation Logic
|
||||
|
||||
The core difference between Cutler's RSI and the standard RSI lies in the smoothing method applied to the price changes.
|
||||
@@ -24,7 +26,6 @@ The core difference between Cutler's RSI and the standard RSI lies in the smooth
|
||||
|
||||
- If $\text{Change}_i > 0$, then $\text{Positive Change}_i = \text{Change}_i$ and $\text{Negative Change}_i = 0$.
|
||||
- If $\text{Change}_i < 0$, then $\text{Positive Change}_i = 0$ and $\text{Negative Change}_i = \text{Abs}(\text{Change}_i)$.
|
||||
- If $\text{Change}_i = 0$, then both are 0.
|
||||
|
||||
3. **Calculate the Simple Moving Average of Changes:** This is the defining step. Apply an SMA with period `N` to both the positive and negative change series.
|
||||
$\text{Avg Positive}_i = \text{SMA}(\text{Positive Change}, N)_i$
|
||||
@@ -34,31 +35,32 @@ The core difference between Cutler's RSI and the standard RSI lies in the smooth
|
||||
$\text{RS}_i = \frac{\text{Avg Positive}_i}{\text{Avg Negative}_i}$
|
||||
$\text{Cutler's RSI}_i = 100 - \frac{100}{1 + \text{RS}_i}$
|
||||
|
||||
5. **Calculate the Signal Line & Oscillator:** The signal line is a moving average of the Cutler's RSI line, and the oscillator is the difference between the two.
|
||||
|
||||
## 3. MQL5 Implementation Details
|
||||
|
||||
Our MQL5 implementation was refactored for maximum stability, clarity, and computational efficiency.
|
||||
Our MQL5 implementations were refactored for maximum stability, clarity, and computational efficiency.
|
||||
|
||||
- **Stability via Full Recalculation:** We employ a "brute-force" full recalculation within the `OnCalculate` function. This is our standard practice for all indicators, especially those with a signal line involving recursive calculations (EMA/SMMA), to ensure stability.
|
||||
- **Stability via Full Recalculation:** We employ a "brute-force" full recalculation within the `OnCalculate` function for maximum stability.
|
||||
|
||||
- **Efficient RSI Calculation:** Instead of using multiple loops or inefficient `SimpleMA` calls on every bar, we calculate the Cutler's RSI in a single `for` loop using an efficient **sliding window sum** technique. We maintain a running sum of positive and negative changes, adding the newest value and subtracting the oldest value in each iteration. This is mathematically equivalent to an SMA but significantly faster.
|
||||
- **Efficient RSI Calculation:** Instead of using multiple loops or inefficient `SimpleMA` calls on every bar, we calculate the Cutler's RSI in a single `for` loop using an efficient **sliding window sum** technique. This is mathematically equivalent to an SMA but significantly faster.
|
||||
|
||||
- **Self-Contained Price Handling:** The indicator does not use external handles like `iMA`. It directly processes the price arrays (`open`, `high`, `low`, `close`) provided by `OnCalculate` and includes logic to handle all standard and calculated `ENUM_APPLIED_PRICE` types.
|
||||
- **Self-Contained Logic:** The indicators are completely self-contained. They do not use external handles and directly process the price arrays provided by `OnCalculate`.
|
||||
|
||||
- **Robust Signal Line:** The optional signal line (a moving average of the Cutler's RSI line) is calculated using our standard, robust `switch` block.
|
||||
- **Fully Manual MA Calculations:** To guarantee 100% accuracy and consistency, all moving average calculations for the signal line (**SMA, EMA, SMMA, LWMA**) are performed **manually**. This makes the indicators independent of the `<MovingAverages.mqh>` library and ensures robust behavior on `non-timeseries` arrays.
|
||||
|
||||
- For recursive types (**EMA, SMMA**), the first value is initialized with a **manual SMA** calculation at the correct starting index to prevent the floating-point overflows that plague less robust implementations.
|
||||
- For non-recursive types (**SMA, LWMA**), we safely use the functions from the `<MovingAverages.mqh>` library.
|
||||
|
||||
- **Heikin Ashi Variant (`CutlerRSI_MA_HeikinAshi.mq5`):**
|
||||
- Our toolkit also includes a Heikin Ashi version of this indicator. The calculation logic is identical, but it uses the smoothed Heikin Ashi `ha_close` values as its input.
|
||||
- This results in a doubly-smoothed oscillator, ideal for traders who want to focus only on the most significant, sustained momentum shifts.
|
||||
- **Indicator Family:**
|
||||
- **Line Versions:** `CutlerRSI_MA.mq5` plots the RSI line and its signal line.
|
||||
- **Oscillator Versions:** `CutlerRSI_Oscillator.mq5` plots the difference between the two lines as a histogram.
|
||||
- **Heikin Ashi Variants:** Both indicators have "pure" Heikin Ashi counterparts, which use the smoothed Heikin Ashi `ha_close` values as their input.
|
||||
|
||||
## 4. Parameters
|
||||
|
||||
- **RSI Period (`InpPeriodRSI`):** The lookback period for the SMA of price changes. Default is `14`.
|
||||
- **Applied Price (`InpAppliedPrice`):** The source price used for the calculation (e.g., `PRICE_CLOSE`).
|
||||
- **MA Period (`InpPeriodMA`):** The lookback period for the optional signal line.
|
||||
- **MA Method (`InpMethodMA`):** The type of moving average for the signal line.
|
||||
- **Applied Price (`InpAppliedPrice`):** The source price for the calculation. Default is `PRICE_CLOSE`.
|
||||
- **Signal Line Settings:**
|
||||
- `InpPeriodMA`: The lookback period for the optional signal line.
|
||||
- `InpMethodMA`: The type of moving average for the signal line.
|
||||
|
||||
## 5. Usage and Interpretation
|
||||
|
||||
@@ -68,5 +70,5 @@ The interpretation of Cutler's RSI is identical to the standard RSI.
|
||||
- **Crossovers:**
|
||||
- **Signal Line Crossover:** When the Cutler's RSI line crosses above its moving average, it can be seen as a bullish signal. A cross below is a bearish signal.
|
||||
- **Centerline Crossover:** A crossover of the RSI line above the 50 level indicates that momentum is shifting to bullish. A crossover below 50 indicates bearish momentum.
|
||||
- **Divergence:** Look for divergences between the RSI and the price action. A bearish divergence (higher price highs, lower RSI highs) can signal a potential top, while a bullish divergence (lower price lows, higher RSI highs) can signal a potential bottom.
|
||||
- **Caution:** As a momentum oscillator, it is most effective in ranging or moderately trending markets. In a very strong trend, it can remain in overbought or oversold territory for extended periods.
|
||||
- **Divergence:** Look for divergences between the RSI and the price action.
|
||||
- **Oscillator (Histogram):** The histogram provides a clear visual of the relationship between the Cutler's RSI and its signal line, highlighting the acceleration and deceleration of momentum.
|
||||
|
||||
Reference in New Issue
Block a user