0
$\begingroup$

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.

Please help :-)

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 = pd.read_csv(io.StringIO(s.decode('utf-8')),header=0)

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
df.rename(columns={'Adjusted_close': 'Close'},inplace=True)



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()

FFT for AAPL since 01-01-2016

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?

$\endgroup$
  • $\begingroup$ Seems to be Gibb´s phenomenon, maybe you can check how to fix that $\endgroup$ – Vitomir Jul 22 at 10:55
  • $\begingroup$ It might help to model returns instead of prices. $\endgroup$ – Bob Jansen Jul 22 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.