|
@@ -101,7 +101,6 @@ class TelegramTradingBot:
|
|
|
|
|
|
# Callback and message handlers
|
|
|
self.application.add_handler(CallbackQueryHandler(self.trading_commands.button_callback))
|
|
|
- self.application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, self.handle_keyboard_message))
|
|
|
|
|
|
async def start_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
|
|
"""Handle the /start command."""
|
|
@@ -109,33 +108,103 @@ class TelegramTradingBot:
|
|
|
await update.message.reply_text("❌ Unauthorized access.")
|
|
|
return
|
|
|
|
|
|
+ # Determine risk management and stop loss details from Config
|
|
|
+ risk_enabled = getattr(Config, 'RISK_MANAGEMENT_ENABLED', False)
|
|
|
+ stop_loss_percentage = getattr(Config, 'STOP_LOSS_PERCENTAGE', 0)
|
|
|
+ bot_heartbeat = getattr(Config, 'BOT_HEARTBEAT_SECONDS', 10)
|
|
|
+
|
|
|
welcome_text = f"""
|
|
|
-🤖 <b>Manual Trading Bot v{self.version}</b>
|
|
|
+🤖 <b>Welcome to Hyperliquid Trading Bot v{self.version}</b>
|
|
|
+
|
|
|
+📱 <b>Quick Actions:</b>
|
|
|
+• Trading: /long {Config.DEFAULT_TRADING_TOKEN} 100 or /short {Config.DEFAULT_TRADING_TOKEN} 50
|
|
|
+• Exit: /exit {Config.DEFAULT_TRADING_TOKEN} (closes position)
|
|
|
+• Info: /balance, /positions, /orders
|
|
|
|
|
|
-🚀 <b>Welcome to your personal trading assistant!</b>
|
|
|
+📊 <b>Market Data:</b>
|
|
|
+• /market - Detailed market overview
|
|
|
+• /price - Quick price check
|
|
|
|
|
|
-📈 <b>Trading Commands:</b>
|
|
|
-• /long [token] [amount] - Open long position
|
|
|
-• /short [token] [amount] - Open short position
|
|
|
-• /exit [token] - Close position
|
|
|
-• /sl [token] [price] - Set stop loss
|
|
|
-• /tp [token] [price] - Set take profit
|
|
|
+<b>⚡ Quick Commands:</b>
|
|
|
+• /balance - Account balance
|
|
|
+• /positions - Open positions
|
|
|
+• /orders - Active orders
|
|
|
+• /market - Market data & prices
|
|
|
|
|
|
-📊 <b>Info Commands:</b>
|
|
|
-• /balance - Check account balance
|
|
|
-• /positions - View open positions
|
|
|
-• /stats - Trading statistics
|
|
|
-• /market [token] - Market data
|
|
|
+<b>🚀 Trading:</b>
|
|
|
+• /long {Config.DEFAULT_TRADING_TOKEN} 100 - Long position
|
|
|
+• /long {Config.DEFAULT_TRADING_TOKEN} 100 45000 - Limit order
|
|
|
+• /long {Config.DEFAULT_TRADING_TOKEN} 100 sl:44000 - With stop loss
|
|
|
+• /short {Config.DEFAULT_TRADING_TOKEN} 50 - Short position
|
|
|
+• /short {Config.DEFAULT_TRADING_TOKEN} 50 3500 sl:3600 - With stop loss
|
|
|
+• /exit {Config.DEFAULT_TRADING_TOKEN} - Close position
|
|
|
+• /coo {Config.DEFAULT_TRADING_TOKEN} - Cancel open orders
|
|
|
|
|
|
-⚙️ <b>Management:</b>
|
|
|
-• /monitoring - Toggle monitoring
|
|
|
-• /alarm - Set price alerts
|
|
|
-• /help - Full command list
|
|
|
+<b>🛡️ Risk Management:</b>
|
|
|
+• Enabled: {'✅ Yes' if risk_enabled else '❌ No'}
|
|
|
+• Auto Stop Loss: {stop_loss_percentage}%
|
|
|
+• Order Stop Loss: Use sl:price parameter
|
|
|
+• /sl {Config.DEFAULT_TRADING_TOKEN} 44000 - Manual stop loss
|
|
|
+• /tp {Config.DEFAULT_TRADING_TOKEN} 50000 - Take profit order
|
|
|
+
|
|
|
+<b>📈 Performance & Analytics:</b>
|
|
|
+• /stats - Complete trading statistics
|
|
|
+• /performance - Token performance ranking & detailed stats
|
|
|
+• /daily - Daily performance (last 10 days)
|
|
|
+• /weekly - Weekly performance (last 10 weeks)
|
|
|
+• /monthly - Monthly performance (last 10 months)
|
|
|
+• /risk - Sharpe ratio, drawdown, VaR
|
|
|
+• /version - Bot version & system information
|
|
|
+• /trades - Recent trade history
|
|
|
+
|
|
|
+<b>🔔 Price Alerts:</b>
|
|
|
+• /alarm - List all active alarms
|
|
|
+• /alarm {Config.DEFAULT_TRADING_TOKEN} 50000 - Set alarm for {Config.DEFAULT_TRADING_TOKEN} at $50,000
|
|
|
+• /alarm {Config.DEFAULT_TRADING_TOKEN} - Show all {Config.DEFAULT_TRADING_TOKEN} alarms
|
|
|
+• /alarm 3 - Remove alarm ID 3
|
|
|
+
|
|
|
+<b>🔄 Automatic Monitoring:</b>
|
|
|
+• Real-time order fill alerts
|
|
|
+• Position opened/closed notifications
|
|
|
+• P&L calculations on trade closure
|
|
|
+• Price alarm triggers
|
|
|
+• External trade detection & sync
|
|
|
+• Auto stats synchronization
|
|
|
+• Automatic stop loss placement
|
|
|
+• {bot_heartbeat}-second monitoring interval
|
|
|
+
|
|
|
+<b>📊 Universal Trade Tracking:</b>
|
|
|
+• Bot trades: Full logging & notifications
|
|
|
+• Platform trades: Auto-detected & synced
|
|
|
+• Mobile app trades: Monitored & recorded
|
|
|
+• API trades: Tracked & included in stats
|
|
|
+
|
|
|
+Type /help for detailed command information.
|
|
|
+
|
|
|
+<b>🔄 Order Monitoring:</b>
|
|
|
+• /monitoring - View monitoring status
|
|
|
+• /logs - View log file statistics and cleanup
|
|
|
+
|
|
|
+<b>⚙️ Configuration:</b>
|
|
|
+• Default Token: {Config.DEFAULT_TRADING_TOKEN}
|
|
|
+• Network: {'Testnet' if Config.HYPERLIQUID_TESTNET else 'Mainnet'}
|
|
|
+
|
|
|
+<b>🛡️ Safety Features:</b>
|
|
|
+• All trades logged automatically
|
|
|
+• Comprehensive performance tracking
|
|
|
+• Real-time balance monitoring
|
|
|
+• Risk metrics calculation
|
|
|
+• Automatic stop loss protection
|
|
|
|
|
|
-🔗 <b>Network:</b> {Config.HYPERLIQUID_TESTNET and "Testnet" or "Mainnet"}
|
|
|
-📊 <b>Default Token:</b> {Config.DEFAULT_TRADING_TOKEN}
|
|
|
+<b>📱 Mobile Optimized:</b>
|
|
|
+• Quick action buttons via /commands
|
|
|
+• Instant notifications
|
|
|
+• Clean, readable layout
|
|
|
|
|
|
-Ready to trade! Use the commands above or tap /help for more options.
|
|
|
+<b>💡 Quick Access:</b>
|
|
|
+• /commands or /c - One-tap button menu for all commands
|
|
|
+
|
|
|
+For support, contact your bot administrator.
|
|
|
"""
|
|
|
|
|
|
await update.message.reply_text(welcome_text, parse_mode='HTML')
|
|
@@ -179,27 +248,6 @@ For support or issues, check the logs or contact the administrator.
|
|
|
|
|
|
await update.message.reply_text(help_text, parse_mode='HTML')
|
|
|
|
|
|
- async def handle_keyboard_message(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
|
|
- """Handle messages from custom keyboard buttons."""
|
|
|
- if not self.is_authorized(update.effective_chat.id):
|
|
|
- await update.message.reply_text("❌ Unauthorized access.")
|
|
|
- return
|
|
|
-
|
|
|
- # For now, just ignore keyboard messages or implement basic ones
|
|
|
- message_text = update.message.text.lower()
|
|
|
-
|
|
|
- # Basic keyboard shortcuts
|
|
|
- if message_text in ['balance', 'positions', 'orders', 'stats']:
|
|
|
- # Route to appropriate command
|
|
|
- if message_text == 'balance':
|
|
|
- await self.info_commands.balance_command(update, context)
|
|
|
- elif message_text == 'positions':
|
|
|
- await self.info_commands.positions_command(update, context)
|
|
|
- elif message_text == 'orders':
|
|
|
- await self.info_commands.orders_command(update, context)
|
|
|
- elif message_text == 'stats':
|
|
|
- await self.info_commands.stats_command(update, context)
|
|
|
-
|
|
|
async def run(self):
|
|
|
"""Run the Telegram bot."""
|
|
|
if not Config.TELEGRAM_BOT_TOKEN:
|
|
@@ -222,9 +270,6 @@ For support or issues, check the logs or contact the administrator.
|
|
|
|
|
|
logger.info("🚀 Starting Telegram trading bot...")
|
|
|
|
|
|
- # Initialize the application
|
|
|
- await self.application.initialize()
|
|
|
-
|
|
|
# Send startup notification
|
|
|
await self.send_message(
|
|
|
f"🤖 <b>Manual Trading Bot v{self.version} Started</b>\n\n"
|
|
@@ -234,31 +279,15 @@ For support or issues, check the logs or contact the administrator.
|
|
|
"Use /start for quick actions or /help for all commands."
|
|
|
)
|
|
|
|
|
|
- # Start the application
|
|
|
- await self.application.start()
|
|
|
-
|
|
|
# Start subsystems
|
|
|
await self.market_monitor.start()
|
|
|
|
|
|
- # Start polling for updates
|
|
|
- logger.info("🔄 Starting update polling...")
|
|
|
-
|
|
|
- last_update_id = 0
|
|
|
- while True:
|
|
|
- try:
|
|
|
- updates = await self.application.bot.get_updates(
|
|
|
- offset=last_update_id + 1,
|
|
|
- timeout=30,
|
|
|
- allowed_updates=None
|
|
|
- )
|
|
|
-
|
|
|
- for update in updates:
|
|
|
- last_update_id = update.update_id
|
|
|
- await self.application.process_update(update)
|
|
|
-
|
|
|
- except Exception as e:
|
|
|
- logger.error(f"Error processing updates: {e}")
|
|
|
- await asyncio.sleep(5)
|
|
|
+ # Start polling for updates using the modern approach
|
|
|
+ logger.info("🔄 Starting bot polling...")
|
|
|
+ await self.application.run_polling(
|
|
|
+ drop_pending_updates=True,
|
|
|
+ stop_signals=None # Let the main process handle signals
|
|
|
+ )
|
|
|
|
|
|
except asyncio.CancelledError:
|
|
|
logger.info("🛑 Bot polling cancelled")
|