加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Load_data.py 4.89 KB
一键复制 编辑 原始数据 按行查看 历史
import os
import numpy as np
import csv
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter
import math
## import data
df = pd.read_csv('DATA.csv', parse_dates=['Date'])
print(df.head())
print(df.tail())
print(df.shape)
print(df.columns)
# Create Apple stock price plot
## https://www.earthdatascience.org/courses/use-data-open-source-python/use-time-series-data-in-python/date-time-types-in-pandas-python/customize-dates-matplotlib-plots-python/
fig, ax = plt.subplots(figsize=(10, 3))
ax.plot(df['Date'], df['Close'], label='Apple stock')
ax.set(xlabel="Date",
ylabel="USD",
title="Apple Stock Price")
date_form = DateFormatter("%Y")
ax.xaxis.set_major_formatter(date_form)
plt.show()
# Calculate technical indicators
def get_technical_indicators(data):
# Create 7 and 21 days Moving Average
data['MA7'] = data.iloc[:, 4].rolling(window=7).mean()
data['MA21'] = data.iloc[:, 4].rolling(window=21).mean()
# Create MACD
data['MACD'] = data.iloc[:, 4].ewm(span=26).mean() - data.iloc[:, 1].ewm(span=12, adjust=False).mean()
# Create Bollinger Bands
data['20SD'] = data.iloc[:, 4].rolling(20).std()
data['upper_band'] = data['MA21'] + (data['20SD'] * 2)
data['lower_band'] = data['MA21'] - (data['20SD'] * 2)
# Create Exponential moving average
data['EMA'] = data.iloc[:, 4].ewm(com=0.5).mean()
# Create LogMomentum
data['logmomentum'] = np.log(data.iloc[:, 4] - 1)
return data
T_df = get_technical_indicators(df)
# Drop the first 21 rows
# For doing the fourier
dataset = T_df.iloc[20:, :].reset_index(drop=True)
# Getting the Fourier transform features
def get_fourier_transfer(dataset):
# Get the columns for doing fourier
data_FT = dataset[['Date', 'Close']]
close_fft = np.fft.fft(np.asarray(data_FT['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))
fft_list = np.asarray(fft_df['fft'].tolist())
fft_com_df = pd.DataFrame()
for num_ in [3, 6, 9]:
fft_list_m10 = np.copy(fft_list);
fft_list_m10[num_:-num_] = 0
fft_ = np.fft.ifft(fft_list_m10)
fft_com = pd.DataFrame({'fft': fft_})
fft_com['absolute of ' + str(num_) + ' comp'] = fft_com['fft'].apply(lambda x: np.abs(x))
fft_com['angle of ' + str(num_) + ' comp'] = fft_com['fft'].apply(lambda x: np.angle(x))
fft_com = fft_com.drop(columns='fft')
fft_com_df = pd.concat([fft_com_df, fft_com], axis=1)
return fft_com_df
# Get Fourier features
dataset_F = get_fourier_transfer(dataset)
Final_data = pd.concat([dataset, dataset_F], axis=1)
print(Final_data.head())
Final_data.to_csv("Finaldata_with_Fourier.csv", index=False)
def plot_technical_indicators(dataset, last_days):
plt.figure(figsize=(16, 10), dpi=100)
shape_0 = dataset.shape[0]
xmacd_ = shape_0 - last_days
dataset = dataset.iloc[-last_days:, :]
x_ = range(3, dataset.shape[0])
x_ = list(dataset.index)
# Plot first subplot
plt.subplot(2, 1, 1)
plt.plot(dataset['MA7'], label='MA 7', color='g', linestyle='--')
plt.plot(dataset['Close'], label='Closing Price', color='b')
plt.plot(dataset['MA21'], label='MA 21', color='r', linestyle='--')
plt.plot(dataset['upper_band'], label='Upper Band', color='c')
plt.plot(dataset['lower_band'], label='Lower Band', color='c')
plt.fill_between(x_, dataset['lower_band'], dataset['upper_band'], alpha=0.35)
plt.title('Technical indicators for Apple - last {} days.'.format(last_days))
plt.ylabel('USD')
plt.legend()
# Plot second subplot
plt.subplot(2, 1, 2)
plt.title('MACD')
plt.plot(dataset['MACD'], label='MACD', linestyle='-.')
plt.hlines(15, xmacd_, shape_0, colors='g', linestyles='--')
plt.hlines(-15, xmacd_, shape_0, colors='g', linestyles='--')
plt.plot(dataset['logmomentum'], label='Momentum', color='b', linestyle='-')
plt.legend()
plt.show()
plot_technical_indicators(T_df, 400)
def plot_Fourier(dataset):
data_FT = dataset[['Date', 'Close']]
close_fft = np.fft.fft(np.asarray(data_FT['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))
fft_list = np.asarray(fft_df['fft'].tolist())
plt.figure(figsize=(14, 7), dpi=100)
fft_list = np.asarray(fft_df['fft'].tolist())
for num_ in [3, 6, 9]:
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(data_FT['Close'], label='Real')
plt.xlabel('Days')
plt.ylabel('USD')
plt.title('Apple (close) stock prices & Fourier transforms')
plt.legend()
plt.show()
plot_Fourier(dataset)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化