Expert Advisor Optimization repeating same results. PLEASE HELP!

To add comments, please log in or register
Patrick Engelbreth
24
Patrick Engelbreth  

I've been programming and testing strategies in my spare time just for fun, but lately a problem with MT4 has made it impossible.

When I run a backtest, change the parameters, and run a backtest again, the second backtest frequently uses the parameters from the first one and so it gives me the exact same results.

This makes optimization completely useless, since I get a list of results that look like this: https://ibb.co/sb9CGsB

It is something about the coding, I just have no idea what in the code is causing the problem.

Has anyone ever had this problem before? Have you been able to fix it? I'm desperate at this point...  

Any help would be greatly appreciated. 

Anm-rkning-2019-08-09-210724
Anm-rkning-2019-08-09-210724
  • ibb.co
Billede Anm-rkning-2019-08-09-210724 hostet i imgbb.com
Alain Verleyen
38624
Alain Verleyen  
How are you expecting coding help without providing any code ?
Patrick Engelbreth
24
Patrick Engelbreth  
Alain Verleyen:
How are you expecting coding help without providing any code ?

That is a good question!!

//+------------------------------------------------------------------+
//|                                                 OBVmod_MA_EA.mq4 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern int EAFastMA=7;
extern int EAFastMAShift=0;
extern int EAFastMAMethod=0;
extern int EAFastMAAppliedTo=0;
extern int EASlowMA=65;
extern int EASlowMAShift=0;
extern int EASlowMAMethod=0;
extern int EASlowMAAppliedTo=0;
extern double LotSize = 0.01;
extern int MagicNumber =0;
extern int ATR_PeriodValue =14;
double pips;

// Defining parameters for opening trades. 
extern int    TakeProfitPercent = 100;
extern double  StopLossPercent = 150;

extern int        Slippage = 3;
 int        MagicNumber1 = 1090608;

double myLots;
double myATR;
double StopLoss, TakeProfit;
double myPoint;

int ticket1 = -1;
int ticket1type = -1;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
  double TickSize = MarketInfo(Symbol(),MODE_TICKSIZE);
   if (TickSize == 0.00001 || TickSize == 0.001)
   pips = TickSize*10;
   else pips = TickSize;
   
     // Making ATR and defining SL & TP
     {
       myATR = iATR(NULL, PERIOD_D1, ATR_PeriodValue, 1)/pips;
        TakeProfit = myATR * TakeProfitPercent/100.0;
        StopLoss = myATR * StopLossPercent/100.0;
     }
  
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

  
  
  
  
//---Defining iCustom Indicators
   {
      double CurrentFast= iCustom(NULL,0,"iOBVmod_indicator_MA",EAFastMA,EAFastMAShift,EAFastMAMethod,1,1);
      double CurrentSlow=iCustom(NULL,0,"iOBVmod_indicator_MA",EASlowMA,EASlowMAShift,EASlowMAMethod,2,1);
      double PreviousFast= iCustom(NULL,0,"iOBVmod_indicator_MA",EAFastMA,EAFastMAShift,EAFastMAMethod,1,2);
      double PreviousSlow=iCustom(NULL,0,"iOBVmod_indicator_MA",EASlowMA,EASlowMAShift,EASlowMAMethod,2,2);
      
      
      int TicketBuy;
      int TicketSell;
      int OrderTicket;
      { 
      
      
          //----code to count orders
          int totals[2];
          ArrayInitialize(totals,0);
          for (int i=0; i<OrdersTotal(); i++)
          {
              if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
              {
                  if(OrderSymbol()==Symbol()   &&     //check symbol
                     //OrderMagicNumber()==magic &&     //check magic number
                     OrderType()<1)                   //ignore pending
                  {
                      totals[OrderType()]++; //add count to buy or sell totals
                  }
              }
          }
                           
      
          
          if(PreviousFast<PreviousSlow && CurrentFast>CurrentSlow && OrdersTotal()>0)
          {
          //code to close Sell order(s)
              int total = OrdersTotal();
              for(int i=total-1;i>=0;i--)
              {
                OrderSelect(i, SELECT_BY_POS);
                int type   = OrderType();
            
                bool result = false;
                
                switch(type)
                {
                  //Close opened long positions
                  case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
                                      break;
                  
                  //Close opened short positions
                  case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
                }
             }

          }   
          
          if(PreviousFast>PreviousSlow && CurrentFast<CurrentSlow && OrdersTotal()>0)
          {
          //code to close buy order(s)
              int total = OrdersTotal();
              for(int i=total-1;i>=0;i--)
              {
                OrderSelect(i, SELECT_BY_POS);
                int type   = OrderType();
            
                bool result = false;
                
                switch(type)
                {
                  //Close opened long positions
                  case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
                                      break;
                  
                  //Close opened short positions
                  case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
                }
              }
 
          }
          //----code to open orders
       if(PreviousFast<PreviousSlow && CurrentFast>CurrentSlow && OrdersTotal()==0) //open buy order if no open orders
          {
              //code to open buy order(s)
              TicketBuy= OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,Ask-(StopLoss*pips),Ask+(TakeProfit*pips),NULL,MagicNumber,0,Green);
          }
       if(PreviousFast>PreviousSlow && CurrentFast<CurrentSlow && OrdersTotal()==0) //open sell order if no open orders
          {
              //code to open sell order(s)
              TicketSell=OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,Bid+(StopLoss*pips),Bid-(TakeProfit*pips),NULL,MagicNumber,0,Red);
              
          }
      }
   }
  }

//+------------------------------------------------------------------+
Keith Watford
Moderator
17619
Keith Watford  
Patrick Engelbreth:

When I run a backtest, change the parameters, and run a backtest again, the second backtest frequently uses the parameters from the first one and so it gives me the exact same results. This makes optimization completely useless.

It is obviously my coding that is the problem, but I really can't find out where...

PLEASE HELP

Where do you change the parameters?

Don't just change them in the code, you must also change them or reset in the tester.

Patrick Engelbreth
24
Patrick Engelbreth  
Keith Watford:

Where do you change the parameters?

Don't just change them in the code, you must also change them or reset in the tester.

When i change the inputs in MT4 nothing happens. But if i manually change inputs in the coding it works fine. It's like it doesn't register, that i am changing the inputs in MT4.
Keith Watford
Moderator
17619
Keith Watford  

Do not make multiple posts on the same subject.

I have deleted your other topic and post.

The previous 2 comments were moved here from the deleted topic.

Keith Watford
Moderator
17619
Keith Watford  

Unless I am not quite following your code correctly..............

When it opens a trade, is it immediately closing it again the next tick, then reopening it ?

William Roeder
19417
William Roeder  
int OnInit(){
⋮
       myATR = iATR(NULL, PERIOD_D1, ATR_PeriodValue, 1)/pips;
  1. Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:
    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

  2. On MT4: Unless the current chart is that specific pair/TF referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4

Keith Watford
Moderator
17619
Keith Watford  
Keith Watford:

Unless I am not quite following your code correctly..............

When it opens a trade, is it immediately closing it again the next tick, then reopening it ?

Have you checked?

Opening a trade and closing it every tick will just run out of balance in the end.

iRick
231
iRick  

Why is you Fast MA set to 1???

Surely you'd need 2+ to calculate a value... It's probably returning 0 which is always < MA 10,11,12 etc and therefore triggering the same trades

To add comments, please log in or register