//--- defines
#define BALANCE_PROFIT_WITHDRAWAL 5 // valor del beneficio en el balance al que se retirarán los fondos de la cuenta en el simulador
//--- input parameters
input double InpLots = 0.1; // Lots
input uint InpStopLoss = 50; // Stop loss in points
input uint InpTakeProfit = 150; // Take Profit in points
sinput ulong InpMagic = 123; // Magic number
sinput ulong InpDeviation = 5; // Deviation
//--- global variables
CTrade trade; // ejemplar de la clase comercial
CSymbolInfo symb; // ejemplar de la clase de símbolo
CAccountInfo account; // ejemplar de clase de cuenta comercial
...
double balance_op_sum; // suma total de las operaciones de balance
uint balance_op_total; // cantidad total de operaciones de balance
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
...
//--- guardamos los valores del balance inicial
balance_prev=account.Balance();
balance_op_sum=0;
balance_op_total=0;
//--- la inicialización ha tenido éxito
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//--- actualizamos las cotizaciones actuales
if(!symb.RefreshRates())
return;
...
//--- si el beneficio del balance es superior al balance actual en el valor especificado en la macrosustitución BALANCE_PROFIT_WITHDRAWAL,
//--- consideramos que es necesario retirar estos fondos de la cuenta y llamar a la función TesterWithdrawal().
//--- comprobamos si el beneficio en el balance es superior a BALANCE_PROFIT_WITHDRAWAL
if(balance_prev!=account.Balance())
{
if(account.Balance()>balance_prev+BALANCE_PROFIT_WITHDRAWAL)
{
double profit=account.Balance()-balance_prev;
PrintFormat("The account balance has been increased by %.2f %s. Need to withdraw these funds from the account.",profit,account.Currency());
if(TesterWithdrawal(profit))
{
balance_op_total++;
balance_op_summ+=profit;
balance_prev=account.Balance();
PrintFormat("Funds have been withdrawn from the account. Account balance: %.2f %s.",account.Balance(),account.Currency());
PrintFormat("Total withdrawals: %lu. Amount of withdrawals: %.2f %s.",balance_op_total,balance_op_summ,account.Currency());
}
/*
Resultado:
The account balance has been increased by 21.00 USD. Need to withdraw these funds from the account.
deal #13 balance -21.00 [withdrawal] done
Funds have been withdrawn from the account. Account balance: 10000.00 USD.
Total withdrawals: 1. Amount of withdrawals: 21.00 USD.
*/
}
}
}
//+------------------------------------------------------------------+
//| Tester function |
//+------------------------------------------------------------------+
double OnTester()
{
//--- el valor de salida del manejador será el valor de la reducción máxima de balance en dinero
double ret=TesterStatistics(STAT_BALANCE_DD);
//--- registremos la reducción, el número de retiradas y su importe total
PrintFormat("%s: Maximum balance drawdown in money: %.2f %s. Total withdrawals: %lu. Amount of withdrawals: %.2f %s.",__FUNCTION__,ret,account.Currency(),balance_op_total,balance_op_summ,account.Currency());
//--- retornamos el resultado
return(ret);
/*
Resultado:
OnTester: Maximum balance drawdown in money: 5188.50 USD. Total withdrawals: 2. Amount of withdrawals: 36.00 USD.
final balance 4867.50 USD
OnTester result 5188.5
*/
}
|