Joined Nov 11, 2007
1,458 Blog Posts

# PercentRank Function for AmiBroker

Friend of the blog, Ramon Cummins has graciously agreed to share his PercentRank function for AmiBroker language. This is a really useful function. It returns the rank of a value in a set of values as a percentage of the set. In other words, if an indicator produces a raw value, this function allows one to rank the raw value as a percentage of previous values.

//PercentRank Function
//As Coded by Ramon Cummins
function PercentRank( Data, Periods )
{
Count = 0;
for ( i = 1; i < Periods + 1 ; i++ )
{
Count = Count + IIf ( Ref( Data, 0 ) > Ref( Data, -i ), 1, 0 );
}
return 100 * Count / Periods;
}

//Now, lets use the function to rank  Average True Range in percentage terms relative to previous ATRs.
//Define ATR as a percentage
ATR_Percentage=(ATR(10)/C)*100;
//Here we define the lookback period (30 days) to compare current to previous ATR%
RankPeriods = Param( “PercentRank Lookback Periods”, 30,5,252,5 );
//Here we use the PercentRank function to rank the ATR% relative to previous ATR%s
ATR_Bounded = PercentRank( ATR_Percentage, RankPeriods );
if ( ParamToggle( “Show ATR_Bounded”, “No|Yes”, 1 ) == 1  )
{
Plot( ATR_Bounded, _DEFAULT_NAME(), ParamColor( “Color”, colorCycle ), ParamStyle( “Style” ) );
}

And below is a screenshot showing the ATR_Bounded applied to the Qs.

## Top Ten Power Dips for Wednesday

1. [[HPJ]]

2. [[IVAN]]

3. Gramercy Capital Corp. [[GKK]]

4. [[SCSS]]

5. The Talbots, Inc. [[TLB]]

6. Quantum Corporation [[QTM]]

7. New Gold Inc. (USA) [[NGD]]

8. Gold Fields Limited (ADR) [[GFI]]

9. Xyratex Ltd. [[XRTX]]

10. Vivo Participacoes SA (ADR) [[VIV]]

There are no sell signals for these currently open positions, listed below:

Align Technology, Inc. [[ALGN]]

Amylin Pharmaceuticals, Inc. [[AMLN]]

Compania de Minas Buenaventura SA (ADR) [[BVN]]

[[DENN]]

DISH Network Corp. [[DISH]]

Flowers Foods, Inc. [[FLO]]

RenaissanceRe Holdings Ltd. [[RNR]]

Silver Standard Resources Inc. (USA) [[SSRI]]

## Power Dip Trades for Tuesday

Still no sell signals for open positions.

On the open, the system will purchase Align Technology, Inc. [[ALGN]] , Amylin Pharmaceuticals, Inc. [[AMLN]] , and Compania de Minas Buenaventura SA (ADR) [[BVN]] .

Be very careful blindly purchasing stocks during earnings season. I’m pretty sure at least one of the above names reports soon.

The system is now long 8 stocks, with a maximum allowance for 10 positions.

Have a great day!

## Checking in on the 5 Day Rate-of-Change Simple System

Last week I wrote about a very simple rotational system where one would rotate in long the top 20 stocks with the highest negative 5 day rate-of-change and hedge those by shorting the top 20 stocks with the highest positive 5 day rate-of-change.

The post generated some interest, and so I wanted to follow up by checking in on how those 40 positions performed. The spreadsheet with the results is below.

The system generated profits of \$236.90 with an average trade of 0.24%. The win percentage was 47.5%.

This type of performance would generate a compound annual rate of 11.32%.

Unfortunately, commissions were not included. If we allowed .005/share for commissions, the system just barely breaks even.

### Some Observations…

As David Varadi has already begun to demonstrate, there are many tweaks and variations to be made before one could begin to consider this a viable system. However, with only one week’s worth of data, we can make some observations that are likely to improve performance.

1. What is the market doing? If the market is in an uptrend, say above the 50 day average, perhaps we reduce our short exposure and increase our long exposure?

2. Using the Maximum Favorable Excursion (MFA) and Maximum Adverse Excursion (MAE) we can begin to think about using stops, profit targets, and dynamic exits to improve performance.

3. Since this system basically just ranks stocks, how do we refine our ranking characteristics/criteria to better select stocks in order to improve performance, and possibly reduce exposure and commissions?

In summary, if one has ever wondered how hedge funds approach the market, when they have hundreds of millions to deploy and need a strategy that scales, this method, while very basic and overly simplistic, with refinement should generate returns better than the benchmarks.

Below, purely for fun and chart porn, is the equity curve from this system, from 1/1/2000 to today. Of course commissions and slippage were not included. It shows a compound annual rate of 18.11% with a max drawdown of 16.77%.

## Power Dip Trades for Monday

The system has bought [[DENN]] and RenaissanceRe Holdings Ltd. [[RNR]] at the open.

It is now long Flowers Foods, Inc. [[FLO]] , Silver Standard Resources Inc. (USA) [[SSRI]] , and DISH Network Corp. [[DISH]] , with the addition of the two positions above.

Have a great day!

## Power Dip System Health and Week In Review

#### For the week, the Power Dip was up 0.53% while the [[SPY]] gained 1.52% (from last Friday’s to this Friday’s close).

Discipline is perhaps the most overlooked factor when considering how to successfully trade a mechanical system. For example, what should one do when his or her system is under-performing a benchmark? Should it be tossed to the curb? Does the under-performance mean it is broken? It may or may not be obvious, but I ask these questions more for rhetorical value, as the Power Dip continues to under-perform the indices. It is during these times that it is most important that one possess the discipline to stick with the system.

Control charts can be used to better understand what is happening with a system. Control charts give a “look under the hood,” allowing the mechanical trader a deeper understanding of system performance. If the control charts show everything to be within normal expectations, one may find it easier to stay disciplined when the system has difficult periods.

### System Health Control Charts

Below I present two charts.

The first shows two averages: the Total Average percentage gain/loss and the 20 Trade Average percentage gain/loss.

The second also shows two averages: the Total Average win percentage and the 20 Trade Average win percentage.

Note the Law of Large Numbers in action as system takes 80 trades before the average trade approximates the backtest generated average trade. In other words, the TotalAvg is volatile until it is smoothed by more and more data.

The 20TradeAvg remains volatile as it will only average the last 20 trades.

The chart shows that the system has seen a worse period, where 20 trade average dipped to ~2.6%. The system recovered quickly and peaked with an average trade near 3.5%.

The Win Percentage graph is interesting as it shows that the system beat historical expectations of percentage of winning trades for a span of approximately 70 trades (or half the number of trades made since I’ve tracked in on the blog).

### How To Interpret the Charts

The charts show that the Power Dip oscillates between over and under-performing. This is normal. Think of it as reversion to the mean. As long as the total average is consistent with historical expectations, and the 20TradeAvg is oscillating above and below the total average, everything is okay.

To get an even better understanding, we would use the data generated from backtesting and then add our real-time data for comparison. The results will not match, due primarily to slippage, but it will give a long history so that we get an idea of what is normal in terms of the oscillation of the short average around the long average.

These types of calculations can also be used to turn systems off and on, but that is beyond the scope of tonight’s post.

Below is the updated spreadsheet, with the metrics for all trades on top and October’s trades below.

The system has open Flowers Foods, Inc. [[FLO]] , DISH Network Corp. [[DISH]] , and Silver Standard Resources Inc. (USA) [[SSRI]] . It will add two more positions on Monday’s open.