我的iCustom指标在EA中的变化频率高于图表

问题描述 投票:0回答:1

我正在使用一个自定义指标,用于绘制分形的箭头。 下面是一小时的时间范围,指标正常工作。

Fractals

问题是当我使用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: [email protected] |
//+--------------------------------------------------------------------+
#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 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

并非所有上述都导致绘制箭头,但绘制的所有箭头都是其中一条线。我相信这是因为它正在使用当前正在绘制的条形来找到分形。在当前条形渲染中的某个点处,它可以形成分形,但不一定在完成并移动到下一个时间段时。

如何判断信号变化何时导致箭头被绘制?我想完全忽略来自当前柱的信号,因为它给了我额外的结果。有没有办法编辑EA所以它只使用前3个条而不是当前条和前2个?

mql4 indicator metatrader4 forex
1个回答
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),因为它用于绘图

© www.soinside.com 2019 - 2024. All rights reserved.