Equity
- calculate single-symbol equity line

Trading system toolbox
(AFL 2.0)


SYNTAX equity( Flags = 0, RangeType = -1, From = 0, To = 0 )
RETURNS ARRAY
FUNCTION NOTE: This function is left here for backward compatibility and is using old, single-security backtester. New coding should rather use portfolio-level equity (special ~~~EQUITY ticker).

Function:
Returns single-security 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.

IMPORTANT NOTE: Equity() function uses so called "old" single-security backtester that offers only subset of features of new backtester. To retrieve value of portfolio-level equity generated by new backtester use Foreign("~~~EQUITY", "C").

EXAMPLE Buy = //your Buy rule;
Sell = //your Sell rule;
Graph0 = Equity();
SEE ALSO

Comments:

Herman van den Bergen
psytek@magma.ca
2003-02-23 09:46:19
When the Equity function is called multiple times in a single formula one must be carefull when using it with ApplyStop().

Tomasz wrote: "Equity(1) changes buy/sell variables (evaluates stops - and writes them back to buy/sell arrays). If you are using non-zero delays both Equity calls will return different values because in first case exits are generated by stops (not delayed) and in second case STOP signals written back to buy/sell arrays are delayed (opposite to the first case).

Equity(1) affects the buy/sell variables. It is not a "no-operation" function. If you want a "no-op" you should use Equity( 0 ) to generate equity line.

This is by design and described in the User's Guide. AFL reference: Equity function and chart
Tomasz Janeczko
tj --at-- amibroker.com
2003-05-21 17:56:46
Using Equity( 1 ) evaluates stops and writes BACK
signals to sell/cover arrays. Equity(1) also removes
all extra signals.

Depending on kind of the stop various values
are written back to sell/cover array to enable you
to distinguish if given signal was generated by regular
rule or by stop.

1 - regular exit
2 - max. loss
3 - profit target
4 - trailing
5 - n-bar stop
6 - ruin stop

... your rules...
ApplyStop( stopTypeTrail, stopModePercent, 10, True );
Equity( 1 );
WriteIf( sell == 1, "Regular exit",
WriteIf( sell == 4, "Trailing stop", "" ) );
Tomasz Janeczko
tj --at-- amibroker.com
2003-05-29 05:27:07
When your formula uses Equity(1) you should avoid using built-in delays. Here is a story why:

Only BACKTESTER implements delays while EXPOLORATION and other modes do NOT.

Therefore Equity(1) must not delay signals by itself. However in order to perform equity calculations delays must be applied to match backtester output, so
AmiBroker when it encounters Equity(1) applies the delays (even in exploration, indicator, etc)
but just before end of the equity call AmiBroker must ADJUST BACK the signals, so Equity-adjusted buy/sell/short/cover arrays do NOT have delay applied.
This involves shifting updated bars back and this may cause problem if signal occurs
on the very last bar (because it is moved by delay outside the range).

To disable this shifting back in exploration (so exploration matches the output of backtester
with NON-ZERO delays) you need to use Equity( 2 )

On the other hand using Equity(2) in backtest formula causes double delay.
(one added by the equity function, second added by the backtester pass).

Built-in delays are designed to be used in BACKTESTER ONLY.
The intention is as follows: set non zero delays in the settings
now SINGLE formula can be used to BACKTEST and to get TODAY SIGNALS
for trading for tommorrow (in SCAN) mode.

Solution 1:
Embbed delays in the AFL code itself:
Buy = Ref( Buy, -1 );
Sell = Ref( Sell, -1 );

Solution 2:
When using Equity AND EXPLORATION
Use EQUITY( 2 ) but except of backtest mode
if( Status("action") != 5 ) e = Equity( 2 );
Tomasz Janeczko
tj --at-- amibroker.com
2006-08-13 08:30:15
IMPORTANT NOTE:

Equity() function is using OLD backtester that is missing some recently added features such as multiple-currency handling and scaling in/out.

New code should rather use new, portfolio-level backtester, i.e. ~~~EQUITY special ticker.

Refer to:
http://www.amibroker.com/guide/a_equity.html

for details about differences between new and old backtesters.

References:

The Equity function is used in the following formulas in AFL on-line library:

More information:

Updated on-line reference