Trading the Equity Curve - Amibroker - Aussie Stock Forums

1. ## Trading the Equity Curve - Amibroker

Hi Howard,

In regards to your code below (thank you for sharing this). Do I run my normal system to produce the equity curve, then run your code below?

OR do I add normal Buy and Sell signals to your code below and then backtest the lot?

I think maybe the first one as you wrote "This code shows one example of applying trading system logic to the equity curve created by another trading system. If this is the case then I guess walk forward wouldn't be able to be done with this idea as it wouldn't work during the walk forward optimisations?

Code:
```///////////////////////////////////////////
//    EquityCurveFeedback.afl
//
//    There are many ways to use the equity
//    in the trading account, and its variation,
//    to act as a filter to allow or block trades.
//
//    This code shows one example of applying
//    trading system logic to the equity curve
//    created by another trading system.
//
//    The first system is a simple moving
//    average crossover -- it could be any system.
//
MALength1 = 14; //Optimize("MALength1",14,2,50,2);
MALength2 = 13; //Optimize("MALength2",13,1,51,2);

e = Equity();

Plot(C,"C",colorBlack,styleCandle);

Plot(e,"equity",colorGreen,styleLine|styleLeftAxisScale,15000,18000);

//    The equity curve is choppy.
//    If there are positive serial correlations
//    between daily equity values, then we expect
//    up days to be followed generally by more up days,
//    and down days by more down days.
//    So we might try applying a trend following system
//    to the equity curve.
//    When the equity curve is above its moving average,
//    the system is working, and we should take trades.
//    When the equity curve is below its moving average,
//    the system is out-of-sync, and we should block trades.

//    EquityMALength is the lookback period for the equity
//    curve moving average.
//    We will set Pass to either 0 or 1.

EquityMALength = Optimize("EquityMALength",12,1,20,1);

EquityMA = DEMA(e,EquityMALength);

Plot(EquityMA,"equityMA",colorBrightGreen,styleLine|styleLeftAxisScale,15000,18000);

Pass = IIf(e>=EquityMA,1,0);

e1 = Equity();
Plot(e1,"FilteredEquity",colorBlue,styleLine|styleLeftAxisScale,15000,18000);

/////////////////////////```
When using the equity curve as a filter, you are applying a trading system to a series. If the series has a tendency to trend, then a trend-following equity curve system will work. If the series has a tendency to be mean reverting, then a mean reversion equity curve system will work.

The equity curve technique can be applied to either the daily marked-to-market equity, as in this example, or to the curve resulting from treating each closed trade as a "bar."

The equity curve filter is usually set up to act as a filter to prevent entry when the system has not been performing well lately. That is, it allows or prohibits entry, as in this example.

If the equity curve is monitored daily (or even intra-day), it can act as an exit signal, causing an exit based on equity performance before the other exit signals in the code. Or, as in this example, it can just wait for the exit logic in the base code to cause the exit.

So --- there are several options to try. When you find something useful, please post it back to this forum.

Thanks,
Howard

2. ## Re: Trading the Equity Curve - Amibroker

Good morning Sir Burr --

The code that I posted assumes you are trading end of day data, making trades on a daily time scale, and wish to use an equity curve module to allow or block entry into new trades, but not cause early exit from existing trades.

Look at the code following the comment in the code says "// The first system is a simple moving average crossover". The next four lines define a simple moving average crossover trading system. Replace those four lines with your trading system.

Further down in the code following the big block of comments is the equity curve filter part of the code. It uses the data series "e" (which contains the daily "shadow equity" -- what the equity would be if there is no filtering) and whatever logic you want to set the "pass" variable. In this case, if current daily shadow equity is above its moving average, pass is set to "1" and "Buy" signals are enabled. If current daily shadow equity falls below its moving average, pass will be set to "0" and "Buy" signals will be blocked. When the equity rises to be above its moving average, Buys will again be allowed.

Enable all the plot statements and look at the charts. Selectively comment out some of the plot statements to see how the equity curve filter acts.

To disable the equity curve filter and allow all trades, comment out the following line:
Pass = IIf(e>=EquityMA,1,0);
and enter this line:
Pass = 1;

Or, to block all trades, enter this line:
Pass = 0;

The code I posted is a combination of two trading systems acting in sequence. The first is the normal system, the second is the equity curve filter. The equity curve filter is a trading system applied to a data series that is the daily equity.

The logic and variables in the afl that contains both the original trading system and the equity curve filter can be optimized. None of the equity curve variables are "special" in any way.

In fact, if you are planning to optimize the equity curve filter code, I recommend that all of the code -- both the original system and the equity curve portion -- be in the same afl. This way, you only need to set up one in-sample and out-of-sample sequence. If the original system is optimized and its "shadow equity curve" is computed and saved, and a separate equity curve filter program is run using the shadow equity curve as input (note that it can only use the out-of-sample portion of the shadow equity curve -- the in-sample portion is meaningless), then it too must be separated into in-sample and out-of-sample.

Thanks,
Howard

3. ## Re: Trading the Equity Curve - Amibroker

Originally Posted by howardbandy

Thanks,
Howard
Howard,

Thank you very much for going to the trouble of posting these details. Much clearer now.

I thought the custom backtester would be needed to perform a backtest using the equity curve.

Yes I will post results.

Regards SB

4. ## Re: Trading the Equity Curve - Amibroker

Originally Posted by howardbandy

Thanks,
Howard
Hi Howard,

Don't think I will post results.

You were right. Down ~30% on the period.

Thanks again, SB.

5. ## Re: Trading the Equity Curve - Amibroker

Howard/Sir Burr.

Ive played with a couple of other ideas,along the lines of an Equity curve "Switch"

(1) Plotting my universe as a composite chart along with my portfolio as another chart. Using an M/A of both charts as a measure of performance.
The system switches off buys when the portfolio under performs the Universe M/A.

(2) Using the universe V the index as another "switch" idea.

Havent done a great deal of work on this as time doesnt really allow me the luxury of hrs of "playing" but thought it worth a mention.

6. ## Re: Trading the Equity Curve - Amibroker

Originally Posted by tech/a
The system switches off buys when the portfolio under performs the Universe M/A.
Hi Tech

I think the other way round would work better, ie the system switches off buys when the Universe under performs the portfolio.

sam

7. ## Re: Trading the Equity Curve - Amibroker

Hi Tech --

Your idea is worth a try. Or, as Sam says, perhaps the opposite. It is easy to code in one more variable that uses your logic "straight up" or "inverted". Then run some tests.

Thanks,
Howard

8. ## Re: Trading the Equity Curve - Amibroker

Happy to try anything.

My "Logic" is that we would be trying with our portfolio to out perform the "Index" of our universe just as we attempt to out perform the Bourse we are trading---with equities.

Thinking a little further you can see that indexes or custom indexes can be made from groups of everything.
I had one going a while ago for all stocks I could find involved in Uranium.
So for instance you could have a portfolio running within them.

Fertilizers and Transport seem to be 2 new up and comers----you see my point.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Aussie Stock Forums