EA doesn't open any order

 

Hello,

I'm novice about MQL4 code and trading on line.

I haven't any long experience as programmer.

I'm studying since many months on books and youtube webinars.

I'm novice about this forum too. I hope to write my post in the right way.

I wrote an expert that I report below.

MT4 suite says that there are any error on my code but if I try to run the EA on the backtest, it doesn't open any position.

I attach a copy of the file on this post.

Anybody can give me any suggestion ?

Thank in advance for your cooperation.


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+


int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+


void OnTick()
  {
  
  if(OrdersTotal() == 0){ 
  Apri_BUY ();
  Chiudi_BUY ();
  Comment ("prezzo chiusura ultima candela " , Prezzo_chiusura_BUY_recente, "\n SMA 10 Periodi: ", SMA10_corrente);
  
  }
  
  }
  
   //++++++++++++++++
  int Ticket=0;
  double media_veloce_vecchia = iMA(Symbol(),PERIOD_CURRENT ,10, 0,MODE_SMA, PRICE_CLOSE,2); //EMA 10 periodi su 2 candele fa;
  double media_veloce_nuova = iMA(Symbol(),PERIOD_CURRENT ,10, 0,MODE_SMA, PRICE_CLOSE,1); //EMA 10 periodi su 1 candele fa;
  double media_lenta_vecchia = iMA(Symbol(),PERIOD_CURRENT ,50, 0,MODE_SMA, PRICE_CLOSE,2); //EMA 50 periodi su 2 candele fa;
  double media_lenta_nuova = iMA(Symbol(),PERIOD_CURRENT ,50, 0,MODE_SMA, PRICE_CLOSE,1); //EMA 50 periodi su 1 candele fa;
  int val_index_recente=iClose (Symbol(),PERIOD_CURRENT,-1); 
  int val_index_passato=iClose (Symbol(),PERIOD_CURRENT,-2);
  double  Prezzo_chiusura_BUY_recente = Close [val_index_recente]; 
  double  Prezzo_chiusura_BUY_passato = Close [val_index_recente]; 
  double SMA10_corrente = iMA(Symbol(),PERIOD_CURRENT ,10, 0,MODE_SMA, PRICE_CLOSE,0);
  
  //+++++++++++++++
  
  void Apri_BUY () {
 
   if ((media_veloce_vecchia < media_lenta_vecchia)&& (media_veloce_nuova > media_lenta_nuova)) 
   { 
   OrderSelect(Ticket,SELECT_BY_TICKET, MODE_TRADES);
   if((OrderCloseTime() == 0) && (OrderType() == OP_SELL))
   {
   OrderClose(OrderTicket (),OrderLots(),Ask,1,clrMagenta);
   Ticket= OrderSend(Symbol(),OP_BUY, 1, Ask, 0, NULL, NULL, NULL, 123456,0, clrGreen);
   }
   else if (OrderCloseTime() != 0) {
   Ticket= OrderSend(Symbol(),OP_BUY, 1, Ask, 0, NULL, NULL, NULL, 123456,0, clrGreen);}
  
   } 
   
  }
  
  void Chiudi_BUY () {
  if ((Prezzo_chiusura_BUY_recente < SMA10_corrente) && 
  (Prezzo_chiusura_BUY_passato >= SMA10_corrente)); { //se il prezzo incrocia la media veloce corrente
   OrderSelect(Ticket,SELECT_BY_TICKET, MODE_TRADES);
   if((OrderCloseTime() == 0) && (OrderType() == OP_BUY))
   {
   OrderClose(OrderTicket (),OrderLots(),Bid,1,clrBeige);
   }
  
   }
   }
//+------------------------------------------------------------------+
 
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I will move your topic to the MQL4 and Metatrader 4 section.
 
Gino Sky:

Hello,

I'm novice about MQL4 code and trading on line.

I haven't any long experience as programmer.

I'm studying since many months on books and youtube webinars.

I'm novice about this forum too. I hope to write my post in the right way.

I wrote an expert that I report below.

MT4 suite says that there are any error on my code but if I try to run the EA on the backtest, it doesn't open any position.

I attach a copy of the file on this post.

Anybody can give me any suggestion ?

Thank in advance for your cooperation.


There is a lot of error in your code and i am not sure about what you want to do.

You cannot open order passing by orderselect, because if there is no order, there is no ticket for SELECT BY TICKET, or program will not enter in the loop needed for orderselect for SELECT BY POSITION

The variables have to be declared on a global scope to be used in another fonction

You cannot close an order with if( orderstotal == 0), because the program will not enter in the bracket when there is an order, ie when orderstotal() == 1

The index for the ima cannot be -1 or -2, the current bar is the 0 bar, the previous 1 and so on


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
  double media_veloce_2;
  double media_veloce_1;
  double media_lenta_2; //EMA 50 periodi su 2 candele fa;
  double media_lenta_1; //EMA 50 periodi su 1 candele fa;
    int Ticket=0;


//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+

void OnTick()
  {
  
  if(OrdersTotal() == 0){ 
  Apri_BUY ();
 // Comment ("prezzo chiusura ultima candela " , Prezzo_chiusura_BUY_recente, "\n SMA 10 Periodi: ", SMA10_corrente);
  }

 if(OrdersTotal() == 1) Chiudi_BUY ();

   media_veloce_2 = iMA(Symbol(),PERIOD_CURRENT ,10, 0,MODE_SMA, PRICE_CLOSE,2); //EMA 10 periodi su 2 candele fa;
   media_veloce_1 = iMA(Symbol(),PERIOD_CURRENT ,10, 0,MODE_SMA, PRICE_CLOSE,1); //EMA 10 periodi su 1 candele fa;
   media_lenta_2 = iMA(Symbol(),PERIOD_CURRENT ,50, 0,MODE_SMA, PRICE_CLOSE,2); //EMA 50 periodi su 2 candele fa;
   media_lenta_1 = iMA(Symbol(),PERIOD_CURRENT ,50, 0,MODE_SMA, PRICE_CLOSE,1); //EMA 50 periodi su 1 candele fa;
 //int val_index_recente=iClose (Symbol(),PERIOD_CURRENT,0); 
 // int val_index_passato=iClose (Symbol(),PERIOD_CURRENT,1);
 // double  Prezzo_chiusura_BUY_recente = Close [val_index_recente]; 
 // double  Prezzo_chiusura_BUY_passato = Close [val_index_recente]; 
 // double SMA10_corrente = iMA(Symbol(),PERIOD_CURRENT ,10, 0,MODE_SMA, PRICE_CLOSE,0);

  }
  
   //++++++++++++++++
  //+++++++++++++++
  
  void Apri_BUY () 
    {
   if ((media_veloce_2 < media_lenta_2)&&
        (media_veloce_1 > media_lenta_1)) 
   { 
       Print("Cross ma ok");

   /* int total = OrdersTotal();
    for( int i=0;i<total;i++)
       {
       if( ! OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue ;
    Print("OrderSelect ok");
    if((OrderCloseTime() == 0) && (OrderType() == OP_SELL))
   {
   OrderClose(OrderTicket (),OrderLots(),OrderClosePrice(), 3, clrMagenta);
   // Ticket= OrderSend(Symbol(),OP_BUY, 1, Ask, 0, NULL, NULL, NULL, 123456,0, clrGreen);
   }
   else// if (OrderCloseTime() != 0) 
   { */
   Ticket= OrderSend(Symbol(),OP_BUY, 1, Ask, 3, 0.0,0.0, "", 123456,0, clrGreen);
    }
    }
  
  void Chiudi_BUY () {
   if ((media_veloce_2 > media_lenta_2)&&
        (media_veloce_1 < media_lenta_1)) 
  { //se il prezzo incrocia la media veloce corrente
  if( OrderSelect(Ticket,SELECT_BY_TICKET, MODE_TRADES) )
  {
   if((OrderCloseTime() == 0) && (OrderType() == OP_BUY))
   {
   OrderClose(OrderTicket (),OrderLots(),Bid,3,clrBeige);
   }
  }
   }
   }
//+
Reason: