wandering trader banner

wandering trader

10 devlogs
30h 14m 58s

A machine learning model for stock prediction. Uses an LSTM model and training with past data with yahoo finance. Tried out some other models like random forest, gradient boosting (xgboost), and a basic neural net.

This project uses AI

used for researching models like random forest and xgboost, and LSTM. also used to code data manipulation for graphing. also used for README ofc

Repository

Loading README...

mattseq

i was pretty much at a wall. nothing i changed seemed to work. i tried using claude to generate a few different programs using binary classification or probability based ones, multi-horizon predictions, etc. none of them worked very well and i had no idea what claude was doing. i went back to my model and just changed some of the hyperparameters like the interval, sequence length, and hidden size. i also added a “smart” strategy which isnt really smart, it just buys only if the model signal is the same as the naive signal. might mess around with combining the model signal with some of these other signals later. but right now i need to get this shippable. here’s graphs of TSLA and WDC

Attachment
Attachment
0
mattseq

ok after a ton of messing around, i cant find a way to make it consistently better than just buying and holding. my theory is that it would work way better with daytrading rather than something this long-term. the shorter the timeframe the more the price is ruled by patterns rather than sentiment. the other path is parsing sentiment data from alpha vantage and giving that to the model, which i think could totally boost its performance. unfortunately alpha vantage intraday data is not free and the sentiment data is rate limited to 25 requests per day. either i find another source or i end the project here.

Attachment
0
mattseq

ok so i kept working on the lstm. i really dont want to give up or start over. i added a cool attention mechanism after the lstm output. not sure how much it helped but i also graphed how much each sequence timestep is valued using that which was cool to see. i also printed some more stuff like sharpe and directional accuracy. the model didnt seem to be picking up anything valuable but it was doing really well on both training and test sets so i made the model smaller to reduce overfitting and made a directional mse loss (not sure if that already exists with pytorch) so that it punishes it more for predicting the wrong signal (which is whats most important). its doing much better, at least for IBM and BA stocks but kind of breaks down for others.

Attachment
Attachment
Attachment
Attachment
0
mattseq

i dont even know what im doing anymore. i am completely and utterly lost. just about every lstm model demo i’ve seen online doesnt actually try to trade stocks based on their data and its clear that they might also struggle with the variance problem (not predicting prices that are varied and instead predicting prices close to the previous one). when i try predicting returns rather than prices or try predicting buy/sell signals, the model signals stagnate into just one buy/sell signal. random forest might actually be more promising

Attachment
Attachment
Attachment
0
mattseq

basically just messed around with smarter strategies and comparing strategies. spent a while thinking of and trying out some smarter strategies paired with the model. so far, i just have a really simple one that doesnt buy unless the predicted return is higher than a certain percent (not just 0). i also decided to plot the model’s prediction compared to the last closing price it was given to see if the model was just parroting the input. it seems like its actually predicting properly bc the difference fluctuates quite a bit and isnt extremely close to 0. lastly, i tried to recursively predict the stock prices by giving the model only the first 60 days and letting it feeds its own next day prediction into itself. i was hoping it might fluctuate at least a little, but it just kinda flops in a gentle slope. here are some of the graphs (not all are from the same cycle). looking at the main one with the cumulative returns, you can actually notice some interesting patterns from the model-based strategy compared to buy and hold

Attachment
Attachment
Attachment
Attachment
0
mattseq

GOT LSTM TO WORK! took a look at a few kaggle notebooks. basically i just made the model bigger, increased the sequence length, had it predict prices rather than just binary classification, and used MinMaxScaler. i also simplified features so its really just the close price.

Attachment
Attachment
0
mattseq

tried an LSTM model. not sure how, but the youtube algorithm figured out i’d be interested in this video about someone making an LSTM stock prediction model: https://www.youtube.com/watch?v=V2l7cZxUpQs. they’re using tensorflow which might actually be easier but theres not that much of a difference. unfortunately their code is not fully opensource, you have to be a github sponsor to gain access to it. i couldnt really get my lstm model to do very well, it kind of just accepts defeat and learns to just output the average of the outputs. here you can see that my targets are just 0 and 1 for up and down and the model just slowly starts to average out to predicting around a 0.5. i think im going to go back to random forest for a little while, there were some things i wanted to try out.

Attachment
0
mattseq

another 2 hrs down the train. i tried a ton of different configurations including relative features, expanding vs rolling windows for training and just made sure that there was no leakage of testing data into the training data. i also added vix as a volatility index but it doesnt look like it helped much. i also realized that i should prob be graphing the actual stock prices not just cumulative returns, so i can see where the model is failing. i also found an video of someone using LSTM for stock prediction, which i want to try, but that might take a lot more work. im probably going to either do that or add sentiment scores and other data to my feature set next.

Attachment
0
mattseq

tried gradient boosting models but they didnt seem that much better, maybe i’ll test them more thoroughly later (xgboost is much faster i think). i went back to random forest and i noticed something interesting. although the cumulative model returns are clearly below the buy and hold returns, it begins to more or less mirror them and actually starts gaining back the distance it lost. i changed the training window to expand instead of roll so that it gets as much training data as possible and then ran it on about 20 yrs. it started off rocky but then clearly began beating the buy and hold returns and compounding. im not sure if this is due to some sort of data leaking again from me, bc i double checked everything and it seems plausible with that much data.

Attachment
0
mattseq

sorry that this devlog is 5 hrs in, i wasnt really sure how far i’d get with this project. at first, i started by using neural networks which im most familiar with but they didnt get great accuracy. then i learned about random forest models (and other decision tree based models) and started using that. i was getting insanely high returns which at first was exciting but then i quickly realized that i was probably doing something wrong. as it turns out, i was accidentally leaking the targets to my model by how i was training it. after fixing that, random forest wasnt much better than buy and hold returns. so now im researching and testing gradient boosting models. this is the latest performance of random forest on IBM stocks from 2005 to 2025

Attachment
0