mirror of
https://github.com/softwaredevelop/mql5.git
synced 2026-07-27 20:47:44 +00:00
new files added
This commit is contained in:
@@ -0,0 +1,230 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| KeltnerChannel_HeikinAshi_Pure.mq5|
|
||||
//| Copyright 2025, xxxxxxxx |
|
||||
//| |
|
||||
//+------------------------------------------------------------------+
|
||||
#property copyright "Copyright 2025, xxxxxxxx"
|
||||
#property link ""
|
||||
#property version "3.01" // Corrected include and completed switch-case
|
||||
#property description "Keltner Channels based entirely on Heikin Ashi data (including ATR)"
|
||||
|
||||
#include <MyIncludes\HeikinAshi_Tools.mqh>
|
||||
#include <MovingAverages.mqh> // <-- FIX: Corrected filename
|
||||
|
||||
//--- Indicator Window and Plot Properties ---
|
||||
#property indicator_chart_window
|
||||
#property indicator_buffers 4 // Upper, Lower, Middle, and Smoothed ATR
|
||||
#property indicator_plots 3
|
||||
|
||||
//--- Plot 1: Upper Band
|
||||
#property indicator_label1 "HA_Upper"
|
||||
#property indicator_type1 DRAW_LINE
|
||||
#property indicator_color1 clrDodgerBlue
|
||||
#property indicator_style1 STYLE_DOT
|
||||
|
||||
//--- Plot 2: Lower Band
|
||||
#property indicator_label2 "HA_Lower"
|
||||
#property indicator_type2 DRAW_LINE
|
||||
#property indicator_color2 clrDodgerBlue
|
||||
#property indicator_style2 STYLE_DOT
|
||||
|
||||
//--- Plot 3: Middle Band (Basis)
|
||||
#property indicator_label3 "HA_Basis"
|
||||
#property indicator_type3 DRAW_LINE
|
||||
#property indicator_color3 clrDodgerBlue
|
||||
#property indicator_style3 STYLE_SOLID
|
||||
#property indicator_width3 1
|
||||
|
||||
//--- Enum for selecting Heikin Ashi price source for the middle line ---
|
||||
enum ENUM_HA_APPLIED_PRICE
|
||||
{
|
||||
HA_PRICE_CLOSE, // Heikin Ashi Close
|
||||
HA_PRICE_OPEN, // Heikin Ashi Open
|
||||
HA_PRICE_HIGH, // Heikin Ashi High
|
||||
HA_PRICE_LOW, // Heikin Ashi Low
|
||||
};
|
||||
|
||||
//--- Input Parameters ---
|
||||
input int InpMaPeriod = 20;
|
||||
input ENUM_MA_METHOD InpMaMethod = MODE_EMA;
|
||||
input ENUM_HA_APPLIED_PRICE InpAppliedPrice = HA_PRICE_CLOSE; // HA price for the middle line
|
||||
input int InpAtrPeriod = 10;
|
||||
input double InpMultiplier = 2.0;
|
||||
|
||||
//--- Indicator Buffers ---
|
||||
double BufferUpper[];
|
||||
double BufferLower[];
|
||||
double BufferMiddle[];
|
||||
double BufferHA_ATR[]; // Buffer for Heikin Ashi ATR
|
||||
|
||||
//--- Intermediate Heikin Ashi Buffers ---
|
||||
double ExtHaOpenBuffer[];
|
||||
double ExtHaHighBuffer[];
|
||||
double ExtHaLowBuffer[];
|
||||
double ExtHaCloseBuffer[];
|
||||
|
||||
//--- Global Objects and Variables ---
|
||||
int g_ExtMaPeriod, g_ExtAtrPeriod;
|
||||
double g_ExtMultiplier;
|
||||
CHeikinAshi_Calculator *g_ha_calculator; // Pointer to our Heikin Ashi calculator
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| Custom indicator initialization function. |
|
||||
//+------------------------------------------------------------------+
|
||||
int OnInit()
|
||||
{
|
||||
g_ExtMaPeriod = (InpMaPeriod < 1) ? 1 : InpMaPeriod;
|
||||
g_ExtAtrPeriod = (InpAtrPeriod < 1) ? 1 : InpAtrPeriod;
|
||||
g_ExtMultiplier = (InpMultiplier <= 0) ? 2.0 : InpMultiplier;
|
||||
|
||||
SetIndexBuffer(0, BufferUpper, INDICATOR_DATA);
|
||||
SetIndexBuffer(1, BufferLower, INDICATOR_DATA);
|
||||
SetIndexBuffer(2, BufferMiddle, INDICATOR_DATA);
|
||||
SetIndexBuffer(3, BufferHA_ATR, INDICATOR_CALCULATIONS);
|
||||
|
||||
ArraySetAsSeries(BufferUpper, false);
|
||||
ArraySetAsSeries(BufferLower, false);
|
||||
ArraySetAsSeries(BufferMiddle, false);
|
||||
ArraySetAsSeries(BufferHA_ATR, false);
|
||||
|
||||
IndicatorSetInteger(INDICATOR_DIGITS, _Digits);
|
||||
int draw_begin = MathMax(g_ExtMaPeriod, g_ExtAtrPeriod);
|
||||
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, draw_begin);
|
||||
PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, draw_begin);
|
||||
PlotIndexSetInteger(2, PLOT_DRAW_BEGIN, g_ExtMaPeriod - 1);
|
||||
IndicatorSetString(INDICATOR_SHORTNAME, StringFormat("HA_KC_Pure(%d,%d,%.1f)", g_ExtMaPeriod, g_ExtAtrPeriod, g_ExtMultiplier));
|
||||
|
||||
//--- 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;
|
||||
}
|
||||
}
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| Keltner Channel on Heikin Ashi 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 = MathMax(g_ExtMaPeriod, g_ExtAtrPeriod);
|
||||
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 Heikin Ashi True Range
|
||||
double ha_tr[];
|
||||
ArrayResize(ha_tr, rates_total);
|
||||
for(int i = 1; i < rates_total; i++)
|
||||
{
|
||||
ha_tr[i] = MathMax(ExtHaHighBuffer[i], ExtHaCloseBuffer[i-1]) - MathMin(ExtHaLowBuffer[i], ExtHaCloseBuffer[i-1]);
|
||||
}
|
||||
|
||||
//--- STEP 3: Select the source Heikin Ashi price array for the middle line
|
||||
double ha_price_source[];
|
||||
switch(InpAppliedPrice)
|
||||
{
|
||||
case HA_PRICE_OPEN:
|
||||
ArrayCopy(ha_price_source, ExtHaOpenBuffer);
|
||||
break;
|
||||
case HA_PRICE_HIGH:
|
||||
ArrayCopy(ha_price_source, ExtHaHighBuffer);
|
||||
break;
|
||||
case HA_PRICE_LOW:
|
||||
ArrayCopy(ha_price_source, ExtHaLowBuffer);
|
||||
break;
|
||||
default:
|
||||
ArrayCopy(ha_price_source, ExtHaCloseBuffer);
|
||||
break;
|
||||
}
|
||||
|
||||
//--- STEP 4: Calculate HA_ATR, Middle, Upper, and Lower bands in a single loop
|
||||
for(int i = 1; i < rates_total; i++)
|
||||
{
|
||||
// --- Calculate Heikin Ashi ATR (using Wilder's smoothing) ---
|
||||
if(i == g_ExtAtrPeriod) // Initialization with SMA
|
||||
{
|
||||
BufferHA_ATR[i] = SimpleMA(i, g_ExtAtrPeriod, ha_tr);
|
||||
}
|
||||
else
|
||||
if(i > g_ExtAtrPeriod) // Recursive calculation
|
||||
{
|
||||
BufferHA_ATR[i] = (BufferHA_ATR[i-1] * (g_ExtAtrPeriod - 1) + ha_tr[i]) / g_ExtAtrPeriod;
|
||||
}
|
||||
|
||||
// --- Calculate the middle line (MA on HA price) ---
|
||||
if(i >= g_ExtMaPeriod - 1)
|
||||
{
|
||||
switch(InpMaMethod)
|
||||
{
|
||||
case MODE_EMA:
|
||||
if(i == g_ExtMaPeriod - 1)
|
||||
BufferMiddle[i] = SimpleMA(i, g_ExtMaPeriod, ha_price_source);
|
||||
else
|
||||
{
|
||||
double pr = 2.0 / (g_ExtMaPeriod + 1.0);
|
||||
BufferMiddle[i] = ha_price_source[i] * pr + BufferMiddle[i-1] * (1.0 - pr);
|
||||
}
|
||||
break;
|
||||
// --- FIX: Added missing cases ---
|
||||
case MODE_SMMA:
|
||||
if(i == g_ExtMaPeriod - 1)
|
||||
BufferMiddle[i] = SimpleMA(i, g_ExtMaPeriod, ha_price_source);
|
||||
else
|
||||
BufferMiddle[i] = (BufferMiddle[i-1] * (g_ExtMaPeriod - 1) + ha_price_source[i]) / g_ExtMaPeriod;
|
||||
break;
|
||||
case MODE_LWMA:
|
||||
BufferMiddle[i] = LinearWeightedMA(i, g_ExtMaPeriod, ha_price_source);
|
||||
break;
|
||||
default: // MODE_SMA
|
||||
BufferMiddle[i] = SimpleMA(i, g_ExtMaPeriod, ha_price_source);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// --- Calculate Upper and Lower bands ---
|
||||
if(i >= start_pos)
|
||||
{
|
||||
BufferUpper[i] = BufferMiddle[i] + (BufferHA_ATR[i] * g_ExtMultiplier);
|
||||
BufferLower[i] = BufferMiddle[i] - (BufferHA_ATR[i] * g_ExtMultiplier);
|
||||
}
|
||||
}
|
||||
|
||||
return(rates_total);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//+------------------------------------------------------------------+
|
||||
Reference in New Issue
Block a user