Getting data from day before entrydate - extra column for backtest

Hello everyone,

After testing the Amibroker software I decided to buy it. I'm convinced this is a good (and fast!) product. I also have the feeling that there is an active and fast responding community which I would like to thank in advance for my future questions (I will probably have a lot). First one:

I'm trying to add an extra column to the backtest result page (trade overview). For this column I'm trying to add the initial stop which was used for that trade. I tried the following code but I get a zero output which is of course not correct:

Movs=MA(Close,mavs);
stopx=MA(ATR(1),atrl)*3;
SetCustomBacktestProc( "" );
buy=(close>Movs);
sell=0;
ApplyStop( 0, 2, stopx, 1 );

if ( Status( "action" ) == actionPortfolio )
{
    bo = GetBacktesterObject();

    bo.Backtest( 1 ); // run default backtest procedure
	SumProfitPerRisk = 0;
	NumTrades = 0;
	array=0;
    for ( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
    {
		dt = DateTime();
		SetForeign( trade.Symbol );
		stop = Lookup(stopx,trade.EntryDateTime,1);
		trade.AddCustomMetric( "Stop", stop);
		RestorePriceArrays();	
    }
}

Thank you in advance.

Robin

1 Like

Congratulations on your purchase. You won't ever regret having done it.


Knowledgbase base is a must read for newbies.
Your question is precisely answered here:

Something along these lines (Note: it is quick fix in forum editor, so might still have errors):

Movs=MA(Close,mavs);
stopx=MA(ATR(1),atrl)*3;

buy=(close>Movs);
sell=0;
ApplyStop( 0, 2, stopx, 1 );

// assign indicator values to ticker-specific variables
StaticVarSet( Name() + "stopx",  stopx );

SetCustomBacktestProc( "" );
if ( Status( "action" ) == actionPortfolio )
{
    bo = GetBacktesterObject();

    bo.Backtest( 1 ); // run default backtest procedure

    for ( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
    {
		symbol_stopx = StaticVarGet( trade.Symbol + "stopx" );
		stop = Lookup(symbol_stopx,trade.EntryDateTime);
		trade.AddCustomMetric( "Stop", stop);		
    }
    
    // generate trade list
    bo.ListTrades( );
}

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.