Files

190 lines
4.0 KiB
Python
Raw Permalink Normal View History

2026-01-13 04:01:42 +08:00
"""
Symbol Mapping and Conversion for MT5
Handles forex symbol normalization and parsing.
"""
from typing import Optional, Tuple
2026-01-13 04:01:42 +08:00
# Common forex pairs with their typical MT5 symbol format
FOREX_PAIRS = {
# Major pairs
"EURUSD",
"GBPUSD",
"USDJPY",
"USDCHF",
"AUDUSD",
"USDCAD",
"NZDUSD",
2026-01-13 04:01:42 +08:00
# Cross pairs
"EURGBP",
"EURJPY",
"EURCHF",
"EURAUD",
"EURCAD",
"EURNZD",
"GBPJPY",
"GBPCHF",
"GBPAUD",
"GBPCAD",
"GBPNZD",
"AUDJPY",
"AUDCHF",
"AUDCAD",
"AUDNZD",
"NZDJPY",
"NZDCHF",
"NZDCAD",
"CADJPY",
"CADCHF",
2026-01-13 04:01:42 +08:00
"CHFJPY",
# Exotic pairs
"USDMXN",
"USDZAR",
"USDTRY",
"USDHKD",
"USDSGD",
"USDNOK",
"USDSEK",
"USDDKK",
"EURTRY",
"EURMXN",
"EURNOK",
"EURSEK",
"EURDKK",
"EURPLN",
"EURHUF",
"EURCZK",
2026-01-13 04:01:42 +08:00
# Metals
"XAUUSD",
"XAGUSD",
"XAUEUR",
2026-01-13 04:01:42 +08:00
# Indices (CFD)
"US30",
"US500",
"USTEC",
"UK100",
"DE40",
"JP225",
"AU200",
2026-01-13 04:01:42 +08:00
# Crypto (if broker supports)
"BTCUSD",
"ETHUSD",
"LTCUSD",
"XRPUSD",
2026-01-13 04:01:42 +08:00
}
def normalize_symbol(symbol: str, broker_suffix: str = "") -> str:
"""
Normalize symbol to MT5 format.
2026-01-13 04:01:42 +08:00
Different brokers may use different suffixes:
- No suffix: "EURUSD"
- With suffix: "EURUSDm", "EURUSD.raw", "EURUSD-ECN"
2026-01-13 04:01:42 +08:00
Args:
symbol: Symbol code (e.g., "EUR/USD", "EURUSD", "eurusd")
broker_suffix: Broker-specific suffix (e.g., "m", ".raw", "-ECN")
2026-01-13 04:01:42 +08:00
Returns:
Normalized MT5 symbol
"""
# Remove common separators and convert to uppercase
normalized = (symbol or "").strip().upper()
normalized = normalized.replace("/", "").replace("-", "").replace("_", "").replace(" ", "")
2026-01-13 04:01:42 +08:00
# Add broker suffix if provided
if broker_suffix:
# Check if symbol already has the suffix
if not normalized.endswith(broker_suffix.upper()):
normalized = normalized + broker_suffix
2026-01-13 04:01:42 +08:00
return normalized
def parse_symbol(symbol: str) -> Tuple[str, Optional[str]]:
"""
Parse symbol and extract base/quote currencies.
2026-01-13 04:01:42 +08:00
Args:
symbol: MT5 symbol (e.g., "EURUSD", "EURUSDm")
2026-01-13 04:01:42 +08:00
Returns:
(clean_symbol, market_type)
"""
clean = (symbol or "").strip().upper()
2026-01-13 04:01:42 +08:00
# Remove common broker suffixes
for suffix in ["M", ".RAW", "-ECN", ".STD", ".PRO", ".", "#"]:
if clean.endswith(suffix):
clean = clean[: -len(suffix)]
2026-01-13 04:01:42 +08:00
# Determine market type based on symbol pattern
if clean in FOREX_PAIRS or (len(clean) == 6 and clean.isalpha()):
return clean, "forex"
2026-01-13 04:01:42 +08:00
if clean.startswith("XAU") or clean.startswith("XAG"):
return clean, "metal"
2026-01-13 04:01:42 +08:00
if clean.startswith("BTC") or clean.startswith("ETH") or clean.startswith("LTC"):
return clean, "crypto"
2026-01-13 04:01:42 +08:00
if any(idx in clean for idx in ["US30", "US500", "USTEC", "UK100", "DE40", "JP225"]):
return clean, "index"
2026-01-13 04:01:42 +08:00
# Default to forex
return clean, "forex"
def get_lot_size_info(symbol: str) -> dict:
"""
Get lot size information for a symbol.
2026-01-13 04:01:42 +08:00
Standard forex lot sizes:
- Standard lot: 100,000 units
- Mini lot: 10,000 units
- Micro lot: 1,000 units
- Nano lot: 100 units
2026-01-13 04:01:42 +08:00
Args:
symbol: MT5 symbol
2026-01-13 04:01:42 +08:00
Returns:
Dict with lot size information
"""
clean, market_type = parse_symbol(symbol)
2026-01-13 04:01:42 +08:00
if market_type == "forex":
return {
"standard_lot": 100000,
"min_lot": 0.01,
"lot_step": 0.01,
"max_lot": 100.0,
}
2026-01-13 04:01:42 +08:00
if market_type == "metal":
# Gold/Silver typically uses oz
return {
"standard_lot": 100, # 100 oz
"min_lot": 0.01,
"lot_step": 0.01,
"max_lot": 50.0,
}
2026-01-13 04:01:42 +08:00
if market_type == "index":
return {
"standard_lot": 1,
"min_lot": 0.1,
"lot_step": 0.1,
"max_lot": 100.0,
}
2026-01-13 04:01:42 +08:00
# Default
return {
"standard_lot": 1,
"min_lot": 0.01,
"lot_step": 0.01,
"max_lot": 100.0,
}