Mike:

Thanks for your reply.  I've had a go at implementing that, but there's a devil 
in the details somewhere.  

I defined a variable MyRisk, which shows up correctly when I run an 
Exploration, however custombacktestproc appears unable to find it when I do a 
Backtest. 

The custommetrics involving MyRisk all result in zero, everything else about 
the code below appears to give correct results, including MyClose giving me the 
correct close price for the symbol/date which I put in there in case I had made 
a syntax error, and my trade count giving the correct number of trades.

Any suggestions would be much appreciated!

Jeff

Code:


//custom backtest indicators to measure system performance
SetOption("UseCustomBacktestProc", True );
SetCustomBacktestProc("",True);
if( Status("action") == actionPortfolio )       // point to correct iteration 
of backtest engine
        { 
        //initialize custom backtest engine
        backtestobject = GetBacktesterObject();         

        // run the standard backtest
        backtestobject.Backtest(1);     

        // pointers to find specific trades
        bars = BarIndex();
        dates = DateTime();     

        // intialize variables for SQN Calculations
        SumR = 0;
        SumR2 = 0;
        Trades = 0;
        expectancyR = 0;
        stdDevR = 0;
        SQN = 0;
        R = 0;

        //add data to trade list, starting with closed trades
    for( trade = backtestobject.GetFirstTrade(); trade; trade = 
backtestobject.GetNextTrade() ) 
        { 
                        SetForeign( trade.Symbol, True, True );
                        entryBar = LastValue( ValueWhen( trade.EntryDateTime == 
dates, bars ) );
                        R       = MyRisk[entryBar];
                        trade.addcustommetric("MyRisk",R);
                        trade.addcustommetric("MyClose",Close[entrybar]);
                sumR += R;
                sumR2 += R^2;
                        Trades++;
                RestorePriceArrays(True);
        }

   //add data for trades still open at end of process
   for( trade = backtestobject.GetFirstOpenPos(); trade; trade = 
backtestobject.GetNextOpenPos() )
                {
                        SetForeign( trade.Symbol, True, True );
                        entryBar = LastValue( ValueWhen( trade.EntryDateTime == 
dates, bars ) );
                        R       = MyRisk[entryBar];
                        trade.addcustommetric("MyRisk",R);
                        trade.addcustommetric("MyClose",Close[entrybar]);
                sumR += R;
                sumR2 += R^2;
                        Trades++;
                RestorePriceArrays(True);
                } 

        //SQN calculations
        if (Trades > 0) 
                {
                        testcalc = Trades * 2;
                expectancyR = sumR / Trades;
                stdDevR = sqrt((sumR2 - (sumR^2 / Trades)) / (Trades - 1));
                SQN = ((expectancyR / stdDevR) * sqrt(Min(100, Trades)));
                } 

        //display results 
        backtestobject.AddCustomMetric("TestCalc", testcalc);           
        backtestobject.AddCustomMetric("Trades", Trades);
        backtestobject.AddCustomMetric("sumR", SumR);
        backtestobject.AddCustomMetric("sumR2", SumR2);
        backtestobject.AddCustomMetric("ExpectancyR ($)", expectancyR);
        backtestobject.AddCustomMetric("StdDevR", stdDevR);
        backtestobject.AddCustomMetric("SQN", SQN);
        backtestobject.ListTrades();

        }



Reply via email to