how to differentiate "OnInit()" caused by timeframe change from "Indicator Attached" ?

 

I have a certain block of code in OnInit() function which I only want to execute once everytime the indicator is attached to the chart,

but I dont want it to be executed when the timeframe is changed , so it there a way to accomplish this ?

 
  1. The indicator is removed and reloaded on TF change.
  2. Perhaps you should read the manual. UninitializeReason
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

 

Remember the uninit reason in a global variable and query it during OnInit().

https://docs.mql4.com/basis/function/events#ondeinit

https://docs.mql4.com/constants/namedconstants/uninit

Event Handling Functions - Functions - Language Basics - MQL4 Reference
Event Handling Functions - Functions - Language Basics - MQL4 Reference
  • docs.mql4.com
Event Handling Functions - Functions - Language Basics - MQL4 Reference
Reason: