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

ParameterWhat it is
actionmt5.ORDER_TYPE_BUY or mt5.ORDER_TYPE_SELL.
symbolThe symbol name.
volumeThe size in lots.
price_openThe open price.
price_closeThe close price.

What it returns

  • A float: the profit, negative for a loss, in the account currency.
  • None when it can't be worked out. Check mt5.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 error codes

All MetaTrader5 Python functions

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.