ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  How to chart historical correlation
Posted by icasper on Feb-15-2012 18:49
Hi, Your product is super great.

I have 2 arrays of stocks data array1,array2 containing stocks closing prices. I want to plot
a historical correlation b/w these two prices. How can I do that. I did saw that somewhere
we have correlation there. Pls. advise with some example code.

Thanks

  Re: How to chart historical correlation
Posted by Peter Kwan on Feb-16-2012 01:26
Hi icasper,

I assume by "correlation", you mean "moving correlation".

To plot something, the first step is to obtain the data. You may compute them with your own code (many of our customers are using their own code to compute their own "secret" technical indicators). The ChartDirector ArrayMath object also implements some common functions, such as moving average, moving correlation, moving percentile, moving standard deviation, etc..

For example, in PHP, the code is like:

#compute 20 days moving correlation
$a = new ArrayMath($firstPriceSeries);
$a->movCorr(20, $secondPriceSeries);
$dataToPlot = $a->result();

#now you can plot the data as usual. For example, in an XYChart, you may use:
$myLineLayer = $c->addLineLayer($dataToPlot, 0x008800);

Hope this can help.

Regards
Peter Kwan

  Re: How to chart historical correlation
Posted by icasper on Feb-16-2012 10:04
This is great. Once quick question on the same.

I have 2 arrays with 2 stocks data with date and last price i.e.

Stock A - array1 (dates, prices)
Stock B - array2 (dates, prices)

Both stocks will have dates common as well. so the table looks like;

date| stock a price | stock b price

I want to plot the correlation b/w them as a line and then put the dates on the X-axis. can u modify the code given earlier as above.

Many thanks

  Re: How to chart historical correlation
Posted by Peter Kwan on Feb-17-2012 00:37
Hi icasper,

You would need to decide what you want to correlate. For example, you can correlate this year's price with last year's price (to check if the price fluctuates periodically or seasonally), or correlates today's price of one stock with the price of another stock one week ago (to check if one stock is a leading indicator of another stock). If you want to correlate two stock price at the same date, please provide two data series representing the prices of the two stocks at the same dates.

Suppose your have obtained the two data series you want to correlate, the code is as explained in my last message:

#compute 20 days moving correlation
$a = new ArrayMath($firstPriceSeries);
$a->movCorr(20, $secondPriceSeries);
$dataToPlot = $a->result();

#now you can plot the data as usual. For example, in an XYChart, you may use:
$myLineLayer = $c->addLineLayer($dataToPlot, 0x008800);

(ChartDirector cannot automatically go to your database to obtain the data you want. You would need to write the necessary database code to get the data you want to correlate. After that you can pass the data to ChartDirector to plot the chart.)

Hope this can help.

Regards
Peter Kwan