Custom Metric Column in Report

I want to add a custom column "StopLoss" in report. Some how I did not see it there. What was I doing wrong here? Here is my script:


if ( Status( "action" ) == actionPortfolio )
{
    bo = GetBacktesterObject();
    // run default backtest procedure without generating the trade list
    bo.Backtest( True );

    // iterate through closed trades
    for ( trade = bo.GetFirstTrade( ); trade; trade = bo.GetNextTrade( ) )
    {
        // read StopLine values and display as custom metric
        symbolStopLine = StaticVarGet( trade.Symbol + "StopLine" );
        trade.AddCustomMetric( "StopLoss", Lookup( symbolStopLine, trade.EntryDateTime ) );
    }

    // iterate through open positions
    for ( trade = bo.GetFirstOpenPos( ); trade; trade = bo.GetNextOpenPos( ) )
    {
        // read StopLine values and display as custom metric
        symbolStopLine = StaticVarGet( trade.Symbol + "StopLine" );
        trade.AddCustomMetric( "StopLoss", Lookup( symbolStopLine, trade.EntryDateTime ) );
    }

    // generate trade list
    bo.ListTrades( );
}

_SECTION_BEGIN("8/10MA Trending R10");
Filter =1; // show all bars//

Setoption("MaxOpenPositions",10000); // Set at 10000 for initial testing so all trades are included in performance report.

MovAvg1=MA(C,8);
MovAvg2=MA(C,10);


Buy = (C>MovAvg1 AND Ref(C,-1)>MovAvg1 AND Ref(C,-2)>MovAvg1) AND (C>MovAvg2 AND Ref(C,-1)>MovAvg2 AND Ref(C,-2)>MovAvg2) ;

Sell = (c<MovAvg1 AND Ref(C,-1)<MovAvg1 AND Ref(C,-2)<MovAvg1)AND (C<MovAvg2 AND Ref(C,-1)<MovAvg2 AND Ref(C,-2)<MovAvg2) AND Ref(cross(MA(C,10),MA(C,8)),-1)AND O<Ref(MA(C,10),-1);

AboveMA8=(C>MovAvg1 AND Ref(C,-1)>MovAvg1 AND Ref(C,-2)>MovAvg1);
AboveMA10=(C>MovAvg2 AND Ref(C,-1)>MovAvg2 AND Ref(C,-2)>MovAvg2);
DayCross=cross(MA(C,8),MA(C,10));

StopLength = 1*ATR(20);//make sure same ATR in RiskPerShare
ProfitLength = 3* ATR(20);

ApplyStop(stopTypeLoss, stopModePoint, stopLength, 1, False ); //initialstop loss fixed at trade entry date 

Equity( 1, 0 ); // evaluate stops, all quotes

InTrade = Flip( Buy, Sell );//true when buy and false when sell

SetOption("EveryBarNullCheck", True );

StopLine = IIf( InTrade, Buy -StopLength, Null );//stopLoss
ProfitLine = IIf (InTrade, Buy+ProfitLength, Null);//Profit target

Buy = ExRem(Buy,Sell);
Sell = ExRem (Sell,Buy);

PlotShapes( shapeUpArrow*Buy, colorGreen,0, L, -20);
PlotShapes( shapeDownArrow*Sell, colorRed,0,H, -20) ;

_SECTION_END();```

You have to tell AmiBroker that you want to use a custom backtest procedure. I don't see this in your code:

SetOption("UseCustomBacktestProc", True );

You should also review this if you haven't already: https://www.amibroker.com/guide/a_custombacktest.html

Sorry, mradtke. I had it in my testing AFL and some how it was not copied altogether when I pasted it over. What else I need to do? Should I see the column"StopLoss" after hitting "Trades" tab?

After searching for it for a while, now I see where it is. It displays "N/A" under "StopLoss" column. What did I do wrong? And how can I have display under "Trades" tab?

I see that you are using StaticVarGet() to retrieve your stop loss values, but I don't see anywhere that you used StaticVarSet() to save them in the first place.

You may also wish to familiarize yourself with the _TRACE() command, which will be useful for debugging the CBT.

1 Like

Thanks, mradtke. I'll look into it.

Thanks, mradtke.
I'm missing that StaticVarSet(). How do I assign indicator values to ticker-specific variables if it is just a stop loss? I want to show the price of the stop loss. I have it like this, StaticVarSet( Name() + "StopLine",StopLine ); How do I turn it into price where the stop loss occurs under StopLIne column?