Spaces:
Paused
Paused
File size: 4,109 Bytes
96e0cc2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
#!/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()
|