Hi,
Regarding to the code below
trades = stats.getValue("AllQty")/(barsInRange/252); // 252 bars per year
calculates the trades per year.
However, I have seen using BarCount instead of barsInRange before. Which one is 
correct if I want to backtest for a certain period? What is the difference 
between BarCount and barsInRange?

--- In [email protected], "Mike" <sfclimb...@...> wrote:
>
> 
> You would do that by defining your own "fitness function" and exposing
> its value, for each backtest results row, as a named custom metric.
> 
> You would then use that named metric as the optimization target from the
> walk forward settings dialog. From that point on, your fitness result
> becomes the value which gets optimized.
> 
> In your case, your fitness function would be the value of CAR multiplied
> by a (possibly sliding) scale factor taking into account the number of
> trades.
> 
> You can see the idea in the code snippet below (which is using Ulcer
> Performance Index rather than CAR). The code is just a snippet taken
> from a larger implementation that included additional weights and had
> custom position sizing on a bar by bar basis. So, the snippet you see is
> not the most efficient in the context of this reply, but should give you
> the idea. You may need to use a multiplier (e.g. 100 in example) to
> raise values to larger numbers for display purposes only.
> 
> A few words of caution:
> 
>     1. This will help guide the optimizer, so it will give less weight to
> lesser values of your fitness function. But, it comes at the cost of
> custom backtest calculations which are not free. It may end up taking
> longer than without the guidance. But, it will result in values that
> better suit your wants.
>     2. Keep in mind that you probably need a ratio of trades for the
> period under study, as opposed to a fixed one size fits all number of
> trades regardless of period duration.
>     3. The Optimizers (e.g. CMAE) are designed to work against continuous
> fitness values. A simple on/off, high value/zero fitness will not work
> well and can lead to very long optimizations (in my experience). Even
> using the sliding scale of the example below, it would be better to have
> a more continuous scale for when there are too few trades rather than
> the sudden drop off factor of 0.001.
>     4. Be sure to type in the exact name of your custom metric, including
> uppercase/lowercase as the target in the walk forward settings.
>     5. CAR is a notoriously poor estimator of fitness.
> 
> Mike
> 
> SetCustomBacktestProc("");
> 
> inRange = Status("barinrange");
> barsInRange = 0;
> 
> if (Status("action") == actionPortfolio) {
>    bo = GetBacktesterObject();
>    bo.Backtest();
> 
>    for (bar = 0; bar < BarCount; bar++) {
>      if (inRange[bar]) {
>      barsInRange++;
>    }
> 
>    stats = bo.getPerformanceStats(0);
>    trades = stats.getValue("AllQty")/(barsInRange/252); // 252 bars per
> year
> 
>    if (trades <= 252) {
>      tMult = .001; // Penalize low trade volume
>    } else if (trades >= 504) {
>      tMult = 1; // 2 or more trades/day ideal
>    } else {
>      tMult = (trades - 252)/252; // Ratio of ideal
>    }
> 
>    upi = stats.getValue("UlcerPerformanceIndex");
>    optimal = upi * 100 * tMult;
> 
>    bo.addCustomMetric("Optimal", optimal);
>    bo.addCustomMetric("tMult", tMult);
> }
> 
> --- In [email protected], Richard <Richard.M.Ferri@> wrote:
> >
> > Is it possibile optimize with a target like CAR excluding results with
> a
> > number of trades to low?
> > On this way the time required for optimization can be saved?
> >
>


Reply via email to