improve trading signals

This commit is contained in:
Dobromir Popov
2025-06-25 13:41:01 +03:00
parent fdb9e83cf9
commit ad76b70788
6 changed files with 1067 additions and 503 deletions

View File

@ -803,3 +803,89 @@ class TradingExecutor:
'sync_available': False,
'error': str(e)
}
def execute_trade(self, symbol: str, action: str, quantity: float) -> bool:
"""Execute a trade directly (compatibility method for dashboard)
Args:
symbol: Trading symbol (e.g., 'ETH/USDT')
action: Trading action ('BUY', 'SELL')
quantity: Quantity to trade
Returns:
bool: True if trade executed successfully
"""
try:
# Get current price
current_price = None
ticker = self.exchange.get_ticker(symbol)
if ticker:
current_price = ticker['last']
else:
logger.error(f"Failed to get current price for {symbol}")
return False
# Calculate confidence based on manual trade (high confidence)
confidence = 1.0
# Execute using the existing signal execution method
return self.execute_signal(symbol, action, confidence, current_price)
except Exception as e:
logger.error(f"Error executing trade {action} for {symbol}: {e}")
return False
def get_closed_trades(self) -> List[Dict[str, Any]]:
"""Get closed trades in dashboard format"""
try:
trades = []
for trade in self.trade_history:
trade_dict = {
'symbol': trade.symbol,
'side': trade.side,
'quantity': trade.quantity,
'entry_price': trade.entry_price,
'exit_price': trade.exit_price,
'entry_time': trade.entry_time,
'exit_time': trade.exit_time,
'pnl': trade.pnl,
'fees': trade.fees,
'confidence': trade.confidence
}
trades.append(trade_dict)
return trades
except Exception as e:
logger.error(f"Error getting closed trades: {e}")
return []
def get_current_position(self, symbol: str = None) -> Optional[Dict[str, Any]]:
"""Get current position for a symbol or all positions
Args:
symbol: Optional symbol to get position for. If None, returns first position.
Returns:
dict: Position information or None if no position
"""
try:
if symbol:
if symbol in self.positions:
pos = self.positions[symbol]
return {
'symbol': pos.symbol,
'side': pos.side,
'size': pos.quantity,
'price': pos.entry_price,
'entry_time': pos.entry_time,
'unrealized_pnl': pos.unrealized_pnl
}
return None
else:
# Return first position if no symbol specified
if self.positions:
first_symbol = list(self.positions.keys())[0]
return self.get_current_position(first_symbol)
return None
except Exception as e:
logger.error(f"Error getting current position: {e}")
return None