Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question, trying to retrofit your code to work with adj close, and volume to predict price #1

Open
thistleknot opened this issue Apr 17, 2021 · 4 comments

Comments

@thistleknot
Copy link

I read your article on medium

https://medium.com/swlh/a-technical-guide-on-rnn-lstm-gru-for-stock-price-prediction-bce2f7f30346

Modified part of the code to accept volume


def ts_train_test(all_data,time_steps,for_periods):
    '''
    input: 
      data: dataframe with dates and price data
    output:
      X_train, y_train: data from 2013/1/1-2018/12/31
      X_test:  data from 2019 -
      sc:      insantiated MinMaxScaler object fit to the training data
    '''
    # create training and test set
    #all_data.iloc[:,[0, -1]].values
    ts_train = all_data[:'2018'].iloc[:,[0, -1]].values
    ts_test  = all_data['2019':].iloc[:,[0, -1]].values
    ts_train_len = len(ts_train)
    ts_test_len = len(ts_test)

    # create training data of s samples and t time steps
    X_train = []
    y_train = []
    y_train_stacked = []
    for i in range(time_steps,ts_train_len-1): 
        X_train.append(ts_train[i-time_steps:i,0:])
        y_train.append(ts_train[i:i+for_periods,0:])
    X_train, y_train = np.array(X_train), np.array(y_train)

    # Reshaping X_train for efficient modelling
    X_train = np.reshape(X_train, (X_train.shape[0],X_train.shape[1],2))

    inputs = pd.concat((all_data[["Adj Close","Volume"]][:'2018'], all_data[["Adj Close","Volume"]]['2019':]),axis=0).values
    inputs = inputs[len(inputs)-len(ts_test) - time_steps:]

    inputs = inputs.reshape(-1,2)
    #inputs

    # Preparing X_test
    X_test = []
    for i in range(time_steps,ts_test_len+time_steps-for_periods):
        X_test.append(inputs[i-time_steps:i,0:])

    X_test = np.array(X_test)
    X_test = np.reshape(X_test, (X_test.shape[0],X_test.shape[1],2))

    return X_train, y_train , X_test

X_train, y_train, X_test = ts_train_test(all_data,5,2)
X_train.shape[0],X_train.shape[1]

but when I get to this part, I'm confused how to modify the hidden layers

def simple_rnn_model(X_train, y_train, X_test):
    '''
    create single layer rnn model trained on X_train and y_train
    and make predictions on the X_test data
    '''
    # create a model
    from keras.models import Sequential
    from keras.layers import Dense, SimpleRNN
    
    my_rnn_model = Sequential()
    my_rnn_model.add(SimpleRNN(64, return_sequences=True))
    #my_rnn_model.add(SimpleRNN(32, return_sequences=True))
    #my_rnn_model.add(SimpleRNN(32, return_sequences=True))
    my_rnn_model.add(SimpleRNN(64))
    my_rnn_model.add(Dense(2)) # The time step of the output

    my_rnn_model.compile(optimizer='rmsprop', loss='mean_squared_error')

    # fit the RNN model
    my_rnn_model.fit(X_train, y_train, epochs=100, batch_size=150, verbose=0)

    # Finalizing predictions
    rnn_predictions = my_rnn_model.predict(X_test)

    return my_rnn_model, rnn_predictions

my_rnn_model, rnn_predictions = simple_rnn_model(X_train, y_train, X_test)
rnn_predictions[1:10]
@Blessvskp
Copy link

The original code of stock.py is reporting syntax error!
I have raised a pull request. Since the author did not respond to you, I am not sure whether he will attend to my pull request.

Can you kindly help me ?
Stock py_Capture

@thistleknot
Copy link
Author

thistleknot commented Aug 26, 2021 via email

@Blessvskp
Copy link

Blessvskp commented Aug 27, 2021 via email

@dataman-git
Copy link
Owner

Dear Dr. Sai:
Python dependencies can result in discontinuity if there is any update in the imported modules. For this tutorial, I incorporated the class code from ta-lib in stock2.py. It does not need to import "ta-lib" and you can replace stock.py with stock2.py. It still needs the 'yfinance' module and 'streamlit' module. You are advised to 'pip3 install streamlit.

https://github.com/dataman-git/codes_for_articles/blob/master/stock2.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants