Hello,
You need to first understand that backtesting is two-pass
process and custom backtesting
does NOT operate on symbol, but on the ~~~Equity ticker.
This is something you need to check first:
A "buy" array in 2nd phase does NOT represent the buy array in
the system code (1st phase).
This is because AB is PORTFOLIO backtester, not
single-security backtester
and first it needs to collect signals from MANY symbols (1st
phase) and then process then in the 2nd
phase.
For this reason 2nd phase operates on portfolio equity and NOT
on individual symbols
and your HHV( C, 20 ) that you put inside custom backtester
operates on EQUITY, not on the symbols under test.
The code is obviously wrong and needs to be rewritten as
follows:
/*Custom Backtesting*/ SetCustomBacktestProc("");
if
(Status("action") == actionPortfolio){
bo=GetBacktesterObject(); bo.Backtest(1);
dt = DateTime();
bi = BarIndex();
for (trade = bo.GetFirstTrade();
trade; trade = bo.GetNextTrade())
{
SetForeign( trade.Symbol ); // need to switch the symbol
entrybar = LastValue( ValueWhen( dt ==
trade.EntryDateTime, bi ) );
test= C[ enrybar ]; // we
need scalar value test1= HHV(C,20)
;
test1 = test1[ entrybar ]; // we
need scalar value
trade.AddCustomMetric("test",test);
trade.AddCustomMetric("test1",test1);
}
bo.AddCustomMetric("Marker","Marker");
bo.ListTrades(); }
Best regards, Tomasz
Janeczko amibroker.com
----- Original Message -----
Sent: Thursday, February 14, 2008 12:34
AM
Subject: [amibroker] Re: Backtesting and
Custom Metrics: How to determine HHV at date of Buy?
The ValueWhen function sounded really promising. But it didn't
work. I couldn't even get the ValueWhen(Buy,C) to work. My code is
below:
_SECTION_BEGIN("Testing1");
/*
The buy/sell
system*/
Buy=Cross(MACD(),Signal()); Sell=Cross(Signal(),
MACD());
/*Custom
Backtesting*/ SetCustomBacktestProc("");
if (Status("action") ==
actionPortfolio){
bo=GetBacktesterObject();
bo.Backtest(1);
for (trade = bo.GetFirstTrade();
trade; trade = bo.GetNextTrade()){
test=ValueWhen(Buy,C);
test1=ValueWhen(Buy,HHV(C,20));
trade.AddCustomMetric("test",test);
trade.AddCustomMetric("test1",test1);
}
bo.AddCustomMetric("Marker","Marker");
bo.ListTrades(); }
_SECTION_END(); So, in the output,
the custom metrics test and test1 (the last two columns) are
both blank. I have posted the exported results below. Note that
when I click the Report the "Marker" custom metric is being displayed, and
responds to changes in the code, so I know I'm working on the right
file.
The other problem is that, even if this works, it looks like I
need to have the custom backtesting code within the same formula file as the
buy/sell system, rather than having it in a different file and referencing it
from the buy/sell formula (because "Buy" is not initialised in the backtesting
code file).
Ticker,Trade,Date,Price,Ex. date,Ex. Price,% chg,Profit,%
Profit,Shares,Position value,Cum. Profit,# bars,Profit/bar,MAE,MFE,Scale
In/Out,test,test1, WPL,Long,31/05/1999,8.97249,30/04/2001,12.2762,36.82%,7314.07,36.62%,2226,19972.75,7314.07,24,304.75,-4.92%,58.92%,0/0,, WPL,Long,31/05/2001,13.5646,31/08/2001,12.6443,-6.78%,-1891.56,-6.93%,2012,27291.89,5422.50,4,-472.89,-9.77%,11.60%,0/0,, WPL,Long,29/08/2003,12.2854,29/09/2006,38.011,209.40%,53134.92,209.24%,2067,25393.93,58557.42,38,1398.29,-3.75%,288.01%,0/0,, WPL,Open
Long,28/09/2007,50.2,31/01/2008,46.35,-7.67%,-6061.40,-7.72%,1564,78512.80,52496.02,6,-1010.23,-18.92%,12.87%,0/0,,
I
may yet have to use some of the more complicated suggestions made in the
replies, but I'm perplexed as to why this ValueWhen doesn't work. Anyone
have any ideas?
Many thanks.
--- In amibroker@xxxxxxxxxxxxxxx, "Tomasz Janeczko"
<groups@xxx> wrote: > > Hello, > > It can be as
simple as: > > ValueWhen( Buy, HHV( C, 20 ) ); > >
but you need to describe FINAL goal that you are after, so actually best
solution > can be offered > > Best regards, > Tomasz
Janeczko > amibroker.com > ----- Original Message ----- >
From: justjuice200 > To: amibroker@xxxxxxxxxxxxxxx > Sent:
Wednesday, February 13, 2008 2:39 AM > Subject: [amibroker] Backtesting
and Custom Metrics: How to determine HHV at date of Buy? > >
> I know this should be simple, but can't figure out how to code it
in > AFL. Many thanks in advance. > > I want to add some
custom metrics to the backtest report. For each > trade, I want to show
two things: > 1) a 20-bar HHV at the date of the buy > 2) a price
at a certain number of bars away from the date of the buy. > > So
far what I have is the following (and not sure I'm on the right > track
either): > > if (Status("action") == actionPortfolio){ >
bo=GetBacktesterObject(); > bo.Backtest(1); > > for (trade
= bo.GetFirstTrade(); trade; trade = bo.GetNextTrade()){ >
ticker=trade.Symbol(); > symbolPriceArray=Foreign(ticker, "C",
1); > Dateoftrade=trade.EntryDateTime(); > BarNumOfTrade=....some
Code Here... > myHigh=HHV(symbolPriceArray,20)/*Also need some way to
define the > HHV with reference to the date of the buy*/ >
> CloseTenDaysAgo=symbolPriceArray[BarNumOfTrade-10]; > >
} > > bo.ListTrades(); > > } >
__._,_.___
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 NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/
For other support material please check also:
http://www.amibroker.com/support.html
__,_._,___
|