For a deeper look into our Eikon Data API, look into:

# question

Accepted
43 4 5 10

## Computation of the correlation between a stock return and a given benchmark

Hi guys,

This is a general question following this thread.

Given a stock (let's say VOWG_p.DE) and a reference index (let's say .STOXX), a customer wants to compute/retrieve the correlation between the day to day return of the stock and the reference index price on a timeframe. Of course the stock returns include the dividends

what does a a python function which uses refinitiv.data library look like?

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvote
Accepted
6.6k 18 2 8

I am afraid there is no direct way of computing the correlation coefficient from the library. If I got your question correct, these are the steps I would take to calculate the coefficient:

1. Get the prices of the stock and the benchmark from Refinitiv.data:

```index_price = rd.get_history(universe = '.STOXX', fields='TRDPRC_1', start = '2023-07-01', end = '2023-08-14')
stock_price = rd.get_history(universe =  'VOWG_p.DE', fields='OFF_CLOSE', start = '2023-07-01', end = '2023-08-14')```

2. Calculate daily returns:

```index_return = index_price['TRDPRC_1'].pct_change().dropna()
stock_return = stock_price['OFF_CLOSE'].pct_change().dropna()```

3. Calculate the required correlation coefficient (e.g. Pearson, spearman) using Scipy library:

```import scipy.stats
scipy.stats.pearsonr(index_return.values, stock_return.values)```

This returns the the coefficient and the p-value:

`(0.45796402563762706, 0.012482344923260497)`

Hope this helps and please let me know if I missed anything while building my answer.

Best regards,

Haykaz

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

43 4 5 10

Thank you so much @haykaz.aramyan .

This is exactly what I was looking for

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvote
1.5k 5 3 6

You can also take the approach with the ADC computation capabilities to calculate beta:

`rd.get_data("VOWG_p.DE","RSLOPE('.STOXX',PERCENT_CHG(TR.PriceClose(Sdate=-5Y,Edate=0D,Curn=EUR,Frq=M),Lag=-1M))")`

1692003729737.png (6.0 KiB)

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.