Withdraw simulation in Strategy Tester --> is it possible?

To add comments, please log in or register
Andrea Bronzini
110
Andrea Bronzini  

Hi folks!

I am wondering whether it is possible to simulate withdrawals while backtesting. I dont like to see the balance going up to hundred thousands k cause it is not realistic... I think everyone agrees that one takes out money when the balance reaches a given amount. Also, as my money management is based on % for the free margin, having too much money on the account strongly affects the simulation.

To overcome to this one should stop the simulation every time the balance reaches the withdrawal level, then start again from there with the original capital... kind of boring process.


I'd like to implement automatic withdrawals in the Strategy Tester and see what really would happen to my account.


Any suggestion?


Zyp

Ser
32
Ser  
Zypkin:

Hi folks!

I am wondering whether it is possible to simulate withdrawals while backtesting. I dont like to see the balance going up to hundred thousands k cause it is not realistic... I think everyone agrees that one takes out money when the balance reaches a given amount. Also, as my money management is based on % for the free margin, having too much money on the account strongly affects the simulation.

To overcome to this one should stop the simulation every time the balance reaches the withdrawal level, then start again from there with the original capital... kind of boring process.


I'd like to implement automatic withdrawals in the Strategy Tester and see what really would happen to my account.


Any suggestion?


Zyp

Take a look at this:https://www.mql5.com/en/articles/1403

It is not exactly what you want but you can modify it to get the desired results

HTH

Andrea Bronzini
110
Andrea Bronzini  
ser01:

Take a look at this:https://www.mql5.com/en/articles/1403

It is not exactly what you want but you can modify it to get the desired results

HTH

Thanks a lot!!!

It will take some time to study it but is seems I can pretty much use it to reset the account from time to time while recording the results of backtesting :)



Cheers,

Zyp

Russell
287
Russell  
#define GET       0
#define PUT       1
#define RESET     2
#define INIT      3

#define EA_BALANCE      0
#define EA_PROFIT       1
#define EA_MARGIN       2
#define EA_FREEMARGIN   3
#define EA_EQUITY       4
#define EA_TOTAL_SELL   5
#define EA_TOTAL_BUY    6

#define DATA_PROFIT     0
#define DATA_MARGIN     1
#define DATA_TOTAL      2
#define DATA_LOTS       3
double gdFund  =200;

/*
   Print(InfoMagicNumber(EA_BALANCE, 1234);

*/


double InfoMagicNumber(int iValue, int iMagicNumber, int iFunction = GET){

   if (IsOptimization() != FALSE || IsTesting() != FALSE){
      //return(InfoMagicNumberOptimization(iValue, iMagicNumber, iFunction));
   }
   
   static double ldData[4][2];
   static int liOHTCounted = 0;
   if(iFunction == INIT){
      
      liOHTCounted = 0;
      ldData[DATA_PROFIT][0] = gdFund;
   
   } else if(iFunction == PUT){
      
      int    liOT = OrdersTotal();
      int    liOHT = OrdersHistoryTotal(); 
            
      ldData[DATA_PROFIT][1] = 0;            
      ldData[DATA_MARGIN][1] = 0;
      ldData[DATA_TOTAL][OP_SELL] = 0;
      ldData[DATA_TOTAL][OP_BUY] = 0;
      ldData[DATA_LOTS][OP_SELL] = 0;
      ldData[DATA_LOTS][OP_BUY] = 0;
      
      for(int i = liOHTCounted; i < liOHT; i++){
         OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == iMagicNumber){
            ldData[DATA_PROFIT][0]+= OrderProfit()+OrderCommission()+OrderSwap();                  
         }         
      }
      liOHTCounted = liOHT;
      
      for(i = 0; i < liOT; i++){
         OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == iMagicNumber){
            ldData[DATA_PROFIT][1]+= OrderProfit()+OrderCommission()+OrderSwap();  
            ldData[DATA_TOTAL][OrderType()]++; 
            ldData[DATA_LOTS][OrderType()]+= OrderLots();   
         }
      }
      
      ldData[DATA_MARGIN][1] = (MarketInfo(OrderSymbol(), MODE_MARGINREQUIRED)*ldData[DATA_LOTS][OP_SELL]*MagicAccountLeverageFactor(GET));
      if (ldData[DATA_LOTS][OP_BUY] > ldData[DATA_LOTS][OP_SELL]){
         ldData[DATA_MARGIN][1] = (MarketInfo(OrderSymbol(), MODE_MARGINREQUIRED)*ldData[DATA_LOTS][OP_BUY]*MagicAccountLeverageFactor(GET));
      }
     
   } else if(iFunction == GET){
      switch(iValue){
         case EA_BALANCE:
            return(ldData[DATA_PROFIT][0]);
            break;
         case EA_PROFIT:
            return(ldData[DATA_PROFIT][1]);
            break;
         case EA_MARGIN:
            return(ldData[DATA_MARGIN][1]);
            break;
         case EA_FREEMARGIN:           
            return(ldData[DATA_PROFIT][0]+ldData[DATA_PROFIT][1]-ldData[DATA_MARGIN][1]);
            break;
         case EA_EQUITY:
            return(ldData[DATA_PROFIT][0]+ldData[DATA_PROFIT][1]);
            break;
         case EA_TOTAL_SELL:
            return(ldData[DATA_TOTAL][OP_SELL]);
            break;
         case EA_TOTAL_BUY:
            return(ldData[DATA_TOTAL][OP_BUY]);
            break;
      }
   }
}

Maybe this can help...


Russell

Eng Keat Ang
1787
Eng Keat Ang  
Russell:

Maybe this can help...


Russell

MagicAccountLeverageFactor is not defined
To add comments, please log in or register