Мой индикатор iCustom меняется в советнике чаще, чем график - PullRequest
0 голосов
/ 25 февраля 2019

Я использую пользовательский индикатор, который рисует стрелки вверх и вниз для фракталов.
Ниже приводится часовой интервал, и индикатор работает правильно.

Fractals

Проблема в том, что я вызываю индикатор с помощью приведенного ниже кода в советнике и отслеживаю эти значения, чтобы обнаружить изменения 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.1917: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:00FractalReader 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 FractalReaderEURUSD, 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

Не все вышеперечисленное привело к появлению стрелкирисуются, но все нарисованные стрелки являются одной из этих линий.Я считаю, что это потому, что он использует текущий бар, чтобы найти фрактал.В какой-то момент текущего рендеринга баров он может сформировать фрактал, но не обязательно, когда это будет сделано и перенесено на следующий период времени.

Как я могу выяснить, когда изменение сигнала приводит к рисованию стрелки?Я хочу полностью игнорировать сигналы от текущего бара, потому что он дает мне дополнительные результаты.Есть ли способ отредактировать советника, чтобы он использовал только 3 предыдущих бара вместо текущего и 2 предыдущих?

1 Ответ

0 голосов
/ 04 марта 2019
#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);
}

И не забудьте вызвать iCustom(_Symbol,0,"Fractals ST Patterns",1,false,10,0,1), так как параметр 10 может быть опущен, так как он используется для рисования

...