# Beginner FFT (Fourier) transforms on closing prices for Apple

I don't know math very well, but I have been programming for many years.

I would like to use FFT as a parameter to a ML model. The FFT is diving down sharply. I tried many stocks and its the same.

import pandas as pd
import io
import requests
import datetime
import matplotlib.pyplot as plt
import numpy as np

############################################################
# API KEY IS FREE FOR AAPL - PLEASE FEEL FREE TO USE PUBLICLY
url = "https://eodhistoricaldata.com/api/eod/AAPL.US?api_token=OeAFFmMliFG5orCUuwAKQ8l4WWFQ67YX&from=2016-01-01"
############################################################

s = requests.get(url).content

df = df[:-1] # drop last row
# df.drop(df.index[:7000])
df['Date'] = pd.to_datetime(df['Date'], format='%Y-%m-%d')
df.drop('Close', axis=1, inplace=True) # Drop unadjusted close

close_fft = np.fft.fft(np.asarray(df['Close'].tolist()))
fft_df = pd.DataFrame({'fft':close_fft})
fft_df['absolute'] = fft_df['fft'].apply(lambda x: np.abs(x))
fft_df['angle'] = fft_df['fft'].apply(lambda x: np.angle(x))
plt.figure(figsize=(14, 7), dpi=100)
fft_list = np.asarray(fft_df['fft'].tolist())
for num_ in [3,6,25]:
fft_list_m10= np.copy(fft_list); fft_list_m10[num_:-num_]=0
plt.plot(np.fft.ifft(fft_list_m10), label='Fourier transform with {} components'.format(num_))
plt.plot( df['Close'])
plt.xlabel('Days')
plt.ylabel('USD')
plt.title('Figure 3: Apple (close) stock prices & Fourier transforms')
# plt.legend()
plt.show()


Is there anything I can do to make the FFTs not drop in the last 30 bars and be where they are supposed to be?

• Seems to be Gibb´s phenomenon, maybe you can check how to fix that Jul 22, 2019 at 10:55
• It might help to model returns instead of prices. Jul 22, 2019 at 11:52