Broker API HubMenu

MT5 Python order_calc_profit()

functionorder_calc_profit()MetaTrader5 Python packageOrders

Works out the profit or loss, in the account's currency, of a trade opened at one price and closed at another.

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.

Independent and unofficialmql5.comchecked

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.

This is arithmetic on prices you supply, for the account and market as they stand. It predicts nothing.

Signature

The form of the call on the vendor's page, with the vendor's comments taken out.

order_calc_profit(
  action,
  symbol,
  volume,
  price_open,
  price_close
)

Arguments

NameTypeDescription
actionrequiredNot statedThe order type. The page allows two values of the ORDER_TYPE enumeration here: ORDER_TYPE_BUY or ORDER_TYPE_SELL.
symbolrequiredNot statedThe instrument's name.
volumerequiredNot statedThe size of the trade.
price_openrequiredNot statedThe opening price.
price_closerequiredNot statedThe closing price.

Return value

Type
float or None
What comes back
The result 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()}")

result = mt5.order_calc_profit(mt5.ORDER_TYPE_BUY, "EURUSD", 0.1, 1.1000, 1.1050)
print(result)

mt5.shutdown()