Optimization of EA

Back to topics list To post a new topic, please log in or register
avatar
80
chuale 2015.04.20 17:48
 
//+------------------------------------------------------------------+
//|                                                 chuale_test1.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.abc.com"
#property version   "1.00"
#property strict

extern double   TakeProfit=150;
extern double   Lots=0.1;
extern double   StopLoss=300;
extern int      Max = 6;
extern int      Min = -9;
extern int      MagicNumber = 12345;
extern int      Slip  = 3;
double     bar1;
double     bar2;
double     bar3;
double     bar3max,bar3min;
int        myTrades,ticket;
datetime   previousTime;
int        xMultiply;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {

//---
if(Digits==3) xMultiply=100;
if(Digits==5) xMultiply=10000;
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---

   return(0);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int start()
  {
 
   myTrades = CountTrades();
//
   bar1=(iClose(Symbol(),0,2)-iClose(Symbol(),0,3))*xMultiply;
   bar2=(iClose(Symbol(),0,1)-iClose(Symbol(),0,2))*xMultiply;
   bar3=(iClose(Symbol(),0,0)-iClose(Symbol(),0,1))*xMultiply;
   //
   if(myTrades>0)
     {
      if(bar3<Min) CloseThisSymbolAll("OP_BUY");
      if(bar3>Max) CloseThisSymbolAll("OP_SELL");
     }
  //
   if(Time[0]==previousTime) return(0);    
   previousTime=Time[0];          
   //
   if(myTrades<1)
     {
      if(bar2>1)
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"Chua EA",MagicNumber,0,Green);
         if(ticket<0)Print("Error OP_BUY order failed. error : "+GetLastError()); 
         return(0);
        }
      if(bar2<-1)
        {
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,Bid+-TakeProfit*Point,"Chua EA",MagicNumber,0,Red);
         if(ticket<0)Print("Error OP_SELL order failed. error : "+GetLastError());
         return(0); 
        }
     }
   return(0);
  }
//========================================================================
int CountTrades()
{
int count=0;
int trade;
for(trade=OrdersTotal()-1;trade>=0;trade--)
   {
   OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
   if(OrderSymbol()!=Symbol() || OrderMagicNumber() != MagicNumber)
   continue;
   if(OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber)
   if(OrderType()==OP_SELL || OrderType()==OP_BUY)
   count++;
   }
return(count);
}
//========================================================================
void CloseThisSymbolAll(string direction)
  {
  string dir = direction;
   int trade;
   for(trade=OrdersTotal();trade>=0;trade--)
     {
      OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()!=Symbol()) continue;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
        {
         if(OrderType()==OP_BUY && dir == "OP_BUY")OrderClose(OrderTicket(),OrderLots(),Bid,Slip,Blue);
         if(OrderType()==OP_SELL&& dir == "OP_SELL")OrderClose(OrderTicket(),OrderLots(),Ask,Slip,Red);
        }
     }
  }
//========================================================================

Hello,

 

I have the above for the optimization, do I need to change the value Max and Min manually  many times during the backtest to get the optimun value? Is there a way to let the program itself finding the optimun value? Also, for an EA, can we optimize three values concurrently, ie stoploss, take profit and lot size?

Thanks 

 

avatar
49
gadget 2015.04.20 17:58 #
 
Perhaps if you frame your question a bit, we'll be able to help. If you use the strategy tester, you can obtain parameter optimization, that will normally be a long series of combinations and you can choose what is the optimisation focused on.
avatar
80
chuale 2015.04.21 05:23 #
 
For example if we want to get the optimum value of stop loss, take profit and lot size of a EA , can the program do the optimisation by itself or we have to manually try different value of SL, TP and Lots  for each backtest to get the optimum value? 
avatar
80
chuale 2015.04.21 05:27 #
 

What I mean is there a way we can write a code in the program so the program itself will find the optimun  value of SL,TP and Lot size through a  loop coding in the program? As you know, to try out manually by input different SL, TP and Lot  size is very time consuming.

 

Thanks 

avatar
1211
deysmacro 2015.04.21 06:59 #
 
You can do it. Depends how much time you are willing to spent in making those logic.
Back to topics list  

To add comments, please log in or register