简体   繁体   中英

Fast Fourier Transform Algorithm and Technical Analysis

My Mini Practice Project requires me to do:

  1. Download stock prices.
  2. Detrend stock prices.
  3. Smooth detrended stock prices.
  4. Apply FFT algorithm.

The Mini Practice Project Python code can be seen below:

#Python code: Download the Daily Stock Prices from Yahoo Finance 
from matplotlib.finance 
import quotes_historical_yahoo 
from pylab
import * import numpy as np import
scipy.signal as sc import
matplotlib.pyplot as plt 
import pandas as pd
ticker='AAPL' begdate=(2013,12,6) enddate=(2015,12,20)
data = quotes_historical_yahoo(ticker, begdate, enddate,asobject=True, adjusted=True)
aapl=data.aclose[1:]
np.count_nonzero(aapl)
plt.plot(aapl)
plt.title(‘Apple stock price movement’)

#Python code: Detrend stock prices
detrend=sc.detrend(aapl)
plt.plot(detrend)
plt.title(‘Apple stock detrended prices’) 

#Python code: Smooth Detrended Apple Stock Prices 
w=np.blackman(20) 

#we selected 20 the parameter of the blackman window function 
y=np.convolve(w/w.sum(),detrend,mode='same')
plt.plot(y) plt.title(‘Blackman window function for detrended Apple stock
price’) 

#Python code: Apply FFT Algorithm 
fft=abs(rfft(y)) plt.plot(fft)
plt.title(‘FFT Algorithm applied to Apple stock price’)

When I type >>> fft, the result should be: array([ 31.29635197, 2706.46455209, 1093.11797192, 904.02261366, 582.27538238, 282.87694269, 244.95336969, 501.27771573, 247.04690328, 554.24978967, 115.67400179, 270.14245787, 194.51970654, 179.0406388, 302.98350318, 170.32131932, 51.39420044, 87.25308608, 87.15654977, 39.34619432])

I keep getting a code error: from matplotlib.finance is an invalid syntax. How do I fix this?

from matplotlib.finance 
import quotes_historical_yahoo 

should be on the same line like so:

from matplotlib.finance import quotes_historical_yahoo

Alternatively, you can break lines:

from matplotlib.finance \
import quotes_historical_yahoo 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM