The GetCurrentProfit Module is a practical utility designed to calculate the real-time floating profit of an Expert Advisor, including commissions, swap, and active position results. By filtering positions by symbol and Magic Number, it provides an accurate profit metric that belongs only to the EA, avoiding interference from manual trades or other robots.
This module is essential for EAs that require dynamic risk management, equity-based decisions, trailing systems, or profit-target logic.
In this imge you can see how Floating P/L is use on GUI to count only this amount of this specific EA:

Purpose
Knowing the exact floating profit of your EA is critical for:
-
Triggering take-profit or stop-loss conditions based on total floating value
-
Managing risk dynamically
-
int OnInit() { Print("Robot Loaded Successfully..."); return(INIT_SUCCEEDED); }
Scaling systems using floating drawdown
-
Monitoring EA performance in real time
-
Closing baskets of trades when a profit threshold is reached
This module provides an accurate and fast method to calculate these values.
How It Works
1. Global Variables and Magic Number
The module begins by creating a variable to store the current floating value, and sets a Magic Number for filtering:
double totalProfit = 0; input long MagicNumber = 7483;
Only positions belonging to the robot will be included.
2. Initialization
Upon loading the EA, a confirmation message is printed:
int OnInit() { Print("Robot Loaded Successfully..."); return(INIT_SUCCEEDED); }
3. Real-Time Execution
On every tick, the EA updates the floating profit:
void OnTick() { GetCurrentProfit(); Comment("Current Floating = " + (string)totalProfit); }
The floating value is displayed directly on the chart.
4. Profit Calculation Algorithm
The GetCurrentProfit() function:
-
Resets the profit variable
-
Iterates through all open positions
-
Filters by symbol and Magic Number
-
Retrieves profit, swap, volume, and applies a commission formula
-
Accumulates the total floating profit
Core function:
double GetCurrentProfit() { totalProfit = 0.0; for (int i = PositionsTotal() - 1; i >= 0; i--) { PositionGetTicket(i); string positionSymbol = PositionGetString(POSITION_SYMBOL); long positionMagicNumber = PositionGetInteger(POSITION_MAGIC); double positionProfit = PositionGetDouble(POSITION_PROFIT); double positionSwap = PositionGetDouble(POSITION_SWAP); double positionVolume = PositionGetDouble(POSITION_VOLUME); double Comission = -(positionVolume * 7); // Broker commission per volume if (positionSymbol == _Symbol && positionMagicNumber == MagicNumber) { totalProfit += positionProfit + positionSwap + Comission; } } return totalProfit; }
The commission is calculated manually using a fixed multiplier, which you can adjust to match your broker’s fee model.
Key Features
-
Accurate floating profit calculation including commissions and swap
-
Magic Number filtering to ensure safety
-
Chart display of real-time floating value
-
Optimized reverse iteration for stable performance
-
Plug-and-play design, easy to integrate into any EA
-
Fully compatible with hedging and netting accounts
Usage Instructions
-
Add the module to your EA.
-
Set the Magic Number to match your EA’s trades.
-
Call GetCurrentProfit() inside OnTick, OnTimer, or within your strategy logic.
-
Use the returned value for risk management, basket closure, or strategy conditions.
Recommended Use Cases
-
Basket trading strategies
-
Martingale or grid systems
-
Equity-based trailing logic
-
Dynamic take-profit modules
-
EA shutdown logic when reaching daily targets
-
Risk control based on floating drawdown
Important Notes
-
The commission formula must be adjusted to match your broker.
-
The module only calculates floating profit for the current chart symbol.
-
Pending orders are not included—only active positions.
-
If you need multi-symbol profit tracking, the module can be expanded easily.
66036