Stock Price Prediction in Python with PyTorch - Full Tutorial
Video Overview & Insights
In this video we will learn how to do stock price prediction in Python with PyTorch using an LSTM-based architecture.
Did you make the video with the indicators? I would like to see it.
DISCLAIMER: None of this is financial advice. This is purely a programming tutorial and I am not responsible for any financial decisions you make. Don't use this for actually trying to predict stock prices.
โพโพโพโพโพโพโพโพโพโพโพโพโพโพโพโพโพ
@NeuralNine Iโm having a hard time understanding yfinance and how live data connection works. Is there a delay? Can ML be applied to faster intraday assets like futures ES1! For shorter predictions next 15 mins or hour?
๐ Programming Books & Merch ๐
๐ The Python Bible Book: https://www.neuralnine.com/books/
Homebrew has no - -cask called YFinance to be install only binance which is a cryptocurrency trading app. ๐ฎ that is because it doesnโt exist in home brewed.
๐ป The Algorithm Bible Book: https://www.neuralnine.com/books/
๐ Programming Merch: https://www.neuralnine.com/shop
Thank you.
๐ผ Services ๐ผ
๐ป Freelancing & Tutoring: https://www.neuralnine.com/services
isn't the standard scaler fit on entire data cause data leakage, as we are using the mean and std of the entire data which is technically not available to us so we shouldn't use it, when testing for out of sample performance we are bound to get a little better results than we'd get in the real world here
๐ฅ๏ธ Setup & Gear ๐ฅ๏ธ: https://neuralnine.com/extras/
๐ Social Media & Contact ๐
bring some real deal, go deep dude. with all those indicators and some features that does make a difference. different targets
๐ฑ Website: https://www.neuralnine.com/
๐ท Instagram: https://www.instagram.com/neuralnine
Has anyone tried this on renko charts to get rid of the noise?
๐ฆ Twitter: https://twitter.com/neuralnine
๐คต LinkedIn: https://www.linkedin.com/company/neuralnine/
where is the code of this video??
๐ GitHub: https://github.com/NeuralNine
๐ Discord: https://discord.gg/JU4xr8U3dm
thanks for this bro
More User Perspectives
please provide LSTM explanation too in detail !
@aamirabbas6289I sent your code to ChatGPT for explanation, and it flagged some issues:
Problems in this code
1. Scaler input shape
StandardScaler.fit_transform expects 2D. You passed a 1D Series.
โ
Use:
df['Close'] = scaler.fit_transform(df[['Close']])
or df['Close'].to_frame().
Data leakage
You fit the scaler on all data before the train/test split. For time series, that leaks future info.
โ
Fit the scaler on the training portion only, then transform both train and test with that fitted scaler.
Window array shape
data = np.array(data) after appending Series gives a 2D array (n_windows, 30) โ no feature dimension.
But later you index ...,:-1,: which assumes 3D (batch, seq_len, features).
โ
Build windows as (seq_len, 1):
data = np.array([seq.values.reshape(-1, 1) for seq in data])
Tensor dtypes & device
torch.from_numpy will give float64 for floats; PyTorch layers expect float32.
And you never move tensors to device.
โ
Cast and move:
X_train = torch.from_numpy(...).float().to(device)
y_train = torch.from_numpy(...).float().to(device)
Constructor typos
__init__(self,input_dim,hidden_dime,num_layers,output_dim) but inside you assign self.hidden_dim = hidden_dim โ mismatch (hidden_dime vs hidden_dim).
โ
Make the arg name consistent.
What exactly youโre predicting
Your window of length 30 is split into:
X: first 29 steps
y: the 30th step within the same window
Thatโs fine (predict โnext step inside windowโ), but often for forecasting we prefer predict the next step after the window (i.e., use a 30-step input to predict step 31). Your current code is okay conceptually for a simple setup, just be clear on intent.
Truly one of the best. Just followed through step by step and coded using VS Code and MT5 XAUUSD Forex data. Any minor problems were 'typos' and using ChatGBT helped resolve very quickly. Printed the graphs to file. Still trying to figure out how to understand & use the information to improve MT5 (EA) trading. Excellent video - many thanks.
Been working with MT5 but this is my first venture with Python, VS Code and NNs / AI -so taken me from ZERO to much further than I could have 'predicted'
Thank you for the very good tutorial on how to use LSTM for predicting time series!
@kancukatattva6570Very useful, does someone know how to implement LSTM faster for a trading algo?? im having a hard time implementing model to test them out in papper money
@tripastian9554Would love to see a model trained on multiple indicators to predict sequences of values, maybe even a markov chain
@jiufu20:31 Would love more discussion here to get a better grasp over the dimension of the matrices.
@aurkomThanks a lot! Very useful and informative.
Nice topic, so please go ahead and make more like that :)
LSTM hell yeah!!! What i NEEDED from you. Chhers mate!!!
@lekhazadapa36๐ I truly appreciate the quality content you bring to this platform. I have a humble request โ could you please make a video on 'WorldQuant BRAIN'? It's a fascinating platform for financial data modeling and crowd-sourced alpha research, but thereโs very little quality information available on YouTube. Your detailed explanations would help so many of us who are curious about finance, quantitative research, and how BRAIN actually works in real life. Hope you consider it! ๐ Thanks for everything you do!"
@machapraveen7737Thanks
@kitzesplana5457you must use forward sliding window , move forward x price tics every epoch , also plot the real price , predicted price and train eval price plot , you need prevent overfiting , use at least 64 layer 3 deep , i can give you the code to play ,is just an advance moving average , you can predict only if you normal distribute a range of prices ....
@johnstream9172If anyone serious about stock price prediction, then you should first learn Econometrics before Deep Learning. People want to jump directly to Deep Learning. There is no shortcut.
@sinan_islamThanks a lot bro this is very helpful to us developers ๐ฅ๐ฏโ ๐๐๐๐๐ This demonstrate the power of python, market and matchine learning...is the feature. Definitely waiting for more content like this โค
@FHULUFHELONORMANMAMUSHIA-ng1upCan you guys stop making these ridiculous stock price prediction videos? You CANNOT predict stock prices and you CANNOT predict asset returns. You are simply overfitting a model and will not be able to generate any profit whatsoever with this stuff.
@JuliusKaesarthis is really good class!
@f_gun20Thanksโค
@andrew-ld1jgThanks ๐โค
@tiwariji9985First pin pls
@ScratchersTechThanks ๐
@andiglazkov4915