66import logging
77import os
88import time
9+ from pathlib import Path
910from typing import Generator
1011
1112import MetaTrader5 as Mt5
@@ -31,39 +32,39 @@ def is_headless() -> bool:
3132
3233
3334@pytest .fixture (scope = "module" , autouse = True )
34- def setup_teardown () -> Generator [None , None , None ]:
35+ def setup_teardown () -> Generator [None , None , None ]: # noqa: C901 - Test setup needs to handle many cases
3536 """Set up and tear down MetaTrader5 connection for the test module."""
3637 if is_headless ():
3738 logger .info ("Running in headless mode - skipping MT5 initialization" )
3839 yield
3940 return
4041
4142 init_result = Mt5 .initialize ()
42-
43+
4344 if not init_result :
4445 common_paths = [
4546 "C:\\ Program Files\\ MetaTrader 5\\ terminal64.exe" ,
46- "C:\\ Program Files (x86)\\ MetaTrader 5\\ terminal.exe"
47+ "C:\\ Program Files (x86)\\ MetaTrader 5\\ terminal.exe" ,
4748 ]
48-
49+
4950 for path in common_paths :
50- if os . path .exists (path ):
51+ if Path ( path ) .exists ():
5152 init_result = Mt5 .initialize (path = path )
5253 if init_result :
5354 logger .info (f"Successfully initialized MT5 with path: { path } " )
5455 break
55-
56+
5657 if not init_result :
5758 pytest .skip (f"MetaTrader5 could not be initialized. Error: { Mt5 .last_error ()} " )
5859
5960 time .sleep (5 )
60-
61+
6162 symbols = Mt5 .symbols_get ()
6263 if not symbols :
6364 logger .warning ("No symbols loaded. Attempting to fix..." )
6465 Mt5 .symbols_total () # Sometimes this helps refresh the symbols list
6566 time .sleep (3 )
66-
67+
6768 symbols = Mt5 .symbols_get ()
6869 if not symbols :
6970 logger .error ("Still no symbols available after retry." )
@@ -432,15 +433,15 @@ def test_open_sell_position(trade: Trade) -> None:
432433
433434
434435@pytest .mark .real_trading
435- def test_close_position (trade : Trade ) -> None :
436+ def test_close_position (trade : Trade ) -> None : # noqa: C901 - Test needs to handle many edge cases
436437 """Test closing a position with real trades."""
437438 # First, ensure we can get symbol info
438439 symbol_info = Mt5 .symbol_info (trade .symbol )
439440 if not symbol_info :
440441 pytest .skip (f"Could not get symbol info for { trade .symbol } " )
441-
442+
442443 logger .info (f"Symbol { trade .symbol } trade mode: { symbol_info .trade_mode } " )
443-
444+
444445 # Check if we have any existing positions to close
445446 positions = Mt5 .positions_get (symbol = trade .symbol )
446447 has_existing_position = False
@@ -450,37 +451,37 @@ def test_close_position(trade: Trade) -> None:
450451 has_existing_position = True
451452 logger .info (f"Found existing position with ticket { position .ticket } " )
452453 break
453-
454+
454455 if not has_existing_position :
455456 # Try to open a new position
456457 try :
457458 logger .info (f"Attempting to open a new position for { trade .symbol } " )
458459 trade .open_buy_position ("Test Position to Close" )
459460 time .sleep (2 ) # Wait for position to open
460-
461+
461462 # Verify position was opened
462463 positions = Mt5 .positions_get (symbol = trade .symbol )
463464 assert positions is not None , "Failed to get positions after opening"
464-
465+
465466 has_position = False
466467 for position in positions :
467468 if position .magic == TEST_MAGIC_NUMBER :
468469 has_position = True
469470 logger .info (f"Successfully opened position with ticket { position .ticket } " )
470471 break
471-
472+
472473 assert has_position , "Failed to find opened position"
473-
474- except Exception as e :
474+
475+ except Exception :
475476 logger .exception ("Error opening position" )
476477 raise
477-
478+
478479 # Now try to close the position
479480 try :
480481 logger .info ("Attempting to close position" )
481482 trade .close_position ("Test Closing Position" )
482483 time .sleep (2 ) # Wait for position to close
483-
484+
484485 # Verify position was closed
485486 positions = Mt5 .positions_get (symbol = trade .symbol )
486487 has_position = False
@@ -490,17 +491,17 @@ def test_close_position(trade: Trade) -> None:
490491 has_position = True
491492 logger .warning (f"Position still exists with ticket { position .ticket } " )
492493 break
493-
494+
494495 assert not has_position , "Position was not closed successfully"
495496 logger .info ("Position closed successfully" )
496-
497- except Exception as e :
497+
498+ except Exception :
498499 logger .exception ("Error during position test" )
499500 raise
500501
501502
502503@pytest .fixture (autouse = True )
503- def skip_real_trading_in_headless (request ) -> None :
504+ def skip_real_trading_in_headless (request : pytest . FixtureRequest ) -> None :
504505 """Skip real trading tests in headless mode."""
505506 if is_headless () and request .node .get_closest_marker ("real_trading" ):
506507 pytest .skip ("Skipping real trading test in headless mode" )
0 commit comments