Expert Advisor problem

 
//+------------------------------------------------------------------+
//|                                                        metro.mq5 |
//|                                           Copyright 2019, Swarthy|
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, Swarthy"
#property link      "https://www.mql5.com"
#property version   "1.00"


input uint PeriodWPR=18;
input int StepSizeFast=34;
input int StepSizeSlow=40;
input int Shift=0;
input int AtrPeriod=14;
input int AtrMultipliersl=1;
input int AtrMultipliertp=1;
input double Lot=0.1;

int EA_Magic=437;
int wpr_handle;                  //handle for wpr metro
int atr_handle;                  //handle for atr
double wpr_fast[];               //array for storing wpr fast values
double wpr_slow[];               //array for storing wpr slow values
double atr_value[];              //array for storing atr values
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   wpr_handle=iCustom(Symbol(),0,"Examples\\metro_wpr", PeriodWPR,StepSizeFast,StepSizeSlow,Shift);
   atr_handle=iATR(Symbol(),0,AtrPeriod);
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   IndicatorRelease(wpr_handle);
   IndicatorRelease(atr_handle);
  }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   MqlTick current_price;     // To be used for getting recent/latest price quotes
   MqlTradeRequest mrequest;  // To be used for sending my trade requests
   MqlTradeResult mresult;    // To be used to get my trade results
   MqlRates mrate[];          // To be used to store the prices, volumes and spread of each bar
   ZeroMemory(mrequest);      // Initialization of mrequest structure

//Array as series
   ArraySetAsSeries(wpr_fast,true);
   ArraySetAsSeries(wpr_slow,true);
   ArraySetAsSeries(atr_value,true);

//Copy last 2 values of my indicators to arrays
   CopyBuffer(wpr_handle,1,0,2,wpr_fast);
   CopyBuffer(wpr_handle,2,0,2,wpr_slow);
   CopyBuffer(atr_handle,0,0,1,atr_value);
   
   if(!SymbolInfoTick(_Symbol,current_price))
     {
      Alert("Error getting the latest price quote - error:",GetLastError(),"!!");
      return;
     }
   if (wpr_fast[0]>wpr_slow[0] && wpr_fast[1]<=wpr_slow[1])
     {
         mrequest.action = TRADE_ACTION_DEAL;                                // immediate order execution
         mrequest.price = current_price.ask;                               // latest ask price
         mrequest.sl = (current_price.ask - atr_value[0]*AtrMultipliersl);   // Stop Loss
         mrequest.tp = (current_price.ask + atr_value[0]*AtrMultipliertp);   // Take Profit
         mrequest.symbol = _Symbol;                                          // currency pair
         mrequest.volume = Lot;                                              // number of lots to trade
         mrequest.magic = EA_Magic;                                          // Order Magic Number
         mrequest.type = ORDER_TYPE_BUY;                                     // Buy Order
         //mrequest.type_filling = ORDER_FILLING_FOK;                          // Order execution type
         //--- send order
         OrderSend(mrequest,mresult);
         // get the result code
         // write the server reply to log  
  }
  }
//+------------------------------------------------------------------+
 
The code doesn't buy at A and buys one candle early at B
Files:
 
To solve your problem yourself MQ has given us the Debugger - now with hist. Quotes. Fix the settings under Tools => Options => Debugger. Then define some breakpoints (F9) and start the debugger ....