[ARCHIVE]Any rookie question, so as not to clutter up the forum. Professionals, don't pass it by. Can't go anywhere without you - 5. - page 309

 
Zhunko thank you very much. Everything comes back with a boomerang and so does the good.
 
artmedia70:
Exactly. The global variable Reason gets a value, but it does not get it in deinit(), but in init() so that when running init(), you can see the reason for the last deinitialization and perform a certain action based on the value. But it does not work as it should.

You have been told how to do it. It is in deinit() that we find out the reason for the last deinitialisation and pass it through a variable declared globally. In init() you check the value of the variable and find out the reason for the last deinitialisation. It works!!!

PS Apparently not everyone realises that init()/deinit() do not change the values of globally declared variables unless explicitly stated in statements within functions.

Actually, it's better not to close orders in init(). There, MarketInfo() often does not work as expected.

int    DeinitReason = 0; // определяем переменную на глобальном уровне
...
int init ()
{
...
      if ( DeinitReason == 3) // если смена таймфрейма
...
}

int deinit()
  {
//----
   DeinitReason = UninitializeReason( );
   Print("DeinitReason ",DeinitReason);
//----
   return(0);
  }
 
Mislaid:
You have been told how to do it. It is in deinit() that we find out the reason for the last deinitialisation and pass it through a variable declared globally. In init() you check the value of the variable and find out the reason for the last deinitialisation. It works!!!
Realised that Artyom has a problem specifically with the switch from TF M5. There's an error there. We should check if this is the case. Maybe it's a bug in MT4.
 
Zhunko:
This suggested what you would do with this feature. A feature like this is usually run through history.

I can't even imagine running the function in a loop...
 
hoz:

I can't imagine that the function would run in a loop...

You're welcome:
for(i=OrdersTotal()-1;i>=0;i--){
    Print(GetOrderInfo(i,OrderMagic,.....));// вызов пользовательской функции 
}

for(i=0;i<OrdersTotal();i++){/// вызов функции "ордерс тотал" на каждой итерации

}
 
Zhunko:
I realized that Artem has a problem with switching from M5 TF. There is an error there. I would like to check if it is so. Maybe it is a bug of MT4.

No, it's not a bug. Here's the EA. Changed timeframes from M1 to H1

//+------------------------------------------------------------------+
//|                                                          111.mq4 |
//|                      Copyright © 2006, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

int    DeinitReason = 0; // определяем переменную на глобальном уровне
int init ()
{
}

int deinit()
  {
//----
   DeinitReason = UninitializeReason( );
   Print("DeinitReason ",DeinitReason);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+ 
19:20:02 111 CADJPY,M15: initialized
19:20:09 111 CADJPY,M15: DeinitReason 3
19:20:09 111 CADJPY,M15: deinitialized
19:20:09 111 CADJPY,M15: uninit reason 3
19:20:09 Supertrend[1] CADJPY,M15: deinitialized
19:20:09 Supertrend[1] CADJPY,M15: uninit reason 3
19:20:09 HLC CADJPY,M15: deinitialized
19:20:09 HLC CADJPY,M15: uninit reason 3
19:20:09 AMA optimized1 CADJPY,M15: deinitialized
19:20:09 AMA optimized1 CADJPY,M15: uninit reason 3
19:20:09 AMA optimized1 CADJPY,M15: deinitialized
19:20:09 AMA optimized1 CADJPY,M15: uninit reason 3
19:20:09 Supertrend[1] CADJPY,H1: initialized
19:20:09 HLC CADJPY,H1: initialized
19:20:09 AMA optimized1 CADJPY,H1: initialized
19:20:09 AMA optimized1 CADJPY,H1: initialized
19:20:09 111 CADJPY,H1: initialized
 
Mislaid:

No, it's not a bug. Here's the EA. Changed timeframes from M1 to H1

That's good! I also have a lot of reasons for deinitialization.

Mislaid:

Actually, it's better not to close orders in init(). There MarketInfo() often does not work as expected.

Yes. You can't. It's written in the help.

hoz:

I can't even imagine that this function would run in a loop...
It's routine.
 
Zhunko:

That's good! Because I too have a lot tied up in the reasons for deinitialisation.

Yes. You cannot. It is written in the help. It's a common thing.

Here is the final variant to check. The previous one was not very successful, because init()/deinit() sometimes do not make friends with print()

//+------------------------------------------------------------------+
//|                                                          111.mq4 |
//|                      Copyright © 2006, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

int    DeinitReason = 0; // определяем переменную на глобальном уровне
bool firststart = true;
int init ()
{
}

int deinit()
  {
//----
   DeinitReason = UninitializeReason( );
   firststart = true;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
  if ( firststart )
  {
      Print( "DeinitReason ", DeinitReason, " ", Period( ) );
      firststart = false;
  }
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
 

I wondered how to write a function. The idea is that when N candles roll back in a certain direction, it should calculate the number of candles. BUT. For this there are several conditions, or more precisely will be a list of them over time.

Let's say the trend, for example, is directed downwards,... ...a pullback goes up. I want, if 5 candlesticks went up, and each candlestick was bullish, and, for example, the size of each candlestick was larger thani_sizeOfCurrBar, and some other conditions, then output the number of continuously bullish bars, and return some result from the function.

What is the best way to write it? At the moment I've started to write, and I understand that I should run it through a loop by bars, and somehow I should limit the view in the depth of history not by a fixed number of bars, but by bars, with those parameters, which we are interested in by conditions.

Here's what I have, got:

int LastCandlesType()
{
   bool up,    // Свеча направлена вверх
        dn;    // Свеча направлена вниз
   int cnt;    // Счётчик идущих друг за другом бычьих свечей
   
   for (int i=Bars-1; i>=Bars-6; i--)
   {
      if ((Close[i] - Open[i]) <= i_sizeOfCurrBar * Point)    // Пропускаем все бары, размера меньше заданного внешним параметром.
          continue;
      
      if (Close[i] > Open[i])                               // 
          up = true;

      cnt++;
   }
   if (cnt == 5)
       return (BULL_CANDLES);
}

At the moment we are just going from the penultimate bar to the bar with index 6, i.e. 5 bars in a row. But I want my Expert Advisor to search only for the bars that are bullish and not for all of them in a row. How to implement it adequately?

I have sort of correctly filtered by size.

And when everything is already written, the counter will calculate the number of continuous bars with nht,etvsvb parameters and if there are enough of such bars, some value of the function will be returned.

 
Mislaid:

You have been told how to do it. It is in deinit() that we find out the reason for the last deinitialisation and pass it through a variable declared globally. In init() you check the value of the variable and find out the reason for the last deinitialisation. It works!!!

PS Apparently not everyone realises that init()/deinit() do not change the values of globally declared variables unless explicitly stated in statements within functions.

Actually, it's better not to close orders in init(). There MarketInfo() often does not work as expected.

And where do you see me closing positions in init()? I asked one question about deletion request, but where I delete them - I didn't say that, much less ask. Why do you think so?
Reason: