Files
mql5/Indicators/MyIndicators/WPR_HeikenAshi.mq5
T

165 lines
13 KiB
Plaintext
Raw Normal View History

2025-08-11 12:06:31 +02:00
//+------------------------------------------------------------------+
//| WPR_HeikenAshi.mq5 |
2025-08-12 11:03:00 +02:00
//| Copyright 2025, xxxxxxxx (Based on MetaQuotes WPR) |
2025-08-11 12:06:31 +02:00
//| |
//+------------------------------------------------------------------+
2025-08-12 11:03:00 +02:00
#property copyright "Copyright 2025, xxxxxxxx"
2025-08-11 12:06:31 +02:00
#property link ""
2025-08-12 11:03:00 +02:00
#property version "2.00" // Refactored to use HA_Tools.mqh
2025-08-11 12:06:31 +02:00
#property description "Larry Williams' Percent Range based on Heiken Ashi candles"
2025-08-12 11:03:00 +02:00
//--- Custom Toolkit Include ---
#include <MyIncludes\HA_Tools.mqh>
//--- Indicator Window and Level Properties ---
2025-08-11 12:06:31 +02:00
#property indicator_separate_window
#property indicator_level1 -20.0
#property indicator_level2 -80.0
#property indicator_levelstyle STYLE_DOT
#property indicator_levelcolor clrSilver
#property indicator_levelwidth 1
#property indicator_maximum 0.0
#property indicator_minimum -100.0
2025-08-12 11:03:00 +02:00
//--- Buffers and Plots ---
#property indicator_buffers 1 // Only one buffer is needed for the WPR line
2025-08-11 12:06:31 +02:00
#property indicator_plots 1
//--- Plot 1: WPR line
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
2025-08-12 11:03:00 +02:00
#property indicator_label1 "HA_WPR"
2025-08-11 12:06:31 +02:00
2025-08-12 11:03:00 +02:00
//--- Input Parameters ---
input int InpWPRPeriod=14; // Period for WPR calculation
2025-08-11 12:06:31 +02:00
2025-08-12 11:03:00 +02:00
//--- Indicator Buffers ---
double BufferHA_WPR[]; // The only buffer, for the final WPR values
2025-08-11 12:06:31 +02:00
2025-08-12 11:03:00 +02:00
//--- Global Objects and Variables ---
int ExtPeriodWPR;
CHA_Calculator g_ha_calculator; // Global instance of our Heiken Ashi calculator
//--- Forward declarations for helper functions ---
double Highest(const double &array[], int period, int current_pos);
double Lowest(const double &array[], int period, int current_pos);
2025-08-11 12:06:31 +02:00
//+------------------------------------------------------------------+
2025-08-12 11:03:00 +02:00
//| Custom indicator initialization function. |
//| Called once when the indicator is first loaded. |
2025-08-11 12:06:31 +02:00
//+------------------------------------------------------------------+
void OnInit()
{
2025-08-12 11:03:00 +02:00
//--- Validate and store the WPR period
2025-08-11 12:06:31 +02:00
ExtPeriodWPR = (InpWPRPeriod < 1) ? 1 : InpWPRPeriod;
2025-08-12 11:03:00 +02:00
//--- Map the buffer to the indicator's internal memory
SetIndexBuffer(0, BufferHA_WPR, INDICATOR_DATA);
2025-08-11 12:06:31 +02:00
2025-08-12 11:03:00 +02:00
//--- Set indicator properties
2025-08-11 12:06:31 +02:00
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, ExtPeriodWPR - 1);
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("HA_WPR(%d)", ExtPeriodWPR));
IndicatorSetInteger(INDICATOR_DIGITS, 2);
}
//+------------------------------------------------------------------+
2025-08-12 11:03:00 +02:00
//| Williams Percent Range on Heiken Ashi. |
//| Called on every new tick or new bar. |
2025-08-11 12:06:31 +02:00
//+------------------------------------------------------------------+
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[])
{
2025-08-12 11:03:00 +02:00
//--- Check if there is enough historical data
2025-08-11 12:06:31 +02:00
if(rates_total < ExtPeriodWPR)
return(0);
2025-08-12 11:03:00 +02:00
//--- STEP 1: Calculate Heiken Ashi bars using our toolkit
if(!g_ha_calculator.Calculate(rates_total, prev_calculated, open, high, low, close))
2025-08-11 12:06:31 +02:00
{
2025-08-12 11:03:00 +02:00
Print("Heiken Ashi calculation failed.");
return(0);
2025-08-11 12:06:31 +02:00
}
2025-08-12 11:03:00 +02:00
//--- STEP 2: Calculate WPR based on the results from the HA calculator
2025-08-11 12:06:31 +02:00
int start_pos;
if(prev_calculated > 0)
2025-08-12 11:03:00 +02:00
start_pos = prev_calculated - 1; // Optimization for subsequent calls
2025-08-11 12:06:31 +02:00
else
2025-08-12 11:03:00 +02:00
start_pos = ExtPeriodWPR - 1; // Start from the first bar with enough data
2025-08-11 12:06:31 +02:00
2025-08-12 11:03:00 +02:00
//--- Main calculation loop
2025-08-11 12:06:31 +02:00
for(int i = start_pos; i < rates_total && !IsStopped(); i++)
{
// Find the highest HA_High and lowest HA_Low over the WPR period
2025-08-12 11:03:00 +02:00
// using the calculator's public buffers
double max_ha_high = Highest(g_ha_calculator.ha_high, ExtPeriodWPR, i);
double min_ha_low = Lowest(g_ha_calculator.ha_low, ExtPeriodWPR, i);
2025-08-11 12:06:31 +02:00
2025-08-12 11:03:00 +02:00
// Calculate WPR using the current HA_Close from the calculator
2025-08-11 12:06:31 +02:00
if(max_ha_high != min_ha_low)
2025-08-12 11:03:00 +02:00
BufferHA_WPR[i] = - (max_ha_high - g_ha_calculator.ha_close[i]) * 100.0 / (max_ha_high - min_ha_low);
2025-08-11 12:06:31 +02:00
else
// If max high equals min low, avoid division by zero
BufferHA_WPR[i] = (i > 0) ? BufferHA_WPR[i-1] : -50.0;
}
2025-08-12 11:03:00 +02:00
//--- Return value of prev_calculated for the next call
2025-08-11 12:06:31 +02:00
return(rates_total);
}
//+------------------------------------------------------------------+
2025-08-12 11:03:00 +02:00
//| Finds the highest value in a given period of an array. |
//| INPUT: array[] - The data array to search in. |
//| period - The number of elements to look back. |
//| current_pos - The starting position (index) to search from.|
//| RETURN: The highest value found in the specified range. |
2025-08-11 12:06:31 +02:00
//+------------------------------------------------------------------+
double Highest(const double &array[], int period, int current_pos)
{
double res = array[current_pos];
2025-08-12 11:03:00 +02:00
//--- Loop backwards from the current position for 'period' bars
2025-08-11 12:06:31 +02:00
for(int i = 1; i < period; i++)
{
int index = current_pos - i;
if(index < 0)
break; // Stop if we go out of bounds
if(res < array[index])
res = array[index];
}
return(res);
}
//+------------------------------------------------------------------+
2025-08-12 11:03:00 +02:00
//| Finds the lowest value in a given period of an array. |
//| INPUT: array[] - The data array to search in. |
//| period - The number of elements to look back. |
//| current_pos - The starting position (index) to search from.|
//| RETURN: The lowest value found in the specified range. |
2025-08-11 12:06:31 +02:00
//+------------------------------------------------------------------+
double Lowest(const double &array[], int period, int current_pos)
{
double res = array[current_pos];
2025-08-12 11:03:00 +02:00
//--- Loop backwards from the current position for 'period' bars
2025-08-11 12:06:31 +02:00
for(int i = 1; i < period; i++)
{
int index = current_pos - i;
if(index < 0)
break; // Stop if we go out of bounds
if(res > array[index])
res = array[index];
}
return(res);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+