Custom Optimization Criteria (TakeProfit and StopLoss)

Created at 26 Feb 2015, 01:20
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
cjdduarte's avatar

cjdduarte

Joined 16.01.2015

Custom Optimization Criteria (TakeProfit and StopLoss)
26 Feb 2015, 01:20


Know tell me that it is correct ?
I wish the takeprofit and the stoploss function as selection criteria

 

 protected override double GetFitness(GetFitnessArgs args)
        {
            //maximize count of winning trades and minimize count of losing trades
            return (args.NetProfit * args.WinningTrades * TakeProfit) / (args.MaxEquityDrawdownPercentages * StopLoss);
        }

 


@cjdduarte
Replies

cjdduarte
26 Feb 2015, 03:31

I discovered how powerful custom criteria .
The example below was exactly what I was looking for optimization.
Test and tell me what you think ?

 

protected override double GetFitness(GetFitnessArgs args)
        {
            //maximize count of winning trades and minimize count of losing trades
            return (Math.Pow(args.NetProfit, 3) * Math.Pow(args.WinningTrades, 2) * TakeProfit) / (Math.Pow(args.MaxEquityDrawdownPercentages, 2) * Math.Pow(StopLoss, 2) * args.MaxEquityDrawdown);
            //return (args.NetProfit * args.WinningTrades) / (args.MaxEquityDrawdownPercentages * StopLoss);
        }

 


@cjdduarte

cjdduarte
26 Feb 2015, 13:30

Always found problems in optimization keep Takprofit greater than the stoploss and still have a consistent gain. The modification below by dividing the (takeprofit / stoploss), he seeks the best results tend to keep esssa major / minor relationship. And put the end to keep both (if possible) lower, as in my case use in M15.

It Works ...


@cjdduarte

cjdduarte
26 Feb 2015, 13:30

protected override double GetFitness(GetFitnessArgs args)
        {
            //maximize count of winning trades and minimize count of losing trades
            return (Math.Pow(args.NetProfit, 3) * Math.Pow(args.WinningTrades, 2) * (TakeProfit/StopLoss)) / (Math.Pow(args.MaxEquityDrawdownPercentages, 2) * TakeProfit * StopLoss * args.MaxEquityDrawdown);
            //return (args.NetProfit * args.WinningTrades) / (args.MaxEquityDrawdownPercentages * StopLoss);
        }

 


@cjdduarte

modarkat
27 Feb 2015, 15:10

There are other interesting functions you can implement in GetFitness:

http://tradinggame.com.au/introduction-to-backtesting-metrics-part-2/

My favorite one is PROM.

 


@modarkat

cjdduarte
28 Feb 2015, 05:16

RE:

Thanks for the info. Looking more about it, I found this code.
I will implement it as a test.

modarkat said:

There are other interesting functions you can implement in GetFitness:

http://tradinggame.com.au/introduction-to-backtesting-metrics-part-2/

My favorite one is PROM.

 

 


@cjdduarte

cjdduarte
28 Feb 2015, 05:16

// MultiCharts Custom Criteria: Pessimistic Return on Margin (PROM) by Robert Pardo

// Change the value below to your account size
var AccountSize = 10000;

/* The code below can be left untouched  */

// check for errors / wrong values
if (AccountSize < 1 || StrategyPerformance.WinningTrades == 0 ||
    StrategyPerformance.LosingTrades == 0) {
    return 0;
}

var sqrtWins = Math.sqrt(StrategyPerformance.WinningTrades);
var sqrtLosses = Math.sqrt(StrategyPerformance.LosingTrades);

return ( ((StrategyPerformance.AvgWinningTrade * (StrategyPerformance.WinningTrades - sqrtWins) ) -
    ( -StrategyPerformance.AvgLosingTrade * (StrategyPerformance.LosingTrades + sqrtLosses) )) /
    AccountSize) * 100;

 


@cjdduarte

prof.edson.nascimento
25 Nov 2015, 20:56

RE:

How I call this method and what arguments I will put to them?

protected override double GetFitness(GetFitnessArgs args)

cjdduarte said:

I discovered how powerful custom criteria .
The example below was exactly what I was looking for optimization.
Test and tell me what you think ?

 

protected override double GetFitness(GetFitnessArgs args)
        {
            //maximize count of winning trades and minimize count of losing trades
            return (Math.Pow(args.NetProfit, 3) * Math.Pow(args.WinningTrades, 2) * TakeProfit) / (Math.Pow(args.MaxEquityDrawdownPercentages, 2) * Math.Pow(StopLoss, 2) * args.MaxEquityDrawdown);
            //return (args.NetProfit * args.WinningTrades) / (args.MaxEquityDrawdownPercentages * StopLoss);
        }

 

 


