@@ -40,7 +40,7 @@ async def get_current_market_price(
4040 """Get current market price with async fallback for closed markets."""
4141 # Try to get real-time price first if available
4242 try :
43- current_price = await suite .data .get_current_price ()
43+ current_price = await suite [ symbol ] .data .get_current_price ()
4444 if current_price :
4545 return float (current_price )
4646 except Exception as e :
@@ -103,10 +103,10 @@ async def display_positions(
103103 if suite :
104104 try :
105105 # Get current market price
106- current_price = await suite .data .get_current_price ()
107- if current_price and suite . instrument :
106+ current_price = await suite [ position . symbol ] .data .get_current_price ()
107+ if current_price and suite [ position . symbol ]. instrument_info :
108108 # Use the instrument already loaded in suite
109- instrument_info = suite . instrument
109+ instrument_info = suite [ position . symbol ]. instrument_info
110110 point_value = instrument_info .tickValue / instrument_info .tickSize
111111
112112 # Calculate P&L using position manager's method
@@ -138,20 +138,32 @@ async def display_risk_metrics(position_manager: "PositionManager") -> None:
138138 if hasattr (position_manager , "get_risk_metrics" ):
139139 risk_check = await position_manager .get_risk_metrics ()
140140 # Check if we're within daily loss limits and position limits
141- within_daily_loss = risk_check ["daily_loss" ] <= risk_check ["daily_loss_limit" ]
142- within_position_limit = risk_check ["position_count" ] <= risk_check ["position_limit" ]
143- within_risk_limits = risk_check ["current_risk" ] <= risk_check ["max_risk" ]
141+ within_daily_loss = (
142+ risk_check ["daily_loss" ] <= risk_check ["daily_loss_limit" ]
143+ )
144+ within_position_limit = (
145+ risk_check ["position_count" ] <= risk_check ["position_limit" ]
146+ )
147+ within_risk_limits = (
148+ risk_check ["current_risk" ] <= risk_check ["max_risk" ]
149+ )
144150
145151 if within_daily_loss and within_position_limit and within_risk_limits :
146152 print ("✅ All positions within risk limits" )
147153 else :
148154 violations = []
149155 if not within_daily_loss :
150- violations .append (f"Daily loss: ${ risk_check ['daily_loss' ]:.2f} / ${ risk_check ['daily_loss_limit' ]:.2f} " )
156+ violations .append (
157+ f"Daily loss: ${ risk_check ['daily_loss' ]:.2f} / ${ risk_check ['daily_loss_limit' ]:.2f} "
158+ )
151159 if not within_position_limit :
152- violations .append (f"Position count: { risk_check ['position_count' ]} / { risk_check ['position_limit' ]} " )
160+ violations .append (
161+ f"Position count: { risk_check ['position_count' ]} / { risk_check ['position_limit' ]} "
162+ )
153163 if not within_risk_limits :
154- violations .append (f"Current risk: ${ risk_check ['current_risk' ]:.2f} / ${ risk_check ['max_risk' ]:.2f} " )
164+ violations .append (
165+ f"Current risk: ${ risk_check ['current_risk' ]:.2f} / ${ risk_check ['max_risk' ]:.2f} "
166+ )
155167 if violations :
156168 print (f"⚠️ Risk limit violations: { ', ' .join (violations )} " )
157169 else :
@@ -162,14 +174,12 @@ async def display_risk_metrics(position_manager: "PositionManager") -> None:
162174 if hasattr (position_manager , "get_risk_metrics" ):
163175 risk_summary = await position_manager .get_risk_metrics ()
164176 print ("\n Risk Summary:" )
177+ print (f" Current Risk: ${ risk_summary .get ('current_risk' , 0 ):,.2f} " )
178+ print (f" Max Risk Allowed: ${ risk_summary .get ('max_risk' , 0 ):,.2f} " )
165179 print (
166- f" Current Risk: ${ risk_summary .get ('current_risk' , 0 ):,.2f} "
167- )
168- print (
169- f" Max Risk Allowed: ${ risk_summary .get ('max_risk' , 0 ):,.2f} "
180+ f" Max Drawdown: { risk_summary .get ('max_drawdown' , 0 ) * 100 :.1f} %"
170181 )
171- print (f" Max Drawdown: { risk_summary .get ('max_drawdown' , 0 )* 100 :.1f} %" )
172- print (f" Win Rate: { risk_summary .get ('win_rate' , 0 )* 100 :.1f} %" )
182+ print (f" Win Rate: { risk_summary .get ('win_rate' , 0 ) * 100 :.1f} %" )
173183 print (f" Profit Factor: { risk_summary .get ('profit_factor' , 0 ):.2f} " )
174184 else :
175185 print ("Risk summary not available" )
@@ -210,10 +220,10 @@ async def monitor_positions(
210220 try :
211221 # Try to calculate real P&L with current prices
212222 if suite and positions :
213- current_price = await suite .data .get_current_price ()
214- if current_price and suite . instrument :
223+ current_price = await suite [ "MNQ" ] .data .get_current_price ()
224+ if current_price and suite [ "MNQ" ]. instrument_info :
215225 # Use the instrument already loaded in suite
216- instrument_info = suite . instrument
226+ instrument_info = suite [ "MNQ" ]. instrument_info
217227 point_value = (
218228 instrument_info .tickValue / instrument_info .tickSize
219229 )
@@ -282,11 +292,11 @@ async def main() -> bool:
282292
283293 # Check for existing positions
284294 print ("\n 📊 Checking existing positions..." )
285- existing_positions = await suite .positions .get_all_positions ()
295+ existing_positions = await suite [ "MNQ" ] .positions .get_all_positions ()
286296
287297 if existing_positions :
288298 print (f"Found { len (existing_positions )} existing positions" )
289- await display_positions (suite .positions , suite )
299+ await display_positions (suite [ "MNQ" ] .positions , suite )
290300 else :
291301 print ("No existing positions found" )
292302
@@ -312,15 +322,17 @@ async def main() -> bool:
312322 try :
313323 response = await loop .run_in_executor (
314324 None ,
315- lambda : input ("\n Place test order? (y/N): " ).strip ().lower (),
325+ lambda : input ("\n Place test order? (y/N): " )
326+ .strip ()
327+ .lower (),
316328 )
317329 except (KeyboardInterrupt , asyncio .CancelledError ):
318330 print ("\n \n ⚠️ Script interrupted by user" )
319331 return False
320332
321333 if response == "y" :
322334 print ("\n Placing market order..." )
323- order_response = await suite .orders .place_market_order (
335+ order_response = await suite [ "MNQ" ] .orders .place_market_order (
324336 contract_id = contract_id ,
325337 side = 0 ,
326338 size = 1 , # Buy
@@ -334,9 +346,9 @@ async def main() -> bool:
334346 await asyncio .sleep (3 )
335347
336348 # Refresh positions
337- existing_positions = (
338- await suite . positions . get_all_positions ()
339- )
349+ existing_positions = await suite [
350+ "MNQ"
351+ ]. positions . get_all_positions ( )
340352 if existing_positions :
341353 print (" ✅ Position created!" )
342354 else :
@@ -345,23 +357,23 @@ async def main() -> bool:
345357 print ("\n ⚠️ Skipping test order" )
346358
347359 # Display comprehensive position information
348- if await suite .positions .get_all_positions ():
360+ if await suite [ "MNQ" ] .positions .get_all_positions ():
349361 print ("\n " + "=" * 80 )
350362 print ("📈 POSITION MANAGEMENT DEMONSTRATION" )
351363 print ("=" * 80 )
352364
353365 # 1. Display current positions
354- await display_positions (suite .positions , suite )
366+ await display_positions (suite [ "MNQ" ] .positions , suite )
355367
356368 # 2. Show risk metrics
357- await display_risk_metrics (suite .positions )
369+ await display_risk_metrics (suite [ "MNQ" ] .positions )
358370
359371 # 3. Portfolio statistics
360372 print ("\n 📊 Portfolio Statistics:" )
361373 print ("-" * 80 )
362374 try :
363- if hasattr (suite .positions , "get_portfolio_pnl" ):
364- stats = await suite .positions .get_portfolio_pnl ()
375+ if hasattr (suite [ "MNQ" ] .positions , "get_portfolio_pnl" ):
376+ stats = await suite [ "MNQ" ] .positions .get_portfolio_pnl ()
365377 print (f" Total Trades: { stats .get ('total_trades' , 0 )} " )
366378 print (f" Winning Trades: { stats .get ('winning_trades' , 0 )} " )
367379 print (f" Average Win: ${ stats .get ('average_win' , 0 ):,.2f} " )
@@ -377,8 +389,8 @@ async def main() -> bool:
377389 print ("\n 📈 Performance Analytics:" )
378390 print ("-" * 80 )
379391 try :
380- if hasattr (suite .positions , "get_portfolio_pnl" ):
381- analytics = await suite .positions .get_portfolio_pnl ()
392+ if hasattr (suite [ "MNQ" ] .positions , "get_portfolio_pnl" ):
393+ analytics = await suite [ "MNQ" ] .positions .get_portfolio_pnl ()
382394 print (f" Total P&L: ${ analytics .get ('total_pnl' , 0 ):,.2f} " )
383395 print (f" Max Drawdown: ${ analytics .get ('max_drawdown' , 0 ):,.2f} " )
384396 print (
@@ -398,10 +410,10 @@ async def main() -> bool:
398410 print ("=" * 80 )
399411
400412 # Monitor for 30 seconds
401- await monitor_positions (suite .positions , suite , duration = 30 )
413+ await monitor_positions (suite [ "MNQ" ] .positions , suite , duration = 30 )
402414
403415 # 6. Offer to close positions
404- if await suite .positions .get_all_positions ():
416+ if await suite [ "MNQ" ] .positions .get_all_positions ():
405417 print ("\n " + "=" * 80 )
406418 print ("🔧 POSITION MANAGEMENT" )
407419 print ("=" * 80 )
@@ -413,19 +425,21 @@ async def main() -> bool:
413425 try :
414426 response = await loop .run_in_executor (
415427 None ,
416- lambda : input ("\n Close all positions? (y/N): " ).strip ().lower (),
428+ lambda : input ("\n Close all positions? (y/N): " )
429+ .strip ()
430+ .lower (),
417431 )
418432 except (KeyboardInterrupt , asyncio .CancelledError ):
419433 print ("\n \n ⚠️ Script interrupted by user" )
420434 return False
421435
422436 if response == "y" :
423437 print ("\n 🔄 Closing all positions..." )
424- positions = await suite .positions .get_all_positions ()
438+ positions = await suite [ "MNQ" ] .positions .get_all_positions ()
425439
426440 for position in positions :
427441 try :
428- result = await suite .orders .close_position (
442+ result = await suite [ "MNQ" ] .orders .close_position (
429443 position .contractId , method = "market"
430444 )
431445 if result and result .success :
@@ -439,7 +453,9 @@ async def main() -> bool:
439453
440454 # Final position check
441455 await asyncio .sleep (3 )
442- final_positions = await suite .positions .get_all_positions ()
456+ final_positions = await suite [
457+ "MNQ"
458+ ].positions .get_all_positions ()
443459 if not final_positions :
444460 print ("\n ✅ All positions closed successfully!" )
445461 else :
@@ -453,8 +469,8 @@ async def main() -> bool:
453469 print ("=" * 80 )
454470
455471 try :
456- if hasattr (suite .positions , "get_portfolio_pnl" ):
457- session_summary = await suite .positions .get_portfolio_pnl ()
472+ if hasattr (suite [ "MNQ" ] .positions , "get_portfolio_pnl" ):
473+ session_summary = await suite [ "MNQ" ] .positions .get_portfolio_pnl ()
458474 print (f" Session Duration: { session_summary .get ('duration' , 'N/A' )} " )
459475 print (
460476 f" Positions Opened: { session_summary .get ('positions_opened' , 0 )} "
@@ -485,7 +501,7 @@ async def main() -> bool:
485501 # Ask user if they want to close positions
486502 if suite :
487503 try :
488- positions = await suite .positions .get_all_positions ()
504+ positions = await suite [ "MNQ" ] .positions .get_all_positions ()
489505 if positions :
490506 print (f"\n ⚠️ You have { len (positions )} open position(s)." )
491507 print ("Would you like to close them before exiting?" )
@@ -511,12 +527,12 @@ async def main() -> bool:
511527 # Check if we need to close positions
512528 if cleanup_positions :
513529 print ("\n 🧹 Performing cleanup..." )
514- positions = await suite .positions .get_all_positions ()
530+ positions = await suite [ "MNQ" ] .positions .get_all_positions ()
515531 if positions :
516532 print (f" Closing { len (positions )} open position(s)..." )
517533 for position in positions :
518534 try :
519- result = await suite .orders .close_position (
535+ result = await suite [ "MNQ" ] .orders .close_position (
520536 position .contractId , method = "market"
521537 )
522538 if result and result .success :
@@ -530,7 +546,9 @@ async def main() -> bool:
530546 await asyncio .sleep (2 )
531547
532548 # Final check
533- final_positions = await suite .positions .get_all_positions ()
549+ final_positions = await suite [
550+ "MNQ"
551+ ].positions .get_all_positions ()
534552 if final_positions :
535553 print (f" ⚠️ { len (final_positions )} position(s) still open" )
536554 else :
0 commit comments