from fpdf import FPDF class PDF(FPDF): def header(self): self.set_font("helvetica", "B", 15) self.cell(0, 10, "Dynamic Momentum & Machine Learning Bot", border=False, align="C") self.ln(20) def footer(self): self.set_y(-15) self.set_font("helvetica", "I", 8) self.cell(0, 10, f"Page {self.page_no()}", align="C") pdf = PDF() pdf.add_page() pdf.set_font("helvetica", size=12) content = """ Your Core Strategy (The Rules) Your bot is a Dynamic Momentum & Machine Learning Swing/Day Trader. Its strict rules are: 1. The Universe: S&P 500 stocks + Highly Liquid ETFs. 2. Liquidity Rule: Only trade assets with > 2,000,000 Average Daily Volume (ADV). 3. Trend Rule: Only buy assets that have a positive 1-week momentum. 4. Price Filter & Preference: Hard cap at $200 per share. Absolute first preference is given to stocks under $100 (Tier 1). Stocks between $100-$200 (Tier 2) are only chosen if there aren't enough Tier 1 stocks. 5. Risk Management: Machine Learning (Kelly Criterion) dictates position size, and Dynamic Volatility Trailing Stops protect profits. -------------------------------------------------- Step-by-Step Daily Execution Flow Step 1: Startup & Market Assessment (Pre 10:30 AM ET) When you turn the bot on, it connects to Alpaca and assesses your portfolio. It looks at the broader market (SPY) using a Regime Filter. If the overall market is crashing, the bot automatically reduces its maximum risk exposure. It waits patiently until 10:30 AM to let the morning volatility settle. Step 2: Universe Generation (10:30 AM ET) At exactly 10:30 AM, the bot initiates your custom dynamic scan: - It scans all S&P 500 stocks and ETFs. - It strips out illiquid stocks (< 2M ADV), expensive stocks (> $200), and negative trending stocks. - It sorts the surviving list, putting all sub-$100 stocks at the very top. - It locks in the Top 40 momentum candidates for the day. - It mass-downloads the historical candlestick data for these 40 specific symbols. Step 3: Technical, Sentiment, and ML Scoring For each of the 40 candidates, the bot digs deep: - Technicals: Calculates MACD, RSI, and Volume breakouts. - Sentiment: Scans the web for recent news headlines and assigns a sentiment score. - Machine Learning Prediction: The ML model evaluates the setup and assigns a strict "Win Probability" (e.g., 65% chance of success). - Final Ranking: The bot sorts the final buy list, mathematically guaranteeing that the highly-rated, sub-$100 stocks are placed at the very front of the line. Step 4: Position Sizing (Kelly Criterion) Before buying, the bot determines exactly how many shares to purchase. Instead of risking a flat 1% on every trade, it uses the Kelly Criterion. If the Machine Learning model detects an "A+" setup, it sizes the position larger. If it's a standard setup, it buys a smaller fraction. Step 5: Execution & Dynamic Trailing Stops The bot routes the order to Alpaca. The moment the order fills, the bot calculates the stock's real-time volatility (ATR) and attaches a Volatility Trailing Stop. If the stock shoots up 3%, your stop-loss moves up automatically behind it. If the stock suddenly reverses, you get stopped out in profit. Step 6: End of Day Sweep & Data Wipe (4:00 PM ET) When the market closes: - Sweep: The bot sells off any short-term positions to lock in profits, avoiding overnight gap-down risks. - The Wipe: The system runs `daily_wipe.py`. It completely deletes the day's database, the market data, the ML models, and the ML trade history. - Sleep: The bot goes to sleep entirely stateless, ready to wake up the next morning and view the market with a completely fresh, unbiased lens. """ # Output multi-cell text pdf.multi_cell(0, 7, content) pdf.output("Bot_Strategy_and_Execution_Flow.pdf") print("PDF created successfully!")