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

 

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

 
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

 
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

 
#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

 
Russell:

Maybe this can help...


Russell

MagicAccountLeverageFactor is not defined
 

This is an old thread but if someone are still interested on it, take a look on this article link

 

Yeah, that way : 

TesterWithdrawal

The special function to emulate the operation of money withdrawal in the process of testing. Can be used in some asset management systems.

bool  TesterWithdrawal( 
   double money      // the sum to withdraw 
   );

Parameters

money

[in]  The sum of money that we need to withdraw (in the deposit currency).

Return Value

If successful, returns true, otherwise - false.

 

See also

TesterDeposit


And since some time it's also possible to simulate a deposit : 

TesterDeposit

The special function that emulates depositing funds during a test. It can be used in some money management systems.

bool  TesterDeposit( 
   double money      // deposited sum 
   );

Parameters

money

[in]  Money to be deposited to an account in the deposit currency.

Return Value

Returns true if successful, otherwise - false.

 

See also

TesterWithdrawal


It works only on MT5 with MQL5, it's a good occasion to upgrade if you yet didn't.

Reason: