mt5.order_calc_profit() in MetaTrader5 Python: work out profit between two prices
Works out the profit or loss of a buy or sell of a given volume from an open price to a close price, in the account currency. Returns a float or None.
Signature
mt5.order_calc_profit(action, symbol, volume, price_open, price_close) What it does
Use order_calc_profit() to turn a stop loss or take profit distance into money, for risk sizing.
Parameters
| Parameter | What it is |
|---|---|
action | mt5.ORDER_TYPE_BUY or mt5.ORDER_TYPE_SELL. |
symbol | The symbol name. |
volume | The size in lots. |
price_open | The open price. |
price_close | The close price. |
What it returns
- A float: the profit, negative for a loss, in the account currency.
Nonewhen it can't be worked out. Checkmt5.last_error().
Example
import MetaTrader5 as mt5
if not mt5.initialize():
raise SystemExit(mt5.last_error())
entry, stop = 1.0850, 1.0820
loss = mt5.order_calc_profit(mt5.ORDER_TYPE_BUY, "EURUSD", 0.10, entry, stop)
if loss is None:
raise SystemExit(mt5.last_error())
print(f"a stop at {stop} would lose {-loss:.2f}")
mt5.shutdown() Common mistakes
- The result leaves out commission and swap. Add them yourself for a full picture.
- Mind the direction. For a sell, a close price below the open price is a profit.
The same with Tickwire on Linux
The package needs a Windows terminal on the same machine. Tickwire runs the terminal for you and gives you a REST API, so the code below runs on Linux, macOS or a server.
Tickwire's terminal route OrderCalcProfit works out the profit of volume lots from priceOpen to priceClose in the account currency. Nothing is placed.
Related functions
- mt5.order_calc_margin() in MetaTrader5 Python: work out the margin for an order
- mt5.order_check() in MetaTrader5 Python: test an order before you send it
- mt5.symbol_info() in MetaTrader5 Python: read a symbol's settings and limits
- mt5.positions_get() in MetaTrader5 Python: read open positions
Related error codes
MT5 from Python, on any machine
One REST API for MT4 and MT5, with idempotent orders and an audit log. Read the MT5 Python API page or the quickstart.