11"""Managed trade context manager for risk-controlled trading."""
22
33import logging
4- from typing import TYPE_CHECKING , Any , Optional
4+ from typing import TYPE_CHECKING , Any
55
66from project_x_py .types import OrderSide , OrderType
77from project_x_py .types .protocols import OrderManagerProtocol , PositionManagerProtocol
@@ -31,8 +31,8 @@ def __init__(
3131 order_manager : OrderManagerProtocol ,
3232 position_manager : PositionManagerProtocol ,
3333 instrument_id : str ,
34- max_risk_percent : Optional [ float ] = None ,
35- max_risk_amount : Optional [ float ] = None ,
34+ max_risk_percent : float | None = None ,
35+ max_risk_amount : float | None = None ,
3636 ):
3737 """Initialize managed trade.
3838
@@ -52,11 +52,11 @@ def __init__(
5252 self .max_risk_amount = max_risk_amount
5353
5454 # Track orders and positions created
55- self ._orders : list [" Order" ] = []
56- self ._positions : list [" Position" ] = []
57- self ._entry_order : Optional [ " Order" ] = None
58- self ._stop_order : Optional [ " Order" ] = None
59- self ._target_order : Optional [ " Order" ] = None
55+ self ._orders : list [Order ] = []
56+ self ._positions : list [Position ] = []
57+ self ._entry_order : Order | None = None
58+ self ._stop_order : Order | None = None
59+ self ._target_order : Order | None = None
6060
6161 async def __aenter__ (self ) -> "ManagedTrade" :
6262 """Enter managed trade context."""
@@ -90,10 +90,10 @@ async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> bool:
9090
9191 async def enter_long (
9292 self ,
93- entry_price : Optional [ float ] = None ,
94- stop_loss : Optional [ float ] = None ,
95- take_profit : Optional [ float ] = None ,
96- size : Optional [ int ] = None ,
93+ entry_price : float | None = None ,
94+ stop_loss : float | None = None ,
95+ take_profit : float | None = None ,
96+ size : int | None = None ,
9797 order_type : OrderType = OrderType .MARKET ,
9898 ) -> dict [str , Any ]:
9999 """Enter a long position with risk management.
@@ -194,10 +194,12 @@ async def enter_long(
194194 bracket = risk_orders ["bracket_order" ]
195195 if "stop_order" in bracket :
196196 self ._stop_order = bracket ["stop_order" ]
197- self ._orders .append (self ._stop_order )
197+ if self ._stop_order :
198+ self ._orders .append (self ._stop_order )
198199 if "limit_order" in bracket :
199200 self ._target_order = bracket ["limit_order" ]
200- self ._orders .append (self ._target_order )
201+ if self ._target_order :
202+ self ._orders .append (self ._target_order )
201203
202204 return {
203205 "entry_order" : self ._entry_order ,
@@ -211,10 +213,10 @@ async def enter_long(
211213
212214 async def enter_short (
213215 self ,
214- entry_price : Optional [ float ] = None ,
215- stop_loss : Optional [ float ] = None ,
216- take_profit : Optional [ float ] = None ,
217- size : Optional [ int ] = None ,
216+ entry_price : float | None = None ,
217+ stop_loss : float | None = None ,
218+ take_profit : float | None = None ,
219+ size : int | None = None ,
218220 order_type : OrderType = OrderType .MARKET ,
219221 ) -> dict [str , Any ]:
220222 """Enter a short position with risk management.
@@ -308,10 +310,12 @@ async def enter_short(
308310 bracket = risk_orders ["bracket_order" ]
309311 if "stop_order" in bracket :
310312 self ._stop_order = bracket ["stop_order" ]
311- self ._orders .append (self ._stop_order )
313+ if self ._stop_order :
314+ self ._orders .append (self ._stop_order )
312315 if "limit_order" in bracket :
313316 self ._target_order = bracket ["limit_order" ]
314- self ._orders .append (self ._target_order )
317+ if self ._target_order :
318+ self ._orders .append (self ._target_order )
315319
316320 return {
317321 "entry_order" : self ._entry_order ,
@@ -326,7 +330,7 @@ async def enter_short(
326330 async def scale_in (
327331 self ,
328332 additional_size : int ,
329- new_stop_loss : Optional [ float ] = None ,
333+ new_stop_loss : float | None = None ,
330334 ) -> dict [str , Any ]:
331335 """Scale into existing position with risk checks.
332336
@@ -380,7 +384,7 @@ async def scale_in(
380384 async def scale_out (
381385 self ,
382386 exit_size : int ,
383- limit_price : Optional [ float ] = None ,
387+ limit_price : float | None = None ,
384388 ) -> dict [str , Any ]:
385389 """Scale out of position with partial exit.
386390
@@ -502,7 +506,7 @@ def _create_mock_order(
502506 self ,
503507 side : OrderSide ,
504508 size : int ,
505- price : Optional [ float ] ,
509+ price : float | None ,
506510 order_type : OrderType ,
507511 ) -> "Order" :
508512 """Create mock order for validation."""
0 commit comments