Take Profit based on current profit
Info
The Take Profit based on current profit is a Library for MetaTrader 4 that introduction many expert advisors (eas) tend to close orders at the take profit level, considering the pip distance from the purchase price. However, the code used by is based mainly on the current profit.
Usage
This tool is typically used for enhancing chart analysis and decision making.
Platform
This Library works exclusively on MetaTrader 4 (both build 600+ and newer versions).
Setup
Place the downloaded file in MQL4/Libraries folder via File ? Open Data Folder in MetaTrader 4.
How to Install and Use Take Profit based on current profit
1. Storage: Place library files in the MQL/Libraries directory to ensure they are accessible to your projects.
2. Implementation: Include the library in your code using the #import directive, ensuring you match the exact function names and parameters.
3. Compilation: Ensure the library is present in the directory before you compile your main EA or script, as the compiler links them during this phase.
4. Management: Keep libraries organized in sub-folders if you manage many custom functions to maintain a clean project structure.
Frequently Asked Questions
Q: What is a library file used for? A: Libraries store reusable code modules, allowing you to centralize common logic used by multiple EAs or indicators.
Q: Is a library executable? A: No, libraries are non-executable files containing functions; they must be imported into an EA, indicator, or script to function.
Q: Can I update a library while the platform is running? A: You should compile your EA or script after updating a library to ensure the latest code changes are integrated.
What this tool does
Introduction Many Expert Advisors (EAs) tend to close orders at the take profit level, considering the pip distance from the purchase price.
Typical Use Case
This Library excels in automated trading and technical analysis on MetaTrader 4.
Compatible Platform & Setup
This Library works on MetaTrader 4. Place the file in the MQL4/Libraries folder and restart the terminal.
Description & Settings
Related: Hidden Stop Loss and Take Profit System - another powerful library for MetaTrader 4 traders.
IntroductionMany Expert Advisors (EAs) tend to close orders at the take profit level, considering the pip distance from the purchase price. However, the code used by is based mainly on the current profit . This approach allows you to easily manage the take profit with multiple open positions, monitoring the total current profit based on the magic number, in case you use multiple bot instances or different EAs simultaneously . and follow my feed to be updated on the news!
Also recommended: Authenticate into an MT4 account from MQ L4 (tested in build 880) - similar library with strong performance on MetaTrader 4.
Using this code can also have a positive impact on some problems that may occur when using a take profit based on pips. For example, a pip-based take profit could change depending on the slippage of your broker, limiting profits . By using a code based on current profit, you can avoid this issue and have more control over your trades.If you want to learn more about how to set up a take profit based on current profit, you can use the code of EA SwingBot as a reference.
…
Total orders
Let's start with the code that calculates the total number of open orders with the same magic number.
The magic number is a unique identifier assigned to an order by the trader or an EA (Expert Advisor).
The code initializes a variable
total_orders
to zero. It then loops through all the open orders using a for loop and selects each order using the
OrderSelect()
function. If an order is successfully selected, it increments the
total_orders
variable by one.
…
Calculating Current Profit
The code initializes two variables:
ProfittoMinimo
and
Profit
. The variable
ProfittoMinimo
is used to activate the take profit at this level, the value is expressed in the currency of the account. The variable
Profit
is used to accumulate the current profit of all open positions that have the same magic number. The variable
StopLoss
is used for the stop loss.
The code uses a for loop to iterate through all open positions using the
OrdersTotal()
function. For each open position, the corresponding order is selected using the
OrderSelect()
function. If the order is successfully selected and has the same magic number, the profit of the order is added to the
Profit
variable.
The minimum profit can be set as an external variable and configured in the EA options:
…
Closing positions if the Profit is reached
The code uses a for loop to iterate through all open orders using the
OrdersTotal()
function. The loop starts from the last order and goes up to the first order. For each order, the corresponding trade is selected using the
OrderSelect()
function.
If the selected trade has the same symbol as the current chart, is of type
OP_BUY
, and has the same magic number as specified in the code, it checks if the
Profit
of the trade is greater than or equal to
ProfittoMinimo
. If it is, it closes the trade at the bid price using the
OrderClose()
function and prints a message indicating that the buy order has been closed.
Similarly, if the selected trade has the same symbol as the current chart, is of type
OP_SELL
, and has the same magic number as specified in the code, it checks if the
Profit
of the trade is greater than or equal to
ProfittoMinimo
. If it is, it closes the trade at the ask price using the
OrderClose()
function and prints a message indicating that the sell order has been closed.
…
Conclusion
This code could be useful for all those position-closing strategies based on take profit, but it could also be combined with a trailing stop based on the increase in current profit. The system is also useful in case of multiple Expert Advisors. If you exclude the
if
condition on the MagicNumber, you can set general take profit levels to simultaneously control all open positions from all active EAs
You may also like: Easy Canvas (i Canvas) - excellent alternative for library users on MetaTrader 4.
⚠ Limitations & Risk Warning
- This tool is provided for educational and testing purposes only.
- Past performance does not guarantee future results.
- Trading involves substantial risk of loss. Use on a demo account first.
- Results may vary depending on market conditions, broker, and settings.
- We recommend thorough backtesting and forward testing before using with real funds.