Init() and DeInit() execution sequence - page 8

 

Option for solving problems when changing timeframes


If you just changed TF

1) It is probably not necessary to delete graphical objects.

2) Maybe it is not necessary to re-initialize some variables


static int ChanGeTF = 0;  // Признак смены ТФ , держим как статическую переменную в памяти эксперта
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   if( ChanGeTF == 0) // при первом вызове код в OnInit полностью обрабатывается  , но не отработает если просто менялся ТФ
     {
      // тут сформируем переменные для нашего эксперта которые отработают  при первом старте экперта
      // возможно сформируем графические объекты 
     }
    else
    {
      ChanGeTF=0;  
     // сюда попадет при смене ТФ 
     // а формировать ничего уже не будем ,  у нас все сформировано и мы не желаем делать инициализацию переменных
     // но если необходимо - что то обработаем в код OnInit 
    }

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   Print(__FUNCTION__,"_UninitReason = ",getUninitReasonText(_UninitReason));   // пишем в лог причину входа в DeInit - очень полезная информация
   if ( REASON_CHARTCHANGE != _UninitReason )
     {
      // сюда не попадаем , если произошла смена ТФ
      // удалим графические объекты - для полноценного завершения работы эксперта 
      // 
     }
    else
    {
      // сюда попадем если просто произошла смена ТФ 
      // сделаем то что нам интересно - но при этом - к примеру - не будем удалять с графика графические объекты
         ChanGeTF=1; 
    }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
string getUninitReasonText(int reasonCode)
  {
   string text="";
//--- 
   switch(reasonCode)
     {
      case REASON_ACCOUNT:
         text="Account was changed";break;
      case REASON_CHARTCHANGE:
         text="Symbol or timeframe was changed";
         break;
      case REASON_CHARTCLOSE:
         text="Chart was closed";break;
      case REASON_PARAMETERS:
         text="Input-parameter was changed";break;
      case REASON_RECOMPILE:
         text="Program "+__FILE__+" was recompiled";
         break;
      case REASON_REMOVE:
         text="Program "+__FILE__+" was removed from chart";break;
      case REASON_TEMPLATE:
         text="New template was applied to chart";break;
      default:text="Another reason";
     }
//--- 
   return text;
  }

 
Yuriy Zaytsev:

Option for solving problems when changing timeframes


If you just changed TF

1) It is probably not necessary to delete graphical objects.

2) Maybe it is not necessary to re-initialize some variables


The only issue is thatstaticdoes not work in the indicator, it is reset. It works in the Expert Advisor, but not in the indicator
.
 
Yuriy Zaytsev:

Option for solving problems when changing timeframes


If there was just a timeframe change

1) Maybe you should not delete graphical objects.

2) Maybe you should not re-initialize any variables



Your code will not work.

Once again:

You cannot control the results of Deinit in Inite, because Deinit may work after Inite is done.

 
Sergey Chalyshev:


Your code will not work.

Once again:

You cannot control the results of Deinit in Inite, because Deinit can be triggered after Inite has run.

You mean it won't work- in indicator? It works in Expert Advisors.
 
Yuriy Zaytsev:
You mean it won't work- in an indicator ? In experts it works.
There is no problem with Expert Advisors. This topic is just about indicators. Read it carefully.
 
Sergey Chalyshev:

How to process these codes of deinitialization in an indicator, what do we need these codes for? After all, there is no possibility to wait in the indicator, Sleep does not work.

Didn't you read what I wrote several times?

There is no way in the indicators. You can't do it in five from the very beginning. Because you download a completely new copy of the indicator with all its consequences.

 
Nikolai Semko:
There is no problem with experts. This topic is just about indicators. Read it carefully.

I read the first post.
It says, and I quote:
"Written Indicator or Co-Assessor"
 
Yuriy Zaytsev:

I read the first post.
It says, and I quote:
"Written by an indicator or co-author."
That's where the tip ends.
It's not always wise to limit yourself to reading the first post before writing something.
 
Nikolai Semko:
That is the end of the Expert Advisors.
It is not always wise to limit yourself to reading the first post before writing anything.

It is not quite correct to say what is reasonable and what is not.
I did a little run through the subject before writing it.

I pasted a code sample into the subject and now those who read the subject can see that this mechanism doesn't work in an indicator and that it works in an EA.

The author of the topic at least got an answer to one question, and with an example.
 
Yuriy Zaytsev:

Well, it's not really accurate to say what's reasonable and what's not.
I did a little research on the subject before writing.

Now those who read the subject can see that this mechanism does not work in an indicator and works in an EA.
Thanks for the code of course. It works fine with Expert Advisors because when we change TF, the variables are not reinitialized, while in indicators they are reinitialized. If you really want to help with advice, please "run" it again in less haste.
Reason: