Try this code. You may want to change a few lines for the data you are
interested in. Note there are two sections that appear identical. One is for
closed trades, then other for open trades.
/********************* CUSTOM BACKTEST PROCEDURE ********************/
function FindValueAtDateTime( array, dt, Value )
{
found = -1;
for( i = 0; i < BarCount AND found == -1; i++ )
{
if( dt[ i ] == Value ) found = i - 1; //Coded by Tomasz = i, but
I want the value from the day BEFORE the signal
}
result = Null;
if( found != -1 ) result = array[ found ];
return result;
}
SetCustomBacktestProc("");
dt = DateTime();
if( Status("action") == actionPortfolio )
{
bo = GetBacktesterObject();
bo.Backtest(1); // run default backtest procedure
// iterate through closed trades and add some info
for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
{
SetForeign(trade.Symbol);
trade.AddCustomMetric("Industry",IndustryID(1) );
//Want to see IndustryID by trade
trade.AddCustomMetric("PScore",trade.Score,0);
//Lookup actual price at trade entry
foi = Foreign( trade.Symbol, "I" );
temp = FindValueAtDateTime( foi, dt, trade.EntryDateTime
);
// _TRACE( "Temp=" + temp );
trade.AddCustomMetric("Actual OI", temp/100 );
//Lookup Volume at trade entty
foi = Foreign( trade.Symbol, "V" );
temp = FindValueAtDateTime( foi, dt, trade.EntryDateTime
);
trade.AddCustomMetric("Volume", temp, 0 );
}
// iterate through open trades and add same info
for( trade = bo.GetFirstOpenPos(); trade; trade = bo.GetNextOpenPos()
)
{
SetForeign(trade.Symbol);
trade.AddCustomMetric("Industry",IndustryID(1));
//Want to see IndustryID by trade
trade.AddCustomMetric("PScore",trade.Score,0);
//Lookup actual price at trade entry
foi = Foreign( trade.Symbol, "I" );
temp = FindValueAtDateTime( foi, dt, trade.EntryDateTime
);
// _TRACE( "Temp=" + temp );
trade.AddCustomMetric("Actual OI", temp );
//Lookup Volume at trade entty
foi = Foreign( trade.Symbol, "V" );
temp = FindValueAtDateTime( foi, dt, trade.EntryDateTime
);
trade.AddCustomMetric("Volume", temp, 0 );
}
bo.ListTrades();
}
--
Terry
-----Original Message-----
From: amibroker@xxxxxxxxxxxxxxx [mailto:amibroker@xxxxxxxxxxxxxxx] On Behalf Of
rdavenportca
Sent: Sunday, May 28, 2006 08:51
To: amibroker@xxxxxxxxxxxxxxx
Subject: [amibroker] Adding exit bar Close via AddCustomMetric
I am trying to add a few columns to my Backtest results page. I'd
like to add the exit bar open and close price and the previous bar's
close price to the output (per trade).
One apparent limitation is that the GetPrice command only works with
the Open Trade list. Additionally, I've never been able to get the
AddCustomMetric to work inside a bar-by-bar loop inside the trade
object. I've tried the following, but it didn't work. Any help
would be appreciated.
SetCustomBacktestProc("");
if( Status("action") == actionPortfolio )
{
bo = GetBacktesterObject();
bo.preprocess();
for( bar = 0; bar < BarCount ; bar++ )
{
bo.ProcessTradeSignals(bar);
for( Openpos = bo.GetFirstOpenPos() ; OpenPos ; OpenPos =
bo.GetNextOpenPos() )
{
xClose= trade.GetPrice(bar, C );
}
bo.AddCustomMetric("Close", xClose, DecPlaces = 2 );
}
bo.PostProcess();
------------------------ Yahoo! Groups Sponsor
--------------------~-->
Home is just a click away. Make Yahoo! your home page now.
http://us.click.yahoo.com/DHchtC/3FxNAA/yQLSAA/GHeqlB/TM
--------------------------------------------------------------------~->
Please note that this group is for discussion between users only.
To get support from AmiBroker please send an e-mail directly to
SUPPORT {at} amibroker.com
For other support material please check also:
http://www.amibroker.com/support.html
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/amibroker/
<*> To unsubscribe from this group, send an email to:
amibroker-unsubscribe@xxxxxxxxxxxxxxx
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/