#!/usr/bin/env python3 import argparse import json import subprocess import sys import threading import watchlist from http.server import ThreadingHTTPServer, BaseHTTPRequestHandler from pathlib import Path from urllib.parse import urlparse ROOT=Path(__file__).resolve().parent STATE=ROOT/'data'/'state.json' TRADES=ROOT/'data'/'trades.jsonl' AGENT='bot.py' SCAN_LOCK=threading.Lock() INDEX='''Local Trading Agent

Local Trading Agent / paper

Loading account…

No real orders. Stops are checked during scans and may fill beyond 1%. $100 → $3,000 requires 2,900% growth; it is a goal, not an expected result.

Download report

Equity history

Positions

MarketQuantityEntryLast markStopUnrealized*

*Includes entry fees; excludes estimated exit costs.

Market signals

MarketStrategyTimeframePriceDecision / data status

Token research watchlist

Research only — excluded from both trading engines. Notes below are user-supplied and unverified. Adding a mint records an identifier; it does not verify legitimacy, liquidity, sellability or ownership rights.

Recent audit log

''' def report_text(st): scan=st.get('last_scan') or {} eq=scan.get('equity_usd',st.get('cash_usd',st.get('starting_cash_usd',100))) start=st.get('starting_cash_usd',100) lines=['LOCAL TRADING AGENT — PAPER REPORT',f"Profile: {st.get('profile','crypto-dip')}", f"Last scan: {scan.get('ts','never')}",f'Equity: ${eq:.2f}', f"Cash: ${st.get('cash_usd',0):.2f}",f'Return: {(eq/start-1)*100:.2f}%', f"Realized net: ${st.get('realized_pnl_usd',0):.2f}", f"Entry pause: {scan.get('block_reason') or 'none'}",''] for sig in st.get('last_signals',[]): lines.append(f"{sig['symbol']}: {sig.get('strategy','dip')} — {sig.get('error') or sig.get('entry_block') or ('entry signal' if sig.get('entry') else 'wait')}") lines+=['','ETF marks may be old while sessions are closed. Stops are polled, not broker-held.', 'Fees/slippage are estimates. No real orders. No income guarantee.'] return '\n'.join(lines) class H(BaseHTTPRequestHandler): def send(self,code,body,ctype='application/json'): b=body.encode();self.send_response(code);self.send_header('Content-Type',ctype);self.send_header('Content-Length',str(len(b)));self.send_header('Cache-Control','no-store');self.send_header('X-Content-Type-Options','nosniff');self.end_headers();self.wfile.write(b) def do_GET(self): p=urlparse(self.path).path if p=='/':return self.send(200,INDEX,'text/html; charset=utf-8') if p=='/api/watchlist':return self.send(200,json.dumps(watchlist.read())) if p=='/api/state': # If someone opens the JSON endpoint directly from a phone/browser, # send them to the dashboard instead of a raw JSON wall. Programmatic # callers and dashboard fetch() still receive JSON. accept=self.headers.get('Accept','') if 'text/html' in accept and 'application/json' not in accept: self.send_response(303);self.send_header('Location','/');self.send_header('Cache-Control','no-store');self.end_headers();return return self.send(200,STATE.read_text() if STATE.exists() else '{}') if p=='/api/report':return self.send(200,report_text(json.loads(STATE.read_text()) if STATE.exists() else {}),'text/plain; charset=utf-8') if p=='/api/log': from collections import deque if not TRADES.exists():return self.send(200,'','text/plain') with TRADES.open() as f:body=''.join(deque(f,maxlen=100)) return self.send(200,body,'text/plain; charset=utf-8') self.send(404,json.dumps({'error':'not found'})) def do_POST(self): if urlparse(self.path).path=='/api/watchlist': if self.headers.get('X-Local-Scan')!='1':return self.send(403,'{}') try: length=int(self.headers.get('Content-Length','0')) if not 0