Помогите разобраться с индикатором

 

15 лет пишу советников и никогда не писал индикаторы. 

вообщем надо на одном чарте поставить синюю метку если веча бычья и красную - если медвежья. Но данные нужно взять с 4 ТФ. Как взять с текущего - разобрался. А вот как посчитать правильный индекс для других ТФ - всю голову сломал. 

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2020, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+


#property indicator_separate_window
#property indicator_buffers   8
#property indicator_plots    8
#property indicator_color1  clrBlue,clrRed,clrGray
#property indicator_maximum 4.5
#property indicator_minimum -0.5

sinput ENUM_TIMEFRAMES tf1 = PERIOD_CURRENT;
input ENUM_TIMEFRAMES tf2 = PERIOD_M15;
input ENUM_TIMEFRAMES tf3 = PERIOD_M30;
input ENUM_TIMEFRAMES tf4 = PERIOD_H1;






double Buffer1[];
double Buffer2[];
double Buffer3[];
double Buffer4[];
double ColorBuffer1[];
double ColorBuffer2[];
double ColorBuffer3[];
double ColorBuffer4[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- set buffers
   SetIndexBuffer(0,Buffer1,INDICATOR_DATA);
   SetIndexBuffer(2,Buffer2,INDICATOR_DATA);
//SetIndexBuffer(2,Buffer3,INDICATOR_CALCULATIONS);
//SetIndexBuffer(3,Buffer4,INDICATOR_CALCULATIONS);

   SetIndexBuffer(1,ColorBuffer1,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(3,ColorBuffer2,INDICATOR_COLOR_INDEX);
//SetIndexBuffer(6,ColorBuffer3,INDICATOR_COLOR_INDEX);
//SetIndexBuffer(7,ColorBuffer4,INDICATOR_COLOR_INDEX);

   PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_COLOR_ARROW);
   PlotIndexSetInteger(2,PLOT_DRAW_TYPE,DRAW_COLOR_ARROW);
//PlotIndexSetInteger(2,PLOT_DRAW_TYPE,DRAW_COLOR_ARROW);
//PlotIndexSetInteger(3,PLOT_DRAW_TYPE,DRAW_COLOR_ARROW);

   string short_name="Vital";
   IndicatorSetString(INDICATOR_SHORTNAME,short_name);


   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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 rates_total1 = iBars(_Symbol,tf1);

   if(prev_calculated>rates_total1 || prev_calculated<=0)
     {
      for(int i = 0; i<rates_total; i++)
        {
         
         Buffer1[i] = iClose(_Symbol, tf1,rates_total-1-i)- iOpen(_Symbol, tf1,rates_total-1-i);
         GetColor(Buffer1[i], ColorBuffer1[i]);
         Buffer1[i] = 4;
         
         Buffer2[i] = iClose(_Symbol, tf2,rates_total-1-i)- iOpen(_Symbol, tf2,rates_total-1-i);
         GetColor(Buffer2[i], ColorBuffer2[i]);
         Buffer2[i] = 3;


        }
     }

   else
     {
      for(int i = prev_calculated-1; i<rates_total1; i++)
        {
         Buffer1[i] = iClose(_Symbol, tf1,rates_total-1-i)- iOpen(_Symbol, tf1,rates_total-1-i);
         GetColor(Buffer1[i], ColorBuffer1[i]);
         Buffer1[i] = 4;
         
         Buffer2[i] = iClose(_Symbol, tf2,rates_total-1-i)- iOpen(_Symbol, tf2,rates_total-1-i);  //Как посчитать правильное  i ? 
         GetColor(Buffer2[i], ColorBuffer2[i]);
         Buffer2[i] = 3;

        }
     }


   return(rates_total1);
  }
//+------------------------------------------------------------------+
void  GetColor(double Buffer, double &ColorBuffer)
  {

   if(Buffer>0)
      ColorBuffer = 0.0;
   if(Buffer<0)
      ColorBuffer = 1.0;
   if(Buffer==0)
      ColorBuffer = 2.0;
  }

////+------------------------------------------------------------------+
 
CopyRates() по времени.
 

вот что- то не пойму . Как его покрасить правильно  ?

#property indicator_separate_window
#property indicator_buffers   8
#property indicator_plots    8
#property indicator_color1  clrBlue, clrRed, clrGray
#property indicator_color2  clrBlue, clrRed, clrGray
#property indicator_color3  clrBlue, clrRed, clrGray
#property indicator_maximum 4.5
#property indicator_minimum -0.5

sinput ENUM_TIMEFRAMES tf1 = PERIOD_CURRENT;
input ENUM_TIMEFRAMES tf2 = PERIOD_M15;
input ENUM_TIMEFRAMES tf3 = PERIOD_M30;
input ENUM_TIMEFRAMES tf4 = PERIOD_H1;

double Buffer1[];
double Buffer2[];
double Buffer3[];
double Buffer4[];
double ColorBuffer1[];
double ColorBuffer2[];
double ColorBuffer3[];
double ColorBuffer4[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- set buffers
   SetIndexBuffer(0,Buffer1,INDICATOR_DATA);
   SetIndexBuffer(1,Buffer2,INDICATOR_DATA);
   SetIndexBuffer(2,Buffer3,INDICATOR_DATA);
   SetIndexBuffer(3,Buffer4,INDICATOR_DATA);
   
   SetIndexBuffer(4,ColorBuffer1,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(5,ColorBuffer2,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(6,ColorBuffer3,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(7,ColorBuffer4,INDICATOR_COLOR_INDEX);
//SetIndexBuffer(3,Buffer4,INDICATOR_CALCULATIONS);

   
   
//SetIndexBuffer(6,ColorBuffer3,INDICATOR_COLOR_INDEX);
//SetIndexBuffer(7,ColorBuffer4,INDICATOR_COLOR_INDEX);

   PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_COLOR_ARROW);
  
//PlotIndexSetInteger(2,PLOT_DRAW_TYPE,DRAW_COLOR_ARROW);
//PlotIndexSetInteger(3,PLOT_DRAW_TYPE,DRAW_COLOR_ARROW);

   string short_name="Vital";
   IndicatorSetString(INDICATOR_SHORTNAME,short_name);


   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {



   if(prev_calculated>rates_total || prev_calculated<=0)
     {
      for(int i = 0; i<rates_total; i++)
        {
         
         Buffer1[i] = iClose(_Symbol, tf1,rates_total-1-i)- iOpen(_Symbol, tf1,rates_total-1-i);
         GetColor(Buffer1[i], ColorBuffer1[i]);
         //Print (ColorBuffer1[i],"   ",Buffer1[i]);
         
         int y = iBarShift(_Symbol,tf2,time[i]);
         Buffer2[i] = iClose(_Symbol, tf2,rates_total-1-y)- iOpen(_Symbol, tf2,rates_total-1-y);
         Buffer2[i] = 3;


        }
     }

   else
     {
      for(int i = prev_calculated-1; i<rates_total; i++)
        {
         Buffer1[i] = iClose(_Symbol, tf1,rates_total-1-i)- iOpen(_Symbol, tf1,rates_total-1-i);
         GetColor(Buffer1[i], ColorBuffer1[i]);
         Buffer1[i] = 4;
         
         Buffer2[i] = iClose(_Symbol, tf2,rates_total-1-i)- iOpen(_Symbol, tf2,rates_total-1-i);  //Как посчитать правильное  i ? 
        int y = iBarShift(_Symbol,tf2,time[i]);
         Buffer2[i] = iClose(_Symbol, tf2,rates_total-1-y)- iOpen(_Symbol, tf2,rates_total-1-y);
         Buffer2[i] = 3;

        }
     }


   return(rates_total);
  }
//+------------------------------------------------------------------+
void  GetColor(double Buffer, double &ColorBuffer)
  {

   if(Buffer>0)
      ColorBuffer = 0.0;
   if(Buffer<0)
      ColorBuffer = 1.0;
   if(Buffer==0)
      ColorBuffer = 2.0;
  }

////+------------------------------------------------------------------+
Вот что-то вообще ничего не отображается. Почему ? 
 
проще пересоздать на новых рельсах(через мастера создания), в современном стиле, сколько уже на этих мелочах попадались, MQ точно в аду будет гореть.
 
Кажется буферы должны чередоваться: буфер данных, буфер индексов, буфер данных, буфер индексов... А всего буферов в два раза больше, чем отображаемых должно быть.
 

А можно пример привести или поправить то, что есть ? 

 
Dmitiry Ananiev:

А можно пример привести или поправить то, что есть ? 

какой пример, все что калькуляте перенести в новый созданный

 
Valeriy Korobeynik:
С такими знаниями вам лучше перейти в режим только чтение… Больше никому не советуйте так делать.
 
Alexey Viktorov:

Вы думаете что если вы перенесёте свой г-код ниже моей критики, то он станет лучше?

Запишу себе "ниже моей критики" как фразеологизм :-)) 
 
Dmitiry Ananiev:

А можно пример привести или поправить то, что есть ? 

//+------------------------------------------------------------------+
//|                                                       iVital.mq5 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
//---
#property indicator_minimum   -0.5
#property indicator_maximum   4.5
//---
#property indicator_buffers   8
#property indicator_plots     4
//---
#property indicator_label1    "1"
#property indicator_type1     DRAW_COLOR_ARROW
#property indicator_color1    clrBlue,clrRed,clrGray
#property indicator_width1    1
//---
#property indicator_label2    "2"
#property indicator_type2     DRAW_COLOR_ARROW
#property indicator_color2    clrBlue,clrRed,clrGray
#property indicator_width2    1
//---
#property indicator_label3    "3"
#property indicator_type3     DRAW_COLOR_ARROW
#property indicator_color3    clrBlue,clrRed,clrGray
#property indicator_width3    1
//---
#property indicator_label4    "4"
#property indicator_type4     DRAW_COLOR_ARROW
#property indicator_color4    clrBlue,clrRed,clrGray
#property indicator_width4    1
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
input    ENUM_TIMEFRAMES   Timeframe1  = PERIOD_M5;      //Timeerame #1
input    ENUM_TIMEFRAMES   Timeframe2  = PERIOD_M15;     //Timeerame #2
input    ENUM_TIMEFRAMES   Timeframe3  = PERIOD_M30;     //Timeerame #3
input    ENUM_TIMEFRAMES   Timeframe4  = PERIOD_H1;      //Timeerame #4
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double      Buf1[], Col1[], Buf2[], Col2[], Buf3[], Col3[], Buf4[], Col4[];
int         sh;
//---
MqlRates    rat1[], rat2[], rat3[], rat4[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   Comment("");
//---
   sh = fmax(PeriodSeconds(Timeframe1), fmax(PeriodSeconds(Timeframe2), fmax(PeriodSeconds(Timeframe3), PeriodSeconds(Timeframe4))));
   sh /= PeriodSeconds(PERIOD_CURRENT);
//---
   ENUM_TIMEFRAMES   tf = Timeframe1;
   if(PeriodSeconds(tf) > PeriodSeconds(Timeframe2))
      tf = Timeframe2;
   if(PeriodSeconds(tf) > PeriodSeconds(Timeframe3))
      tf = Timeframe3;
   if(PeriodSeconds(tf) > PeriodSeconds(Timeframe4))
      tf = Timeframe4;
   if(PeriodSeconds() > PeriodSeconds(tf))
      ChartSetSymbolPeriod(0, Symbol(), tf);
//---
   iClose(Symbol(), Timeframe1, 1000);
   iClose(Symbol(), Timeframe2, 1000);
   iClose(Symbol(), Timeframe3, 1000);
   iClose(Symbol(), Timeframe4, 1000);
//---
   int   cnt = 0;
   SetBuffer(cnt, Buf1, INDICATOR_DATA, true);
   SetBuffer(cnt, Col1, INDICATOR_COLOR_INDEX, true);
   SetBuffer(cnt, Buf2, INDICATOR_DATA, true);
   SetBuffer(cnt, Col2, INDICATOR_COLOR_INDEX, true);
   SetBuffer(cnt, Buf3, INDICATOR_DATA, true);
   SetBuffer(cnt, Col3, INDICATOR_COLOR_INDEX, true);
   SetBuffer(cnt, Buf4, INDICATOR_DATA, true);
   SetBuffer(cnt, Col4, INDICATOR_COLOR_INDEX, true);
//---
   cnt = 0;
   PlotIndexSetInteger(cnt, PLOT_ARROW, 108);
   PlotIndexSetString(cnt, PLOT_LABEL,
                      StringSubstr(EnumToString((Timeframe1 == PERIOD_CURRENT) ? Period() : Timeframe1), StringLen("PERIOD_")));
   cnt++;
   PlotIndexSetInteger(cnt, PLOT_ARROW, 108);
   PlotIndexSetString(cnt, PLOT_LABEL,
                      StringSubstr(EnumToString((Timeframe2 == PERIOD_CURRENT) ? Period() : Timeframe2), StringLen("PERIOD_")));
   cnt++;
   PlotIndexSetInteger(cnt, PLOT_ARROW, 108);
   PlotIndexSetString(cnt, PLOT_LABEL,
                      StringSubstr(EnumToString((Timeframe3 == PERIOD_CURRENT) ? Period() : Timeframe3), StringLen("PERIOD_")));
   cnt++;
   PlotIndexSetInteger(cnt, PLOT_ARROW, 108);
   PlotIndexSetString(cnt, PLOT_LABEL,
                      StringSubstr(EnumToString((Timeframe4 == PERIOD_CURRENT) ? Period() : Timeframe4), StringLen("PERIOD_")));
//---
   IndicatorSetInteger(INDICATOR_HEIGHT, 100);
   IndicatorSetInteger(INDICATOR_DIGITS, 0);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
   ArraySetAsSeries(time, true);
   int   limit = rates_total - prev_calculated;
   if(limit > 1 || limit < 0)
     {
      for(int i = 0; i < rates_total; i++)
        {
         Buf1[i] = EMPTY_VALUE;
         Buf2[i] = EMPTY_VALUE;
         Buf3[i] = EMPTY_VALUE;
         Buf4[i] = EMPTY_VALUE;
        }
      limit = rates_total - 2;
     }
   else
      limit += (sh + 1);
//---
   for(int i = limit; i > 0; i--)
     {
      Buf1[i] = EMPTY_VALUE;
      Buf2[i] = EMPTY_VALUE;
      Buf3[i] = EMPTY_VALUE;
      Buf4[i] = EMPTY_VALUE;
      if(CopyRates(_Symbol, Timeframe1, time[i], 1, rat1) != 1 ||
         CopyRates(_Symbol, Timeframe2, time[i], 1, rat2) != 1 ||
         CopyRates(_Symbol, Timeframe3, time[i], 1, rat3) != 1 ||
         CopyRates(_Symbol, Timeframe4, time[i], 1, rat4) != 1)
         continue;
      Buf1[i] = 0.5;
      Buf2[i] = 1.5;
      Buf3[i] = 2.5;
      Buf4[i] = 3.5;
      //---
      Col1[i] = (rat1[0].close > rat1[0].open) ? 0.0 :
                (rat1[0].close < rat1[0].open) ? 1.0 :
                2.0;
      Col2[i] = (rat2[0].close > rat2[0].open) ? 0.0 :
                (rat2[0].close < rat2[0].open) ? 1.0 :
                2.0;
      Col3[i] = (rat3[0].close > rat3[0].open) ? 0.0 :
                (rat3[0].close < rat3[0].open) ? 1.0 :
                2.0;
      Col4[i] = (rat4[0].close > rat4[0].open) ? 0.0 :
                (rat4[0].close < rat4[0].open) ? 1.0 :
                2.0;
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int SetBuffer(int &vCnt, double &vBuf[], const ENUM_INDEXBUFFER_TYPE vType, const bool vSetAsSeries)
  {
   ArraySetAsSeries(vBuf, vSetAsSeries);
   SetIndexBuffer(vCnt, vBuf, vType);
   vCnt++;
   return(vCnt);
  }
//+------------------------------------------------------------------+
 
Valeriy Korobeynik:

Спасибо. Теперь буду разбираться.