Spaces:
Paused
Paused
| #!/usr/bin/env python3 | |
| """ | |
| Bybit Scalping Bot - FastAPI Control Interface | |
| This bot now uses FastAPI for remote control instead of direct command line operation. | |
| Use the API endpoints to start/stop trading sessions for individual pairs. | |
| API Endpoints: | |
| - GET /status - Get bot status | |
| - POST /start/{symbol} - Start trading for a symbol (18h default) | |
| - POST /stop/{symbol} - Stop trading for a symbol | |
| - GET /sessions - Get all trading sessions | |
| - GET /report/{session_id} - Get session report | |
| - POST /emergency_stop - Emergency stop all trading | |
| Examples: | |
| - Start BTC trading: POST /start/BTCUSDT | |
| - Stop ETH trading: POST /stop/ETHUSDT | |
| - Check status: GET /status | |
| """ | |
| import sys | |
| import subprocess | |
| import os | |
| def show_usage(): | |
| print(""" | |
| π Bybit Scalping Bot - FastAPI Control Interface | |
| βββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| π‘ API SERVER CONTROL: | |
| β’ Start API Server: python api_server.py | |
| β’ Server runs on: http://localhost:8000 | |
| π API ENDPOINTS: | |
| GET /status | |
| Returns current bot status and active sessions | |
| POST /start/{symbol}?duration_hours=18 | |
| Start trading session for specific symbol | |
| Examples: | |
| β’ POST /start/BTCUSDT | |
| β’ POST /start/ETHUSDT?duration_hours=24 | |
| POST /stop/{symbol} | |
| Stop trading session for specific symbol | |
| Example: POST /stop/BTCUSDT | |
| GET /sessions | |
| Get all trading sessions (active and completed) | |
| GET /report/{session_id} | |
| Get detailed report for a completed session | |
| POST /emergency_stop | |
| Emergency stop all trading sessions | |
| π§ͺ TESTING & BACKTESTING: | |
| python main.py backtest | |
| Run backtesting for all configured pairs | |
| python test_api_raw.py | |
| Test API connectivity | |
| π CONFIGURED PAIRS: | |
| β’ BTCUSDT | |
| β’ ETHUSDT | |
| β’ SOLUSDT | |
| βοΈ TRADING PARAMETERS: | |
| β’ Leverage: 20x | |
| β’ Take Profit: 2.5% | |
| β’ Stop Loss: 1% | |
| β’ Risk per Trade: 2% | |
| β’ Strategy: EMA 9/21 + RSI 14 + Volume + Orderbook | |
| π± TELEGRAM ALERTS: | |
| Configure in .env file for trade notifications | |
| βββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| """) | |
| def run_backtest(): | |
| """Run backtesting mode""" | |
| from backtester.engine import BacktestingEngine | |
| from services.logger import log | |
| import yaml | |
| log("π¬ Starting backtesting mode") | |
| settings = yaml.safe_load(open("config/settings.yaml")) | |
| pairs = yaml.safe_load(open("config/pairs.yaml"))["pairs"] | |
| backtester = BacktestingEngine() | |
| results = {} | |
| for symbol in pairs: | |
| log(f"π Backtesting {symbol}...") | |
| result = backtester.run_backtest(symbol) | |
| results[symbol] = result | |
| if 'error' not in result: | |
| log(f"β {symbol}: {result['total_trades']} trades, PnL: ${result['total_pnl']:.2f}, Win Rate: {result['win_rate']:.1%}") | |
| else: | |
| log(f"β {symbol}: {result['error']}") | |
| backtester.save_results() | |
| log("\n" + "="*50) | |
| log("BACKTESTING REPORT") | |
| log("="*50) | |
| for symbol in pairs: | |
| if symbol in results and 'error' not in results[symbol]: | |
| report = backtester.generate_report(symbol) | |
| print(report) | |
| def start_api_server(): | |
| """Start the FastAPI server""" | |
| print("π Starting FastAPI server...") | |
| print("π‘ Server will be available at: http://localhost:8000") | |
| print("π API documentation at: http://localhost:8000/docs") | |
| print() | |
| try: | |
| subprocess.run([sys.executable, "api_server.py"]) | |
| except KeyboardInterrupt: | |
| print("\nπ API server stopped") | |
| if __name__ == "__main__": | |
| if len(sys.argv) > 1: | |
| if sys.argv[1] == "backtest": | |
| run_backtest() | |
| elif sys.argv[1] == "api": | |
| start_api_server() | |
| elif sys.argv[1] == "help" or sys.argv[1] == "-h": | |
| show_usage() | |
| else: | |
| print(f"β Unknown command: {sys.argv[1]}") | |
| print("Use 'python main.py help' for available commands") | |
| else: | |
| show_usage() | |