Gr8/getAllTrades.py

37 lines
1 KiB
Python
Raw Permalink Normal View History

2025-09-10 15:02:31 +03:00
import MetaTrader5 as mt5
import logging
# -----------------------------
# Logging
# -----------------------------
logging.basicConfig(level=logging.INFO, format="%(asctime)s | %(levelname)s | %(message)s")
log = logging.getLogger()
# -----------------------------
# Initialize MT5
# -----------------------------
if not mt5.initialize():
log.error(f"MT5 initialize failed: {mt5.last_error()}")
quit()
log.info("Connected to MT5 terminal")
# -----------------------------
# Get all open positions
# -----------------------------
positions = mt5.positions_get()
if positions is None:
log.info("No open positions")
else:
log.info(f"Total open positions: {len(positions)}")
for pos in positions:
log.info(
f"Ticket: {pos.ticket}, Symbol: {pos.symbol}, Type: {'Buy' if pos.type == 0 else 'Sell'}, "
f"Volume: {pos.volume}, Price: {pos.price_open}, SL: {pos.sl}, TP: {pos.tp}"
)
# -----------------------------
# Shutdown MT5
# -----------------------------
mt5.shutdown()
log.info("MT5 shutdown complete")