To make FX predictions in deep learning (CNN) From CSV data (date and time, opening price, high price, low price, closing price are described) I would like to generate a large number of chart images.
Create the following chart image from CSV.
The CSV file is an hourly chart of USDJPY. (January 2007-September 2020) If you don't have 1 hour CSV data, please refer to this article . The contents of CSV are as follows. There are more than 80,000 lines.
The code is below.
import matplotlib.pylab as plt
import pandas as pd
import numpy as np
def make_sma(parab, arr=None):
"""
Function description: Adds an average moving line to the array received as an argument.
parab:period
arr:Array consisting of date and time, open price, high price, low price, close price
"""
row = arr.shape[1] #Get the number of columns in the array
arr = np.c_[arr, np.zeros((len(arr),1))] #Add column
for i in range(parab, len(arr)):
tmp = arr[i-parab+1:i+1,4].astype(np.float) #Enter the number within the period
arr[i,row] = np.mean(tmp) #Enter the value of the moving average
return arr
def dataframe_to_img(chart_range, img_name, df=None):
"""
Function description: Convert DataFrame to image and save.
chart_range:DataFrame range
img_name:Image save destination
df:DataFrame to draw
"""
df = df[0:chart_range]
plt.figure()
df.plot(legend=None) #Delete legend
plt.axis('off') #Border removal
plt.tick_params(labelbottom=False,
labelleft=False,
bottom=False,
left=False) #Border removal
plt.box(False) #Border removal
plt.savefig(img_name,bbox_inches="tight") #Remove margins and save
plt.close('all')
#Load csv into array
arr = np.loadtxt(r'CSV file', delimiter=",", skiprows=1, dtype='object')
#Added technical indicators
arr = make_sma(parab=25, arr=arr)
#Convert to DataFrame
col_name = ['Date',"Open","High","Low","Close","SMA"]
df = pd.DataFrame(arr,columns=col_name)
#Convert to DataFrame and save the image
df = df[df!=0].dropna() #Delete line 0
df = df[['Close','SMA']] #Only the columns to draw on the graph
df = df.astype('float') #Convert to float
chart_range = 360
for i in range(20):
try:
img_name = str(i) + '.png' #Image save destination
dataframe_to_img(chart_range, img_name, df=df[i:chart_range+i])
except IndexError:
pass
#DataFrame is also saved because it will be used as a correct label later.
df.to_csv(r'tarintest_labels.csv',encoding='utf_8_sig')
A description of the code. First, load the CSV with numpy.
Then, with ʻarr = make_sma (parab = 25, arr = arr)
`
Add a moving average to arr. The period of the moving average is specified by parab.
If you want to add long-term lines such as 75 and 200 instead of 25,
You can add it by changing the value of parab and adding it.
After adding the technical indicators,
With df = pd.DataFrame (arr, columns = col_name)
Convert arr to DataFrame.
chart_range = 360
is the range of charts to display.
This time it is a 1-hour CSV, so if it is 360, it will be displayed for 360 hours.
Finally, in the for statement, pass the DataFrame to the dataframe_to_img function
and save it one by one.
dataframe_to_img uses matplotlib.
Borders etc. are deleted because they interfere with learning in deep learning.
After outputting the image, save the DataFrame as CSV. This is to use it as a correct label for deep learning.
Make sure the image is saved.
This time it's just a simple moving average, It is also possible to display Bollinger Bands and Ichimoku Kinko Hyo.
If you find it helpful, please use LGTM. It will be encouraging of the update.
Recommended Posts