Correlation Indicator for Amibroker

A reader got in touch asking me to code a correlation indicator for Amibroker, it so happens that I’ve already done so and I figured that if one person wants the indicator then others might too.

Here is the Amibroker Formula for the Correlation Indicator…

////CorrelationIndicator////
Ticker2 = ParamStr(“Ticker”,”SPY”);
Index = Foreign(ticker2,”C”);
CorrLB = Param(“Lookback”,60,1,2000,1); //correlation look-back period//
Corr = Correlation( log(C/Ref(C,-1)), log(index/Ref(index,-1)), CorrLB);

Plot(Corr,”(” + Ticker2 + “)”,IIf(Corr>=0.8, colorRed, IIf(Corr>=0.6
AND Corr<0.8,colorOrange,IIf(Corr>=0.4
AND Corr<0.6, colorYellow,IIf(Corr>=0.2
AND Corr<0.4,colorLime,IIf(corr>=0.0
AND corr<0.2,colorPaleGreen,colorPink))))),0,0,0,0,0,3);

///////////////////////////////////////////////////

Copy and paste the formula to your editor and click Apply when viewing a chart pane.

If you right-click on the indicator and click “parameters”, you are able to choose the second ticker which is used to calculate the correlation co-efficient. The ticker already being viewed in your chart-pane is the first ticker.

You can also change the look-back period used to calculate the correlation co-efficient.

In the following example, I am viewing Facebook in my Chart pane and I want to see how correlated the daily returns of FB and Twitter (TWTR) are.

For this example I’ll use 100 days for the calculation..

Parameters

The results look like this…

CorrelationIndicatorExample

You’ll notice that the indicator is colour coded. These are the conditional colour parameters…

Very Highly Correlated = RED

Highly Correlated = ORANGE

Moderately Correlated = YELLOW

Weakly Correlated = GREEN

Very Weak to No Correlation = PALE GREEN

Negatively Correlated = PINK

Strategy Correlation

Analysing the correlation which exists between different stocks or asset classes is useful, but you can also use the indicator to determine how correlated different strategies are to one another.

To do this in Amibroker you need to add the following snippet of code to your strategy formula…

Source: https://www.amibroker.com/kb/2006/03/11/how-to-create-copy-of-portfolio-equity/

// The code for AmiBroker 5.50 and above //

// YOUR TRADING SYSTEM HERE // ….

SetCustomBacktestProc(“”);

if( Status(“action”) == actionPortfolio )

{

bo = GetBacktesterObject();

bo.Backtest();

AddToComposite( bo.EquityArray,

“~~~MY_EQUITY_COPY”, “X”,

atcFlagDeleteValues | atcFlagEnableInPortfolio );

}

////////////////////////////////////////////////////////

Before running your strategy back-test, remember to change “~~~MY_EQUITY_COPY” to something unique to your strategy.

For example, if you’re testing a Mean-Reversion Strategy, you might use.. “~~~MeanReversionStrat”

Once you’ve run the back-test, search your database of symbols for the newly created equity curve ticker. In this example, we’d search for ticker “~~~MeanReversionStrat”.

If you click on the ticker, the equity curve produced by your strategy will populate the chart pane…

MR STRAT

Next, repeat the process for a different strategy. For this example, I’ll test a breakout strategy and name the equity curve ticker “~~~SPYTrendStrat”.

After running the test, search your database of symbols for ticker “~~~SPYTrendStrat”. (Because the tickers begin with a treble tilde, they’re easy to find)

If you now open a Price(Foreign) chart pane, you can look at the equity curve produced by each strategy on a single screen…

Strategies

 

Finally, we can apply our correlation Indicator and using the indicator parameters setting we can select the second strategy equity curve ticker…

Correlation Indicator Amibroker settings

You can now analyse how correlated the daily returns of your strategies are, as well as analyse how correlated they have been in the past.

StrategyCorrelationCharts

 

Leave A Response

* Denotes Required Field