feat: Add core trading modules for risk management, backtesting, and execution algorithms, alongside a new ML transparency widget and related frontend dependencies.
Some checks are pending
Documentation / build-docs (push) Waiting to run
Tests / test (macos-latest, 3.11) (push) Waiting to run
Tests / test (macos-latest, 3.12) (push) Waiting to run
Tests / test (macos-latest, 3.13) (push) Waiting to run
Tests / test (macos-latest, 3.14) (push) Waiting to run
Tests / test (ubuntu-latest, 3.11) (push) Waiting to run
Tests / test (ubuntu-latest, 3.12) (push) Waiting to run
Tests / test (ubuntu-latest, 3.13) (push) Waiting to run
Tests / test (ubuntu-latest, 3.14) (push) Waiting to run
Some checks are pending
Documentation / build-docs (push) Waiting to run
Tests / test (macos-latest, 3.11) (push) Waiting to run
Tests / test (macos-latest, 3.12) (push) Waiting to run
Tests / test (macos-latest, 3.13) (push) Waiting to run
Tests / test (macos-latest, 3.14) (push) Waiting to run
Tests / test (ubuntu-latest, 3.11) (push) Waiting to run
Tests / test (ubuntu-latest, 3.12) (push) Waiting to run
Tests / test (ubuntu-latest, 3.13) (push) Waiting to run
Tests / test (ubuntu-latest, 3.14) (push) Waiting to run
This commit is contained in:
@@ -11,6 +11,7 @@ from src.core.database import Order, OrderSide, OrderType, OrderStatus
|
||||
from src.core.repositories import OrderRepository, PositionRepository
|
||||
from src.core.logger import get_logger
|
||||
from src.trading.paper_trading import get_paper_trading
|
||||
from src.trading.advanced_orders import get_advanced_order_manager
|
||||
|
||||
router = APIRouter()
|
||||
logger = get_logger(__name__)
|
||||
@@ -21,12 +22,20 @@ async def create_order(
|
||||
order_data: OrderCreate,
|
||||
trading_engine=Depends(get_trading_engine)
|
||||
):
|
||||
"""Create and execute a trading order."""
|
||||
"""Create and execute a trading order.
|
||||
|
||||
Supports advanced order types:
|
||||
- Trailing stop: Set trail_percent (e.g., 0.02 for 2%)
|
||||
- Bracket orders: Set bracket_take_profit and bracket_stop_loss
|
||||
- OCO orders: Set oco_price for the second order
|
||||
- Iceberg orders: Set visible_quantity
|
||||
"""
|
||||
try:
|
||||
# Convert string enums to actual enums
|
||||
side = OrderSide(order_data.side.value)
|
||||
order_type = OrderType(order_data.order_type.value)
|
||||
|
||||
# Execute the base order
|
||||
order = await trading_engine.execute_order(
|
||||
exchange_id=order_data.exchange_id,
|
||||
strategy_id=order_data.strategy_id,
|
||||
@@ -41,8 +50,71 @@ async def create_order(
|
||||
if not order:
|
||||
raise HTTPException(status_code=400, detail="Order execution failed")
|
||||
|
||||
# Handle advanced order types
|
||||
advanced_manager = get_advanced_order_manager()
|
||||
|
||||
# Trailing stop
|
||||
if order_type == OrderType.TRAILING_STOP and order_data.trail_percent:
|
||||
if not order_data.stop_loss_price:
|
||||
raise HTTPException(status_code=400, detail="stop_loss_price required for trailing stop")
|
||||
advanced_manager.create_trailing_stop(
|
||||
base_order_id=order.id,
|
||||
initial_stop_price=order_data.stop_loss_price,
|
||||
trail_percent=order_data.trail_percent
|
||||
)
|
||||
|
||||
# Take profit
|
||||
if order_data.take_profit_price:
|
||||
advanced_manager.create_take_profit(
|
||||
base_order_id=order.id,
|
||||
target_price=order_data.take_profit_price
|
||||
)
|
||||
|
||||
# Bracket order (entry + take profit + stop loss)
|
||||
if order_data.bracket_take_profit or order_data.bracket_stop_loss:
|
||||
if order_data.bracket_take_profit:
|
||||
advanced_manager.create_take_profit(
|
||||
base_order_id=order.id,
|
||||
target_price=order_data.bracket_take_profit
|
||||
)
|
||||
if order_data.bracket_stop_loss:
|
||||
advanced_manager.create_trailing_stop(
|
||||
base_order_id=order.id,
|
||||
initial_stop_price=order_data.bracket_stop_loss,
|
||||
trail_percent=Decimal("0.0") # Fixed stop, not trailing
|
||||
)
|
||||
|
||||
# OCO order
|
||||
if order_type == OrderType.OCO and order_data.oco_price:
|
||||
# Create second order
|
||||
oco_order = await trading_engine.execute_order(
|
||||
exchange_id=order_data.exchange_id,
|
||||
strategy_id=order_data.strategy_id,
|
||||
symbol=order_data.symbol,
|
||||
side=OrderSide.SELL if side == OrderSide.BUY else OrderSide.BUY,
|
||||
order_type=OrderType.LIMIT,
|
||||
quantity=order_data.quantity,
|
||||
price=order_data.oco_price,
|
||||
paper_trading=order_data.paper_trading
|
||||
)
|
||||
if oco_order:
|
||||
advanced_manager.create_oco(order.id, oco_order.id)
|
||||
|
||||
# Iceberg order
|
||||
if order_type == OrderType.ICEBERG and order_data.visible_quantity:
|
||||
advanced_manager.create_iceberg(
|
||||
total_quantity=order_data.quantity,
|
||||
visible_quantity=order_data.visible_quantity,
|
||||
symbol=order_data.symbol,
|
||||
side=side,
|
||||
price=order_data.price
|
||||
)
|
||||
|
||||
return OrderResponse.model_validate(order)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Error creating order: {e}", exc_info=True)
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user