@@ -31,7 +31,9 @@ def __init__(self, suite: TradingSuite):
3131 self .profit_target_ticks = 20
3232 self .stop_loss_ticks = 10
3333
34- async def analyze_market (self ) -> dict | None :
34+ async def analyze_market (
35+ self ,
36+ ) -> dict [str , float | int | str | dict [str , float ]] | None :
3537 """Analyze market using simplified data access."""
3638 # Use new get_data_or_none for cleaner code
3739 data = await self .data .get_data_or_none ("5min" , min_bars = 50 )
@@ -47,7 +49,12 @@ async def analyze_market(self) -> dict | None:
4749 price_range = await self .data .get_price_range (bars = 20 )
4850 volume_stats = await self .data .get_volume_stats (bars = 20 )
4951
50- if not all ([current_price , ohlc , price_range , volume_stats ]):
52+ if (
53+ current_price is None
54+ or ohlc is None
55+ or price_range is None
56+ or volume_stats is None
57+ ):
5158 return None
5259
5360 # Analyze trend
@@ -56,21 +63,30 @@ async def analyze_market(self) -> dict | None:
5663 atr = float (data ["atr_14" ][- 1 ])
5764
5865 # Price position within range
59- price_position = (current_price - price_range ["low" ]) / price_range ["range" ]
66+ price_position = (float (current_price ) - float (price_range ["low" ])) / float (
67+ price_range ["range" ]
68+ )
6069
6170 return {
62- "price" : current_price ,
63- "trend" : "bullish" if current_price > sma20 else "bearish" ,
64- "trend_strength" : abs (current_price - sma20 ) / sma20 ,
71+ "price" : float ( current_price ) ,
72+ "trend" : "bullish" if float ( current_price ) > sma20 else "bearish" ,
73+ "trend_strength" : abs (float ( current_price ) - sma20 ) / sma20 ,
6574 "rsi" : rsi ,
6675 "atr" : atr ,
6776 "price_position" : price_position ,
68- "volume_relative" : volume_stats ["relative" ],
69- "range" : price_range ["range" ],
70- "ohlc" : ohlc ,
77+ "volume_relative" : float (volume_stats ["relative" ]),
78+ "range" : float (price_range ["range" ]),
79+ "ohlc" : {
80+ "open" : float (ohlc ["open" ]),
81+ "high" : float (ohlc ["high" ]),
82+ "low" : float (ohlc ["low" ]),
83+ "close" : float (ohlc ["close" ]),
84+ },
7185 }
7286
73- async def check_positions (self ) -> dict :
87+ async def check_positions (
88+ self ,
89+ ) -> dict [str , float | int | list [dict [str , float | int | str ]]]:
7490 """Check positions using enhanced model properties."""
7591 positions = await self .positions .get_all_positions ()
7692
@@ -83,7 +99,7 @@ async def check_positions(self) -> dict:
8399 }
84100
85101 current_price = await self .data .get_latest_price ()
86- if not current_price :
102+ if current_price is None :
87103 return position_summary
88104
89105 for pos in positions :
@@ -96,7 +112,7 @@ async def check_positions(self) -> dict:
96112 position_summary ["total_exposure" ] += pos .total_cost
97113
98114 # Calculate P&L using the unrealized_pnl method
99- pnl = pos .unrealized_pnl (current_price , tick_value = 5.0 ) # MNQ tick value
115+ pnl = pos .unrealized_pnl (float ( current_price ) , tick_value = 5.0 )
100116
101117 position_summary ["positions" ].append (
102118 {
@@ -113,7 +129,9 @@ async def check_positions(self) -> dict:
113129
114130 return position_summary
115131
116- async def check_orders (self ) -> dict :
132+ async def check_orders (
133+ self ,
134+ ) -> dict [str , float | int | list [dict [str , float | int | str ]]]:
117135 """Check orders using enhanced model properties."""
118136 orders = await self .orders .search_open_orders ()
119137
@@ -145,13 +163,17 @@ async def check_orders(self) -> dict:
145163 "size" : order .size ,
146164 "remaining" : order .remaining_size , # New property
147165 "filled_pct" : order .filled_percent , # New property
148- "price" : order .limitPrice or order .stopPrice ,
166+ "price" : float (order .limitPrice )
167+ if order .limitPrice
168+ else float (order .stopPrice )
169+ if order .stopPrice
170+ else 0.0 ,
149171 }
150172 )
151173
152174 return order_summary
153175
154- async def execute_strategy (self ):
176+ async def execute_strategy (self ) -> None :
155177 """Execute trading strategy using all Phase 4 improvements."""
156178 print ("\n === Strategy Execution ===" )
157179
@@ -221,7 +243,9 @@ async def execute_strategy(self):
221243 )
222244 print (f" Reason: { signal ['reason' ]} " )
223245
224- def _generate_signal (self , analysis : dict , positions : dict ) -> dict | None :
246+ def _generate_signal (
247+ self , analysis : dict , positions : dict
248+ ) -> dict [str , float | str ] | None :
225249 """Generate trading signal based on analysis."""
226250 # No signal if we have max positions
227251 if positions ["total_positions" ] >= self .max_position_size :
@@ -264,7 +288,7 @@ def _generate_signal(self, analysis: dict, positions: dict) -> dict | None:
264288 return None
265289
266290
267- async def demonstrate_phase4_improvements ():
291+ async def demonstrate_phase4_improvements () -> None :
268292 """Demonstrate all Phase 4 improvements in action."""
269293
270294 async with await TradingSuite .create (
@@ -365,7 +389,7 @@ async def demonstrate_phase4_improvements():
365389 print ("\n ✅ Overall: Cleaner, more readable, less error-prone code!" )
366390
367391
368- async def main ():
392+ async def main () -> None :
369393 """Run Phase 4 comprehensive test."""
370394 try :
371395 await demonstrate_phase4_improvements ()
0 commit comments