EA ERROR: "Failed prices for 0.00 [Invalid Request]" MT5

 

Hi,

Im trying to make a simple ea

Moving average crossover.

It wants to make trades on the backtest but it cant because ive screwed up somewhere

here is complete code [MT5]

//+------------------------------------------------------------------+
//|                                          Two Moving Average Crossover EA.mq4 |
//|                                                           Reilly |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Reilly"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

//////////////////////////////////////////////////////////////////////
//INPUT//
//////////////////////////////////////////////////////////////////////
extern double takeprofit=0.005;
extern double stoploss=0.0025;
extern int fastma=5;
extern int fastmashift=0;
//^ ma shift is offset of linegraph on chart
extern int fastmamethod=0;
//^ma method is simple/exponential moving average etc
extern int slowma=21;
extern int slowmashift=0;
extern int slowmamethod=0;
extern double lotsize=0.01;
extern int maxopentrades=3;
extern int magicnumber = 1337;

#include <IsNewBar.mqh>
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()

  {
//---
/*
   double ticksize = MarketInfo(Symbol(),MODE_TICKSIZE);
   if (ticksize = 0.00001 || 0.001)
   double pips = (ticksize*10);
   else pips = ticksize;
   */

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
if(OrdersTotal()>=maxopentrades) return;

MqlTradeRequest req={0};MqlTradeResult  res={0};


double previousfast = iMA(NULL,0,fastma,fastmashift,MODE_SMA,PRICE_CLOSE);
double currentfast = iMA(NULL,0,fastma,fastmashift,MODE_SMA,PRICE_CLOSE);
//^(NULL=moving average on the current currency pair. 0=the current timeframe. fastma= the moving average period.
double previousslow = iMA(NULL,0,slowma,slowmashift,MODE_SMA,PRICE_CLOSE);
double currentslow = iMA(NULL,0,slowma,slowmashift,MODE_SMA,PRICE_CLOSE);



//----
   double iClose1[1],iClose2[1];
//---- declaration of static variables
   static bool Recount1=true,Recount2=true;
   static CIsNewBar NB1,NB2;
//+----------------------------------------------+
//| Detecting market entry signals               |
//+----------------------------------------------+
   if(NB1.IsNewBar(Symbol(),PERIOD_D1) || Recount1) // checking for a new bar
     {
      Recount1=false;
      //--- copy newly appeared data in the arrays
      if(CopyClose(Symbol(),PERIOD_D1,1,1,iClose1)<=0) {Recount1=true; return;}
      /* Here is a trading signal 1 receiving block code */
     }

   if(NB2.IsNewBar(Symbol(),PERIOD_H4) || Recount2) // checking for a new bar
     {
      Recount2=false;
      //--- copy newly appeared data in the arrays
      if(CopyClose(Symbol(),PERIOD_H4,1,1,iClose2)<=0) {Recount2=true; return;}
      /* Here is a trading signal 2 receiving block code */
     }
  

    


if(previousfast<previousslow && currentfast>currentslow)

{



    req.action      = TRADE_ACTION_DEAL;
    req.symbol      = _Symbol;
    req.magic       = magicnumber;
    req.volume      = lotsize;
    req.type        = ORDER_TYPE_BUY;
    req.price       = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
    req.sl          = stoploss;
    req.tp          = takeprofit;
    req.deviation   = 10;
    req.comment     = "Buy using OrderSendAsync()";
    
    if(!OrderSendAsync(req,res))
      {
       Print(__FUNCTION__,": error ",GetLastError(),", retcode = ",res.retcode);
      }
    else PlaySound("ok.wav.");                         // MagicNumber of the order
   }
if(OrderSend(req,res))
Print(GetLastError());


  



  if (previousfast>previousslow && currentfast<currentslow)


{
      
    req.action      = TRADE_ACTION_DEAL;
    req.symbol      = _Symbol;
    req.magic       = magicnumber;
    req.volume      = lotsize;
    req.type        = ORDER_TYPE_SELL;
    req.price       = SymbolInfoDouble(_Symbol,SYMBOL_BID);
    req.sl          = stoploss;
    req.tp          = takeprofit;
    req.deviation   = 10;
    req.comment     = "Sell using OrderSendAsync()";
    
    if(!OrderSendAsync(req,res))
      {
       Print(__FUNCTION__,": error ",GetLastError(),", retcode = ",res.retcode);
      }
    else PlaySound("ok.wav.");                        // MagicNumber of the order
   }

  
      
if (OrderSend(req,res))
Print(GetLastError());
}
 

Please read the documentation : iMA()

Your SL/TP should be calculated from current market price.

 
Alain Verleyen:

Please read the documentation : iMA()

Your SL/TP should be calculated from current market price.

please suggest link to learn how to calculate?
 
Alain Verleyen:

Please read the documentation : iMA()

Your SL/TP should be calculated from current market price.

I looked at the iMA() link
do I really need to put all that in my code to make it work?

It seemed to work fine with that small 1 line iMA code that I used for MT4 

 
ReillyFox:

I looked at the iMA() link
do I really need to put all that in my code to make it work?

It seemed to work fine with that small 1 line iMA code that I used for MT4 

I was preparing a long answer, but I stopped. I definitely don't know what to answer to such message.
 

Alain Verleyen:
I was preparing a long answer, but I stopped. I definitely don't know what to answer to such message.

 

 

A simple yes or no atleast???