Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 273

 
Lowech:
   int nomber = OrdersHistoryTotal();
   int tip=OrderType();
   if(OrderSelect(nomber,SELECT_BY_POS,MODE_HISTORY))  
Good afternoon. I need some help. If there is a closed Sell order, the next order may be opened only for Buy. What is wrong in the code? Please advise.

You have already been told that the second line of this fragment contains an error: Before the OrderType() function is called, the order must be selected using the OrderSelect() function.

There is an error in line 3: But is there an order with the number 1? If the minimum number was 1, i.e., the account starts with 1, then the maximum number would be equal to the number (let there be 5 orders in total: order numbers 1, 2, 3, 4, 5). But the orders are numbered from 0 - put the cursor on OrdersHistoryTotal and press F1 to see an example. So what is the maximum order number?

 
Aleksey Vyazmikin:

Earlier you suggested dividing the date of the month by 7 and rounding, which is done in the second table.

I don't understand your algorithm.

Now here is what I am thinking:

1. Determine the day of the week of the first day of the month.

2. Find out how many days left to the end of the week (was Wednesday (3) to Monday 7-3 = 4).

3. The 5th day is the beginning of the second week

4. then in the loop, we are looking for a week, if the number falls within the range of 5 + 7 * x to 5 + 7 * (x +1), where x - number of week minus one, then we have found the week

But it's complicated, maybe there is an easier way?

Alexey, we are talking about different things. The problem was not to determine which week of the month is the number of the month, but the first day of the week with this name in the month. These are different tasks and, consequently, different solutions.

For 63 years I still can not figure out how to calculate the number of week in the month. For example, in Russia Monday is considered the first day of the week. If the month begins on Sunday, then how do you count Monday? Has the second week begun? Or what??? The question is rhetorical, there is no need to start a polemic about it.

 
Alexey Viktorov:

Alexei, we are talking about different things. The problem was not to determine which week of the month the number of the month is in, but whether it is the first day of the week with that name in that month. These are different tasks and, consequently, different solutions.

For 63 years I still do not understand how to count the number of the week in the month. Let us say that in Russia the first day of the week is Monday. If the month begins on Sunday, then how is Monday counted? Has the second week begun? Or what??? The question is rhetorical, there is no need to start a polemic about it.


I see. It's a definite question for me...

 
Alexey Viktorov:

Alexei, we are talking about different things. The problem was not to determine which week of the month the number of the month is in, but whether it is the first day of the week with that name in that month. These are different tasks and, consequently, different solutions.

For 63 years I still can not figure out how to calculate the number of week in the month. For example, in Russia Monday is considered the first day of the week. If the month begins on Sunday, then how do you count Monday? Has the second week begun? Or what??? The question is rhetorical, there is no need to start a polemic about it.

I looked up the answer on Yandex - 4 ways. I like the Fourth Way: "Crosscutting". January 1 begins the first week of the year, seven days later, January 8 the second week of the year and so on to the end of the year. It's the same month by month
Онлайн калькулятор: Определение номера недели по дате
  • Timur пользователь planetcalc
  • planetcalc.ru
Калькулятор ниже определяет порядковый номер недели в году. Как выяснилось, это не совсем тривиальный вопрос, и есть целых четыре разных способа подсчитать номер недели. Первый способ: Стандарт ISO 8601 и соответствующий ему ГОСТ ИСО 8601-2001 «ПРЕДСТАВЛЕНИЕ ДАТ И ВРЕМЕНИ» — официальный способ расчета номера недели в Российской Федерации. По...
 
Darirunu It comes out very complicated. There must be a simpler option. Can anyone share ?
//+-------------------------------------------------------+
//| Проверка дней недели                       PRVERKA.mq4|
//+-------------------------------------------------------+

#property strict
string Дни[7]={"Вс","Понедельник","Вторник","Среда","Четверг","Пятница","Сб"};
void OnStart()
{
  int День=Day(), ДеньН=DayOfWeek();
  Alert("----------------------");
  Alert("Сейчас на компьютере: ", TimeToStr(TimeLocal(),TIME_DATE));
  Alert("Последний тик с сервера: ", TimeToStr(TimeCurrent(),TIME_DATE), " Число ", День, " ", Дни[ДеньН]);

  int ДеньНачалаМесяца = (ДеньН-День+36) % 7;
  Alert("ДеньНеделиПервогоЧисла = ", Дни[ДеньНачалаМесяца]);

  int ЧислоПервойПятницы = ДеньНачалаМесяца==6?7:6-ДеньНачалаМесяца;
  int ЧислоВторогоПонедл = (ДеньНачалаМесяца<2?9:16)-ДеньНачалаМесяца;

  Alert("ЧислоПервойПятницы = ", ЧислоПервойПятницы, "  ЧислоВторогоПонедл = ", ЧислоВторогоПонедл);

  if(ЧислоВторогоПонедл>День)
  {
    Alert("Надо подождать числа ", ЧислоВторогоПонедл+1);
    return;
  }

  // Смотрим свечи, бары, бычьи, медвежьи
}
here is the algorithm above read from bottom to top
 
STARIJ:

You have already been told that the second line of this fragment contains an error: Before the OrderType() function is called, the order must be selected using the OrderSelect() function.

There is an error in line 3: But is there an order with the number 1? If the minimum number was 1, i.e., the account starts with 1, then the maximum number would be equal to the number (let there be 5 orders in total: order numbers 1, 2, 3, 4, 5). But the orders are numbered from 0 - put the cursor on OrdersHistoryTotal and press F1 to see an example. So what is the maximum order number?

int 1 = OrdersHistoryTotal();
if (OrderSelect(1,SELECT_BY_POS,MODE_HISTORY))
   int tip=OrderType();
   

Is it correct now?

 
Lowech:
int 1 = OrdersHistoryTotal();
if (OrderSelect(1,SELECT_BY_POS,MODE_HISTORY))   - так какой наибольший номер ордера?
   int tip=OrderType();

and now is it right?

write a script and after each statement display the information + GetLastError() with Alert() - see example script just above
 
STARIJ:
write a script and after each operator print the information + GetLastError() with Alert() - see the example script above

but you'd better take the error code from the _LastError variable in order to print it, otherwise you may destroy the logic with a light "debug print"

GetLastError() at the same time does an unobvious thing for GetXXX() - it changes its internal state resetting error code to 0.

 
Maxim Kuznetsov:

only for printing it is better to take error code from _LastError variable, otherwise it is possible to destroy logic by a light "debug print"

GetLastError() at the same time does an unobvious thing for GetXXX() - it changes internal state, resetting error code to 0.

well, it's a matter of taste - either store the error code in a variable and reset the error or store it in _LastError for later use

 
STARIJ:
here's the algorithm above to read from the bottom up
That was quick. How will you search for bars? In a loop or use the time structure?
Reason: