Loading…

Basket Profit Calculator

Summing profit across a basket of positions looks like a one-line loop, and the version most people write leaves out two things. First, it sums raw position profit alone and quietly ignores swap and commission, so the number it prints does not match what the terminal shows as the account's floating result - the gap gets larger the longer positions sit open on a carry-heavy symbol. Second, on MQL4 it forgets that OrdersTotal() includes pending orders, which have no meaningful profit, and including them either throws off the count or requires the caller to remember the OP_BUY/OP_SELL filter every single time. This header filters pending orders out before the caller has to think about it (automatically on MQL5, where PositionsTotal() never contains one), and every total is profit plus swap plus commission together. Filtering uses sentinel values rather than function overloads, so one function signature covers "all orders," "one magic number," "one symbol," "one direction," or any combination: magic -1 means any magic, symbol NULL means any symbol, type -1 means both buy and sell. Alongside the profit total, BasketNetLots gives signed exposure - buys positive, sells negative - which is the number that actually answers "is this basket hedged or just holding both sides unevenly." BasketProfitPercentOfBalance turns the raw figure into something comparable across accounts of different sizes, with the division guarded against a zero balance. Notes and limitations - All totals are computed on demand by scanning the live position pool. There is no caching, so calling several of these functions back to back scans the pool several times - fine for periodic checks, wasteful if called every tick in a tight loop with many open positions. Cache the result yourself if that matters. - On MQL5, BasketPositionCommission adds one HistorySelectByPosition call per matching position per invocation - noticeably more work than MQL4's direct OrderCommission() read. Avoid calling BasketProfit in a tight per-tick loop over a large basket; cache it on a timer instead. - Profit figures are in account currency as reported by the position/order, not converted or normalised further. - On a clean account with nothing open, every function correctly returns zero - that is the expected result, not a sign the header is broken. - Does not touch closed-trade history for the total itself (only for the MQL5 commission lookup, scoped to one position's own deals). This is a live open-positions snapshot only.

Script · MT4 · v1.0 · Free

About Basket Profit Calculator

Basket Profit Calculator is a script for MetaTrader 4. It has 1 setting, such as Magic filter. MQL source code included.

Files in the download: BasketProfit.mqh, BasketProfit.txt, BasketProfitDemo.mq4.

Settings

SettingDefaultWhat it does
Magic filter990099Magic number used for the "no match" filter test

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria