How to show the maximum profit of a Transaction in the backtest?

I try to make a backtest report show the max profit of each Short and Buy trades but failed with the following coded

I want it show in the following way but failed, please help

Date | Buy/Short | Max Profit (e.g. HHV of the holding period - Buy price)

my code

bprice=ValueWhen(Buy,C);
sprice = ValueWhen(Short,C);

hsz = HighestSince(Buy, H);
HighBuySell = ValueWhen(Sell, hsz);

uptrendpts = HighBuySell - bprice;

lsz = HighestSince(Short, L);
LowShortCover = ValueWhen(Cover, lsz);

dntrendpts = sprice-LowShortCover;

Filter = 1;
AddColumn(uptrendpts,"HHV Buy/Sell", 1.2,colorblack, colorGreen,-1,Null);
AddColumn(dntrendpts,"LLV Short/Cover", 1.2,colorblack, colorgreen,-1,Null);

Thanks !

You have to write high level CBT (custom backtest) code but not exploration code.
https://www.amibroker.com/guide/a_custombacktest.html
https://www.amibroker.com/guide/a_custommetrics.html
Further CBT examples
http://www.amibroker.com/kb/category/afl/custom-backtest/

So iterate trades and calculate max. profit (see GetProfit() or GetPercentProfit() methods of trade object). Also there is IsLong property. Short simply is NOT IsLong.

As for largest winner there is WinnersLargestWin metrix name of GetValue method of stats object in addition.

For output use AddCustomMetric (NOTE: there two ones... one for backtester object and one for trade object). See links above.


As aside please read here before and watch animation. It is mandatory rule to use code tags for inserting code to forum post.

1 Like

Many Thanks!!!! Learns a lot from u!!!

Hahah....Just Read the links, really High Level for me...