Some of you may find this useful. I'm not sure if this has been
discussed before. If you are writing a fitness function with the CBT,
you can also easily add goals. The code below generates a fitness
value and then penalizes the fitness value for each goal it fails to
attain. If you are using IO, you can do this directly with
IO directives. This is an alternative way to implement goals with or
without IO.
-Steve
SetOption("UseCustomBacktestProc", true);
if ( Status("action") == actionPortfolio )
{
bo = GetBacktesterObject();
bo.Backtest(); // run default backtest procedure
st = bo.GetPerformanceStats(0); // get stats for all trades
kratio = st.GetValue("KRatio");
rarmdd = st.GetValue("RAR/MDD");
car = st.GetValue("CAR");
mdd = -st.GetValue("MaxSystemDrawdownPercent");
upi = st.GetValue("UlcerPerformanceIndex");
winpct = st.GetValue("WinnersPercent");
allqty = st.GetValue("AllQty");
tradesPerYear = allqty / (BarCount / 252);
fitness = Max(0,kratio) * Max(0,car) * Max(0,upi) * Max(0,rarmdd);
// Goal : trades per year > 2
if (tradesPerYear < 2)
fitness = tradesPerYear/2 * fitness;
// Goal : MDD < 10%
if (mdd > 10)
fitness = 10/mdd * fitness;
// Goal : CAR > 10%
if (car < 10)
fitness = car/10 * fitness;
// Goal : winpct > 60%
if (winpct < 60)
fitness = winpct/60 * fitness;
bo.AddCustomMetric("Fitness", fitness);
}