السلام عليكم
لدي كود بايثون واحتاج اضافته على بيانات السوق المالي ( السعودي )
يتحرك هذا الكود على التنبؤ بحركة السعر القادمة
وهذا هو الكود
#importing required libraries
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM
#to plot within notebook
import matplotlib.pyplot as plt
# function to calculate percentage difference considering baseValue as 100%
def percentageChange(baseValue, currentValue):
return((float(currentValue)-baseValue) / abs(baseValue)) *100.00
# function to get the actual value using baseValue and percentage
def reversePercentageChange(baseValue, percentage):
return float(baseValue) + float(baseValue * percentage / 100.00)
# function to transform a list of values into the list of percentages. For calculating percentages for each element in the list
# the base is always the previous element in the list.
def transformToPercentageChange(x):
baseValue = x[0]
x[0] = 0
for i in range(1,len(x)):
pChange = percentageChange(baseValue,x[i])
baseValue = x[i]
x[i] = pChange
# function to transform a list of percentages to the list of actual values. For calculating actual values for each element in the list
# the base is always the previous calculated element in the list.
def reverseTransformToPercentageChange(baseValue, x):
x_transform = []
for i in range(0,len(x)):
value = reversePercentageChange(baseValue,x[i])
baseValue = value
x_transform.append(value)
return x_transform
#read the data file
df = pd.read_csv('D:\\python3\\data\\SensexHistoricalData.csv')
# store the first element in the series as the base value for future use.
baseValue = df['Close'][0]
# create a new dataframe which is then transformed into relative percentages
data = df.sort_index(ascending=True, axis=0)
new_data = pd.DataFrame(index=range(0,len(df)),columns=['Date', 'Close'])
for i in range(0,len(data)):
new_data['Date'][i] = data['Date'][i]
new_data['Close'][i] = data['Close'][i]
# transform the 'Close' series into relative percentages
transformToPercentageChange(new_data['Close'])
# set Dat column as the index
new_data.index = new_data.Date
new_data.drop('Date', axis=1, inplace=True)
# create train and test sets
dataset = new_data.values
train, valid = train_test_split(dataset, train_size=0.99, test_size=0.01, shuffle=False)
# convert dataset into x_train and y_train.
# prediction_window_size is the size of days windows which will be considered for predicting a future value.
prediction_window_size = 60
x_train, y_train = [], []
for i in range(prediction_window_size,len(train)):
x_train.append(dataset[i-prediction_window_size:i,0])
y_train.append(dataset[i,0])
x_train, y_train = np.array(x_train), np.array(y_train)
x_train = np.reshape(x_train, (x_train.shape[0],x_train.shape[1],1))
# create and fit the LSTM network
# Initialising the RNN
model = Sequential()
# Adding the first LSTM layer and some Dropout regularisation
model.add(LSTM(units = 50, return_sequences = True, input_shape = (x_train.shape[1], 1)))
model.add(Dropout(0.2))
# Adding a second LSTM layer and some Dropout regularisation
model.add(LSTM(units = 50, return_sequences = True))
model.add(Dropout(0.2))
# Adding a third LSTM layer and some Dropout regularisation
model.add(LSTM(units = 50, return_sequences = True))
model.add(Dropout(0.2))
# Adding a fourth LSTM layer and some Dropout regularisation
model.add(LSTM(units = 50))
model.add(Dropout(0.2))
# Adding the output layer
model.add(Dense(units = 1))
# Compiling the RNN
model.compile(optimizer = 'adam', loss = 'mean_squared_error')
# Fitting the RNN to the Training set
model.fit(x_train, y_train, epochs = 100, batch_size = 32)
#predicting future values, using past 60 from the train data
# for next 10 yrs total_prediction_days is set to 3650 days
total_prediction_days = 3650
inputs = new_data[-total_prediction_days:].values
inputs = inputs.reshape(-1,1)
# create future predict list which is a two dimensional list of values.
# the first dimension is the total number of future days
# the second dimension is the list of values of prediction_window_size size
X_predict = []
for i in range(prediction_window_size,inputs.shape[0]):
X_predict.append(inputs[i-prediction_window_size:i,0])
X_predict = np.array(X_predict)
# predict the future
X_predict = np.reshape(X_predict, (X_predict.shape[0],X_predict.shape[1],1))
future_closing_price = model.predict(X_predict)
train, valid = train_test_split(new_data, train_size=0.99, test_size=0.01, shuffle=False)
date_index = pd.to_datetime(train.index)
#converting dates into number of days as dates cannot be passed directly to any regression model
x_days = (date_index - pd.to_datetime('1970-01-01')).days
# we are doing prediction for next 5 years hence prediction_for_days is set to 1500 days.
prediction_for_days = 1500
future_closing_price = future_closing_price[:prediction_for_days]
# create a data index for future dates
x_predict_future_dates = np.asarray(pd.RangeIndex(start=x_days[-1] + 1, stop=x_days[-1] + 1 + (len(future_closing_price))))
future_date_index = pd.to_datetime(x_predict_future_dates, origin='1970-01-01', unit='D')
# transform a list of relative percentages to the actual values
train_transform = reverseTransformToPercentageChange(baseValue, train['Close'])
# for future dates the base value the the value of last element from the training set.
baseValue = train_transform[-1]
valid_transform = reverseTransformToPercentageChange(baseValue, valid['Close'])
future_closing_price_transform = reverseTransformToPercentageChange(baseValue, future_closing_price)
# recession peak date is the date on which the index is at the bottom most position.
recessionPeakDate = future_date_index[future_closing_price_transform.index(min(future_closing_price_transform))]
minCloseInFuture = min(future_closing_price_transform);
print("The stock market will reach to its lowest bottom on", recessionPeakDate)
print("The lowest index the stock market will fall to is ", minCloseInFuture)
# plot the graphs
plt.figure(figsize=(16,8))
df_x = pd.to_datetime(new_data.index)
plt.plot(date_index,train_transform, label='Close Price History')
plt.plot(future_date_index,future_closing_price_transform, label='Predicted Close')
# set the title of the graph
plt.suptitle('Stock Market Predictions', fontsize=16)
# set the title of the graph window
fig = plt.gcf()
fig.canvas.set_window_title('Stock Market Predictions')
#display the legends
plt.legend()
#display the graph
plt.show()
وكانت البيانات المرفقة في الكود على السوق المالي ( الهندي )
من يقوم بهذا العمل في اقرب وقت مع الجودة في المخرجات
يمكنني القيام بالعمل المطلوب , تم تحميل الكود و جعله يعمل, راسلني لرؤية النتائج و مناقشة التفاصيل اكثر , في انتظار ردك. اتطلع للعمل معك.
عزيزي صاحب المشروع إن الكود المرفق يستخدم تقنية Deep-Learning التعلم العميق في الذكاء الصنعي وهو محور اختصاصي يمكنني شرحه لك بسهولة مع إعداد تقرير مبسط
السلام عليكم أستاذ عبدالله. تحياتي وأرجو أن تكون بخير، بعد رؤية الكود والملف المرفق يمكنني ببساطة القيام بتنفيذ المشروع بكل أريحية وسهولة فهو يستخدم تقنيات الذك...