Trendline Problem

 

When placing my trendline EA, no trendlines appear on my chart. When back testing they appear, why is that? 

If you know the problem to this please assist. 

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int a)
 {
  ObjectDelete(0,"TLUpper");
  
  ObjectDelete(0,"TLLower");
   
 }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int FindPeak(int Mode,int Count, int StartBar)
 {
  if(Mode!=MODE_HIGH&&Mode!=MODE_LOW)return(-1);
  int CurrentBar=StartBar;
  int FoundBar=FindNextPeak(Mode,Count*2+1,CurrentBar-Count);
  if(FoundBar!=CurrentBar)
  {
   CurrentBar=FindNextPeak(Mode,Count,CurrentBar+1);
   FoundBar=FindNextPeak(Mode,Count*2+1,CurrentBar-Count);
  }
  
  return(CurrentBar);
 }

int FindNextPeak(int Mode,int Count, int StartBar)
 {
  if(StartBar<0)
  {
   Count+=StartBar;
   StartBar=0;
  }
  
  return(Mode==MODE_HIGH)?
  iHighest(Symbol(),0,(ENUM_SERIESMODE)Mode,Count,Start):
  iLowest(Symbol(),0,(ENUM_SERIESMODE)Mode,Count,Start);
 }
 
void OnTick()
 {
  static datetime BarCurrent=WRONG_VALUE;
  datetime BarPrevious=BarCurrent;
  BarCurrent=iTime(Symbol(),0,0);
  bool NewBarEvent=(BarCurrent!=BarPrevious);
  
  int Number=20;
  int Bar1;
  int Bar2;
  
  if(BarCurrent)
  {
   Bar1=FindPeak(MODE_HIGH,Number,0);
   Bar2=FindPeak(MODE_HIGH,Number,Bar1+1);
   
   Bar1=FindPeak(MODE_LOW,Number,0);
   Bar2=FindPeak(MODE_LOW,Number,Bar1+1);
   
   if(BarPrevious==WRONG_VALUE)
   {
    ObjectDelete(0,"TLUpper");
    ObjectCreate(0,"TLUpper",OBJ_TREND,0,Time[Bar2],High[Bar2],Time[Bar1],High[Bar1]);
    ObjectSetInteger(0,"TLUpper",OBJPROP_COLOR,clrRed);
    ObjectSetInteger(0,"TLUpper",OBJPROP_STYLE,STYLE_DASH);
    ObjectSetInteger(0,"TLUpper",OBJPROP_RAY_RIGHT,true);
  
    ObjectDelete(0,"TLLower");
    ObjectCreate(0,"TLLower",OBJ_TREND,0,Time[Bar2],Low[Bar2],Time[Bar1],Low[Bar1]);
    ObjectSetInteger(0,"TLLower",OBJPROP_COLOR,clrGreen);
    ObjectSetInteger(0,"TLLower",OBJPROP_STYLE,STYLE_DASH);
    ObjectSetInteger(0,"TLLower",OBJPROP_RAY_RIGHT,true);
   }
   else
   {
    ObjectMove(0,"TLUpper",0,Time[Bar2],High[Bar2]);
    ObjectMove(0,"TLUpper",0,Time[Bar1],High[Bar1]);
    
    ObjectMove(0,"TLLower",0,Time[Bar2],Low[Bar2]);
    ObjectMove(0,"TLLower",0,Time[Bar1],Low[Bar1]);
   }
  }
  
 }
 
  1.    BarCurrent=iTime(Symbol(),0,0);
    ⋮
      if(BarCurrent)

    When will that ever be false?

  2. Scalper8: no trendlines appear on my chart. 

    Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

  3. int FindPeak(int Mode,int Count, int StartBar){
      if(Mode!=MODE_HIGH&&Mode!=MODE_LOW)return(-1);
      ⋮
    int FindNextPeak(int Mode,int Count, int StartBar){
      ⋮
      return(Mode==MODE_HIGH)?
      iHighest(Symbol(),0,(ENUM_SERIESMODE)Mode,Count,Start):
    Since iHighest/iLowest requires a ENUM_SERIESMODE, your two functions also require it. Make Mode that, and drop the cast.
 
Scalper8:

When placing my trendline EA, no trendlines appear on my chart. When back testing they appear, why is that? 

If you know the problem to this please assist. 

Place all the OnTick code in a function called check();

Place #property strict on top of your code

Place check() in OnInit function

Place check() in OnTick function

Your problem was -likely- not calling it on the start of the program and there are no tick events at this moment 

Also as William pointed out , change if(BarCurrent) to if(NewBarEvent)
 
Lorentzos Roussos #: Place check() in OnInit function

don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

  1. Terminal starts.
  2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
  3. OnInit is called.
  4. For indicators OnCalculate is called with any existing history.
  5. Human may have to enter password, connection to server begins.
  6. New history is received, OnCalculate called again.
  7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
 
William Roeder #:

don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

  1. Terminal starts.
  2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
  3. OnInit is called.
  4. For indicators OnCalculate is called with any existing history.
  5. Human may have to enter password, connection to server begins.
  6. New history is received, OnCalculate called again.
  7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

Yes sir

 
William Roeder #:
  1. When will that ever be false?

  2. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

  3. Since iHighest/iLowest requires a ENUM_SERIESMODE, your two functions also require it. Make Mode that, and drop the cast.

Tried debugging out the code, The points you detailed out have not solved my problem especially the "appearing" part. 

Reason: