We should try to avoid unnecessary cycles. We have a flood of quotes, and you need to make the right entries on the fly, so that you don't have to dig through history and waste computer resources.
We should try to avoid unnecessary loops. We have quotes coming in streams, and we need to make the right entries on the fly, so we don't have to dig through the history and waste computer resources.
I ran your code.
Is there something a little wrong here?
#property version"1.00"#property strict//символ и таймфрейм текущего графикаstring symbol;
ENUM_TIMEFRAMES frame;
datetime time;
struct BarData
{
struct Elem
{
int number; //порядковый номер периода (дня, месяца или года)double high; //максимум периодаdouble low; //минимум периодаdatetime time_high; //время максимумаdatetime time_low; //время минимума
} Arr[]; //массив периодаint index; //текущий индекс массиваdouble max; //последнее максимальное значение периодаdouble min; //последнее минимальное значение периодаdatetime time_max; //время максимумаdatetime time_min; //время минимума//при создании структуры указываем, что массив пустой
BarData(){index=-1;}
//функция записывает текущие экстремумыvoid WriteBar(int eNumber, string eSymbol, ENUM_TIMEFRAMES eFrame, datetime eTime)
{
int eShift=iBarShift(eSymbol,eFrame,eTime);
double eHigh=iHigh(eSymbol,eFrame,eShift);
double eLow=iLow(eSymbol,eFrame,eShift);
//если элементов ещё нет или период сменилсяif(index<0 || eNumber!=Arr[index].number)
{
ArrayResize(Arr,++index+1);
Arr[index].number=eNumber;
Arr[index].high=eHigh;
Arr[index].low=eLow;
Arr[index].time_high=eTime;
Arr[index].time_low=eTime;
}
//если произошло обновление текущего максимумаif(eHigh-Arr[index].high>0)
{
Arr[index].high=eHigh;
Arr[index].time_high=eTime;
}
//если произошло обновление текущего минимумаif(Arr[index].low-eLow>0)
{
Arr[index].low=eLow;
Arr[index].time_low=eTime;
}
//если произошло обновление предыдущего максимумаif(eHigh-max>0)
{
for(int i=index+1; i>=0; i--)
{
if(Arr[index].high-eHigh>0)
{
max=Arr[index].high;
time_max=Arr[index].time_high;
break;
}
}
}
//если произошло обновление предыдущего минимумаif(min-eLow>0)
{
for(int i=index+1; i>=0; i--)
{
if(eLow-Arr[index].low>0)
{
min=Arr[index].low;
time_min=Arr[index].time_low;
break;
}
}
}
}
} day, month, year;
intOnInit()
{
symbol=Symbol();
frame=(ENUM_TIMEFRAMES)Period();
return(INIT_SUCCEEDED);
}
voidOnTick()
{
//текущее время закрытого бара
time=iTime(symbol,frame,1);
MqlDateTime date;
TimeToStruct(time,date);
//делаем записи каждого периода
day.WriteBar(date.day,symbol,frame,time);
month.WriteBar(date.mon,symbol,frame,time);
year.WriteBar(date.year,symbol,frame,time);
//теперь имеем значения Comment(TimeToString(day.time_max)+" : "+DoubleToString(day.max)+" : "+DoubleToString(day.Arr[day.index].high)+"\n"+TimeToString(day.time_min)+" : "+DoubleToString(day.Arr[day.index].low)+" : "+DoubleToString(day.min));
}
Here's how it looks
1) Low_D1 level of the previous day
2) Min_D closest daily low on history
//+------------------------------------------------------------------+//| Test_Level.mq4 |//| Copyright 2020, MetaQuotes Software Corp. |//| https://www.mql5.com |//+------------------------------------------------------------------+#property copyright"Copyright 2020, MetaQuotes Software Corp."#property link"https://www.mql5.com"#property version"1.00"#property strictdouble Bar_data_D1 [][6]; // Копирует в массив данные баров указанного графика и возвращает количество скопированных баров D1double Low_D1_Level; // Возвращает значение минимальной цены бара D1double Min_D_Level ; // ближайшей минимальный D уровеньdatetime Time_Day;
//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+intOnInit()
{
Level();
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+//| Expert deinitialization function |//+------------------------------------------------------------------+voidOnDeinit(constint reason)
{
}
//+------------------------------------------------------------------+//| Expert tick function |//+------------------------------------------------------------------+voidOnTick()
{
On_Timer();
}
//+------------------------------------------------------------------+//+------------------------------------------------------------------+//| Функция Level //+------------------------------------------------------------------+void Level()
{
ArrayCopyRates(Bar_data_D1,_Symbol,PERIOD_D1); // Копирует в массив данные баров указанного графика и возвращает количество скопированных баров
Low_D1_Level = iLow (_Symbol,PERIOD_D1,1); // Возвращает значение минимальной цены бара D1//--- Min_D_Leve //for(int i = 1; i<ArrayRange(Bar_data_D1,0) ;i++)for(int i = 1; ;i++)
{
Print(" i = ",i," Bar_data_D1 [i][2] = ",Bar_data_D1 [i][2]);
if(Bar_data_D1 [i][2]>=0)
{
if( Bar_data_D1 [i][2] < Low_D1_Level)
{
Min_D_Level = Bar_data_D1 [i][2];break;
}
}
}
//+-------------------------Low_D1_Level----------------------------+ if(ObjectFind("Low_D1")!=Low_D1_Level)
{
ObjectDelete("Low_D1");
if(ObjectFind("Low_D1")!=0)
{
ObjectCreate("Low_D1",OBJ_HLINE, 0, Time[0],Low_D1_Level);
ObjectSet("Low_D1", OBJPROP_COLOR, clrMaroon);
ObjectSet("Low_D1", OBJPROP_WIDTH, 1);
}
}
if(ObjectFind("Low_D1_label")!=Low_D1_Level)
{
ObjectDelete("Low_D1_label");
if(ObjectFind("Low_D1_label") != 0)
{
ObjectCreate("Low_D1_label", OBJ_TEXT, 0, Time[13], Low_D1_Level);
ObjectSetText("Low_D1_label", "Low_D1: " + DoubleToStr(Low_D1_Level,_Digits), 8,"Verdana", Brown);
}
}
//+-------------------------Min_D_Level----------------------------+ if(ObjectFind("Min_D")!= Min_D_Level)
{
ObjectDelete("Min_D");
if(ObjectFind("Min_D")!=0)
{
ObjectCreate("Min_D",OBJ_HLINE, 0, Time[0],Min_D_Level);
ObjectSet("Min_D", OBJPROP_COLOR, clrMaroon);
ObjectSet("Min_D", OBJPROP_WIDTH, 1);
}
}
if(ObjectFind("Min_D_label")!=Min_D_Level)
{
ObjectDelete("Min_D_label");
if(ObjectFind("Min_D_label") != 0)
{
ObjectCreate("Min_D_label", OBJ_TEXT, 0, Time[30], Min_D_Level);
ObjectSetText("Min_D_label", "Min_D: " + DoubleToStr(Min_D_Level,_Digits), 8,"Verdana", Brown);
}
}
}
//+------------------------------------------------------------------+//| функция удаление всех объектов созданных советником//+------------------------------------------------------------------+void DestroyObject()
{
int tot=ObjectsTotal();
for( int i=tot; i>=0; i--)
{
if(ObjectName(i)=="Low_MN1"){ObjectDelete(0,"Low_MN1");Print("<< Объект Low_MN удалён >>");}
if(ObjectName(i)=="Low_MN1_label"){ObjectDelete(0,"Low_MN1_label");Print("<< Объект Low_MN1_label удалён >>");}
if(ObjectName(i)=="Min_D"){ObjectDelete(0,"Min_D");Print("<< Объект Min_D удалён >>");}
if(ObjectName(i)=="Min_D_label"){ObjectDelete(0,"Min_D_label");Print("<< Объект Min_D_label удалён >>");}
}
}
//+-------------------------------------------------------------------------+ // функция Timer //+-------------------------------------------------------------------------+void On_Timer()
{
if(Day()!= Time_Day)
{
Level();
Time_Day = Day();
}
}
The algorithm does not always draw and count as intended, but as written. Writing as written is usually difficult.
In the picture you showed the minima, and in the condition you have the minimum price of the daily bar Low. And the algorithm found it in the trend zone, and it is not the minimum between 2 adjacent bars.
The first nuance, so far the algorithm does not count from the beginning of the tester's history, but from the start. Therefore, the date 1970.01.01, which is zero, appears.
To make it count from the beginning of the story, you need to insert a loop in OnTick. Something like this:
datetime time=0, current;
voidOnTick()
{
//текущее время закрытого бара
current=iTime(symbol,frame,1);
do
{
MqlDateTime date;
TimeToStruct(time,date);
//делаем записи каждого периода
day.WriteBar(date.day,symbol,frame,time);
month.WriteBar(date.mon,symbol,frame,time);
year.WriteBar(date.year,symbol,frame,time);
if(time<current) {time=iTime(symbol,frame,(iBarShift(symbol,frame,time)-1));} elsebreak;
}
while(time<=current);
//теперь имеем значения Comment(TimeToString(day.time_max)+" : "+DoubleToString(day.max)+" : "+DoubleToString(day.Arr[day.index].high)+"\n"+TimeToString(day.time_min)+" : "+DoubleToString(day.Arr[day.index].low)+" : "+DoubleToString(day.min));
}
And in the function of bar record add a check for zero date:
:) it all comes with experience, I don't understand a lot of things either and I'm learning as I go along. It's not supposed to be a structure, it's supposed to be a class. We're almost at the level of OOP. In which I'm still floating.
Regarding ArrayCopyRates, Alexey correctly said. It's written in the help:
Копирует в двумерный массив вида double RateInfo[][6] данные баров указанного графика и возвращает количество скопированных баров.
So you have to write it:
int total=ArrayCopyRates(Bar_data_D1,_Symbol,PERIOD_D1); // Копирует в массив данные баров указанного графика и возвращает количество скопированных баров
Low_D1_Level = iLow (_Symbol,PERIOD_D1,1); // Возвращает значение минимальной цены бара D1for(int i = 1; i<total ;i++)
{
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Been sitting here for a while:
We should try to avoid unnecessary cycles. We have a flood of quotes, and you need to make the right entries on the fly, so that you don't have to dig through history and waste computer resources.I feel like a 1st grader, even though I'm 42.
I feel like a 1st grader, even though I'm 42.
That's a good thing. It means everything is ahead of me.
That's good. So everything is ahead of you.
The most important thing is the result!!!
3.47 I'm going to bed.
I'll digest it all tomorrow and if I have any questions I'll let you know !
Thanks a lot everyone!
Omsk?
Been sitting around for a while:
We should try to avoid unnecessary loops. We have quotes coming in streams, and we need to make the right entries on the fly, so we don't have to dig through the history and waste computer resources.I ran your code.
Is there something a little wrong here?
Here's how it looks
1) Low_D1 level of the previous day
2) Min_D closest daily low on history
Omsk?
Yes
Yes
at +3 somehow not many cities)
The algorithm does not always draw and count as intended, but as written. Writing as written is usually difficult.
In the picture you showed the minima, and in the condition you have the minimum price of the daily bar Low. And the algorithm found it in the trend zone, and it is not the minimum between 2 adjacent bars.
I ran your code.
Is something a little wrong here?
To make it count from the beginning of the story, you need to insert a loop in OnTick. Something like this:
And in the function of bar record add a check for zero date:
And second, in day.Arr[day.index].high is the current day. If you want yesterday, it's in the previous element:
I guess the logic should be correct now. Didn't check it too hard, but it seems to be correct.
I feel like a 1st grader even though I'm 42.
Regarding ArrayCopyRates, Alexey correctly said. It's written in the help:
Копирует в двумерный массив вида double RateInfo[][6] данные баров указанного графика и возвращает количество скопированных баров.
So you have to write it:
But that's not economical code.