Change timeframes using objects?

 

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. 

#property strict
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+

void OnDeinit(const int reason)
 {
  ObjectDelete(0,"Support");
  
  ObjectDelete(0,"Resistance");
  
 }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
 {
  static datetime BarCurrent=WRONG_VALUE;
  datetime BarPrevious=BarCurrent;
  BarCurrent=iTime(Symbol(),0,0);
  bool NewBarEvent=(BarCurrent!=BarPrevious);
  
  if(BarPrevious==WRONG_VALUE)
  {
   ObjectCreate(0,"Support",OBJ_HLINE,0,TimeCurrent(),iLow(Symbol(),PERIOD_H4,1));
   ObjectSetInteger(0,"Support",OBJPROP_COLOR,clrGreen);
   
   ObjectCreate(0,"Resistance",OBJ_HLINE,0,TimeCurrent(),iHigh(Symbol(),PERIOD_H4,1));
   ObjectSetInteger(0,"Resistance",OBJPROP_COLOR,clrRed);
  }
  else
  {
   ObjectMove(0,"Support",0,TimeCurrent(),iLow(Symbol(),PERIOD_H4,1));
   ObjectMove(0,"Support",1,0,iLow(Symbol(),PERIOD_H4,1));
   
   ObjectMove(0,"Resistance",0,TimeCurrent(),iHigh(Symbol(),PERIOD_H4,1));
   ObjectMove(0,"Resistance",1,0,iHigh(Symbol(),PERIOD_H4,1));
  }
   
 }

 
Scalper8:

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.

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

Uninitialization Reason Codes - Named Constants - Constants, Enumerations and Structures - MQL4 Reference
Uninitialization Reason Codes - Named Constants - Constants, Enumerations and Structures - MQL4 Reference
  • docs.mql4.com
Uninitialization Reason Codes - Named Constants - Constants, Enumerations and Structures - MQL4 Reference
Reason: