I am using a custom indicator that draws up and down arrows for fractals.
Below is one the hour timeframe and the indicator is working correctly.
The problem is when I call the indicator using below code in an EA and monitor those values to detect changes OnTick():
static double firstUpArrowValue = 2147483647;
static double firstDownArrowValue = 2147483647;
static bool firstUpArrowIsSet = false;
static bool firstDownArrowIsSet = false;
int OnInit() {
return (INIT_SUCCEEDED);
}
void OnTick() {
double UpArrowValue = iCustom(_Symbol, PERIOD_H1, "Fractals ST Patterns", 1, 10.0, 0, 1);
double DownArrowValue = iCustom(_Symbol, PERIOD_H1, "Fractals ST Patterns", 1, 10.0, 1, 1);
UpArrowValue = NormalizeDouble(UpArrowValue, Digits);
DownArrowValue = NormalizeDouble(DownArrowValue, Digits);
if (UpArrowValue != 2147483647 && !firstUpArrowIsSet) {
//this is our first up arrow
firstUpArrowValue = UpArrowValue;
firstUpArrowIsSet = True;
Print(GetDateAndTime() + " First Up Arrow: " + firstUpArrowValue);
}
if (firstUpArrowIsSet && (UpArrowValue != firstUpArrowValue) && (UpArrowValue != 2147483647)) {
//up arrow value changed
Print(GetDateAndTime() + " Up Arrow Value Changed: " + UpArrowValue);
firstUpArrowValue = UpArrowValue;
}
}
string GetDateAndTime() {
return (string(Year()) + "-" + StringFormat("%02d", Month()) + "-" + StringFormat("%02d", Day()) + " " + StringFormat("%02d", Hour()) + ":" + StringFormat("%02d", Minute()));
}
Here is the indicator code as well (modified for cleaner variable names and translated Russian):
//+--------------------------------------------------------------------+
//| Fractals ST patterns |
//| Skype: |
//| E-mail: stpatterns@gmail.com |
//+--------------------------------------------------------------------+
#property copyright "Copyright by Vladimir Poltoratskiy"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 clrBlue;
#property indicator_width1 1;
#property indicator_color2 clrRed;
#property indicator_width2 1;
input int bars_surrounding = 1; //Bars around
extern double arrow_offset = 10; //Arrow offset
double UP[];
double DN[];
int kod_Arrow_Up = 217;
int kod_Arrow_Down = 218;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit() {
//--- indicator buffers mapping
arrow_offset *= Point;
SetIndexBuffer(0, UP);
SetIndexBuffer(1, DN);
SetIndexStyle(0, DRAW_ARROW, EMPTY);
SetIndexStyle(1, DRAW_ARROW, EMPTY);
SetIndexArrow(0, kod_Arrow_Up);
SetIndexArrow(1, kod_Arrow_Down);
//---
return (INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
//---
}
//+------------------------------------------------------------------+
//| Custom indicator iteration 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 i, j;
int lim;
if (prev_calculated == 0) {
lim = rates_total - bars_surrounding - 10;
} else {
lim = bars_surrounding + 2;
}
for (i = 0; i <= lim; i++) {
//+------------------------------------------------------------------+
//--- LOWER Fractal
//+------------------------------------------------------------------+
DN[i] = EMPTY_VALUE;
bool R_Plecho = true; //Check for fulfillment of the condition RIGHT from the current bar
bool L_Plecho = true; //Check for fulfillment of the condition to the left of the current bar
//---
//--- Determine whether the candle is the peak in relation to the standing next to the left
for (j = i + 1; j <= i + bars_surrounding; j++)
if (NormalizeDouble(Low[j], Digits) < NormalizeDouble(Low[i], Digits)) {
L_Plecho = false;
break;
}
if (L_Plecho)
if (i >= bars_surrounding) {
for (j = i - 1; j >= 0 && j >= i - bars_surrounding; j--)
if (NormalizeDouble(Low[j], Digits) < NormalizeDouble(Low[i], Digits)) {
R_Plecho = false;
break;
}
}
else R_Plecho = false;
if (R_Plecho && L_Plecho) DN[i] = Low[i] - arrow_offset;
//+------------------------------------------------------------------+
//--- UPPER fractal
//+------------------------------------------------------------------+
UP[i] = EMPTY_VALUE;
R_Plecho = true; //Check for fulfillment of the condition RIGHT from the current bar
L_Plecho = true; //Check for fulfillment of the condition to the left of the current bar
//---
//--- Check for fulfillment of the condition to the left of the current bar
for (j = i + 1; j <= i + bars_surrounding; j++)
if (NormalizeDouble(High[j], Digits) > NormalizeDouble(High[i], Digits)) {
L_Plecho = false;
break;
}
if (L_Plecho)
if (i >= bars_surrounding) {
for (j = i - 1; j >= 0 && j >= i - bars_surrounding; j--)
if (NormalizeDouble(High[j], Digits) > NormalizeDouble(High[i], Digits)) {
R_Plecho = false;
break;
}
}
else R_Plecho = false;
if (R_Plecho && L_Plecho) UP[i] = High[i] + arrow_offset;
}
return (rates_total);
}
I see the following changes:
2019.02.19 19:00:00 FractalReader EURUSD,H1: 2019-02-19 19:00 Up Arrow Value Changed: 1.1366
2019.02.19 18:00:00 FractalReader EURUSD,H1: 2019-02-19 18:00 Up Arrow Value Changed: 1.1352
2019.02.19 17:00:00 FractalReader EURUSD,H1: 2019-02-19 17:00 Up Arrow Value Changed: 1.1349
2019.02.19 16:00:00 FractalReader EURUSD,H1: 2019-02-19 16:00 Up Arrow Value Changed: 1.1331
2019.02.19 15:00:00 FractalReader EURUSD,H1: 2019-02-19 15:00 Up Arrow Value Changed: 1.131
2019.02.19 11:00:00 FractalReader EURUSD,H1: 2019-02-19 11:00 Up Arrow Value Changed: 1.1334
2019.02.19 10:00:00 FractalReader EURUSD,H1: 2019-02-19 10:00 Up Arrow Value Changed: 1.1333
2019.02.19 09:00:00 FractalReader EURUSD,H1: 2019-02-19 09:00 Up Arrow Value Changed: 1.1314
2019.02.19 07:00:00 FractalReader EURUSD,H1: 2019-02-19 07:00 Up Arrow Value Changed: 1.1309
2019.02.19 02:02:00 FractalReader EURUSD,H1: 2019-02-19 02:02 Up Arrow Value Changed: 1.1323
2019.02.19 01:00:00 FractalReader EURUSD,H1: 2019-02-19 01:00 First Up Arrow: 1.1322
2019.02.19 00:00:00 FractalReader EURUSD,H1: 2019-02-19 00:00 First Down Arrow: 1.1298
Not all of the above resulted in an arrow being drawn, but all of the arrows drawn are one of those lines. I believe this is because it is using the current bar being drawn to find a fractal. At some point in the current bars rendering it may form a fractal, but not necessarily when it is done and moved onto the next time period.
How can I figure out when a signal change results in an arrow being drawn? I want to completely ignore signals from the current bar because it gives me extra results. Is there a way to edit the EA so it only uses the previous 3 bars instead of current bar and the previous 2 ?
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 clrBlue;
#property indicator_width1 1;
#property indicator_color2 clrRed;
#property indicator_width2 1;
input int bars_surrounding = 1; //Bars around
input bool check_right=true; //check Right candles
extern double arrow_offset = 10; //Arrow offset
double UP[];
double DN[];
int kod_Arrow_Up = 217;int kod_Arrow_Down = 218;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit() {
//--- indicator buffers mapping
arrow_offset *= Point;
SetIndexBuffer(0, UP);
SetIndexBuffer(1, DN);
SetIndexStyle(0, DRAW_ARROW, EMPTY);
SetIndexStyle(1, DRAW_ARROW, EMPTY);
SetIndexArrow(0, kod_Arrow_Up);
SetIndexArrow(1, kod_Arrow_Down);
//---
return (INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
//---
}
//+------------------------------------------------------------------+
//| Custom indicator iteration 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 i, j;
int lim;
if (prev_calculated == 0) {
lim = rates_total - bars_surrounding - 10;
} else {
lim = bars_surrounding + 2;
}
for (i = 0; i <= lim; i++) {
//+------------------------------------------------------------------+
//--- LOWER Fractal
//+------------------------------------------------------------------+
DN[i] = EMPTY_VALUE;
bool R_Plecho = true; //Check for fulfillment of the condition RIGHT from the current bar
bool L_Plecho = true; //Check for fulfillment of the condition to the left of the current bar
//---
//--- Determine whether the candle is the peak in relation to the standing next to the left
for (j = i + 1; j <= i + bars_surrounding; j++)
if (low[j] < low[i]) {
L_Plecho = false;
break;
}
if (L_Plecho)
if (i >= bars_surrounding && check_right) {
for (j = i - 1; j >= 0 && j >= i - bars_surrounding; j--)
if (low[j] < low[i]) {
R_Plecho = false;
break;
}
}
//else R_Plecho = false;//no need because L_shoulder is left
if (R_Plecho && L_Plecho) DN[i] = Low[i] - arrow_offset;
//+------------------------------------------------------------------+
//--- UPPER fractal
//+------------------------------------------------------------------+
UP[i] = EMPTY_VALUE;
R_Plecho = true; //Check for fulfillment of the condition RIGHT from the current bar
L_Plecho = true; //Check for fulfillment of the condition to the left of the current bar
//---
//--- Check for fulfillment of the condition to the left of the current bar
for (j = i + 1; j <= i + bars_surrounding; j++)
if (high[j] > high[i]) {
L_Plecho = false;
break;
}
if (L_Plecho)
if (i >= bars_surrounding && check_right) {
for (j = i - 1; j >= 0 && j >= i - bars_surrounding; j--)
if (high[j] > high[i]) {
R_Plecho = false;
break;
}
}
//else R_Plecho = false;
if (R_Plecho && L_Plecho) UP[i] = High[i] + arrow_offset;
}
return (rates_total);
}
And do not forget to call iCustom(_Symbol,0,"Fractals ST Patterns",1,false,10,0,1)
by the way parameter 10 might be omitted as it is used for drawing