Hi All, I want to use my EA on different timeframes without deleting my objects. When I change timeframes my objects don't appear, why is that? if you know the solution please help.
Hello , you were deleting your lines on deinit (when the program exits and restarts) , added a block to not do so if the tf or symbol changes (the later may cause issues if you change symbol).
Added a parameter for which timeframe to monitor , you should also monitor the timeframe of which bar 1's high and low create the lines for new bars .
Also added provisions for timeframe change from the inputs .
#property strict input ENUM_TIMEFRAMES TargetTF=PERIOD_H4;//Target Timeframe //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ int OnInit(){ return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { //the reason of deinitialization https://docs.mql4.com/constants/namedconstants/uninit //reason 3 means chart or period changed , so , if reason is 3 we do not delete objects if(reason!=3){ ObjectDelete(0,"Support"); ObjectDelete(0,"Resistance"); } } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { static datetime BarCurrent=WRONG_VALUE; static ENUM_TIMEFRAMES LastTimeframe=NULL; datetime BarPrevious=BarCurrent; BarCurrent=iTime(Symbol(),TargetTF,0); bool NewBarEvent=(BarCurrent!=BarPrevious); if(LastTimeframe!=TargetTF){BarPrevious=WRONG_VALUE;LastTimeframe=TargetTF;} if(BarPrevious==WRONG_VALUE) { ObjectCreate(0,"Support",OBJ_HLINE,0,TimeCurrent(),iLow(Symbol(),TargetTF,1)); ObjectSetInteger(0,"Support",OBJPROP_COLOR,clrGreen); ObjectCreate(0,"Resistance",OBJ_HLINE,0,TimeCurrent(),iHigh(Symbol(),TargetTF,1)); ObjectSetInteger(0,"Resistance",OBJPROP_COLOR,clrRed); } else if(NewBarEvent) { ObjectMove(0,"Support",0,TimeCurrent(),iLow(Symbol(),TargetTF,1)); ObjectMove(0,"Support",1,0,iLow(Symbol(),TargetTF,1)); ObjectMove(0,"Resistance",0,TimeCurrent(),iHigh(Symbol(),TargetTF,1)); ObjectMove(0,"Resistance",1,0,iHigh(Symbol(),TargetTF,1)); } }
And this is the page with the deinitiazliation reasons : These are the codes the const int reason sends to the OnDeinit function so you can now why the program exits.
- docs.mql4.com
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi All, I want to use my EA on different timeframes without deleting my objects. When I change timeframes my objects don't appear, why is that? if you know the solution please help.