GithubHelp home page GithubHelp logo

stockprediction_python's Introduction

Introduction

Idea of this project is about to trying to predict stock price by using Recurrent Neural Network and Machine Learning.
This project is only about Python programming using financial data.
Source from: https://www.youtube.com/watch?v=PuZY9q-aKLw&list=WL&index=145&t=447s

How it work

We use ['Close'] data from Yahoo Finance from 2012-1-1 to 2021-5-28

# Load Data
stock_symbol = 'TSLA'

start_date = dt.datetime(2012, 1, 1)
end_date = dt.datetime(2021, 5, 28)

with 60 of prediction days

# how many days for the prediction days
prediction_days = 60

Then created 3 layers of LSTM with 50 nodes

# Build the model
model = Sequential()

# 1st layers
model.add(LSTM(units=50, return_sequences=True, input_shape=(x_train.shape[1], 1)))
model.add(Dropout(0.2))
# 2nd layers
model.add(LSTM(units=50, return_sequences=True))
model.add(Dropout(0.2))
# 3rd layers
model.add(LSTM(units=50))
model.add(Dropout(0.2))
model.add(Dense(units=1))  # Output Node, Prediction of the next closing price

Load testing Data

# Load Test Data
test_start = dt.datetime(2020,1,1)
test_end = dt.datetime.now()

test_data = web.DataReader(stock_symbol, 'yahoo', test_start, test_end)
actual_prices = test_data['Close'].values

Plot

# Plot the Test predictions
plt.plot(actual_prices, color='black', label=f"Actual {stock_symbol} price")
plt.plot(predicted_prices, color='green', label=f"Predict {stock_symbol} price")
plt.title(f"{stock_symbol} share price")
plt.xlabel('time')
plt.ylabel(f'{stock_symbol} share price')
plt.legend()
plt.show()

Predict price for next day

# Predicting the future price (next day)
real_data = [model_inputs[len(model_inputs)+1 - prediction_days:len(model_inputs+1)],0]
real_data = np.array(real_data)
real_data = np.reshape(real_data, (real_data.shape[0], real_data.shape[1],1))

prediction = model.predict(real_data)
prediction = scaler.inverse_transform(prediction)
print(f"Prediction: {prediction}")

stockprediction_python's People

Contributors

nordize avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.