Enhance SL/TP calculation and order handling in LiveRegimeStrategy and OKXClient
- Updated `calculate_sl_tp` method to handle invalid entry prices and sides, returning (None, None) when necessary. - Improved logging for SL/TP values in `LiveTradingBot` to display "N/A" for invalid values. - Refined order placement in `OKXClient` to ensure guaranteed fill price retrieval, with fallback mechanisms for fetching order details and ticker prices if needed. - Added error handling for scenarios where fill prices cannot be determined.
This commit is contained in:
@@ -261,9 +261,9 @@ class LiveRegimeStrategy:
|
||||
|
||||
def calculate_sl_tp(
|
||||
self,
|
||||
entry_price: float,
|
||||
entry_price: Optional[float],
|
||||
side: str
|
||||
) -> tuple[float, float]:
|
||||
) -> tuple[Optional[float], Optional[float]]:
|
||||
"""
|
||||
Calculate stop-loss and take-profit prices.
|
||||
|
||||
@@ -272,10 +272,23 @@ class LiveRegimeStrategy:
|
||||
side: "long" or "short"
|
||||
|
||||
Returns:
|
||||
Tuple of (stop_loss_price, take_profit_price)
|
||||
Tuple of (stop_loss_price, take_profit_price), or (None, None) if
|
||||
entry_price is invalid
|
||||
|
||||
Raises:
|
||||
ValueError: If side is not "long" or "short"
|
||||
"""
|
||||
if entry_price is None or entry_price <= 0:
|
||||
logger.error(
|
||||
f"Invalid entry_price for SL/TP calculation: {entry_price}"
|
||||
)
|
||||
return None, None
|
||||
|
||||
if side not in ("long", "short"):
|
||||
raise ValueError(f"Invalid side: {side}. Must be 'long' or 'short'")
|
||||
|
||||
sl_pct = self.config.stop_loss_pct
|
||||
tp_pct = self.config.take_profit_pct
|
||||
tp_pct = self.config.take_profit_pct
|
||||
|
||||
if side == "long":
|
||||
stop_loss = entry_price * (1 - sl_pct)
|
||||
|
||||
Reference in New Issue
Block a user