Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

Python Digital Signal Processing For Traders
#1

Digital Signal Processing is an important subject that is taught in Electrical Engineering degree courses. There are many books that try to apply digital signal processing to financial market prediction and algorithmic trading. I have started this thread in which I would discuss whether we can apply digital signal processing techniques in predicting the financial market. First we read the 1 minute GBPUSD price data and then use the scipy signal package to determine the peaks/valleys.


Code:
import scipy.signal
from peakutils.plot import plot as pplot
print('Detect peaks without any filters.')
df2=np.array(df['Close'])
df2=df2[-300:]
#indexes = scipy.signal.find_peaks_cwt(df2, np.arange(1, 30),\
#    max_distances=np.arange(1, 30)*2)
indexes = scipy.signal.find_peaks_cwt(-df2, np.arange(1, 20))
indexes = np.array(indexes) - 1
print('Peaks are: %s' % (indexes))
x=np.arange(0,len(df2))
y=df2
plt.figure(figsize=(40,40))
plt.title("Price")
plt.plot(x,y)
plt.show()
print(x[indexes], y[indexes])
plt.figure(figsize=(20,20))
pplot(x, y, indexes)
plt.title('Peaks')

#determine the peaks easily with this code
indexes = scipy.signal.find_peaks_cwt(df2, np.arange(1, 20))
Below is the screenshot of the valleys identified by scipy.signal.find_peaks_cwt package. This package first uses a wavelet transform and then determines the peaks.
[Image: peak1.png]
These were the valleys. We can also determine the peaks also very easily with the above code.
[Image: peak2.png]
Looking at the above two screenshots, you can judge how good is the algorithm in determining the peaks/valleys.

Subscribe My YouTube Channel:
https://www.youtube.com/channel/UCUE7VPo...F_BCoxFXIw

Join Our Million Dollar Trading Challenge:
https://www.doubledoji.com/million-dolla...challenge/
Reply
#2

There are problems with the scipy.signal.find_peaks_cwt algorithm. You can see in the screenshots that the peaks/valleys are not identified accurately. Let's try another package peakutils and see how good it is at identifying the peaks/valleys.
Code:
x=np.arange(0,len(df2))
y=df2
import peakutils as pu
indexes = pu.indexes(-y,thres=0.5, min_dist=30)
plt.figure(figsize=(40,40))
plt.title("Price")
plt.plot(x,y)
plt.show()
print(x[indexes], y[indexes])
plt.figure(figsize=(40,40))
pplot(x, y, indexes)
plt.title('Peaks')

#determine the peaks
indexes = pu.indexes(y,thres=0.5, min_dist=30)


Below is the screenshot for the valleys!
[Image: peak3.png]
Now these were the valleys. Let's check what are the peaks.
[Image: peak4.png]
As you can see peakutils package has a better performance as compared to scipy.signal.find_peaks_cwt.

Subscribe My YouTube Channel:
https://www.youtube.com/channel/UCUE7VPo...F_BCoxFXIw

Join Our Million Dollar Trading Challenge:
https://www.doubledoji.com/million-dolla...challenge/
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)