new files added

This commit is contained in:
Toh4iem9
2025-08-19 09:47:42 +02:00
parent f4ec27d951
commit 181f22a00f
+268
View File
@@ -0,0 +1,268 @@
//+------------------------------------------------------------------+
//| SMI_HeikinAshi.mq5 |
//| Copyright 2025, xxxxxxxx |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, xxxxxxxx"
#property link ""
#property version "2.00" // Refactored for full recalculation and stability
#property description "Stochastic Momentum Index (SMI) on Heikin Ashi data"
// --- Custom Toolkit Include ---
#include <MyIncludes\HeikinAshi_Tools.mqh>
//--- Indicator Window and Level Properties ---
#property indicator_separate_window
#property indicator_level1 40.0
#property indicator_level2 0.0
#property indicator_level3 -40.0
#property indicator_levelstyle STYLE_DOT
//--- Buffers and Plots ---
#property indicator_buffers 8 // SMI, Signal, and 6 calculation buffers
#property indicator_plots 2
//--- Plot 1: SMI line
#property indicator_label1 "HA_SMI"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrBlue
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- Plot 2: Signal line (EMA of SMI)
#property indicator_label2 "HA_Signal"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrOrange
#property indicator_style2 STYLE_DOT
#property indicator_width2 1
//--- Input Parameters ---
input int InpLengthK = 10; // %K Length
input int InpLengthD = 3; // %D Length (for double smoothing)
input int InpLengthEMA = 3; // EMA Length (for signal line)
//--- Indicator Buffers ---
double BufferSMI[];
double BufferSignal[];
double BufferHighestLowestRange[];
double BufferRelativeRange[];
double BufferEma_Relative[];
double BufferEma_Range[];
double BufferEmaEma_Relative[];
double BufferEmaEma_Range[];
//--- Intermediate Heikin Ashi Buffers ---
double ExtHaOpenBuffer[];
double ExtHaHighBuffer[];
double ExtHaLowBuffer[];
double ExtHaCloseBuffer[];
//--- Global Objects and Variables ---
int g_ExtLengthK, g_ExtLengthD, g_ExtLengthEMA;
CHeikinAshi_Calculator *g_ha_calculator; // Pointer to our Heikin 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);
//+------------------------------------------------------------------+
//| Custom indicator initialization function. |
//+------------------------------------------------------------------+
int OnInit()
{
//--- Validate and store inputs
g_ExtLengthK = (InpLengthK < 1) ? 1 : InpLengthK;
g_ExtLengthD = (InpLengthD < 1) ? 1 : InpLengthD;
g_ExtLengthEMA = (InpLengthEMA < 1) ? 1 : InpLengthEMA;
//--- Map the buffers
SetIndexBuffer(0, BufferSMI, INDICATOR_DATA);
SetIndexBuffer(1, BufferSignal, INDICATOR_DATA);
SetIndexBuffer(2, BufferHighestLowestRange, INDICATOR_CALCULATIONS);
SetIndexBuffer(3, BufferRelativeRange, INDICATOR_CALCULATIONS);
SetIndexBuffer(4, BufferEma_Relative, INDICATOR_CALCULATIONS);
SetIndexBuffer(5, BufferEma_Range, INDICATOR_CALCULATIONS);
SetIndexBuffer(6, BufferEmaEma_Relative, INDICATOR_CALCULATIONS);
SetIndexBuffer(7, BufferEmaEma_Range, INDICATOR_CALCULATIONS);
//--- Set all buffers to non-timeseries
ArraySetAsSeries(BufferSMI, false);
ArraySetAsSeries(BufferSignal, false);
ArraySetAsSeries(BufferHighestLowestRange, false);
ArraySetAsSeries(BufferRelativeRange, false);
ArraySetAsSeries(BufferEma_Relative, false);
ArraySetAsSeries(BufferEma_Range, false);
ArraySetAsSeries(BufferEmaEma_Relative, false);
ArraySetAsSeries(BufferEmaEma_Range, false);
//--- Set indicator properties
IndicatorSetInteger(INDICATOR_DIGITS, 2);
int smi_draw_begin = g_ExtLengthK + g_ExtLengthD - 2;
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, smi_draw_begin);
PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, smi_draw_begin + g_ExtLengthEMA - 1);
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("HA_SMI(%d,%d,%d)", g_ExtLengthK, g_ExtLengthD, g_ExtLengthEMA));
//--- Create the calculator instance
g_ha_calculator = new CHeikinAshi_Calculator();
if(CheckPointer(g_ha_calculator) == POINTER_INVALID)
{
Print("Error creating CHeikinAshi_Calculator object");
return(INIT_FAILED);
}
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function. |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- Free the calculator object
if(CheckPointer(g_ha_calculator) != POINTER_INVALID)
{
delete g_ha_calculator;
g_ha_calculator = NULL;
}
}
//+------------------------------------------------------------------+
//| Stochastic Momentum Index calculation function. |
//+------------------------------------------------------------------+
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[])
{
int start_pos = g_ExtLengthK + g_ExtLengthD + g_ExtLengthEMA - 2;
if(rates_total <= start_pos)
return(0);
//--- Resize intermediate buffers
ArrayResize(ExtHaOpenBuffer, rates_total);
ArrayResize(ExtHaHighBuffer, rates_total);
ArrayResize(ExtHaLowBuffer, rates_total);
ArrayResize(ExtHaCloseBuffer, rates_total);
//--- STEP 1: Calculate Heikin Ashi bars
g_ha_calculator.Calculate(rates_total, open, high, low, close,
ExtHaOpenBuffer, ExtHaHighBuffer, ExtHaLowBuffer, ExtHaCloseBuffer);
//--- STEP 2: Calculate Highest, Lowest, and Ranges
for(int i = g_ExtLengthK - 1; i < rates_total; i++)
{
double highest_high = Highest(ExtHaHighBuffer, g_ExtLengthK, i);
double lowest_low = Lowest(ExtHaLowBuffer, g_ExtLengthK, i);
BufferHighestLowestRange[i] = highest_high - lowest_low;
BufferRelativeRange[i] = ExtHaCloseBuffer[i] - (highest_high + lowest_low) / 2.0;
}
//--- STEP 3-7: Calculate all smoothed values and final SMI in a single loop
double pr_d = 2.0 / (g_ExtLengthD + 1.0);
double pr_ema = 2.0 / (g_ExtLengthEMA + 1.0);
int ema1_start = g_ExtLengthK + g_ExtLengthD - 2;
int ema2_start = ema1_start + g_ExtLengthD - 1;
int signal_start = ema2_start + g_ExtLengthEMA - 1;
for(int i = g_ExtLengthK - 1; i < rates_total; i++)
{
// --- 1st EMA Smoothing ---
if(i == g_ExtLengthK - 1) // Initialization
{
BufferEma_Relative[i] = BufferRelativeRange[i];
BufferEma_Range[i] = BufferHighestLowestRange[i];
}
else // Recursive
{
BufferEma_Relative[i] = BufferRelativeRange[i] * pr_d + BufferEma_Relative[i-1] * (1.0 - pr_d);
BufferEma_Range[i] = BufferHighestLowestRange[i] * pr_d + BufferEma_Range[i-1] * (1.0 - pr_d);
}
// --- 2nd EMA Smoothing ---
if(i == ema1_start) // Initialization with manual SMA
{
double sum_rel=0, sum_ran=0;
for(int j=0; j<g_ExtLengthD; j++)
{
sum_rel += BufferEma_Relative[i-j];
sum_ran += BufferEma_Range[i-j];
}
BufferEmaEma_Relative[i] = sum_rel / g_ExtLengthD;
BufferEmaEma_Range[i] = sum_ran / g_ExtLengthD;
}
else
if(i > ema1_start) // Recursive
{
BufferEmaEma_Relative[i] = BufferEma_Relative[i] * pr_d + BufferEmaEma_Relative[i-1] * (1.0 - pr_d);
BufferEmaEma_Range[i] = BufferEma_Range[i] * pr_d + BufferEmaEma_Range[i-1] * (1.0 - pr_d);
}
// --- Final SMI Value ---
if(i >= ema1_start)
{
if(BufferEmaEma_Range[i] != 0)
BufferSMI[i] = 100 * (BufferEmaEma_Relative[i] / (BufferEmaEma_Range[i] / 2.0));
else
BufferSMI[i] = 0;
}
// --- Signal Line ---
if(i == signal_start) // Initialization with manual SMA
{
double sum_smi=0;
for(int j=0; j<g_ExtLengthEMA; j++)
sum_smi += BufferSMI[i-j];
BufferSignal[i] = sum_smi / g_ExtLengthEMA;
}
else
if(i > signal_start) // Recursive
{
BufferSignal[i] = BufferSMI[i] * pr_ema + BufferSignal[i-1] * (1.0 - pr_ema);
}
}
return(rates_total);
}
//+------------------------------------------------------------------+
//| Finds the highest value in a given period of an array. |
//+------------------------------------------------------------------+
double Highest(const double &array[], int period, int current_pos)
{
double res = array[current_pos];
for(int i = 1; i < period; i++)
{
int index = current_pos - i;
if(index < 0)
break;
if(res < array[index])
res = array[index];
}
return(res);
}
//+------------------------------------------------------------------+
//| Finds the lowest value in a given period of an array. |
//+------------------------------------------------------------------+
double Lowest(const double &array[], int period, int current_pos)
{
double res = array[current_pos];
for(int i = 1; i < period; i++)
{
int index = current_pos - i;
if(index < 0)
break;
if(res > array[index])
res = array[index];
}
return(res);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+