Skip to content

Commit

Permalink
Refs #15. Looks like Heiken_Ashi_Smoothed indicator works correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
nseam committed Dec 15, 2023
1 parent fa62b9f commit 5968581
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 24 deletions.
42 changes: 29 additions & 13 deletions Price/Multi/OHLC/Heiken_Ashi_Smoothed.mq4
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@
#property copyright "Copyright © 2006, Forex-TSD.com "
#property link "http://www.forex-tsd.com/"

#property indicator_chart_window
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_type1 DRAW_HISTOGRAM
#property indicator_color1 LightSalmon
#property indicator_width1 1
#property indicator_type2 DRAW_HISTOGRAM
#property indicator_color2 Lime
#property indicator_width2 1
#property indicator_type3 DRAW_HISTOGRAM
#property indicator_color3 LightSalmon
#property indicator_width3 5
#property indicator_type4 DRAW_HISTOGRAM
#property indicator_color4 Lime
#property indicator_width4 5

//---- parameters
extern int MaMetod = 2;
Expand Down Expand Up @@ -77,24 +81,35 @@ int deinit() {
int start() {
double maOpen, maClose, maLow, maHigh;
double haOpen, haHigh, haLow, haClose;

if (Bars <= 10)
return (0);

ExtCountedBars = IndicatorCounted();

//---- check for possible errors
if (ExtCountedBars < 0)
return (-1);

//---- last counted bar will be recounted
if (ExtCountedBars > 0)
ExtCountedBars--;

int pos = Bars - ExtCountedBars - 1;
int pos2 = pos;

while (pos >= 0) {
maOpen = iMA(NULL, 0, MaPeriod, 0, MaMetod, PRICE_OPEN, pos);
maClose = iMA(NULL, 0, MaPeriod, 0, MaMetod, PRICE_CLOSE, pos);
maLow = iMA(NULL, 0, MaPeriod, 0, MaMetod, PRICE_LOW, pos);
maHigh = iMA(NULL, 0, MaPeriod, 0, MaMetod, PRICE_HIGH, pos);

haOpen = (ExtMapBuffer5[pos + 1] + ExtMapBuffer6[pos + 1]) / 2;
if ((pos + 1) >= Bars) {
haOpen = maOpen;
} else {
haOpen = (ExtMapBuffer5[pos + 1] + ExtMapBuffer6[pos + 1]) / 2;
}

haClose = (maOpen + maHigh + maLow + maClose) / 4;
haHigh = MathMax(maHigh, MathMax(haOpen, haClose));
haLow = MathMin(maLow, MathMin(haOpen, haClose));
Expand All @@ -113,17 +128,18 @@ int start() {
}

int i;
for (i = 0; i < pos2; i++)
ExtMapBuffer1[i] = iMAOnArray(ExtMapBuffer7, 0, MaPeriod2, 0, MaMetod2, i);
for (i = 0; i < pos2; i++)
ExtMapBuffer2[i] = iMAOnArray(ExtMapBuffer8, 0, MaPeriod2, 0, MaMetod2, i);
for (i = 0; i < pos2; i++)
ExtMapBuffer3[i] = iMAOnArray(ExtMapBuffer5, 0, MaPeriod2, 0, MaMetod2, i);
for (i = 0; i < pos2; i++)
ExtMapBuffer4[i] = iMAOnArray(ExtMapBuffer6, 0, MaPeriod2, 0, MaMetod2, i);

Alert(ExtMapBuffer1[i], ", ", ExtMapBuffer2[i], ", ", ExtMapBuffer3[i], ", ",
ExtMapBuffer4[i]);
for (i = 0; i <= pos2; i++)
ExtMapBuffer1[i] =
iMAOnArray(ExtMapBuffer7, Bars, MaPeriod2, 0, MaMetod2, i);
for (i = 0; i <= pos2; i++)
ExtMapBuffer2[i] =
iMAOnArray(ExtMapBuffer8, Bars, MaPeriod2, 0, MaMetod2, i);
for (i = 0; i <= pos2; i++)
ExtMapBuffer3[i] =
iMAOnArray(ExtMapBuffer5, Bars, MaPeriod2, 0, MaMetod2, i);
for (i = 0; i <= pos2; i++)
ExtMapBuffer4[i] =
iMAOnArray(ExtMapBuffer6, Bars, MaPeriod2, 0, MaMetod2, i);

//----
return (0);
Expand Down
65 changes: 54 additions & 11 deletions Price/Multi/OHLC/Heiken_Ashi_Smoothed.mq5
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
#define ERR_USER_INVALID_HANDLE 1
#define ERR_USER_INVALID_BUFF_NUM 2
#define ERR_USER_ITEM_NOT_FOUND 3
#define ERR_USER_ARRAY_IS_EMPTY 1000

// Defines indicator properties.
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots 4
#property indicator_color1 LightSalmon
#property indicator_color2 Lime
#property indicator_color3 LightSalmon
#property indicator_color4 Lime
#property indicator_buffers 9
#property indicator_plots 1
#property indicator_type1 DRAW_COLOR_CANDLES
#property indicator_style1 STYLE_SOLID
#property indicator_color1 clrNONE, clrLime, clrLightSalmon
#property indicator_width1 1
#property indicator_label1 "HA Open;HA High;HA Low;HA Close"

// Includes EA31337 framework.
// clang-format off
Expand All @@ -28,23 +28,45 @@

// Defines macros.
#define extern input
//#define Bars fmin(10000, (ChartStatic::iBars(_Symbol, _Period)))
#define Bars ChartStatic::iBars(_Symbol, _Period)
#define Bars fmin(1000, (ChartStatic::iBars(_Symbol, _Period)))
#define Bid (SymbolInfoStatic::GetBid(_Symbol))
#define TimeDayOfWeek (DateTime::DateOfWeek())

// Includes the main file.
#include "Heiken_Ashi_Smoothed.mq4"

double ExtMapBufferCandleColor[];

// Custom indicator initialization function.
void OnInit() { init(); }
void OnInit() {
init();

IndicatorBuffers(9);

// Note switched buffers! We've mapped them from OHLC values into problem
// index buffers.
SetIndexBuffer(0, ExtMapBuffer3, INDICATOR_DATA);
SetIndexBuffer(1, ExtMapBuffer1, INDICATOR_DATA);
SetIndexBuffer(2, ExtMapBuffer2, INDICATOR_DATA);
SetIndexBuffer(3, ExtMapBuffer4, INDICATOR_DATA);
SetIndexBuffer(4, ExtMapBufferCandleColor, INDICATOR_COLOR_INDEX);

SetIndexBuffer(5, ExtMapBuffer5, INDICATOR_CALCULATIONS);
SetIndexBuffer(6, ExtMapBuffer6, INDICATOR_CALCULATIONS);
SetIndexBuffer(7, ExtMapBuffer7, INDICATOR_CALCULATIONS);
SetIndexBuffer(8, ExtMapBuffer8, INDICATOR_CALCULATIONS);

SetIndexStyle(0, DRAW_COLOR_CANDLES, STYLE_SOLID, 1, clrLime);

MathSrand(GetTickCount());
}

// Custom indicator iteration function.
int OnCalculate(const int rates_total, const int prev_calculated,
const int begin, const double &price[]) {
IndicatorCounted(fmin(prev_calculated, Bars));
ResetLastError();

ArraySetAsSeries(ExtMapBuffer1, true);
ArraySetAsSeries(ExtMapBuffer2, true);
ArraySetAsSeries(ExtMapBuffer3, true);
Expand All @@ -56,6 +78,14 @@ int OnCalculate(const int rates_total, const int prev_calculated,

int _res = start() >= 0 ? rates_total : 0;

if (prev_calculated <= 0) {
// Initializing haOpen and haClose with open/close prices.
for (int i = 0; i < rates_total; ++i) {
ExtMapBuffer5[i] = ::iOpen(NULL, PERIOD_CURRENT, i);
ExtMapBuffer6[i] = ::iClose(NULL, PERIOD_CURRENT, i);
}
}

ArraySetAsSeries(ExtMapBuffer1, false);
ArraySetAsSeries(ExtMapBuffer2, false);
ArraySetAsSeries(ExtMapBuffer3, false);
Expand All @@ -65,5 +95,18 @@ int OnCalculate(const int rates_total, const int prev_calculated,
ArraySetAsSeries(ExtMapBuffer7, false);
ArraySetAsSeries(ExtMapBuffer8, false);

for (int i = prev_calculated; i < rates_total; ++i) {
// Filling candle color index.
ExtMapBufferCandleColor[i] = ExtMapBuffer1[i] > ExtMapBuffer4[i] ? 2 : 1;
}

for (int i = MathMax(0, prev_calculated - 1); i < rates_total; ++i) {
// Inserting H/L values into proper slots.
double _high = MathMax(ExtMapBuffer1[i], ExtMapBuffer2[i]);
double _low = MathMin(ExtMapBuffer1[i], ExtMapBuffer2[i]);
ExtMapBuffer1[i] = _high;
ExtMapBuffer2[i] = _low;
}

return _res;
}

0 comments on commit 5968581

Please sign in to comment.