Request for help on an EA please!

 

I am sorry to post here, but I am a beginner and I am really stuck. My EA is only 164 lines, so can someone kind please help with me? It can compile without error but it can not run on the backtester of Metatrader4.

//+------------------------------------------------------------------+
//|                                               Tim Lo trading.mq4 |
//|                                              Copyright 2015, Tim |
//|                                             yau1232b@hotmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, Tim"
#property link      "yau1232b@hotmail.com"
#property version   "1.00"
#property description "Please e-mail me if you have any enquiries"
#property strict
extern int MagicNumber=10001;
extern double Lots =0.1;
extern double StopLoss=20;
extern double TakeProfit=0;
extern int TrailingStop=0;
extern int Slippage=3;
extern int highnum = 0;
extern int lownum = 0;

extern bool up = false;
extern bool down = false;


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

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

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   int bidprices[20];
   int askprices[20];
   int prices[20];
   
   for(int i = 0; i < 20; i++){
   
   bidprices[18-i] = bidprices[19-i];
   
   }
   
   for(int i = 0; i < 20; i++){
   
   askprices[18-i] = askprices[19-i];
   
   }
   
   bidprices[0] = Bid;
   askprices[0] = Ask;
   
   for(int i = 0; i < 20; i++){
   
   prices[i] = (bidprices[i] + askprices[i])/2;
   
   }
   
   for(int i = 0; i <20; i++){
   
   highnum = 0;
   lownum = 0;
   
   if(prices[i] > prices[i+1]){
   highnum++;
   }
   
   if(prices[i] < prices[i+1]){
   lownum++;
   }
   
   if(highnum>15){
   for (int g = OrdersTotal()-1; g >= 0; g--)
   {
      if ( OrderSelect(g, SELECT_BY_POS) )
      {  
         bool closeorder = false;
         if (OrderType() == OP_SELL)
           closeorder = OrderClose(g,1,Ask,3,clrGreen);
           if( closeorder = true )
           {Print("Order closed");
           }
           else{
           Print("Order close filed, error # ", GetLastError() );
           }
      }
   }
   if(up){
   }
   else{
   int TicketNumberbuy = 0;
   TicketNumberbuy = OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"Open Buy",MagicNumber,0,clrLimeGreen);
   Print("Buy Order");
   up = true;
   down = false;
   if( TicketNumberbuy > 0 )
   {
   Print("Order placed # ", TicketNumberbuy);
   }
else
   {
   Print("Order Send failed, error # ", GetLastError() );
   
   }
   }
   }
   
   if(lownum>15){
     for (int g = OrdersTotal()-1; g >= 0; g--)
   {
      if ( OrderSelect(g, SELECT_BY_POS) )
      {
         bool closeorder = false; 
         if (OrderType() == OP_BUY)
           closeorder = OrderClose(g,1,Bid,3,clrRed);
           if( closeorder = true )
           {Print("Order closed");
           }
           else{
           Print("Order close filed, error # ", GetLastError() );
           }
  
      }
   }
   if(down){
   }
   else{
   int TicketNumbersell = 0;
   TicketNumbersell = OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"Open Sell",MagicNumber,0,clrRed);
   Print("Sell Order");
   up = false;
   down = true;
   if( TicketNumbersell > 0 )
   {
   Print("Order placed # ", TicketNumbersell);
   }
else
   {
   Print("Order Send failed, error # ", GetLastError() );
   
   }   
   }
   }
   
    
   }

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

I only read your code as far as this

   int bidprices[20];
   int askprices[20];
   int prices[20];
   
   for(int i = 0; i < 20; i++){
   
   bidprices[18-i] = bidprices[19-i];
   
   }

 When i reaches 19, you are trying to access

 bidprices[18-19] 

which is

bidprices[-1]

which doesn't exist so you have an array out of range error.

If you checked the journal. you should see this  

 
GumRai:

I only read your code as far as this

 When i reaches 19, you are trying to access

 bidprices[18-19] 

which is

bidprices[-1]

which doesn't exist so you have an array out of range error.

If you checked the journal. you should see this  

Thank you for your help. However, the journal did not record that as an error and after I changed it, it still does not work. What should I do?
 
What have you changed it to?
 
  1. simplify and correct
    // for(int i = 0; i < 20; i++){
    / bidprices[18-i] = bidprices[19-i];
    
    for(int i=19; i > 0; --i)
       bidPrices[i] = bidprices[i-1];

  2.    TicketNumbersell = OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"Open Sell",MagicNumber,0,clrRed);
       Print("Sell Order");
    Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  3. if(prices[i] > prices[i+1]){
       highnum++;
       }
       
       if(prices[i] < prices[i+1]){
       lownum++;
       }
    ////////////// Shouldn't the counting loop be finished here?   
       if(highnum>15){

  4. GumRai: bidprices[-1]

    which doesn't exist so you have an array out of range error.

    If you checked the journal. you should see this  

    Use #property strict and you would have seen the error.
  5. Add print statements before and inside your if statements printing variable values and find out why.
 
WHRoeder:
  1. simplify and correct

  2. Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

  3. GumRai: bidprices[-1]

    which doesn't exist so you have an array out of range error.

    If you checked the journal. you should see this  

    Use #property strict and you would have seen the error.
  4. Add print statements before and inside your if statements printing variable values and find out why.
My dear friend, I really appreciate your help. I do not quite understand what you mean by 2 and 3 though, can you please explain a bit further?
 
  1. What part of "Check your return codes" is unclear? Where is you check if OrderSend succeeded or failed?
  2. Shouldn't the counting loop be finished here?
    
    Shouldn't you finish counting before you start testing the counts? currently you check inside the for loop every iteration.
Reason: