diff --git a/.streamlit/config.toml b/.streamlit/config.toml new file mode 100644 index 0000000..2914bd0 --- /dev/null +++ b/.streamlit/config.toml @@ -0,0 +1,14 @@ +[theme] +primaryColor = "#667eea" +backgroundColor = "#FFFFFF" +secondaryBackgroundColor = "#F0F2F6" +textColor = "#262730" +font = "sans serif" + +[server] +headless = true +enableCORS = false +port = 8501 + +[browser] +gatherUsageStats = false diff --git a/ANDROID_TABLET_GUIDE.md b/ANDROID_TABLET_GUIDE.md new file mode 100644 index 0000000..407e8cf --- /dev/null +++ b/ANDROID_TABLET_GUIDE.md @@ -0,0 +1,287 @@ +# 📱 Android Tablet Quick Access Guide + +## For Samsung Galaxy S10 Ultra & Other Android Tablets + +--- + +## đŸŽ¯ Option 1: Access Already-Deployed Dashboard (Fastest) + +### If Someone Already Deployed It: + +1. **Open Chrome** (or Samsung Internet) on your tablet +2. **Go to your Streamlit app URL**: + ``` + https://your-app-name.streamlit.app + ``` + *(Ask the person who deployed it for this URL)* + +3. **Add to Home Screen** for easy access: + - Tap the **⋮** menu (three dots) in the top-right + - Select **"Add to Home screen"** + - Name it "Trading Bot" + - Tap **"Add"** + +4. **Done!** Now you have an app icon on your home screen + +--- + +## 🚀 Option 2: Deploy It Yourself (From Your Tablet) + +### You Can Deploy Directly From Your Android Tablet! + +#### Step 1: Open Browser & Go to Streamlit Cloud + +1. Open **Chrome** on your tablet +2. Go to: **[share.streamlit.io](https://share.streamlit.io)** +3. Sign in with **GitHub** (you'll need your GitHub account) + +#### Step 2: Deploy the App + +1. Tap **"New app"** button +2. Fill in these fields: + ``` + Repository: Netrade1/Institutional-Microstructure- + Branch: copilot/convert-flask-dashboard-to-streamlit + Main file path: app.py + ``` +3. Tap **"Deploy"** +4. Wait 2-3 minutes âąī¸ + +#### Step 3: Get Your URL + +You'll see a URL like: +``` +https://institutional-microstructure.streamlit.app +``` + +**Bookmark this** or **Add to Home Screen**! + +--- + +## 💡 Using the Dashboard on Your Tablet + +### Perfect for Galaxy S10 Ultra's Large Screen! + +#### Best Viewing Mode: +- ✅ **Landscape mode** - Use horizontal orientation for best experience +- ✅ **Full screen** - Tap address bar and scroll to hide it +- ✅ **Zoom** - Pinch to zoom on charts if needed + +#### How to Use: + +1. **Initialize Bot** 🚀 + - Tap the "🚀 Initialize Bot" button + - Wait for green "Initialized" status + +2. **Train Models** 🎓 + - Tap "🎓 Train Models" + - This takes 3-5 minutes (be patient!) + - Watch for "Trained" status + +3. **Start Trading** â–ļī¸ + - Tap "â–ļī¸ Start Trading" + - Bot will execute one cycle + - View results instantly + +4. **Explore Tabs** 📊 + - **Dashboard** - See your portfolio + - **Performance** - View metrics + - **Configuration** - Change settings + - **ML Models** - See AI info + +--- + +## 🎨 Tablet-Specific Tips + +### Make It Look Great on Your S10 Ultra: + +1. **Use Samsung Internet** (alternative to Chrome): + - Better battery life + - Built-in ad blocker + - Smooth scrolling + +2. **Enable Desktop Mode** (for more space): + - Chrome menu ⋮ → Settings → Desktop site ✓ + - Shows more content at once + +3. **Split Screen** (multitask while monitoring): + - Swipe from bottom → Recent apps + - Tap app icon → Open in split screen + - Watch bot + browse other apps + +4. **Dark Mode** (save battery on AMOLED): + - Use the theme selector in the app + - Or enable system-wide dark mode + +--- + +## 📱 Accessing From Your Tablet + +### Three Ways to Access: + +#### 1. **Home Screen Icon** (Recommended) +After adding to home screen, just tap the icon! + +#### 2. **Bookmark** +- Chrome menu ⋮ → ⭐ Bookmark +- Access from bookmarks bar + +#### 3. **Direct URL** +Just type the URL in your browser + +--- + +## âš™ī¸ Adjusting Settings on Tablet + +### Configuration Tab is Touch-Friendly: + +1. **Change Trading Symbols**: + - Tap text box + - Keyboard pops up + - Type symbols (BTC/USDT, AAPL, etc.) + +2. **Adjust Sliders**: + - Drag sliders with your finger + - Very responsive on touchscreen + - Stop Loss, Position Size, etc. + +3. **Save Changes**: + - Tap "Save Configuration" + - Changes apply immediately + +--- + +## 🔋 Battery & Performance Tips + +### Your S10 Ultra Can Handle It: + +- ✅ Dashboard is lightweight +- ✅ Won't drain battery fast +- ✅ No installation needed +- ✅ Updates happen in browser + +### To Save Battery: +- Use dark theme +- Lower screen brightness +- Close tab when not monitoring +- The bot runs in the cloud, not on your tablet! + +--- + +## 📊 Reading Charts on Tablet + +### Plotly Charts are Touch-Friendly: + +- **Zoom**: Pinch to zoom in/out +- **Pan**: Drag to move around chart +- **Hover**: Tap and hold to see values +- **Reset**: Double-tap to reset view + +### Perfect for Your Large Screen: +- Charts look amazing on S10 Ultra +- See all details clearly +- No need to scroll much + +--- + +## 🔄 Refreshing Data + +### Keep Dashboard Updated: + +1. **Auto-refresh**: Dashboard updates when you perform actions +2. **Manual refresh**: Pull down to refresh (browser refresh) +3. **Re-run**: Tap buttons again to update data + +--- + +## 🆘 Troubleshooting on Android + +### "Page won't load" +- Check WiFi/data connection +- Try different browser (Samsung Internet vs Chrome) +- Clear browser cache + +### "Buttons don't respond" +- Scroll up to see all buttons +- Try landscape mode +- Reload page + +### "Charts too small" +- Rotate to landscape +- Pinch to zoom +- Enable desktop site mode + +### "App is slow" +- Training takes 3-5 min (normal) +- Check your internet speed +- Close other browser tabs + +--- + +## đŸŽ¯ Quick Start Checklist for Your Tablet + +- [ ] Open Chrome or Samsung Internet +- [ ] Go to Streamlit Cloud or your app URL +- [ ] Sign in (if deploying) +- [ ] Deploy or access the app +- [ ] Add to home screen +- [ ] Rotate to landscape mode +- [ ] Tap "Initialize Bot" +- [ ] Tap "Train Models" (wait 5 min) +- [ ] Tap "Start Trading" +- [ ] Enjoy your dashboard! 🎉 + +--- + +## 📞 Need Help on Your Tablet? + +### Can't Deploy? +- Make sure you're signed into GitHub in your browser +- Try using desktop mode in browser +- Or deploy from a computer, then access from tablet + +### Want to Test First? +You can't run it locally on Android (needs Python), but you CAN: +- Deploy to Streamlit Cloud (free) +- Access instantly from your tablet +- No installation needed! + +--- + +## ✨ Your S10 Ultra Advantages + +Your tablet is **perfect** for this dashboard: + +✅ **Large screen** - See everything at once +✅ **Touch optimized** - All controls work great +✅ **Portable** - Monitor from anywhere +✅ **Always connected** - Check anytime +✅ **Long battery** - Monitor for hours + +--- + +## đŸŽŦ Quick Video Steps (Text Version) + +### From Your Tablet Right Now: + +1. **Tap Chrome icon** 📱 +2. **Go to**: `share.streamlit.io` 🌐 +3. **Sign in with GitHub** 🔐 +4. **Tap "New app"** ➕ +5. **Fill form with repo details** 📝 +6. **Tap Deploy** 🚀 +7. **Wait 2 minutes** âąī¸ +8. **Get your URL** 🔗 +9. **Add to home screen** 🏠 +10. **Start using!** 🎉 + +--- + +**Ready?** Open Chrome on your tablet and start with Step 1 above! 🚀 + +**Questions?** See [DEPLOY_NOW.md](DEPLOY_NOW.md) for more details. + +--- + +*Optimized for Samsung Galaxy S10 Ultra and all Android tablets* +*Last Updated: March 2026* diff --git a/DEPLOYMENT_SUMMARY.md b/DEPLOYMENT_SUMMARY.md new file mode 100644 index 0000000..b289fd8 --- /dev/null +++ b/DEPLOYMENT_SUMMARY.md @@ -0,0 +1,283 @@ +# 📋 Deployment Documentation Summary + +## What Was Created + +This document summarizes all the deployment guides available for the AI Trading Bot dashboard. + +--- + +## đŸŽ¯ **Start Here (Based on Your Device)** + +### 📱 On Android Tablet Right Now? +âžĄī¸ **[START_HERE_TABLET.md](START_HERE_TABLET.md)** +- Visual step-by-step guide with 8 simple steps +- Perfect for Samsung Galaxy S10 Ultra and all Android tablets +- Shows exactly what to tap and where +- ASCII boxes make it easy to follow + +### 📱 Want More Tablet Details? +âžĄī¸ **[ANDROID_TABLET_GUIDE.md](ANDROID_TABLET_GUIDE.md)** +- Complete guide for Android tablets +- Tips for large screens (landscape mode, split screen) +- Battery and performance optimization +- Troubleshooting for mobile devices +- Touch-friendly chart interactions + +### đŸ’ģ On Desktop/Laptop? +âžĄī¸ **[DEPLOY_NOW.md](DEPLOY_NOW.md)** +- Simple 3-step deployment guide +- Works for all devices +- Quick reference format +- Common Q&A section + +### 📚 Want Full Details? +âžĄī¸ **[STREAMLIT_DEPLOYMENT.md](STREAMLIT_DEPLOYMENT.md)** +- Comprehensive deployment guide +- All configuration options +- Advanced features +- Security considerations +- Complete usage instructions + +### ⚡ Just Want to Test Locally? +âžĄī¸ **[QUICKSTART.md](QUICKSTART.md)** +- Original quick start guide +- Command-line focused +- Local development setup + +--- + +## īŋŊīŋŊ Comparison of Guides + +| Guide | Best For | Length | Focus | +|-------|----------|--------|-------| +| START_HERE_TABLET.md | Android tablets, first-time users | 13KB | Visual, step-by-step | +| ANDROID_TABLET_GUIDE.md | Tablet optimization | 6.6KB | Mobile tips, troubleshooting | +| DEPLOY_NOW.md | Quick deployment | 4.1KB | Fast deployment | +| STREAMLIT_DEPLOYMENT.md | Complete reference | 4.5KB | All features | +| QUICKSTART.md | Developers | 2.7KB | CLI usage | + +--- + +## 🚀 Deployment Process (Summary) + +### For Streamlit Cloud (Recommended): + +1. **Go to**: [share.streamlit.io](https://share.streamlit.io) +2. **Sign in** with GitHub +3. **Create new app** with: + - Repository: `Netrade1/Institutional-Microstructure-` + - Branch: `copilot/convert-flask-dashboard-to-streamlit` + - Main file: `app.py` +4. **Deploy** and wait 2-3 minutes +5. **Access** your dashboard at the provided URL + +### For Local Testing: + +```bash +pip install streamlit +streamlit run app.py +# Open http://localhost:8501 +``` + +--- + +## 📱 Device-Specific Features + +### Android Tablets +- Touch-optimized buttons and sliders +- Pinch-to-zoom charts +- Landscape mode support +- Add to home screen capability +- Split-screen multitasking +- Dark mode for battery saving + +### Desktop/Laptop +- Full-screen dashboard +- Keyboard shortcuts +- Multiple browser tabs +- Developer tools access + +### iOS Devices +- Safari optimization +- Add to home screen +- Gesture navigation +- Dark mode support + +--- + +## đŸŽ¯ After Deployment + +### First-Time Setup: + +1. **Initialize Bot** (🚀 button) + - Sets up trading system + - Takes a few seconds + +2. **Train Models** (🎓 button) + - Trains AI models + - Takes 3-5 minutes + - Don't close browser! + +3. **Start Trading** (â–ļī¸ button) + - Executes trading cycle + - Updates portfolio + - View results instantly + +### Dashboard Tabs: + +- **📊 Dashboard**: Portfolio overview, equity curve, positions, trades +- **📈 Performance**: Sharpe ratio, max drawdown, win rate, profit factor +- **âš™ī¸ Configuration**: Trading symbols, capital, risk parameters +- **īŋŊīŋŊ ML Models**: Model information and weights + +--- + +## 🔧 Configuration Options + +### Trading Settings: +- Symbols (BTC/USDT, ETH/USDT, AAPL, GOOGL, etc.) +- Initial capital +- Max position size (% of portfolio) +- Stop loss (% per trade) +- Take profit (% per trade) + +### Risk Management: +- Max daily loss (% of portfolio) +- Max portfolio risk (total exposure) +- Diversification minimums + +--- + +## âš ī¸ Important Notes + +### For All Users: +- ✅ Dashboard runs in the cloud (no local installation needed) +- ✅ Access from any device with internet +- ✅ Automatic updates when you push to GitHub +- ✅ HTTPS secure connection +- âš ī¸ For educational/testing purposes only +- âš ī¸ Use paper trading for real testing + +### Security: +- No API keys in code +- Environment variables for secrets +- Streamlit Cloud handles infrastructure +- Session-based state management + +--- + +## 🆘 Common Issues & Solutions + +### "Can't find deployment button" +- Make sure you're signed into GitHub first +- Try refreshing the page +- Use Chrome or Firefox + +### "Deployment failed" +- Check repository name is exact +- Verify branch name is correct +- Ensure app.py exists in repo + +### "Training takes forever" +- This is normal (3-5 minutes) +- Don't close browser tab +- ML models take time to train + +### "Dashboard looks weird on mobile" +- Try landscape orientation +- Use desktop site mode +- Pinch to zoom if needed + +--- + +## 📞 Getting Help + +### If You're Stuck: + +1. **Check the specific guide** for your device/situation +2. **Read troubleshooting** sections in the guides +3. **Review common questions** in DEPLOY_NOW.md +4. **Open an issue** on GitHub +5. **Check existing issues** for similar problems + +### Useful Commands: + +```bash +# Check if Streamlit is installed +pip show streamlit + +# Install Streamlit +pip install streamlit + +# Run locally +streamlit run app.py + +# Check Python version +python --version # Should be 3.8+ +``` + +--- + +## 🎓 Additional Resources + +### In This Repository: +- `README.md` - Main documentation +- `ARCHITECTURE.md` - System architecture +- `SYSTEM_OVERVIEW.md` - Technical overview +- `config.yaml` - Configuration file +- `requirements.txt` - Dependencies + +### External Links: +- [Streamlit Documentation](https://docs.streamlit.io) +- [Streamlit Cloud](https://share.streamlit.io) +- [GitHub Docs](https://docs.github.com) + +--- + +## 📈 What You Get After Deployment + +✓ **Live Dashboard**: Access from anywhere +✓ **Shareable URL**: https://your-app.streamlit.app +✓ **Mobile Access**: Phone and tablet friendly +✓ **Auto-Updates**: Syncs with GitHub automatically +✓ **No Maintenance**: Streamlit handles infrastructure +✓ **Free Tier**: No cost for basic usage +✓ **HTTPS**: Secure connection +✓ **Analytics**: Optional usage tracking + +--- + +## đŸŽ¯ Quick Reference + +### URLs to Know: +- Streamlit Cloud: https://share.streamlit.io +- Your Repo: https://github.com/Netrade1/Institutional-Microstructure- +- Branch: copilot/convert-flask-dashboard-to-streamlit + +### Files to Know: +- Main App: `app.py` +- Config: `config.yaml` +- Requirements: `requirements.txt` +- Streamlit Config: `.streamlit/config.toml` + +### Key Info: +- Main File Path: `app.py` +- Python Version: 3.8+ +- Deployment Time: 2-3 minutes +- Training Time: 3-5 minutes + +--- + +## 📝 Version History + +- **March 2026**: Created comprehensive deployment guides + - Added START_HERE_TABLET.md for visual guidance + - Added ANDROID_TABLET_GUIDE.md for mobile optimization + - Added DEPLOY_NOW.md for quick deployment + - Updated README.md with clear navigation + +--- + +**Choose your guide above and get started! 🚀** + +*For immediate help on Android tablet: Open START_HERE_TABLET.md* diff --git a/DEPLOY_NOW.md b/DEPLOY_NOW.md new file mode 100644 index 0000000..9d1c759 --- /dev/null +++ b/DEPLOY_NOW.md @@ -0,0 +1,169 @@ +# 🚀 Deploy Your Trading Bot in 3 Steps + +**Not sure how to deploy? Follow these simple steps!** + +--- + +## 📱 **ON AN ANDROID TABLET RIGHT NOW?** + +âžĄī¸ **[GO HERE: ANDROID_TABLET_GUIDE.md](ANDROID_TABLET_GUIDE.md)** +*Specific instructions for Samsung Galaxy S10 Ultra and all Android tablets!* + +--- + +## 📱 Option 1: Deploy to Streamlit Cloud (Recommended for Mobile) + +### Step 1: Get Your GitHub Repository Ready ✅ + +Your code is already on GitHub! You're at: +``` +https://github.com/Netrade1/Institutional-Microstructure- +``` + +### Step 2: Go to Streamlit Cloud 🌐 + +1. **Open this link**: [share.streamlit.io](https://share.streamlit.io) +2. **Sign in** with your GitHub account (the same one you use for this repo) +3. Click the big **"New app"** button + +### Step 3: Configure Your App âš™ī¸ + +Fill in these fields: + +``` +Repository: Netrade1/Institutional-Microstructure- +Branch: copilot/convert-flask-dashboard-to-streamlit +Main file path: app.py +``` + +Then click **"Deploy"**! + +### That's It! 🎉 + +Wait 2-3 minutes for deployment. You'll get a URL like: +``` +https://your-app-name.streamlit.app +``` + +**Share this URL** to access your dashboard from any device (phone, tablet, computer)! + +--- + +## đŸ’ģ Option 2: Run Locally (For Testing) + +### Quick Local Test + +```bash +# 1. Install Streamlit (if not already installed) +pip install streamlit + +# 2. Run the app +streamlit run app.py +``` + +Open your browser to: `http://localhost:8501` + +--- + +## đŸŽ¯ What to Do After Deployment + +### First Time Using the Dashboard? + +1. **Click "🚀 Initialize Bot"** - Sets up your trading bot +2. **Click "🎓 Train Models"** - Trains AI models (takes 2-5 minutes) +3. **Click "â–ļī¸ Start Trading"** - Executes one trading cycle +4. **View Results** - Check portfolio, positions, and performance + +### Need to Change Settings? + +Go to the **"âš™ī¸ Configuration"** tab to adjust: +- Trading symbols (BTC, ETH, AAPL, etc.) +- Initial capital amount +- Risk parameters (stop loss, position size) + +--- + +## ❓ Common Questions + +### Q: "I deployed but the app crashes" + +**A:** Check that all these files exist in your repository: +- ✅ `app.py` (main file) +- ✅ `requirements.txt` (dependencies) +- ✅ `config.yaml` (settings) +- ✅ `trading_bot/` folder (bot code) + +### Q: "Training takes forever" + +**A:** That's normal! ML model training can take 3-5 minutes. The page will update when done. + +### Q: "Can I use this on my phone?" + +**A:** Yes! Once deployed to Streamlit Cloud: +1. Open the URL in your phone's browser (Chrome, Safari) +2. Tap "Add to Home Screen" for quick access +3. Use landscape mode for better chart viewing + +### Q: "Is my data secure?" + +**A:** +- Your dashboard runs on Streamlit Cloud (secure HTTPS) +- No API keys or passwords are stored in the code +- All trading is simulated (educational purpose) +- For real trading, use paper trading accounts only + +### Q: "How do I update my deployed app?" + +**A:** Just push changes to GitHub! Streamlit Cloud auto-updates from your repository. + +```bash +git add . +git commit -m "Updated settings" +git push +``` + +Your app will redeploy automatically in 1-2 minutes. + +--- + +## 🆘 Still Stuck? + +### Need More Help? + +1. **Read the full guide**: Check `STREAMLIT_DEPLOYMENT.md` for detailed instructions +2. **Check examples**: Run `python demo.py` to see how the bot works +3. **Review configuration**: See `config.yaml` for all settings +4. **Ask for help**: Open an issue on GitHub + +### Quick Links + +- 📖 [Full Deployment Guide](STREAMLIT_DEPLOYMENT.md) +- đŸŽ¯ [Quick Start](QUICKSTART.md) +- 📚 [Complete Documentation](README.md) +- âš™ī¸ [System Architecture](ARCHITECTURE.md) + +--- + +## ✨ Pro Tips + +### For Mobile Users +- Use landscape mode for charts +- Pinch to zoom on graphs +- Swipe between tabs +- Add to home screen for app-like experience + +### For Advanced Users +- Edit `.streamlit/config.toml` for custom themes +- Set environment variables for API keys +- Use Streamlit secrets for sensitive data +- Enable authentication (Streamlit Pro) + +--- + +**Ready to Deploy?** Start with Option 1 above! 🚀 + +**Just Testing?** Use Option 2 for local testing first! đŸ’ģ + +--- + +*Last Updated: March 2026* diff --git a/README.md b/README.md index e0afac9..6a9e70f 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,36 @@ A state-of-the-art cutting-edge machine learning augmented intelligence autonomous AI Trading Bot Platform System and Dashboard. +--- + +## 🚀 **WANT TO DEPLOY NOW?** + +### 📱 **ON YOUR ANDROID TABLET RIGHT NOW?** + +**âžĄī¸ [START HERE: START_HERE_TABLET.md](START_HERE_TABLET.md)** âŦ…ī¸ +*Step-by-step visual guide for Samsung Galaxy S10 Ultra and all Android tablets!* + +--- + +### Choose Your Path: + +#### 📱 Android Tablet Users +1. **[START_HERE_TABLET.md](START_HERE_TABLET.md)** - Visual step-by-step guide (START HERE!) +2. **[ANDROID_TABLET_GUIDE.md](ANDROID_TABLET_GUIDE.md)** - Complete tablet guide + +#### đŸ’ģ Desktop/Laptop Users +1. **[DEPLOY_NOW.md](DEPLOY_NOW.md)** - Simple 3-step deployment +2. **[STREAMLIT_DEPLOYMENT.md](STREAMLIT_DEPLOYMENT.md)** - Detailed guide + +#### 🏃 Just Want to Test Locally? +```bash +pip install streamlit +streamlit run app.py +``` +Open: `http://localhost:8501` + +--- + ## 🌟 Features ### Advanced Machine Learning diff --git a/START_HERE_TABLET.md b/START_HERE_TABLET.md new file mode 100644 index 0000000..06d745c --- /dev/null +++ b/START_HERE_TABLET.md @@ -0,0 +1,166 @@ +═══════════════════════════════════════════════════════════════════ + 📱 ANDROID TABLET - QUICK START (From Your Device Right Now!) +═══════════════════════════════════════════════════════════════════ + +YOU ARE HERE: On your Samsung Galaxy S10 Ultra tablet ✓ + +WHAT YOU NEED TO DO: +═══════════════════════════════════════════════════════════════════ + +┌─────────────────────────────────────────────────────────────────┐ +│ Step 1: Open Chrome Browser │ +│ ───────────────────────────────────────────────────────────── │ +│ → Tap the Chrome icon on your tablet │ +│ → Make sure you're connected to WiFi or mobile data │ +└─────────────────────────────────────────────────────────────────┘ + +┌─────────────────────────────────────────────────────────────────┐ +│ Step 2: Go to Streamlit Cloud │ +│ ───────────────────────────────────────────────────────────── │ +│ → Type this in the address bar: │ +│ │ +│ https://share.streamlit.io │ +│ │ +│ → Press Enter/Go │ +└─────────────────────────────────────────────────────────────────┘ + +┌─────────────────────────────────────────────────────────────────┐ +│ Step 3: Sign In with GitHub │ +│ ───────────────────────────────────────────────────────────── │ +│ → You'll see a "Sign in" button │ +│ → Tap it and choose "Sign in with GitHub" │ +│ → Use your GitHub username and password │ +│ → (If you don't have GitHub, create free account first) │ +└─────────────────────────────────────────────────────────────────┘ + +┌─────────────────────────────────────────────────────────────────┐ +│ Step 4: Create New App │ +│ ───────────────────────────────────────────────────────────── │ +│ → Look for the "New app" button (usually blue) │ +│ → Tap it │ +│ → A form will appear │ +└─────────────────────────────────────────────────────────────────┘ + +┌─────────────────────────────────────────────────────────────────┐ +│ Step 5: Fill in the Form │ +│ ───────────────────────────────────────────────────────────── │ +│ Repository: │ +│ Netrade1/Institutional-Microstructure- │ +│ │ +│ Branch: │ +│ copilot/convert-flask-dashboard-to-streamlit │ +│ │ +│ Main file path: │ +│ app.py │ +│ │ +│ → Tap each field and type carefully │ +│ → Use the keyboard on your tablet │ +└─────────────────────────────────────────────────────────────────┘ + +┌─────────────────────────────────────────────────────────────────┐ +│ Step 6: Deploy! │ +│ ───────────────────────────────────────────────────────────── │ +│ → Tap the "Deploy" button at the bottom │ +│ → Wait 2-3 minutes (don't close the browser!) │ +│ → You'll see a loading animation │ +└─────────────────────────────────────────────────────────────────┘ + +┌─────────────────────────────────────────────────────────────────┐ +│ Step 7: Get Your URL │ +│ ───────────────────────────────────────────────────────────── │ +│ → When deployment finishes, you'll see a URL like: │ +│ │ +│ https://your-app-name.streamlit.app │ +│ │ +│ → THIS IS YOUR DASHBOARD URL! │ +│ → Bookmark it or add to home screen │ +└─────────────────────────────────────────────────────────────────┘ + +┌─────────────────────────────────────────────────────────────────┐ +│ Step 8: Add to Home Screen (Optional but Recommended) │ +│ ───────────────────────────────────────────────────────────── │ +│ → Tap the ⋮ menu (three dots) in Chrome │ +│ → Select "Add to Home screen" │ +│ → Name it "Trading Bot" │ +│ → Tap "Add" │ +│ → Now you have an app icon! 🎉 │ +└─────────────────────────────────────────────────────────────────┘ + +═══════════════════════════════════════════════════════════════════ + WHAT HAPPENS NEXT? +═══════════════════════════════════════════════════════════════════ + +Your dashboard is now LIVE on the internet! + +You can: +✓ Access it from any device +✓ Share the URL with others +✓ Use it on your tablet, phone, or computer +✓ Monitor your trading bot from anywhere + +═══════════════════════════════════════════════════════════════════ + USING THE DASHBOARD (After Deployment) +═══════════════════════════════════════════════════════════════════ + +1. TAP "🚀 Initialize Bot" + → Sets up the trading system + → Takes a few seconds + +2. TAP "🎓 Train Models" + → Trains the AI models + → Takes 3-5 minutes (be patient!) + → You'll see "Trained" status when done + +3. TAP "â–ļī¸ Start Trading" + → Executes one trading cycle + → Updates portfolio and positions + → See results immediately! + +4. EXPLORE THE TABS + → Dashboard: See portfolio and charts + → Performance: View metrics + → Configuration: Change settings + → ML Models: See AI information + +═══════════════════════════════════════════════════════════════════ + TIPS FOR YOUR TABLET +═══════════════════════════════════════════════════════════════════ + +🔄 Use LANDSCAPE MODE for best viewing +📊 Pinch to ZOOM on charts +👆 Tap and hold to see CHART VALUES +🌙 Use DARK THEME to save battery (in app settings) +📱 ADD TO HOME SCREEN for quick access + +═══════════════════════════════════════════════════════════════════ + NEED MORE HELP? +═══════════════════════════════════════════════════════════════════ + +📖 Read: ANDROID_TABLET_GUIDE.md - Complete guide +📖 Read: DEPLOY_NOW.md - General deployment guide +📖 Read: STREAMLIT_DEPLOYMENT.md - Detailed instructions + +═══════════════════════════════════════════════════════════════════ + TROUBLESHOOTING +═══════════════════════════════════════════════════════════════════ + +❓ Can't find "New app" button? + → Make sure you're signed in to GitHub first + +❓ Deployment failed? + → Check that you typed the repository name correctly + → Check the branch name is exact + +❓ Training takes forever? + → This is normal! ML training takes 3-5 minutes + → Don't close the browser, just wait + +❓ Dashboard looks weird? + → Try landscape mode (rotate tablet) + → Try desktop site mode (in Chrome settings) + +═══════════════════════════════════════════════════════════════════ + + 🎉 READY TO START? Follow Step 1 above on your tablet now! + +═══════════════════════════════════════════════════════════════════ diff --git a/STREAMLIT_DEPLOYMENT.md b/STREAMLIT_DEPLOYMENT.md new file mode 100644 index 0000000..43ae19f --- /dev/null +++ b/STREAMLIT_DEPLOYMENT.md @@ -0,0 +1,167 @@ +# AI Trading Bot - Streamlit Dashboard Deployment Guide + +## Overview +This guide explains how to deploy the AI Trading Bot dashboard to Streamlit Cloud for easy access from any device, including Android phones. + +## Features +- 🤖 Real-time bot monitoring and control +- 📊 Portfolio tracking with live metrics +- 📈 Interactive equity curve visualization +- âš™ī¸ Configuration management interface +- 🧠 ML model performance monitoring +- 🌓 Dark/Light theme toggle +- 📱 Mobile-responsive design + +## Local Development + +### Prerequisites +- Python 3.8 or higher +- pip package manager + +### Installation + +1. Clone the repository: +```bash +git clone https://github.com/Netrade1/Institutional-Microstructure-.git +cd Institutional-Microstructure- +``` + +2. Install dependencies: +```bash +pip install -r requirements.txt +``` + +3. Configure the bot: +Edit `config.yaml` to set your trading parameters: +- Trading symbols +- Initial capital +- Risk parameters +- ML model settings + +### Running Locally + +Start the Streamlit dashboard: +```bash +streamlit run app.py +``` + +The dashboard will be available at `http://localhost:8501` + +## Deploying to Streamlit Cloud + +### Step 1: Prepare Your Repository + +1. Ensure all files are committed to your GitHub repository +2. Make sure `requirements.txt` is up to date +3. Verify `config.yaml` has sensible defaults + +### Step 2: Deploy to Streamlit Cloud + +1. Go to [share.streamlit.io](https://share.streamlit.io) +2. Sign in with your GitHub account +3. Click "New app" +4. Select your repository: `Netrade1/Institutional-Microstructure-` +5. Set the main file path: `app.py` +6. Click "Deploy" + +### Step 3: Access from Mobile + +Once deployed, you'll get a URL like: +``` +https://your-app-name.streamlit.app +``` + +Open this URL in any mobile browser (Chrome, Safari, etc.) to access the dashboard. + +## Usage Guide + +### 1. Initialize the Bot +Click the "🚀 Initialize Bot" button to create a bot instance with your configuration. + +### 2. Train Models +Click "🎓 Train Models" to train the ML models (LSTM, Random Forest, XGBoost) on historical data. +- This may take several minutes +- Models are trained on technical indicators + +### 3. Start Trading +Click "â–ļī¸ Start Trading" to execute a trading cycle: +- Fetches latest market data +- Generates predictions +- Creates trading signals +- Executes trades based on risk management rules + +### 4. Monitor Performance +Use the tabs to view: +- **Dashboard**: Portfolio overview, equity curve, positions, trades +- **Performance**: Sharpe ratio, max drawdown, win rate, profit factor +- **Configuration**: Adjust trading and risk parameters +- **ML Models**: View model information and weights + +### 5. Stop Trading +Click "âšī¸ Stop Trading" to halt the bot. + +## Configuration Options + +### Trading Settings +- **Symbols**: Assets to trade (e.g., BTC/USDT, AAPL, GOOGL) +- **Initial Capital**: Starting portfolio value +- **Max Position Size**: Maximum % of portfolio per trade +- **Stop Loss**: Maximum loss per trade before exit +- **Take Profit**: Target profit per trade + +### Risk Management +- **Max Daily Loss**: Maximum portfolio loss per day +- **Max Portfolio Risk**: Maximum total portfolio risk exposure + +## Mobile Tips + +### Android Devices +1. Open Chrome or your preferred browser +2. Navigate to your Streamlit Cloud URL +3. For best experience, use landscape mode for charts +4. Add to home screen for quick access: + - Menu → Add to Home screen + +### iOS Devices +1. Open Safari +2. Navigate to your Streamlit Cloud URL +3. Tap the Share button +4. Select "Add to Home Screen" + +## Troubleshooting + +### Bot Not Initializing +- Check that `config.yaml` exists and is valid +- Verify all required dependencies are installed + +### Training Takes Too Long +- Reduce `history_days` in config.yaml +- Use fewer symbols +- Consider using a more powerful deployment option + +### Mobile Layout Issues +- Try rotating to landscape mode +- Zoom out if content is too large +- Clear browser cache + +## Security Considerations + +âš ī¸ **Important**: This dashboard is for educational and testing purposes. + +For production use: +1. Never commit API keys or secrets to the repository +2. Use environment variables for sensitive data +3. Enable authentication on Streamlit Cloud (Pro plan) +4. Regularly update dependencies for security patches +5. Use paper trading or testnet APIs for testing + +## Support + +For issues or questions: +1. Check the [GitHub Issues](https://github.com/Netrade1/Institutional-Microstructure-/issues) +2. Review the code documentation +3. Contact the repository maintainers + +## License + +See LICENSE file in the repository. diff --git a/app.py b/app.py new file mode 100644 index 0000000..a59f26e --- /dev/null +++ b/app.py @@ -0,0 +1,635 @@ +""" +Streamlit Dashboard for AI Trading Bot +Provides real-time monitoring and control interface +""" +import streamlit as st +import pandas as pd +import plotly.graph_objects as go +from datetime import datetime +import yaml +import json +import logging +from typing import Dict, List, Any + +from trading_bot.bot import AITradingBot + +# Configure logging +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +# Constants +DEFAULT_INITIAL_CAPITAL = 100000 + +# Page configuration +st.set_page_config( + page_title="AI Trading Bot Dashboard", + page_icon="📈", + layout="wide", + initial_sidebar_state="expanded" +) + +# Custom CSS for better mobile responsiveness and professional look +st.markdown(""" + +""", unsafe_allow_html=True) + + +def initialize_session_state(): + """Initialize Streamlit session state variables""" + if 'bot' not in st.session_state: + st.session_state.bot = None + if 'bot_status' not in st.session_state: + st.session_state.bot_status = { + 'initialized': False, + 'trained': False, + 'running': False, + 'last_update': None + } + if 'theme' not in st.session_state: + st.session_state.theme = 'light' + if 'config' not in st.session_state: + try: + with open('config.yaml', 'r') as f: + st.session_state.config = yaml.safe_load(f) + except Exception as e: + logger.error(f"Error loading config: {e}") + st.session_state.config = {} + + +def load_config() -> Dict: + """Load configuration from YAML file""" + try: + with open('config.yaml', 'r') as f: + return yaml.safe_load(f) + except Exception as e: + logger.error(f"Error loading config: {e}") + st.error(f"Failed to load configuration: {e}") + return {} + + +def save_config(config: Dict): + """Save configuration to YAML file""" + try: + with open('config.yaml', 'w') as f: + yaml.dump(config, f, default_flow_style=False) + st.success("Configuration saved successfully!") + except Exception as e: + logger.error(f"Error saving config: {e}") + st.error(f"Failed to save configuration: {e}") + + +def initialize_bot(): + """Initialize the trading bot""" + try: + with st.spinner("Initializing bot..."): + st.session_state.bot = AITradingBot() + st.session_state.bot_status['initialized'] = True + st.session_state.bot_status['last_update'] = datetime.now().isoformat() + logger.info("Bot initialized via Streamlit") + st.success("✅ Bot initialized successfully!") + st.rerun() + except Exception as e: + logger.error(f"Error initializing bot: {e}") + st.error(f"Failed to initialize bot: {e}") + + +def train_bot(): + """Train the bot models""" + if st.session_state.bot is None: + st.error("Please initialize the bot first") + return + + try: + with st.spinner("Training models... This may take several minutes."): + data = st.session_state.bot.fetch_and_prepare_data() + st.session_state.bot.train_models(data) + st.session_state.bot_status['trained'] = True + st.session_state.bot_status['last_update'] = datetime.now().isoformat() + logger.info("Bot trained via Streamlit") + st.success("✅ Bot trained successfully!") + st.rerun() + except Exception as e: + logger.error(f"Error training bot: {e}") + st.error(f"Failed to train bot: {e}") + + +def execute_trading_cycle(): + """Execute one trading cycle""" + if st.session_state.bot is None: + st.error("Please initialize the bot first") + return + + if not st.session_state.bot.is_trained: + st.error("Please train the bot first") + return + + try: + with st.spinner("Executing trading cycle..."): + st.session_state.bot_status['running'] = True + st.session_state.bot_status['last_update'] = datetime.now().isoformat() + + data = st.session_state.bot.fetch_and_prepare_data() + st.session_state.bot.execute_trading_cycle(data) + + logger.info("Trading cycle executed via Streamlit") + st.success("✅ Trading cycle completed successfully!") + st.rerun() + except Exception as e: + logger.error(f"Error in trading cycle: {e}") + st.session_state.bot_status['running'] = False + st.error(f"Failed to execute trading cycle: {e}") + + +def stop_trading(): + """Stop trading bot""" + st.session_state.bot_status['running'] = False + st.session_state.bot_status['last_update'] = datetime.now().isoformat() + logger.info("Trading stopped via Streamlit") + st.success("Trading stopped") + st.rerun() + + +def render_header(): + """Render dashboard header""" + col1, col2 = st.columns([3, 1]) + + with col1: + st.title("🤖 AI Trading Bot Dashboard") + st.caption("Autonomous Trading System with Machine Learning") + + with col2: + # Theme toggle + theme = st.selectbox( + "Theme", + ["Light", "Dark"], + index=0 if st.session_state.theme == 'light' else 1, + key="theme_selector" + ) + if theme.lower() != st.session_state.theme: + st.session_state.theme = theme.lower() + st.rerun() + + +def render_status_bar(): + """Render status bar with bot status badges""" + st.subheader("System Status") + + col1, col2, col3, col4 = st.columns(4) + + with col1: + status_class = "status-active" if st.session_state.bot_status['initialized'] else "status-inactive" + status_text = "Initialized" if st.session_state.bot_status['initialized'] else "Not Initialized" + st.markdown(f'
{status_text}
', unsafe_allow_html=True) + + with col2: + status_class = "status-active" if st.session_state.bot_status['trained'] else "status-inactive" + status_text = "Trained" if st.session_state.bot_status['trained'] else "Not Trained" + st.markdown(f'
{status_text}
', unsafe_allow_html=True) + + with col3: + status_class = "status-active" if st.session_state.bot_status['running'] else "status-inactive" + status_text = "Running" if st.session_state.bot_status['running'] else "Stopped" + st.markdown(f'
{status_text}
', unsafe_allow_html=True) + + with col4: + if st.session_state.bot_status['last_update']: + update_time = datetime.fromisoformat(st.session_state.bot_status['last_update']) + st.caption(f"Last Update: {update_time.strftime('%H:%M:%S')}") + + +def render_controls(): + """Render bot control buttons""" + st.subheader("Bot Controls") + + col1, col2, col3, col4 = st.columns(4) + + with col1: + if st.button("🚀 Initialize Bot", type="primary", disabled=st.session_state.bot_status['initialized']): + initialize_bot() + + with col2: + if st.button("🎓 Train Models", disabled=not st.session_state.bot_status['initialized']): + train_bot() + + with col3: + if st.button("â–ļī¸ Start Trading", disabled=not st.session_state.bot_status['trained'] or st.session_state.bot_status['running']): + execute_trading_cycle() + + with col4: + if st.button("âšī¸ Stop Trading", disabled=not st.session_state.bot_status['running']): + stop_trading() + + +def render_portfolio_metrics(): + """Render portfolio metrics""" + if st.session_state.bot is None or not st.session_state.bot_status['initialized']: + st.info("Initialize the bot to view portfolio metrics") + return + + st.subheader("📊 Portfolio Overview") + + portfolio = st.session_state.bot.portfolio + + # Main metrics + col1, col2, col3, col4 = st.columns(4) + + with col1: + st.metric( + "Total Value", + f"${portfolio.total_value:,.2f}", + f"{portfolio.total_return * 100:.2f}%" + ) + + with col2: + st.metric( + "Available Cash", + f"${portfolio.cash:,.2f}" + ) + + with col3: + st.metric( + "Total P&L", + f"${portfolio.total_profit_loss:,.2f}", + delta_color="normal" if portfolio.total_profit_loss >= 0 else "inverse" + ) + + with col4: + st.metric( + "Number of Trades", + len(portfolio.trade_history) + ) + + +def render_performance_metrics(): + """Render performance metrics""" + if st.session_state.bot is None or not st.session_state.bot_status['initialized']: + return + + try: + metrics = st.session_state.bot.get_performance_metrics() + + st.subheader("📈 Performance Metrics") + + col1, col2, col3 = st.columns(3) + + with col1: + st.metric("Total Return", f"{metrics.get('total_return', 0) * 100:.2f}%") + st.metric("Open Positions", metrics.get('num_positions', 0)) + + with col2: + sharpe = metrics.get('sharpe_ratio', 0) + st.metric("Sharpe Ratio", f"{sharpe:.2f}" if sharpe else "N/A") + st.metric("Max Drawdown", f"{metrics.get('max_drawdown', 0) * 100:.2f}%") + + with col3: + win_rate = metrics.get('win_rate', 0) + st.metric("Win Rate", f"{win_rate * 100:.2f}%" if win_rate else "N/A") + st.metric("Profit Factor", f"{metrics.get('profit_factor', 0):.2f}") + + except Exception as e: + logger.error(f"Error getting performance metrics: {e}") + + +def render_equity_curve(): + """Render equity curve visualization""" + if st.session_state.bot is None or not st.session_state.bot_status['initialized']: + return + + st.subheader("💹 Equity Curve") + + portfolio = st.session_state.bot.portfolio + + # Get trade history + if portfolio.trade_history: + # Create equity curve data + equity_data = [] + running_value = st.session_state.config.get('trading', {}).get('initial_capital', DEFAULT_INITIAL_CAPITAL) + + for trade in portfolio.trade_history: + if 'timestamp' in trade and 'profit_loss' in trade: + running_value += trade.get('profit_loss', 0) + equity_data.append({ + 'timestamp': trade['timestamp'], + 'equity': running_value + }) + + if equity_data: + df = pd.DataFrame(equity_data) + + # Create Plotly figure + fig = go.Figure() + + fig.add_trace(go.Scatter( + x=df['timestamp'], + y=df['equity'], + mode='lines', + name='Portfolio Value', + line=dict(color='#667eea', width=2), + fill='tozeroy', + fillcolor='rgba(102, 126, 234, 0.2)' + )) + + # Add initial capital line + initial_capital = st.session_state.config.get('trading', {}).get('initial_capital', DEFAULT_INITIAL_CAPITAL) + fig.add_hline( + y=initial_capital, + line_dash="dash", + line_color="gray", + annotation_text="Initial Capital" + ) + + fig.update_layout( + title="Portfolio Equity Curve", + xaxis_title="Time", + yaxis_title="Portfolio Value ($)", + hovermode='x unified', + template='plotly_white', + height=400 + ) + + st.plotly_chart(fig, use_container_width=True) + else: + st.info("No trade history available yet. Execute a trading cycle to see the equity curve.") + + +def render_positions(): + """Render current positions""" + if st.session_state.bot is None or not st.session_state.bot_status['initialized']: + return + + st.subheader("📍 Current Positions") + + portfolio = st.session_state.bot.portfolio + + if portfolio.positions: + positions_data = [] + for symbol, pos in portfolio.positions.items(): + positions_data.append({ + 'Symbol': symbol, + 'Shares': pos.shares, + 'Entry Price': f"${pos.entry_price:.2f}", + 'Current Price': f"${pos.current_price:.2f}", + 'Value': f"${pos.value:.2f}", + 'P&L': f"${pos.profit_loss:.2f}", + 'P&L %': f"{pos.profit_loss_pct * 100:.2f}%" + }) + + df = pd.DataFrame(positions_data) + st.dataframe(df, use_container_width=True) + else: + st.info("No open positions") + + +def render_trade_history(): + """Render trade history""" + if st.session_state.bot is None or not st.session_state.bot_status['initialized']: + return + + st.subheader("📜 Trade History") + + portfolio = st.session_state.bot.portfolio + + if portfolio.trade_history: + # Get last 50 trades + trades = portfolio.trade_history[-50:] + + # Convert to DataFrame + df = pd.DataFrame(trades) + + # Format columns if they exist + if not df.empty: + if 'timestamp' in df.columns: + df['timestamp'] = pd.to_datetime(df['timestamp']) + if 'price' in df.columns: + df['price'] = df['price'].apply(lambda x: f"${x:.2f}") + if 'profit_loss' in df.columns: + df['profit_loss'] = df['profit_loss'].apply(lambda x: f"${x:.2f}") + + st.dataframe(df, use_container_width=True) + else: + st.info("No trade history available yet") + + +def render_configuration(): + """Render configuration management interface""" + st.subheader("âš™ī¸ Configuration") + + config = st.session_state.config + + with st.expander("Trading Settings", expanded=False): + col1, col2 = st.columns(2) + + with col1: + symbols = st.text_area( + "Trading Symbols (one per line)", + value="\n".join(config.get('trading', {}).get('symbols', [])), + height=100 + ) + + initial_capital = st.number_input( + "Initial Capital ($)", + value=config.get('trading', {}).get('initial_capital', DEFAULT_INITIAL_CAPITAL), + min_value=1000, + step=1000 + ) + + max_position_size = st.slider( + "Max Position Size (%)", + min_value=5, + max_value=50, + value=int(config.get('trading', {}).get('max_position_size', 0.2) * 100), + step=5 + ) + + with col2: + stop_loss = st.slider( + "Stop Loss (%)", + min_value=1, + max_value=10, + value=int(config.get('trading', {}).get('stop_loss', 0.02) * 100), + step=1 + ) + + take_profit = st.slider( + "Take Profit (%)", + min_value=1, + max_value=20, + value=int(config.get('trading', {}).get('take_profit', 0.05) * 100), + step=1 + ) + + if st.button("Save Configuration"): + # Update config + if 'trading' not in config: + config['trading'] = {} + + config['trading']['symbols'] = [s.strip() for s in symbols.split('\n') if s.strip()] + config['trading']['initial_capital'] = initial_capital + config['trading']['max_position_size'] = max_position_size / 100 + config['trading']['stop_loss'] = stop_loss / 100 + config['trading']['take_profit'] = take_profit / 100 + + save_config(config) + st.session_state.config = config + + with st.expander("Risk Management Settings", expanded=False): + col1, col2 = st.columns(2) + + with col1: + max_daily_loss = st.slider( + "Max Daily Loss (%)", + min_value=1, + max_value=20, + value=int(config.get('risk', {}).get('max_daily_loss', 0.05) * 100), + step=1 + ) + + with col2: + max_portfolio_risk = st.slider( + "Max Portfolio Risk (%)", + min_value=5, + max_value=50, + value=int(config.get('risk', {}).get('max_portfolio_risk', 0.15) * 100), + step=5 + ) + + if st.button("Save Risk Settings"): + if 'risk' not in config: + config['risk'] = {} + + config['risk']['max_daily_loss'] = max_daily_loss / 100 + config['risk']['max_portfolio_risk'] = max_portfolio_risk / 100 + + save_config(config) + st.session_state.config = config + + +def render_ml_model_info(): + """Render ML model information""" + st.subheader("🧠 ML Model Information") + + if st.session_state.bot is None or not st.session_state.bot_status['trained']: + st.info("Train the bot to view model information") + return + + col1, col2, col3 = st.columns(3) + + with col1: + st.markdown("**LSTM Model**") + st.caption("Deep learning for time series") + st.caption("Weight: 40%") + + with col2: + st.markdown("**Random Forest**") + st.caption("Tree ensemble classifier") + st.caption("Weight: 30%") + + with col3: + st.markdown("**XGBoost**") + st.caption("Gradient boosting") + st.caption("Weight: 30%") + + +def main(): + """Main application""" + # Initialize session state + initialize_session_state() + + # Render header + render_header() + + st.divider() + + # Render status bar + render_status_bar() + + st.divider() + + # Render controls + render_controls() + + st.divider() + + # Main content + tab1, tab2, tab3, tab4 = st.tabs(["📊 Dashboard", "📈 Performance", "âš™ī¸ Configuration", "🧠 ML Models"]) + + with tab1: + render_portfolio_metrics() + st.divider() + render_equity_curve() + st.divider() + render_positions() + st.divider() + render_trade_history() + + with tab2: + render_performance_metrics() + + with tab3: + render_configuration() + + with tab4: + render_ml_model_info() + + # Footer + st.divider() + st.caption("AI Trading Bot Dashboard - Powered by Streamlit | Last refreshed: " + datetime.now().strftime("%Y-%m-%d %H:%M:%S")) + + +if __name__ == "__main__": + main() diff --git a/requirements.txt b/requirements.txt index 0626b80..360e0e9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,6 +13,7 @@ yfinance>=0.2.28 # API and Web Framework flask>=3.0.0 flask-cors>=4.0.0 +streamlit>=1.28.0 requests>=2.31.0 # Visualization diff --git a/trading_bot/data.py b/trading_bot/data.py new file mode 100644 index 0000000..7abdad6 --- /dev/null +++ b/trading_bot/data.py @@ -0,0 +1,217 @@ +""" +Data Module - Market data fetching and feature engineering +Provides data acquisition and technical indicator calculation +""" +import pandas as pd +import numpy as np +import yfinance as yf +from typing import Dict, List, Tuple +import logging +from datetime import datetime, timedelta + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + + +class MarketDataFetcher: + """Fetches market data from various sources""" + + def __init__(self, symbols: List[str], interval: str = '1h', history_days: int = 365): + """ + Initialize market data fetcher + + Args: + symbols: List of trading symbols (e.g., ['BTC/USDT', 'AAPL']) + interval: Data interval (e.g., '1h', '1d') + history_days: Number of days of historical data + """ + self.symbols = symbols + self.interval = interval + self.history_days = history_days + logger.info(f"MarketDataFetcher initialized for {len(symbols)} symbols") + + def fetch_all(self) -> Dict[str, pd.DataFrame]: + """ + Fetch data for all symbols + + Returns: + Dictionary mapping symbol to DataFrame with OHLCV data + """ + data = {} + for symbol in self.symbols: + try: + df = self.fetch_symbol(symbol) + if df is not None and len(df) > 0: + data[symbol] = df + logger.info(f"Fetched {len(df)} records for {symbol}") + except Exception as e: + logger.error(f"Error fetching data for {symbol}: {e}") + + return data + + def fetch_symbol(self, symbol: str) -> pd.DataFrame: + """ + Fetch data for a single symbol + + Args: + symbol: Trading symbol + + Returns: + DataFrame with OHLCV columns + """ + try: + # Convert symbol format for yfinance (BTC/USDT -> BTC-USD) + yf_symbol = self._convert_symbol_format(symbol) + + # Calculate date range + end_date = datetime.now() + start_date = end_date - timedelta(days=self.history_days) + + # Fetch data from yfinance + ticker = yf.Ticker(yf_symbol) + df = ticker.history(start=start_date, end=end_date, interval=self._convert_interval()) + + # Select only OHLCV columns (yfinance already provides standardized names) + df = df[['Open', 'High', 'Low', 'Close', 'Volume']] + + return df + + except Exception as e: + logger.error(f"Error fetching {symbol}: {e}") + return pd.DataFrame() + + def _convert_symbol_format(self, symbol: str) -> str: + """Convert trading symbol to yfinance format""" + # Handle crypto pairs + if '/' in symbol: + base, quote = symbol.split('/') + if quote == 'USDT': + return f"{base}-USD" + return f"{base}-{quote}" + # Handle stock symbols + return symbol + + def _convert_interval(self) -> str: + """Convert interval format to yfinance format""" + interval_map = { + '1h': '1h', + '1d': '1d', + '1m': '1m', + '5m': '5m', + '15m': '15m', + '30m': '30m', + '1wk': '1wk' + } + return interval_map.get(self.interval, '1d') + + +class FeatureEngineering: + """Feature engineering for trading data""" + + @staticmethod + def add_technical_indicators(df: pd.DataFrame) -> pd.DataFrame: + """ + Add technical indicators to OHLCV data + + Args: + df: DataFrame with OHLCV columns + + Returns: + DataFrame with additional technical indicator columns + """ + df = df.copy() + + # Moving Averages + df['SMA_20'] = df['Close'].rolling(window=20).mean() + df['SMA_50'] = df['Close'].rolling(window=50).mean() + df['EMA_12'] = df['Close'].ewm(span=12, adjust=False).mean() + df['EMA_26'] = df['Close'].ewm(span=26, adjust=False).mean() + + # RSI (Relative Strength Index) + df['RSI'] = FeatureEngineering._calculate_rsi(df['Close']) + + # MACD + df['MACD'] = df['EMA_12'] - df['EMA_26'] + df['MACD_Signal'] = df['MACD'].ewm(span=9, adjust=False).mean() + df['MACD_Hist'] = df['MACD'] - df['MACD_Signal'] + + # Bollinger Bands + df['BB_Middle'] = df['Close'].rolling(window=20).mean() + bb_std = df['Close'].rolling(window=20).std() + df['BB_Upper'] = df['BB_Middle'] + (bb_std * 2) + df['BB_Lower'] = df['BB_Middle'] - (bb_std * 2) + + # Volume indicators + df['Volume_MA'] = df['Volume'].rolling(window=20).mean() + df['Volume_Ratio'] = df['Volume'] / df['Volume_MA'] + + # Volatility + df['Returns'] = df['Close'].pct_change() + df['Volatility'] = df['Returns'].rolling(window=20).std() + + # ATR (Average True Range) + df['ATR'] = FeatureEngineering._calculate_atr(df) + + # Rate of Change + df['ROC'] = df['Close'].pct_change(periods=10) + + # Stochastic Oscillator + df['Stoch_K'], df['Stoch_D'] = FeatureEngineering._calculate_stochastic(df) + + return df + + @staticmethod + def _calculate_rsi(prices: pd.Series, period: int = 14) -> pd.Series: + """Calculate Relative Strength Index""" + delta = prices.diff() + gain = (delta.where(delta > 0, 0)).rolling(window=period).mean() + loss = (-delta.where(delta < 0, 0)).rolling(window=period).mean() + + rs = gain / loss + rsi = 100 - (100 / (1 + rs)) + return rsi + + @staticmethod + def _calculate_atr(df: pd.DataFrame, period: int = 14) -> pd.Series: + """Calculate Average True Range""" + high_low = df['High'] - df['Low'] + high_close = np.abs(df['High'] - df['Close'].shift()) + low_close = np.abs(df['Low'] - df['Close'].shift()) + + ranges = pd.concat([high_low, high_close, low_close], axis=1) + true_range = ranges.max(axis=1) + atr = true_range.rolling(window=period).mean() + + return atr + + @staticmethod + def _calculate_stochastic(df: pd.DataFrame, k_period: int = 14, d_period: int = 3) -> Tuple[pd.Series, pd.Series]: + """Calculate Stochastic Oscillator""" + low_min = df['Low'].rolling(window=k_period).min() + high_max = df['High'].rolling(window=k_period).max() + + stoch_k = 100 * ((df['Close'] - low_min) / (high_max - low_min)) + stoch_d = stoch_k.rolling(window=d_period).mean() + + return stoch_k, stoch_d + + @staticmethod + def create_sequences(data: np.ndarray, lookback: int) -> Tuple[np.ndarray, np.ndarray]: + """ + Create sequences for LSTM training + + Args: + data: Input data array + lookback: Number of time steps to look back + + Returns: + Tuple of (X sequences, y targets) + """ + X, y = [], [] + + for i in range(lookback, len(data)): + X.append(data[i-lookback:i]) + # Predict next close price (assuming close is first feature) + y.append(data[i, 0] if len(data.shape) > 1 else data[i]) + + return np.array(X), np.array(y)