The Journey of Building the AI+ML Perp Trading Bot Inspiration
Video of Bot operation: https://x.com/degen4lyfe_/status/1904367631857078279
My fascination with cryptocurrency trading and artificial intelligence sparked the idea for this project. I was inspired by the potential of combining machine learning with AI-driven decision-making to create a bot that could trade perpetual futures autonomously. Reading about advanced trading strategies and the success of algorithmic trading in traditional markets motivated me to explore this for crypto, specifically BTC/USDT on Phemex. The challenge of building something that could analyze market data and make profitable decisions in real-time was thrilling.
What I Learned
This project taught me a lot about machine learning and trading. I deepened my understanding of technical indicators like RSI, MACD, and ATR, and how they can be used to predict price movements. I learned to work with the LightGBM model for classification, training it to predict price changes based on historical data. Integrating the Grok API from xAI was a new experience, showing me how AI can enhance decision-making beyond raw data. I also gained experience with Flask for building a simple API to control the bot, and I improved my skills in handling real-time data and managing errors in a live trading environment.
How I Built the Project
I started by setting up the Phemex exchange connection using the ccxt library in testnet mode to safely test trades. I chose BTC/USDT perpetual futures with 50x leverage for the high volatility and potential returns. The bot fetches 5-minute historical data (sometimes from Binance for better volume) and calculates indicators like RSI, moving averages, Bollinger Bands, MACD, and ATR using pandas and numpy. These features train a LightGBM model to predict if the price will rise (>0.5%) or fall (<-0.5%). Every 60 seconds, the bot consults the Grok API, passing the latest data and ML prediction to decide whether to go long or short. Trades are executed with a 0.02 lot size, a 70% profit target, and a -20% stop loss, with a trailing stop (activates at 25% profit, trails by 10%). After each trade, the bot retrains the ML model with new data. I added a Flask API to start, stop, and monitor the bot’s status, ensuring I could control it remotely. Logging with logging helped track all actions and debug issues.
Challenges Faced
Building this bot came with several hurdles. One major challenge was handling the Phemex API—initially, I struggled with setting leverage and position modes correctly, as the documentation was unclear, leading to failed trades. Another issue was data quality; sometimes the historical data had gaps, causing the ML model to underperform, so I added fallbacks like using Binance data. Training the LightGBM model was tricky—balancing the dataset to avoid overfitting required multiple iterations of feature engineering and cross-validation. Integrating the Grok API also posed problems; rate limits and occasional errors forced me to add robust error handling and default decisions. Finally, managing the bot’s real-time operation was tough—ensuring it didn’t over-trade after closing positions required implementing a 15-minute delay, which took careful tuning to balance responsiveness and stability.
This journey was a mix of excitement and frustration, but seeing the bot place its first successful trade made it all worthwhile. It’s a project I’m proud of, and I’m eager to keep improving it!
Built With
- api
- crypto
- python
- render
Log in or sign up for Devpost to join the conversation.