Rafael Martínez - 4 months ago 39

Python Question

I am looking for seasonality in Google's Stock data. I already did it succesfully with R

`library(quantmod)`

library(TSA)

a=getYahooData("GOOGL",start=20130101,end=20160127,freq="daily")

a=log(a$Close)-lag(log(a$Close))

a=na.exclude(a)

periodogram(a)

Periodogram with R

Now I want to do it with Python because I've found better support for algorithmic trading available.

Here is my code

`import numpy as np`

from scipy import signal

import matplotlib.pyplot as plt

import pandas_datareader.data as web

import datetime

start = datetime.datetime(2013, 1, 1)

end = datetime.datetime(2016, 1, 27)

df = web.DataReader("GOOGL", 'yahoo', start, end)

# import data from yahoo finance

z=np.array(df)

# convert data to array in order to manipulate with numpy

y=z[:,5]

# column with close prices

logR=np.diff(np.log(y))

# logarithmic returns

periodgram = signal.periodogram(logR)

plt.plot(periodgram)

plt.show()

# periodgram graph

Periodogram with Python

What is wrong with my code in Python? Why do my periodograms look so different?

Answer

`periodogram`

returns a pair, `(f, Pxx)`

. You should be using:

```
...
f, Pxx = signal.periodogram(logR)
plt.plot(f, Pxx)
...
```

It still looks quite different from the R output; maybe you wanted a different column?

Note that you can access the columns of the pandas data frame directly, using:

```
logR = np.diff(np.log(df["Adj Close"]))
```

or if you want Close instead, using:

```
logR = np.diff(np.log(df.Close))
```