How to add this custom column about volume to backtest results

I would like to add the following volume information as a new custom column to the backtest results (list of columns generated when I click on the backtest button on Analysis window).

VolumeGain = Volume/ (MA( Volume, 50 ) );

The VolumeGain should show the value corresponding to the entry date of the trade.

Thank you for your help.

Many many articles (having taken time to write up) are provided at AmiBroker knowledge base already.
Those articles cover most asked questions by (new or inexperienced) users.
Please read them (it's best practice to read all of the articles before asking questions in forum).

Your question has been covered there too.
http://www.amibroker.com/kb/2014/11/20/how-to-show-indicator-values-in-backtest-trade-list/

2 Likes

I would like to answer my own question, thanks to the link provided by fxshrat.

SetCustomBacktestProc( "" );

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 Vol values and display as custom metric
        symbolVol = StaticVarGet( trade.Symbol + "VolG" );
        trade.AddCustomMetric( "VolumeGain", Lookup( symbolVol, trade.EntryDateTime ) );
    }

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

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

// assign indicator values to ticker-specific variables
//GetVolGain() is function to calculate the desired volume information
StaticVarSet( Name() + "VolG", GetVolGain(NUM_BARS_MID_TERM) );

Hi! I've looked at both the linked guide and the solution here, but I can't figure out why my formula presents me only with an N/A.

Formula linked below. I'm trying to make it display the highest value for each stock in the holding period, but for simplicity's sake I went for just showing the highest value for each stock over 120 days.

Any assistance would be greatly appreciated and I apologise if this has been answered before, I could not find a solution nor could I figure it out from reading the manual/guides.

image

Your Static var Name is incorrect.

I will not post corrected code because you did not post code properly too.
Please do not post code as picture only!

2 Likes

First, read the forum rules and learn how to post your code using code tags. Screenshots are not convenient if others want to try to run your code.

Second, your StaticVarGet() calls are incorrect. You probably want to be doing something like this:

symbolHigh = StaticVarGet(trade.Symbol + "High");

Third, Equity() is a function that runs a single-symbol backtest to generate an equity curve. Valid inputs for the first parameter are 0, 1, or 2 as explained here: AFL Function Reference - EQUITY If you are trying to set your initial equity, use SetOption().

When posting the formula, please make sure that you use Code Tags (using </> code button) as explained here: How to use this site.

Using code button

Code tags are required so formulas can be properly displayed and copied without errors.

@Tomasz @fxshrat @mradtke Thank you for the assistance. I will correct my posting methods in future.