MT5 Python order_calc_margin()
Works out the margin, in the account's currency, that a trade of a given type, symbol, volume and price would need.
Risk warning: CFDs and spread bets are complex instruments and come with a high risk of losing money rapidly due to leverage. You can lose money on these products, and each provider publishes its own loss figure under FCA rules. You should consider whether you understand how they work and whether you can afford to take the high risk of losing your money. Automated and API trading can amplify losses through bugs, connectivity failures and runaway orders.
This is a Python function, not an HTTP endpoint. It has no method, no path and no address to send a request to: the package talks to a MetaTrader 5 terminal running on the same computer.
The page says the figure is for the account as it stands and the market as it stands, and that it leaves pending orders and open positions out of the sum.
Signature
The form of the call on the vendor's page, with the vendor's comments taken out.
order_calc_margin( action, symbol, volume, price )
Arguments
| Name | Type | Description |
|---|---|---|
| actionrequired | Not stated | The order type, as a value of the ORDER_TYPE enumeration. The comment in the page's signature names ORDER_TYPE_BUY and ORDER_TYPE_SELL. |
| symbolrequired | Not stated | The instrument's name. |
| volumerequired | Not stated | The size of the trade. |
| pricerequired | Not stated | The opening price. |
Return value
- Type
- float or None
- What comes back
- The margin as a real number, or None on failure, in which case last_error() gives the reason.
Sample
Written by this site from the signature and arguments recorded on this page. It has not been run against a terminal, a server or an account, and the values in it are placeholders. No credential is typed into it: where one is needed it is read from an environment variable on your own machine. Nothing is entered on this site.
import MetaTrader5 as mt5
if not mt5.initialize():
raise SystemExit(f"initialize() failed: {mt5.last_error()}")
tick = mt5.symbol_info_tick("EURUSD")
if tick is not None:
margin = mt5.order_calc_margin(mt5.ORDER_TYPE_BUY, "EURUSD", 0.1, tick.ask)
print(margin)
mt5.shutdown()