Using relative drawdown instead of maximal drawdown while optimizing

 

The default option to optimize using "Drawdown min" uses "Equity drawdown maximal". I would like to code a custom criteria which uses "Equity drawdown relative" to optimize. I looked up the OnTester documentation but couldn't really understand it.

How could I define a custom max criteria which is  "Equity drawdown relative" on the OnTester function? Thanks in advance.

 
35117153:

The default option to optimize using "Drawdown min" uses "Equity drawdown maximal". I would like to code a custom criteria which uses "Equity drawdown relative" to optimize. I looked up the OnTester documentation but couldn't really understand it.

How could I define a custom max criteria which is  "Equity drawdown relative" on the OnTester function? Thanks in advance.

Show your attempt if you need coding help.

What don't you understand in the documentation exactly ?

 
35117153:

The default option to optimize using "Drawdown min" uses "Equity drawdown maximal". I would like to code a custom criteria which uses "Equity drawdown relative" to optimize. I looked up the OnTester documentation but couldn't really understand it.

How could I define a custom max criteria which is  "Equity drawdown relative" on the OnTester function? Thanks in advance.


Use the Custom OnTester() function below, it does what you asked for.

Insert it on your code, compile, and for optimization choose "Custom Max" on the dropdown menu.


double OnTester()
{

    double  param                 = 0.0;
    double  profit                = TesterStatistics(STAT_PROFIT);
    double  pfactor               = TesterStatistics(STAT_PROFIT_FACTOR);
    double  vendas                = TesterStatistics(STAT_SHORT_TRADES);
    double  compras               = TesterStatistics(STAT_LONG_TRADES);
    double  total_trades          = TesterStatistics(STAT_TRADES);
    double  expected_payoff       = TesterStatistics(STAT_EXPECTED_PAYOFF);
    double  recover_factor        = TesterStatistics(STAT_RECOVERY_FACTOR);
    double  SharpeRatio           = TesterStatistics(STAT_SHARPE_RATIO);
    double PerdaConsecutiva       = TesterStatistics(STAT_MAX_CONLOSS_TRADES);
    double GanhoConsecutivo       = TesterStatistics(STAT_MAX_CONPROFIT_TRADES);
    double MargemLevelMin         = TesterStatistics(STAT_MIN_MARGINLEVEL);


    double  drawdown_pct;


    // To Use EQUITY Drawdown RELATIVE
    drawdown_pct          = TesterStatistics(STAT_EQUITY_DDREL_PERCENT);


    // Uncomment if want to use the Equity DD Maximum. (which is the same as the default optimization uses)
    //
    // drawdown_pct          = TesterStatistics(STAT_EQUITYDD_PERCENT);



    // Invert the percentage for use on the final calculation
    // -----------------------------------------------------
    double  drawdown_pct_inverso = 0;

    if(drawdown_pct > 0 && drawdown_pct < 100) {
        drawdown_pct_inverso = (100 - drawdown_pct) / 100;
    } else {
        drawdown_pct_inverso = 1 / 100;
    }
    // -----------------------------------------------------



    // ------------------  Final calculation  ----------------------------------------------
    double ret = (((drawdown_pct_inverso * 3) * recover_factor * (SharpeRatio * 1.2))); 

    ret = (ret * ((100 - drawdown_pct) / 100)); 
    ret = (ret * (GanhoConsecutivo + 1) / (PerdaConsecutiva + 1) );    

    if (ret > 0 && profit < 0) {           // positive ret, only if profit too
        ret = -ret;
    }
    // -------------------------------------------------------------------------------------


    /* Done, Send the result to tester */
    return(ret);
}