Download MetaTrader 5
To add comments, please log in or register
Make screenshots in MetaTrader and manage them in the gallery!
marana77
16
marana77 2015.10.22 01:51 

Hi, I am currently backtesting a strategy and the problems are

1) orders are being placed at every tick

2) orders are only in one direction and usually at the beginning of the test even if the conditions are satisfied later on

 I am attaching the code for the EA along with the backtest report. I would really appreciate any help. Many thanks in advance

//-----------------------------------------------------------------1-
// External Variables (user defined)

      extern int Period_ROC_MA = 7;       // MA of ROC indicator
      extern int Period_MA_1 = 11;        // Period of fast HULL MA
      extern int Period_MA_2 = 17;        // Period of slow HULL MA
      
      extern double Lots = 0.1;           // Lot size to be traded
      extern double TP = 40;              // TP in points
      extern double SL = 40;              // SL in points
      
//Global Variables

      string Symb;                        // Security name
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   double
   HULL_MA_1_t,                           // Current value fast HULL MA
   HULL_MA_2_t,                           // Current value slow HULL MA
   ROC_t0,                                // Current ROC value shifted
   ROC_t1;                                // ROC value shifted by 1
   
   int
   Ticket,
   Total;                                 // Amount of orders in a window
   
   bool
   Opn_Buy = false,                       // Criterion for opening Buy
   Opn_Sell = false;                      // Criterion for opening sell
      
//----------------------------------------------------------------2-
// Orders processing

      Total = OrdersTotal();              // Amount of orders
      int ord=0;                    
      
      for(int i=0; i < Total; i++)        // Loop through orders
         {                                // Analysing orders:
          OrderSelect(i,SELECT_BY_POS==true);
          {
          if(OrderSymbol() == Symb)       // Found an order so 
          ord++;                          // increase counter by 1 
          if(ord > 0)                     // No more than one order
          return;  
          }
         }
         
//-----------------------------------------------------------------3-
// Trading criteria

   HULL_MA_1_t = iCustom(Symb,PERIOD_M5,"Downloads//hma",Period_MA_1,0,MODE_LWMA,5,0,0);
   HULL_MA_2_t = iCustom(Symb,PERIOD_M5,"Downloads//hma",Period_MA_2,0,MODE_LWMA,5,0,0);
   ROC_t0  = iCustom(Symb,PERIOD_M5,"ROC",Period_ROC_MA,false,0,0);
   ROC_t1  = iCustom(Symb,PERIOD_M5,"ROC",Period_ROC_MA,false,0,1);
   
   if((ROC_t1<0 && ROC_t0>0) && HULL_MA_1_t>HULL_MA_2_t)
      {
       Opn_Buy = true;                    // Criterion for opening buy
      }
   
   if((ROC_t1>0 && ROC_t0<0) && HULL_MA_2_t>HULL_MA_1_t)
      {
       Opn_Sell = true;                   // Criterion for opening sell
      }
      
//---------------------------------------------------------------4-
// Opening orders

   while(true)                            // Orders opening loop
      {
       if(Opn_Buy==true)                  // criterion for opening Buy   
         {                             
          RefreshRates();
          SL = 40*Point;                  // Calculating SL of opening Buy
          TP = 40*Point;                  // Calculating TP of opening Buy
          Alert("Attempt to open Buy. Waiting for response..");
          Ticket = OrderSend(Symb, OP_BUY, Lots, Ask, 2, NormalizeDouble(Bid - SL,Digits), NormalizeDouble(Bid+TP,Digits),NULL,0,0,clrBlue); // Opening buy
          
          if(Ticket > 0)
          {
           Alert("Opened order Buy ",Ticket);
           return;                        // Exit start
          }                               // Exit start
         }
         
       if(Opn_Sell==true)                 // criterion for opening sell
         {                                
          RefreshRates();  
          SL = 40*Point;                  // Calculating SL for opening Sell
          TP = 40*Point;                  // Calculating TP for opening Sell
          Alert("Attempt to open Sell. Waiting for response..");
          Ticket = OrderSend(Symb,OP_SELL,Lots,Bid,2,NormalizeDouble(Ask+SL,Digits),NormalizeDouble(Ask-TP,Digits),NULL,0,0,clrRed);   // Opening sell
          
          if(Ticket > 0)
          {
           Alert("Opened order sell ",Ticket);
           return;                        // Exit start
          }                               // Exit start
         }
    break;                                // exit while loop
      }
      
//---------------------------------------------------------------5-

   return;                                // Exit start
   
  }
//+------------------------------------------------------------------+
Keith Watford
Moderator
7888
Keith Watford 2015.10.22 02:15  

I don't know what

          OrderSelect(i,SELECT_BY_POS==true);

 does

Did you mean

          if(OrderSelect(i,SELECT_BY_POS))

 ....

/
To add comments, please log in or register