Tutorial - Code complete EA for MetaTrader 5 in 20 Minutes! - Chart display seems out of sync

 

Hi

As per my previous post on 'Invalid Stops' I'm in the process of learning how to code EAs. I do have some transferable skills as I used to code in Borland C back in 1990, so some aspects of this process are familiar, but coding for this environment is obvioulsy very new and I hit problems with 'invalid stops' with the first tutorial by Dillon Gech that I followed. A member here suggested I start with the tutorials listed on the forum by Trustfultrading, so that is what I have done.

However, I have problems with the results of these tutorials too. When I run the code it seems the EA is just not making trades when I would expect.

For example, the first truthfulltrading tutorial should simply open a trade at 10:00am and close at 12:00, but if you look at my screen grab that is attached, the graphical display is showing the time as 9:51 and there is a buy trade sitting in the 'middle of nowhere' at 9:51? The second attached file shows the end, again seemingly 'early'. 

The vertical positioning of the blue arrow would seem to be down to the difference between the bid and ask price, but when I watch the tutorial his chart seems to show the buy being on the price line? 

Many thanks for any help given.

Here is the code:

#include <Trade\Trade.mqh>
//+------------------------------------------------------------------+
//|Global variables and inputs                                  |
//+------------------------------------------------------------------+
input   int   openHour;
input   int   closeHour;
bool    isTradeOpen   =false;
CTrade  trade;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  //get current time
  MqlDateTime timeNow;
  TimeToStruct(TimeCurrent(),timeNow);

  //check for trade open
  if(openHour==timeNow.hour && !isTradeOpen){

    trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,0.01,SymbolInfoDouble(_Symbol,SYMBOL_ASK),0,0);
    //set flag
    isTradeOpen=true;
  
  }
//check for trade close
  if(closeHour==timeNow.hour && isTradeOpen){

    trade.PositionClose(_Symbol);
    //set flag
    isTradeOpen=false;
  
  }


  }
//+------------------------------------------------------------------+