Bifalgorithm post

Let's build an AI trading bot that can trade stocks while you sleep. And we're pulling all of our data from the Yahoo Finance API which gives us access to all the trading data since the year 2000. And I'm caching all my data locally so every run I don't have to repool the data and reprocess it all over again. And the data I'm pulling is from 4 funds, the S&P 500, the NASDAQ, International Stocks and Government Bonds. And I built out 4 small programs that each work together to determine how much of the market we should own today. And the first program I kept pretty simple and it's basically just an if statement with 25 years of data. So the way that it works is that if the NASDAQ is above the average of its last 200 days, it stays in and if not, it gets out completely. And it also auto shrinks the position when the prices start swinging a bunch and this program gets 40% of the vote. For program two, it runs once a month and it compares US stocks to international stocks over the past year and it buys whichever one of those are winning. And if both of them are losing, it stops everything in bonds and it just waits. Program three was left pretty simple and it basically just buys the dip. So if the market drops a ton for two days straight, but the trend is still up, it buys and it sells as soon as it bounces back. And program four is the most complex out of all of them since it actually has the machine learning portion. It does gradient boosting on 14 features and then it predicts where the market is up or down. and And once we combine all four votes from each of the programs, those get blended into one target portfolio. And the trading bot goes ahead and finds a difference between this and compares it against what the account actually holds. So this means that it will only trade the differences. And to test it out, I replayed 21 years of market data through the code, including the last six years, which none of the rules were actually trained on. And the trading bot made money in 17 of the last 22 years. And when the market crashed 32%, the bot was only down 18. So when we actually go ahead and run it, it pulls today's prices, retrains the model and every program gets to vote. And I connected the bot to Alpaca so I can try this with paper trading. And when we run it, it buys a stock and it can hold it until the next day when it decides on what it wants to do next with that. And so far the bot has gained a bit of profit, but I'll have to see how it changes over time and how it improves as it gets more data. But the main issue is that it only works as long as your laptop's open, but once it dies or it falls asleep, it can't run the next cycle in the next day. So follow for part two where I show you how to host it completely in the cloud and allow it to trade automatically and improve by itself.

Let's build an AI trading bot that can trade stocks while you sleep. And we're pulling all of our data from the Yahoo Finance API which gives us access to all…

Ideas in this flowchart

  • AI Trading Bot Development
  • Data Sources
  • Yahoo Finance API
  • Data Caching
  • Data Types
  • S&P 500
  • NASDAQ
  • International Stocks
  • Government Bonds
  • Programs
  • Program 1: NASDAQ Analysis
  • Program 2: US vs International Stocks
  • Program 3: Buy the Dip
  • Program 4: Machine Learning
  • Portfolio Adjustment
  • Performance Testing
  • 21 Years of Data Replay
  • Profitability
  • Limitations
  • Laptop Dependency
  • Cloud Hosting Plan
Feed/

Let's build an AI trading bot that can trade stocks while you sleep. And we're pulling all of our data from the Yahoo Finance API which gives us access to all the trading data since the year 2000. And I'm caching all my data locally so every run I don't have to repool the data and reprocess it all over again. And the data I'm pulling is from 4 funds, the S&P 500, the NASDAQ, International Stocks and Government Bonds. And I built out 4 small programs that each work together to determine how much of the market we should own today. And the first program I kept pretty simple and it's basically just an if statement with 25 years of data. So the way that it works is that if the NASDAQ is above the average of its last 200 days, it stays in and if not, it gets out completely. And it also auto shrinks the position when the prices start swinging a bunch and this program gets 40% of the vote. For program two, it runs once a month and it compares US stocks to international stocks over the past year and it buys whichever one of those are winning. And if both of them are losing, it stops everything in bonds and it just waits. Program three was left pretty simple and it basically just buys the dip. So if the market drops a ton for two days straight, but the trend is still up, it buys and it sells as soon as it bounces back. And program four is the most complex out of all of them since it actually has the machine learning portion. It does gradient boosting on 14 features and then it predicts where the market is up or down. and And once we combine all four votes from each of the programs, those get blended into one target portfolio. And the trading bot goes ahead and finds a difference between this and compares it against what the account actually holds. So this means that it will only trade the differences. And to test it out, I replayed 21 years of market data through the code, including the last six years, which none of the rules were actually trained on. And the trading bot made money in 17 of the last 22 years. And when the market crashed 32%, the bot was only down 18. So when we actually go ahead and run it, it pulls today's prices, retrains the model and every program gets to vote. And I connected the bot to Alpaca so I can try this with paper trading. And when we run it, it buys a stock and it can hold it until the next day when it decides on what it wants to do next with that. And so far the bot has gained a bit of profit, but I'll have to see how it changes over time and how it improves as it gets more data. But the main issue is that it only works as long as your laptop's open, but once it dies or it falls asleep, it can't run the next cycle in the next day. So follow for part two where I show you how to host it completely in the cloud and allow it to trade automatically and improve by itself.

"Let's build an AI trading bot that can trade stocks while you sleep. And we're pulling all of our data from the Yahoo"

👍
0
0 comments0 shares

Sign in to view and post comments.