mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-08-18 23:08:07 +00:00
refactor: harmonized input parameter names
This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
#property copyright "Copyright 2025, xxxxxxxx"
|
#property copyright "Copyright 2025, xxxxxxxx"
|
||||||
#property link ""
|
#property link ""
|
||||||
#property version "2.00" // Refactored for full recalculation and stability
|
#property version "2.01" // Harmonized input parameter names
|
||||||
#property description "Slow Stochastic Oscillator on Heikin Ashi data"
|
#property description "Slow Stochastic Oscillator on Heikin Ashi data"
|
||||||
|
|
||||||
#include <MyIncludes\HeikinAshi_Tools.mqh>
|
#include <MyIncludes\HeikinAshi_Tools.mqh>
|
||||||
@@ -34,24 +34,18 @@
|
|||||||
#property indicator_width2 1
|
#property indicator_width2 1
|
||||||
|
|
||||||
//--- Input Parameters ---
|
//--- Input Parameters ---
|
||||||
input int InpKPeriod = 5; // %K Period
|
input int InpKPeriod = 5;
|
||||||
input int InpDPeriod = 3; // %D Period (signal line smoothing)
|
input int InpSlowingPeriod = 3;
|
||||||
input int InpSlowing = 3; // Slowing (initial %K smoothing)
|
input int InpDPeriod = 3;
|
||||||
|
|
||||||
//--- Indicator Buffers ---
|
//--- Indicator Buffers ---
|
||||||
double BufferHA_K[]; // Plotted buffer for the main (Slow) %K line
|
double BufferHA_K[];
|
||||||
double BufferHA_D[]; // Plotted buffer for the signal %D line
|
double BufferHA_D[];
|
||||||
double BufferRawK[]; // Calculation buffer for raw %K before slowing
|
double BufferRawK[];
|
||||||
|
|
||||||
//--- Intermediate Heikin Ashi Buffers ---
|
|
||||||
double ExtHaOpenBuffer[];
|
|
||||||
double ExtHaHighBuffer[];
|
|
||||||
double ExtHaLowBuffer[];
|
|
||||||
double ExtHaCloseBuffer[];
|
|
||||||
|
|
||||||
//--- Global Objects and Variables ---
|
//--- Global Objects and Variables ---
|
||||||
int g_ExtKPeriod, g_ExtDPeriod, g_ExtSlowing;
|
int g_ExtKPeriod, g_ExtDPeriod, g_ExtSlowingPeriod;
|
||||||
CHeikinAshi_Calculator *g_ha_calculator; // Pointer to our Heikin Ashi calculator
|
CHeikinAshi_Calculator *g_ha_calculator;
|
||||||
|
|
||||||
//--- Forward declarations for helper functions ---
|
//--- Forward declarations for helper functions ---
|
||||||
double Highest(const double &array[], int period, int current_pos);
|
double Highest(const double &array[], int period, int current_pos);
|
||||||
@@ -62,12 +56,10 @@ double Lowest(const double &array[], int period, int current_pos);
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
int OnInit()
|
int OnInit()
|
||||||
{
|
{
|
||||||
//--- Validate and store input periods
|
g_ExtKPeriod = (InpKPeriod < 1) ? 1 : InpKPeriod;
|
||||||
g_ExtKPeriod = (InpKPeriod < 1) ? 1 : InpKPeriod;
|
g_ExtDPeriod = (InpDPeriod < 1) ? 1 : InpDPeriod;
|
||||||
g_ExtDPeriod = (InpDPeriod < 1) ? 1 : InpDPeriod;
|
g_ExtSlowingPeriod = (InpSlowingPeriod < 1) ? 1 : InpSlowingPeriod;
|
||||||
g_ExtSlowing = (InpSlowing < 1) ? 1 : InpSlowing;
|
|
||||||
|
|
||||||
//--- Map the buffers and set as non-timeseries
|
|
||||||
SetIndexBuffer(0, BufferHA_K, INDICATOR_DATA);
|
SetIndexBuffer(0, BufferHA_K, INDICATOR_DATA);
|
||||||
SetIndexBuffer(1, BufferHA_D, INDICATOR_DATA);
|
SetIndexBuffer(1, BufferHA_D, INDICATOR_DATA);
|
||||||
SetIndexBuffer(2, BufferRawK, INDICATOR_CALCULATIONS);
|
SetIndexBuffer(2, BufferRawK, INDICATOR_CALCULATIONS);
|
||||||
@@ -76,20 +68,17 @@ int OnInit()
|
|||||||
ArraySetAsSeries(BufferHA_D, false);
|
ArraySetAsSeries(BufferHA_D, false);
|
||||||
ArraySetAsSeries(BufferRawK, false);
|
ArraySetAsSeries(BufferRawK, false);
|
||||||
|
|
||||||
//--- Set indicator display properties
|
|
||||||
IndicatorSetInteger(INDICATOR_DIGITS, 2);
|
IndicatorSetInteger(INDICATOR_DIGITS, 2);
|
||||||
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, g_ExtKPeriod + g_ExtSlowing - 2);
|
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, g_ExtKPeriod + g_ExtSlowingPeriod - 2);
|
||||||
PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, g_ExtKPeriod + g_ExtSlowing + g_ExtDPeriod - 3);
|
PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, g_ExtKPeriod + g_ExtSlowingPeriod + g_ExtDPeriod - 3);
|
||||||
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("HA_Slow_Stoch(%d,%d,%d)", g_ExtKPeriod, g_ExtDPeriod, g_ExtSlowing));
|
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("HA_Slow_Stoch(%d,%d,%d)", g_ExtKPeriod, g_ExtSlowingPeriod, g_ExtDPeriod));
|
||||||
|
|
||||||
//--- Create the calculator instance
|
|
||||||
g_ha_calculator = new CHeikinAshi_Calculator();
|
g_ha_calculator = new CHeikinAshi_Calculator();
|
||||||
if(CheckPointer(g_ha_calculator) == POINTER_INVALID)
|
if(CheckPointer(g_ha_calculator) == POINTER_INVALID)
|
||||||
{
|
{
|
||||||
Print("Error creating CHeikinAshi_Calculator object");
|
Print("Error creating CHeikinAshi_Calculator object");
|
||||||
return(INIT_FAILED);
|
return(INIT_FAILED);
|
||||||
}
|
}
|
||||||
|
|
||||||
return(INIT_SUCCEEDED);
|
return(INIT_SUCCEEDED);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,7 +87,6 @@ int OnInit()
|
|||||||
//+------------------------------------------------------------------+
|
//+------------------------------------------------------------------+
|
||||||
void OnDeinit(const int reason)
|
void OnDeinit(const int reason)
|
||||||
{
|
{
|
||||||
//--- Free the calculator object
|
|
||||||
if(CheckPointer(g_ha_calculator) != POINTER_INVALID)
|
if(CheckPointer(g_ha_calculator) != POINTER_INVALID)
|
||||||
{
|
{
|
||||||
delete g_ha_calculator;
|
delete g_ha_calculator;
|
||||||
@@ -120,47 +108,46 @@ int OnCalculate(const int rates_total,
|
|||||||
const long &volume[],
|
const long &volume[],
|
||||||
const int &spread[])
|
const int &spread[])
|
||||||
{
|
{
|
||||||
//--- Check if there is enough historical data
|
int start_pos = g_ExtKPeriod + g_ExtSlowingPeriod + g_ExtDPeriod - 2;
|
||||||
if(rates_total < g_ExtKPeriod + g_ExtSlowing + g_ExtDPeriod - 2)
|
if(rates_total <= start_pos)
|
||||||
return(0);
|
return(0);
|
||||||
|
|
||||||
//--- Resize intermediate buffers
|
//--- Intermediate Heikin Ashi Buffers
|
||||||
ArrayResize(ExtHaOpenBuffer, rates_total);
|
double ha_open[], ha_high[], ha_low[], ha_close[];
|
||||||
ArrayResize(ExtHaHighBuffer, rates_total);
|
ArrayResize(ha_open, rates_total);
|
||||||
ArrayResize(ExtHaLowBuffer, rates_total);
|
ArrayResize(ha_high, rates_total);
|
||||||
ArrayResize(ExtHaCloseBuffer, rates_total);
|
ArrayResize(ha_low, rates_total);
|
||||||
|
ArrayResize(ha_close, rates_total);
|
||||||
|
|
||||||
//--- STEP 1: Calculate Heikin Ashi bars
|
//--- STEP 1: Calculate Heikin Ashi bars
|
||||||
g_ha_calculator.Calculate(rates_total, open, high, low, close,
|
g_ha_calculator.Calculate(rates_total, open, high, low, close, ha_open, ha_high, ha_low, ha_close);
|
||||||
ExtHaOpenBuffer, ExtHaHighBuffer, ExtHaLowBuffer, ExtHaCloseBuffer);
|
|
||||||
|
|
||||||
//--- STEP 2: Calculate Raw %K (Fast %K) using Heiken Ashi data
|
//--- STEP 2: Calculate Raw %K (Fast %K) using Heikin Ashi data
|
||||||
for(int i = g_ExtKPeriod - 1; i < rates_total; i++)
|
for(int i = g_ExtKPeriod - 1; i < rates_total; i++)
|
||||||
{
|
{
|
||||||
double highest_ha_high = Highest(ExtHaHighBuffer, g_ExtKPeriod, i);
|
double highest_ha_high = Highest(ha_high, g_ExtKPeriod, i);
|
||||||
double lowest_ha_low = Lowest(ExtHaLowBuffer, g_ExtKPeriod, i);
|
double lowest_ha_low = Lowest(ha_low, g_ExtKPeriod, i);
|
||||||
|
|
||||||
double range = highest_ha_high - lowest_ha_low;
|
double range = highest_ha_high - lowest_ha_low;
|
||||||
if(range > 0)
|
if(range > 0)
|
||||||
BufferRawK[i] = (ExtHaCloseBuffer[i] - lowest_ha_low) / range * 100.0;
|
BufferRawK[i] = (ha_close[i] - lowest_ha_low) / range * 100.0;
|
||||||
else
|
else
|
||||||
BufferRawK[i] = (i > 0) ? BufferRawK[i-1] : 50.0;
|
BufferRawK[i] = (i > 0) ? BufferRawK[i-1] : 50.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//--- STEP 3: Calculate Slow %K (Main Line) by smoothing Raw %K
|
//--- STEP 3: Calculate Slow %K (Main Line) by smoothing Raw %K
|
||||||
int k_slow_start_pos = g_ExtKPeriod + g_ExtSlowing - 2;
|
int k_slow_start_pos = g_ExtKPeriod + g_ExtSlowingPeriod - 2;
|
||||||
for(int i = k_slow_start_pos; i < rates_total; i++)
|
for(int i = k_slow_start_pos; i < rates_total; i++)
|
||||||
{
|
{
|
||||||
double sum = 0;
|
double sum = 0;
|
||||||
for(int j = 0; j < g_ExtSlowing; j++)
|
for(int j = 0; j < g_ExtSlowingPeriod; j++)
|
||||||
{
|
{
|
||||||
sum += BufferRawK[i-j];
|
sum += BufferRawK[i-j];
|
||||||
}
|
}
|
||||||
BufferHA_K[i] = sum / g_ExtSlowing;
|
BufferHA_K[i] = sum / g_ExtSlowingPeriod;
|
||||||
}
|
}
|
||||||
|
|
||||||
//--- STEP 4: Calculate %D (Signal Line) by smoothing Slow %K
|
//--- STEP 4: Calculate %D (Signal Line) by smoothing Slow %K
|
||||||
int d_start_pos = g_ExtKPeriod + g_ExtSlowing + g_ExtDPeriod - 3;
|
int d_start_pos = g_ExtKPeriod + g_ExtSlowingPeriod + g_ExtDPeriod - 3;
|
||||||
for(int i = d_start_pos; i < rates_total; i++)
|
for(int i = d_start_pos; i < rates_total; i++)
|
||||||
{
|
{
|
||||||
double sum = 0;
|
double sum = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user