Equity function, Individual and Portfolio Equity Charts

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.

Individual (single-security) Equity chart

To display single security Equity chart is is enough to click on the drop down arrow on the "Equity" button and choose "Individual Equity" from the menu 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.

IMPORTANT: individual equity chart is single-security equity that does not show portfolio-level effects like skipping some of trades due to reaching maximum open position limit or funds being allocated to other securities, it also does not use some advanced functionality offered only by portfolio-level backtester. For more information see this.

Portfolio-level Equity chart

To display portfolio-level equity chart is is enough to double click on "Equity" button in the Automatic Analysis window or click on the drop down arrow on the "Equity" button and choose "Portfolio Equity" from the menu AFTER running a backtest.

Portfolio-level equity represents equity of your entire portfolio and reflects ALL real-world effects like skipping trades due to insufficient funds, reaching maximum number of open positions. It also reflects all scaling in/out, HoldMinBars effect, early exit fees and any other feature that you may have been using in your formula. Portfolio-level equity also by default shows the remaining cash in the portfolio. Using Parameters window (click with RIGHT mouse button over equity chart and select "Parameters" from the menu) you can turn on display of drawdown (underwater equity curve), number of bars sincel last equity high and linear regression of the equity.

Equity function

Equity() function is a single-security 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 Formula Editor and press Apply:

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.
  5. Equity() function is single-security and does not reflect portfolio-level effects like skipping trades, does not handle some advanced functionality offered only by portfolio-backtester. For more information see that table.

Portfolio Equity special symbol

After running portfolio-level backtest, AmiBroker writes the values of portfolio equity to special symbol "~~~EQUITY". This allows you to access portfolio-level equity of last backtest inside your formula. To do so, use Foreign function:

PortEquity = Foreign("~~~EQUITY", "C" );

This is exactly what for example built-in portfolio equity chart formula is doing.

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.

Just use Portfolio Level equity chart (see above). It represents actual portfolio backtest equity, so if you run your backtest on the watch list it will represent what you need. You can also write your own formula that does the same thing:

Plot( Foreign("~~~EQUITY", "C" ), "PL Equity", colorRed );