Trading Module¶
mt5cli.trading ¶
Trading-capable MetaTrader 5 session helpers and operational utilities.
ExecutionStatus
module-attribute
¶
OrderTimeMode
module-attribute
¶
POSITION_COLUMNS
module-attribute
¶
POSITION_COLUMNS = (
"ticket",
"time",
"symbol",
"type",
"volume",
"price_open",
"sl",
"tp",
"price_current",
"profit",
"swap",
"comment",
)
__all__
module-attribute
¶
__all__ = [
"POSITION_COLUMNS",
"ExecutionStatus",
"MarginVolume",
"OrderExecutionResult",
"OrderFillingMode",
"OrderLimits",
"OrderSide",
"OrderTimeMode",
"PositionSide",
"calculate_margin_and_volume",
"calculate_new_position_margin_ratio",
"calculate_positions_margin",
"calculate_spread_ratio",
"calculate_volume_by_margin",
"close_open_positions",
"create_trading_client",
"detect_position_side",
"determine_order_limits",
"ensure_symbol_selected",
"estimate_order_margin",
"fetch_latest_closed_rates_for_trading_client",
"get_account_snapshot",
"get_positions_frame",
"get_symbol_snapshot",
"get_tick_snapshot",
"mt5_trading_session",
"normalize_order_volume",
"place_market_order",
"update_sltp_for_open_positions",
]
MarginVolume ¶
Bases: TypedDict
Affordable volume bounds derived from account margin and symbol constraints.
OrderExecutionResult ¶
Bases: TypedDict
Normalized result from market-order and position-management helpers.
OrderLimits ¶
calculate_margin_and_volume ¶
calculate_margin_and_volume(
client: Mt5TradingClient,
symbol: str,
unit_margin_ratio: float,
preserved_margin_ratio: float,
) -> MarginVolume
Calculate tradable margin and volumes from account free margin.
Applies preserved_margin_ratio to keep a reserve off margin_free,
then allocates unit_margin_ratio of the remainder as the margin budget
for proportional volume sizing on both buy and sell sides. A
unit_margin_ratio of 0 requests exactly one minimum valid unit per
side when the post-reserve margin can afford it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Mt5TradingClient
|
Connected |
required |
symbol
|
str
|
Symbol used for minimum-lot margin and volume calculations. |
required |
unit_margin_ratio
|
float
|
Fraction of post-reserve margin to allocate per unit. |
required |
preserved_margin_ratio
|
float
|
Fraction of |
required |
Returns:
| Type | Description |
|---|---|
MarginVolume
|
Dictionary with |
MarginVolume
|
|
MarginVolume
|
clamped to |
Source code in mt5cli/trading.py
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 | |
calculate_new_position_margin_ratio ¶
calculate_new_position_margin_ratio(
client: Mt5TradingClient,
*,
symbol: str,
new_position_side: OrderSide | None = None,
new_position_volume: float = 0.0,
) -> float
Return total margin/equity ratio after an optional hypothetical position.
Raises:
| Type | Description |
|---|---|
Mt5TradingError
|
If equity or required tick data is invalid. |
Source code in mt5cli/trading.py
calculate_positions_margin ¶
calculate_positions_margin(
client: Mt5TradingClient,
*,
symbols: Sequence[str] | None = None,
) -> float
Return the sum of estimated current margin for open positions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Mt5TradingClient
|
Connected |
required |
symbols
|
Sequence[str] | None
|
Optional symbol filter. When omitted, all open positions are included. |
None
|
Returns:
| Type | Description |
|---|---|
float
|
Total estimated margin, or |
Source code in mt5cli/trading.py
calculate_spread_ratio ¶
Return (ask - bid) / ((ask + bid) / 2) for the latest tick.
Raises:
| Type | Description |
|---|---|
Mt5TradingError
|
If bid or ask is unavailable or non-positive. |
Source code in mt5cli/trading.py
calculate_volume_by_margin ¶
calculate_volume_by_margin(
client: Mt5TradingClient,
symbol: str,
available_margin: float,
order_side: OrderSide,
) -> float
Calculate max normalized volume affordable for one side.
Returns:
| Type | Description |
|---|---|
float
|
Affordable volume rounded down to symbol volume constraints. |
Raises:
| Type | Description |
|---|---|
Mt5TradingError
|
If symbol volume constraints or tick data are invalid. |
Source code in mt5cli/trading.py
close_open_positions ¶
close_open_positions(
client: Mt5TradingClient,
*,
symbols: str | list[str] | None = None,
tickets: list[int] | None = None,
dry_run: bool = False,
) -> list[OrderExecutionResult]
Close matching open positions.
Returns:
| Type | Description |
|---|---|
list[OrderExecutionResult]
|
Normalized execution results for matching positions. |
Source code in mt5cli/trading.py
create_trading_client ¶
create_trading_client(
*,
config: Mt5Config | None = None,
login: int | str | None = None,
password: str | None = None,
server: str | None = None,
path: str | None = None,
timeout: int | None = None,
retry_count: int = 0,
) -> Mt5TradingClient
Return an initialized and logged-in trading client.
Source code in mt5cli/trading.py
detect_position_side ¶
detect_position_side(
client: Mt5TradingClient, symbol: str
) -> PositionSide | None
Detect the net open position side for a symbol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Mt5TradingClient
|
Connected |
required |
symbol
|
str
|
Symbol to inspect. |
required |
Returns:
| Type | Description |
|---|---|
PositionSide | None
|
|
PositionSide | None
|
|
PositionSide | None
|
|
Source code in mt5cli/trading.py
determine_order_limits ¶
determine_order_limits(
client: Mt5TradingClient,
symbol: str,
side: PositionSide | str,
stop_loss_limit_ratio: float | None = None,
take_profit_limit_ratio: float | None = None,
) -> OrderLimits
Derive entry and protective order prices from current market quotes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Mt5TradingClient
|
Connected |
required |
symbol
|
str
|
Symbol used for the quote lookup. |
required |
side
|
PositionSide | str
|
Position side as |
required |
stop_loss_limit_ratio
|
float | None
|
Relative distance from entry for stop loss in
|
None
|
take_profit_limit_ratio
|
float | None
|
Relative distance from entry for take profit in
|
None
|
Returns:
| Type | Description |
|---|---|
OrderLimits
|
Dictionary with |
OrderLimits
|
Omitted protective levels are returned as |
Raises:
| Type | Description |
|---|---|
Mt5TradingError
|
If required tick data is invalid or computed SL/TP
prices violate available |
Source code in mt5cli/trading.py
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 | |
ensure_symbol_selected ¶
Ensure a symbol is visible in Market Watch before sending orders.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Mt5TradingClient
|
Connected |
required |
symbol
|
str
|
Symbol to select. |
required |
Raises:
| Type | Description |
|---|---|
Mt5TradingError
|
If the symbol cannot be selected in Market Watch or
|
Source code in mt5cli/trading.py
estimate_order_margin ¶
estimate_order_margin(
client: Mt5TradingClient,
symbol: str,
order_side: OrderSide | str,
volume: float,
) -> float
Estimate required margin for one order at the current market price.
Returns:
| Type | Description |
|---|---|
float
|
Positive finite margin required for the order at the current quote. |
Raises:
| Type | Description |
|---|---|
Mt5TradingError
|
If volume, tick data, or margin estimation is invalid. |
Source code in mt5cli/trading.py
fetch_latest_closed_rates_for_trading_client ¶
fetch_latest_closed_rates_for_trading_client(
client: Mt5TradingClient,
*,
symbol: str,
granularity: str,
count: int,
) -> DataFrame
Fetch the latest closed bars from a connected trading client.
Returns:
| Type | Description |
|---|---|
DataFrame
|
Up to |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Mt5TradingError
|
If the trading client cannot fetch rate data. |
Source code in mt5cli/trading.py
get_account_snapshot ¶
Return normalized account state with stable keys.
Source code in mt5cli/trading.py
get_positions_frame ¶
Return open positions as a DataFrame with stable baseline columns.
Source code in mt5cli/trading.py
get_symbol_snapshot ¶
get_symbol_snapshot(
client: Mt5TradingClient, symbol: str
) -> dict[str, float | int | str | bool | None]
Return normalized symbol metadata required for trading decisions.
Source code in mt5cli/trading.py
get_tick_snapshot ¶
Return normalized latest tick data, including bid, ask, and timestamp.
Source code in mt5cli/trading.py
mt5_trading_session ¶
mt5_trading_session(
config: Mt5Config | None = None,
*,
login: int | str | None = None,
password: str | None = None,
server: str | None = None,
path: str | None = None,
timeout: int | None = None,
retry_count: int = 0,
) -> Iterator[Mt5TradingClient]
Open a trading-capable MT5 session and always shut down safely.
Launches the MetaTrader 5 terminal using Mt5Config.path when set,
initializes and logs in via initialize_and_login_mt5(), yields a
connected :class:~pdmt5.Mt5TradingClient, and calls shutdown() on
exit even when an error is raised inside the context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
Mt5Config | None
|
MT5 connection configuration. Defaults to an empty config that attaches to a running terminal. |
None
|
login
|
int | str | None
|
Optional trading account login. |
None
|
password
|
str | None
|
Optional trading account password. |
None
|
server
|
str | None
|
Optional trading server name. |
None
|
path
|
str | None
|
Optional terminal executable path. |
None
|
timeout
|
int | None
|
Optional connection timeout in milliseconds. |
None
|
retry_count
|
int
|
Number of initialization retries passed to
|
0
|
Yields:
| Type | Description |
|---|---|
Mt5TradingClient
|
Connected |
Source code in mt5cli/trading.py
normalize_order_volume ¶
normalize_order_volume(
volume: float,
*,
volume_min: float,
volume_max: float,
volume_step: float,
) -> float
Normalize a requested order volume to broker volume constraints.
Returns:
| Type | Description |
|---|---|
float
|
Volume floored to the nearest valid broker step from |
float
|
capped at |
float
|
deterministically. Returns |
float
|
invalid, non-finite, or the capped request is below |
Source code in mt5cli/trading.py
place_market_order ¶
place_market_order(
client: Mt5TradingClient,
*,
symbol: str,
volume: float,
order_side: OrderSide,
order_filling_mode: OrderFillingMode = "IOC",
order_time_mode: OrderTimeMode = "GTC",
sl: float | None = None,
tp: float | None = None,
position: int | None = None,
dry_run: bool = False,
) -> OrderExecutionResult
Place one normalized market order or return a dry-run result.
pdmt5.Mt5TradingClient.order_send() raises only when MT5 returns no
response. When MT5 returns a response with a known non-success retcode, this
helper returns status="failed" and keeps the normalized response
details for callers to inspect.
Returns:
| Type | Description |
|---|---|
OrderExecutionResult
|
Normalized execution result containing request and response details. |
Raises:
| Type | Description |
|---|---|
Mt5TradingError
|
If volume or required tick data is invalid. |
Source code in mt5cli/trading.py
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 | |
update_sltp_for_open_positions ¶
update_sltp_for_open_positions(
client: Mt5TradingClient,
*,
symbol: str | None = None,
tickets: list[int] | None = None,
stop_loss: float | None = None,
take_profit: float | None = None,
dry_run: bool = False,
) -> list[OrderExecutionResult]
Update SL/TP for matching open positions.
Returns:
| Type | Description |
|---|---|
list[OrderExecutionResult]
|
Normalized execution results for matching positions. |
Source code in mt5cli/trading.py
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 | |
Trading-capable MT5 sessions¶
create_trading_client() and mt5_trading_session() complement the read-only
mt5_session() helper in sdk.py. They return or yield an initialized
pdmt5.Mt5TradingClient, use Mt5Config.path to launch the terminal when
configured, and mt5_trading_session() always calls shutdown() on exit.
from mt5cli import create_trading_client, mt5_trading_session
with mt5_trading_session(
path=r"C:\Program Files\MetaTrader 5\terminal64.exe",
login="12345",
password="secret",
server="Broker-Demo",
retry_count=2,
) as client:
positions = client.positions_get_as_df(symbol="EURUSD")
client = create_trading_client(login=12345, server="Broker-Demo")
try:
account = client.account_info_as_dict()
finally:
client.shutdown()
login accepts int, numeric str, or an empty string; empty strings are
treated as unset. path, password, server, and timeout are forwarded to
pdmt5.Mt5Config, and omitted timeout values keep the lower-level default.
The read-only Mt5CliClient / mt5_session() API is unchanged.
State and order helpers¶
These helpers are strategy-agnostic and do not depend on signal detection, betting logic, or scheduling code in downstream applications.
from mt5cli import (
calculate_positions_margin,
calculate_spread_ratio,
calculate_margin_and_volume,
close_open_positions,
detect_position_side,
determine_order_limits,
estimate_order_margin,
fetch_latest_closed_rates_for_trading_client,
get_account_snapshot,
get_positions_frame,
get_symbol_snapshot,
get_tick_snapshot,
normalize_order_volume,
place_market_order,
)
account = get_account_snapshot(client)
symbol = get_symbol_snapshot(client, "EURUSD")
tick = get_tick_snapshot(client, "EURUSD")
positions = get_positions_frame(client, "EURUSD")
side = detect_position_side(client, "EURUSD")
spread_ratio = calculate_spread_ratio(client, "EURUSD")
volume = normalize_order_volume(
0.15,
volume_min=symbol["volume_min"],
volume_max=symbol["volume_max"],
volume_step=symbol["volume_step"],
)
buy_margin = (
estimate_order_margin(client, "EURUSD", "BUY", volume) if volume > 0 else 0.0
)
open_margin = calculate_positions_margin(client, symbols=["EURUSD"])
closed_bars = fetch_latest_closed_rates_for_trading_client(
client,
symbol="EURUSD",
granularity="M1",
count=100,
)
sizing = calculate_margin_and_volume(
client,
"EURUSD",
unit_margin_ratio=0.5,
preserved_margin_ratio=0.2,
)
limits = determine_order_limits(
client,
"EURUSD",
side="long",
stop_loss_limit_ratio=0.01,
take_profit_limit_ratio=0.02,
)
preview = place_market_order(
client,
symbol="EURUSD",
volume=sizing["buy_volume"],
order_side="BUY",
sl=limits["stop_loss"],
tp=limits["take_profit"],
dry_run=True,
)
closed = close_open_positions(client, symbols="EURUSD", dry_run=True)
detect_position_side() returns long for buy-only exposure, short for
sell-only exposure, and None for no positions or mixed long/short exposure.
calculate_spread_ratio() uses (ask - bid) / ((ask + bid) / 2) and raises
Mt5TradingError when bid or ask is missing or non-positive.
normalize_order_volume() returns 0.0 for invalid constraints or
sub-minimum requests; check the result before calling estimate_order_margin(),
which requires a positive finite volume. calculate_positions_margin() silently
skips rows with missing symbols, non-positive volumes, non-finite volumes, or
unsupported position types, but propagates Mt5TradingError from estimate_order_margin() when a valid row
encounters invalid tick data or margin results from the broker.
SL/TP ratios for determine_order_limits() must satisfy 0 <= ratio < 1; 0
omits that level. SL/TP prices are rounded with symbol digits metadata when
available. determine_order_limits() pre-validates computed SL/TP prices against
available trade_stops_level * point metadata when present; violations raise
Mt5TradingError. This is a planning helper only: it does not guarantee broker
acceptance because live validation can still depend on price movement, bid/ask
side, freeze levels, and server-side rules, and it does not validate
trade_freeze_level. When symbol metadata cannot be loaded, protective prices
still round with digits=8 and stop-level validation is skipped.
unit_margin_ratio and preserved_margin_ratio for calculate_margin_and_volume()
accept 0 <= ratio <= 1; unit_margin_ratio=0 requests one minimum valid unit
when the post-reserve margin can afford it. Negative margin_free is clamped to
0.0 before sizing. Execution helpers return normalized OrderExecutionResult
dictionaries containing the request, response, status, retcode, and dry_run
flag; dry_run=True never sends an order or mutates Market Watch visibility.
ensure_symbol_selected() adds hidden symbols to Market Watch before live order
placement and SL/TP updates. Failed, malformed, or unknown broker retcodes are
fail-closed and returned as status="failed" while keeping the normalized
response for inspection.
Order planning return contracts¶
from mt5cli import MarginVolume, OrderLimits, OrderExecutionResult
sizing: MarginVolume = calculate_margin_and_volume(
client,
"EURUSD",
unit_margin_ratio=0.5,
preserved_margin_ratio=0.2,
)
limits: OrderLimits = determine_order_limits(
client,
"EURUSD",
side="long",
stop_loss_limit_ratio=0.01,
take_profit_limit_ratio=0.02,
)
preview: OrderExecutionResult = place_market_order(
client,
symbol="EURUSD",
volume=sizing["buy_volume"],
order_side="BUY",
sl=limits["stop_loss"],
tp=limits["take_profit"],
dry_run=True,
)
updates: list[OrderExecutionResult] = update_sltp_for_open_positions(
client,
symbol="EURUSD",
stop_loss=limits["stop_loss"],
dry_run=True,
)
Closes issue #33: strategy-neutral order planning and execution helpers exposed through the stable package root without embedding entry/exit policy.
Migration from application-local helpers¶
| Application-local concern | mt5cli replacement |
|---|---|
| Manual terminal spawn/kill around trading code | mt5_trading_session() |
| Local position-side detection | detect_position_side() |
| Local margin/volume sizing | calculate_margin_and_volume() |
| Local broker volume step normalization | normalize_order_volume() |
| Local order or position margin estimation | estimate_order_margin(), calculate_positions_margin() |
| Local closed-bar fetch from a trading session | fetch_latest_closed_rates_for_trading_client() |
| Local SL/TP price derivation | determine_order_limits() |
| Throttled SQLite history loop with ad-hoc error handling | ThrottledHistoryUpdater(suppress_errors=True) |
Keep read-only data collection on mt5_session() / Mt5CliClient; use
mt5_trading_session() only where order placement or trading calculations are
required.