@prof.edson.nascimento

Spotware
25 Nov 2015, 23:26

Dear Trader,

Please have a look at the Optimization Criteria section of cAlgo support site.


@Spotware

davidp13
02 Dec 2016, 10:56

RE:

Hi. How will you go about calculating the AvgWinning or AvgLosing trade?

cjdduarte said:

// MultiCharts Custom Criteria: Pessimistic Return on Margin (PROM) by Robert Pardo

// Change the value below to your account size
var AccountSize = 10000;

/* The code below can be left untouched  */

// check for errors / wrong values
if (AccountSize < 1 || StrategyPerformance.WinningTrades == 0 ||
    StrategyPerformance.LosingTrades == 0) {
    return 0;
}

var sqrtWins = Math.sqrt(StrategyPerformance.WinningTrades);
var sqrtLosses = Math.sqrt(StrategyPerformance.LosingTrades);

return ( ((StrategyPerformance.AvgWinningTrade * (StrategyPerformance.WinningTrades - sqrtWins) ) -
    ( -StrategyPerformance.AvgLosingTrade * (StrategyPerformance.LosingTrades + sqrtLosses) )) /
    AccountSize) * 100;

 

 


@davidp13

Cam_maC
11 Sep 2019, 12:13

Hello Internet!

>Hi. How will you go about calculating the AvgWinning or AvgLosing trade?

I have below I think working code for the whole getFitness example: (along with more examples)

        protected override double GetFitness(GetFitnessArgs args)
        {
            //return _fit_winLossRatio(args);
            //return _fit_MaxWinTrade_MinLosses(args);
            //return _fit_MaxWinTrade_MinLosses2(args);
            return _fit_PessimisticReturnOnMargin(args);
        }

        private double _fit_winLossRatio(GetFitnessArgs args)
        {
            return args.WinningTrades / (args.WinningTrades + args.LosingTrades + 1);
        }

        //maximize count of winning trades and minimize count of losing trades
        private double _fit_MaxWinTrade_MinLosses(GetFitnessArgs args)
        {
            return (args.NetProfit * args.WinningTrades * TP) / (args.MaxEquityDrawdownPercentages * SL);
        }

        private double _fit_MaxWinTrade_MinLosses2(GetFitnessArgs args)
        {
            return (Math.Pow(args.NetProfit, 3) * Math.Pow(args.WinningTrades, 2) * (TP / SL)) / (Math.Pow(args.MaxEquityDrawdownPercentages, 2) * TP * SL * args.MaxEquityDrawdown);
        }

        private double _fit_PessimisticReturnOnMargin(GetFitnessArgs args)
        {
            // Change the value below to your account size
            var AccountSize = 10000;
            // check for errors / wrong values
            if (AccountSize < 1 || args.WinningTrades == 0 || args.LosingTrades == 0)
            {
                return 0;
            }

            double WinTradeTotal = 0.0;
            double LossTradeTotal = 0.0;
            int WinTradeCount = 0;
            int LossTradeCount = 0;
            double AvgWinningTrade = 0.0;
            double AvgLosingTrade = 0.0;
            foreach (HistoricalTrade trade in History)
            {
                if (trade.NetProfit > 0.01)
                {
                    WinTradeTotal = WinTradeTotal + trade.NetProfit;
                    WinTradeCount++;
                    //Print("WinTradeTotal: {0}, WinTradeCount {1}", WinTradeTotal, WinTradeCount);
                }
                else
                {
                    LossTradeTotal = LossTradeTotal + trade.NetProfit;
                    LossTradeCount++;
                    //Print("LossTradeTotal: {0}, LossTradeCount {1}", LossTradeTotal, LossTradeCount);
                }
            }
            AvgWinningTrade = Math.Round(WinTradeTotal / WinTradeCount, 2);
            AvgLosingTrade = (Math.Round(LossTradeTotal / LossTradeCount, 2) * -1);
            //Print("AvgWinningTrade: {0}, AvgLosingTrade {1}", AvgWinningTrade, AvgLosingTrade);

            var sqrtWins = Math.Sqrt(args.WinningTrades);
            var sqrtLosses = Math.Sqrt(args.LosingTrades);

            return (((AvgWinningTrade * (args.WinningTrades - sqrtWins)) - (AvgLosingTrade * (args.LosingTrades - sqrtLosses))) / AccountSize) * 100;
        }

 


@Cam_maC