Equity function and chart

Introduction

The equity line is probably the best diagnostic tool for trading system developer. In one graph it shows the sum total of the success or failure of the system being tested, and the resulting effect on your equity.

Numbers from Report telling of a drawdown is nice but with a graph, one can see how things were going before, during, and after a drawdown.

The line produced by the equity function tracks the amount of equity in your account. So, for example, if you backtest a system over the last year, you should see that, at the beginning of that year, the line starts at the amount of your your initial equity, and then rises and falls because, as each trade makes or loses money, the equity in your account will rise and fall. It shows how much money is in your account throughout the backtest period, so you can visually see how your system performed.

So, clearly you dont want to see the line go down - that means you lost money. It is generally accepted that you want to revise your system test parameters in order to get as close as possible to a smooth, straight, rising line. This means that your system has performed consistantly over time, and presumably over different market conditions. A line that goes up and down frequently means that your system works well only under certain conditions, and poorly under other conditions.

Single security Equity chart

To display single security Equity chart is is enough to click on "Equity" button in the Automatic Analysis window AFTER running a backtest.

This will plot the equity for currently active symbol and recently backtested system. If you want to see the Equity curve for another symbol - just switch to this symbol and Equity line will be recalculated automatically.

You can also choose symbol that was not included in the original backtest set and AmiBroker will calculate correct equity curve as it would look if real backtest was performed on it.

Equity function

Equity() function is a backtester-in-a-box. It has many interesting applications that will be outlined here. Let's look at the definition of Equity function:

SYNTAX equity( Flags = 0, RangeType = -1, From = 0, To = 0 )
RETURNS ARRAY
FUNCTION

Returns Equity line based on buy/sell/short/cover rules, buy/sell/short/coverprice arrays, all apply stops, and all other backtester settings.

Flags - defines the behaviour of Equity function

0 : (default) Equity works as in 3.98 - just calculates the equity array
1 : works as 0 but additionally updates buy/sell/short/cover arrays so all redundant signals are removed exactly as it is done internally by the backtester plus all exits by stops are applied so it is now possible to visualise ApplyStop() stops.
2 : (advanced) works as 1 but updated signals are not moved back to their original positions if buy/sell/short/cover delays set in preferences are non-zero. Note: this value of flag is documented but in 99% of cases should not be used in your formula. Other values are reserved for the future.

RangeType - defines quotations range being used:

-1 : (default) use range set in the Automatic analysis window
0 : all quotes
1 : n last quotes (n defined by 'From' parameter)
2 : n last days (n defined by 'From' parameter)
3 : From/To dates

From : defines start date (datenum) (when RangeType == 3) or "n" parameter (when RangeType == 1 or 2)

To: defines end date (datenum) (when RangeType == 3) otherwise ignored

datenum defines date the same way as DateNum() function as YYYMMDD
where YYY is (year - 1900), MM is month, DD is day

December 31st, 1999 has a datenum of 991231
May 21st, 2001 has a datenum of 1010521

All these parameters are evaluated at the time of the call of Equity function. Complete equity array is generated at once. Changes to buy/sell/short/cover rules made after the call have no effect. Equity function can be called multiple times in single formula.

EXAMPLE buy = your buy rule;
sell = your sell rule;
graph0 = Equity();
SEE ALSO  


Using Equity function we can build up Equity "indicator" that will work without the need to run backtester. Just type the following formula in the Indicator Builder:

buy = ... your buy rule ...
sell = .... your sell rule ...

graph0 = Equity();

Equity() function uses the buy/sell/short/cover rules that are defined BEFORE this function is called. The whole backtesting procedure is done inside Equity function that generates equity line.

Notes:

  1. Equity line is dependant of the parameters in the Automatic Analysis settings
  2. Equity traces Interest Earnings when you are OUT of the market.
    If you don't want this just enter 0 into "Annual interest rate" field in the settings.
  3. Equity also traces commissions. If commissions are not zero entry commission is taken using position size of the entry and exit commission is taken for each point to simulate how much money would you have if you closed position at given bar.
  4. AmiBroker uses SellPrice array for long trades and CoverPrice array for short trades to calculate current equity value.

Can I calculate system statistics using Equity function?

Yes you can. Here is a couple of example calculations kindly provided by Herman van den Bergen:

E = Equity();

//How rich you were :-)
Plot(Highest(E,"I should have sold",1,3);

//Your Current Drawdown:
Plot(Highest(E) - E,"Current DrawDown",4,1);

//Trade profits:
LongProfit = IIf(Sell,E - ValueWhen(Buy,E),0);
ShortProfit = IIf(Cover,ValueWhen(Short,E)-E,0);
Plot(IIf(Sell,LongProfit,0),"LProfit",8,2+4);
Plot(IIf(Cover,ShortProfit,0),"SProfit",4,2+4);

//Current Trade Profit:
Lastbar = Cum(1) == LastValue( Cum(1) );
Plot(IIf(LastBar AND pos,E-ValueWhen(Buy,E),ValueWhen(Short,E) - E),"Current Profit",9,2+4);

//DailyProfits:
Plot(IIf(pos,E-Ref(E,-1),Ref(E,-1)-E),"Daily Profits",7,2);

How do you plot a composite Equity Curve ? I don't want the whole database, but would like to see the curve based on the watchlist I use for the backtesting.

You can do this using the Equity and another powerful function called AddToComposite(). First modify your trading system by adding AddToComposite function as shown below:

/* your original system here */

Buy=Your Buy rule
Sell=Your Sell rule

Short=Your Short sell rule
Cover=Your Cover short rule

/* add the following line */

AddToComposite( Equity(), "~CompositeEquity", "X" );

Now run Scan (yes Scan, not back test) over the watch list of your choice (use Filter button in the Automatic Analysis to choose filtering criteria)

Now you will see ~CompositeEquity ticker created in your symbol list. You can just select it and the price chart you will see represents composite equity line for all symbols included in the scan.

You can access this line from AFL code at any time using Foreign function:

CompEq = Foreign( "~CompositeEquity", "C" );