A problem about initialization of EA.

 

Now I have made an EA which is attached into USDJPY's chart whose timeframe is 5 minutes; after 10 minutes,  I changed time frame to 15 minutes from 5 minutes; I find that the EA is initialized again, which I think is EA's principle.

but is there any method to avoid this second-initialization? I never want EA to be initialized again when i viewed other time-frame chart at EA running-time, shall I? 

 

In init() you can check reason of uninitialization and do only partial initialization if init() is run due to change of chart (i.e. REASON_CHARTCHANGE). More doc here. Unfortunately, REASON_CHARTCHANGE covers both symbol and timeframe change so you will need to for example set a global var in the first init() run to know in the second init() run if the initialization is done after timeframe change or symbol change.

-Jozef 

 
Each time you change TF/pair etc. you get a deinit/init cycle. But the EA is loaded only once, so common (globally declared) variables and static's are not reset.
bool isFirst = true;
int init(){
   if(isFirst){ isFirst = false; 
     // Do one time initialization
   }
   // Do every time initialization
}
 
Reinitialization is very useful thing. However you can use the EA on one chart and open another one of the same currency for your review.
 

Thanks all.

I got it.