我正在使用一个自定义指标,该指标会向上和向下绘制分形箭头。
下面是一个小时的时间范围,指示器运行正常。
问题是,当我在EA中使用以下代码调用指标并监视这些值以检测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()));
}
这里也是指示器代码(针对更干净的变量名和俄语翻译进行了修改):
//+--------------------------------------------------------------------+
//| 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);
}
我看到以下变化:
2019.02.19 19:00:00 FractalReader EURUSD,H1:2019-02-19 19:00向上箭头值更改为:1.1366
2019.02.19 18:00:00 FractalReader EURUSD,H1:2019-02-19 18:00向上箭头值更改为:1.1352
2019.02.19 17:00:00 FractalReader EURUSD,H1:2019-02-19 17:00向上箭头值更改为:1.1349
2019.02.19 16:00:00 FractalReader EURUSD,H1:2019-02-19 16:00向上箭头值更改为:1.1331
2019.02.19 15:00:00 FractalReader EURUSD,H1:2019-02-19 15:00向上箭头值更改为1.131
2019.02.19 11:00:00 FractalReader EURUSD,H1:2019-02-19 11:00向上箭头值更改为:1.1334
2019.02.19 10:00:00 FractalReader EURUSD,H1:2019-02-19 10:00向上箭头值更改为:1.1333
2019.02.19 09:00:00 FractalReader EURUSD,H1:2019-02-19 09:00向上箭头值更改为:1.1314
2019.02.19 07:00:00 FractalReader EURUSD,H1:2019-02-19 07:00向上箭头值更改为:1.1309
2019.02.19 02:02:00 FractalReader EURUSD,H1:2019-02-19 02:02向上箭头值更改为:1.1323
2019.02.19 01:00:00 FractalReader EURUSD,H1:2019-02-19 01:00第一个向上箭头:1.1322
2019.02.19 00:00:00 FractalReader EURUSD,H1:2019-02-19 00:00第一向下箭头:1.1298
并非所有上述情况都导致绘制了箭头,但是绘制的所有箭头都是这些线之一。我相信这是因为它正在使用当前绘制的条来查找分形。在当前柱状图的某个时刻,它可能会形成分形,但不一定会在完成后移到下一个时间段。
如何确定信号变化何时导致绘制箭头?我想完全忽略当前条形信号,因为它给了我额外的结果。有没有一种方法可以编辑EA,使其仅使用前3个小节,而不使用当前的前2个小节?
答案 0 :(得分:1)
#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);
}
也不要忘记通过省略参数10来调用iCustom(_Symbol,0,"Fractals ST Patterns",1,false,10,0,1)
,因为它用于绘制