Fixed mql5 code performance bug in custom mql5 code block
-
Hello, please help me with the problem
In this project, I tried to close the transaction in situations where the transaction enters the profit and moves in the opposite direction without encountering the takeProfit before encountering the stoploss and keeping a percentage of the profit.
The desired code was written by Chat GBT in mql5 language and I tried to introduce the required variables of the code, but the problem in executing the code is that every session is closed exactly at the entry point. -
https://fxdreema.com/shared/xho6p6zVb
As you can see, in this project, the entry point is not important to me at all, but what is important to me is keeping the highest number seen in the profit after 100 pips (for example) and comparing it with the currentProfit and closing the deal after N % of decrease in maxProfit -
Write a code in mql5 language
When a new transaction is opened, whether it is buying or selling, when it enters profit and the profit of the transaction is more than 100 pips, from this tick onward, set the maxProfit variable equal to the highest numerical value of the currentProfit of the transaction, and when the value of the instant profit of the transaction is equal to 62 % of max profit has decreased, activate block 60 and execute this command from the beginning after each transaction is closed at the beginning of a new transaction.// Global variables
double maxProfit = 0.0;
bool block60Activated = false;//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Initialize variables
maxProfit = 0.0;
block60Activated = false;return(INIT_SUCCEEDED);}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// Reset variables
maxProfit = 0.0;
block60Activated = false;
}//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Check if a new transaction is opened
if (OrdersTotal() > 0)
{
// Get the ticket of the latest order
int latestOrderTicket = OrderGetTicket(OrdersTotal() - 1);// Check if the order is in profit and profit is more than 100 pips if (OrderSelect(latestOrderTicket, SELECT_BY_TICKET) && OrderProfit() > 0 && OrderProfit() > 100 * Point()) { // Update maxProfit if current profit is higher if (OrderProfit() > maxProfit) maxProfit = OrderProfit(); // Check if the instant profit has decreased by 62% of maxProfit if (OrderProfit() <= (0.62 * maxProfit)) { // Activate block 60 block60Activated = true; // Perform your desired actions for block 60 // ... // ... // Reset maxProfit for the next transaction maxProfit = 0.0; } } }}
This code should be placed in an MQL5 Expert Advisor file (.mq5). It initializes the maxProfit and block60Activated variables in the OnInit() function and resets them in the OnDeinit() function. The OnTick() function is called on every tick, and it checks if a new transaction is opened. If the transaction is profitable and the profit is more than 100 pips, it updates the maxProfit variable if necessary. If the instant profit decreases by 62% of maxProfit, it activates "block 60" and performs the desired actions. Finally, the maxProfit is reset for the next transaction.
-
AFAIK ChatGPT is very bad in writing MT4/5 code. I'm not a programmer so I' afraid I cannot be of any help here, sorry. I hope someone else can.
-
You can get the correct code from chatGPT, as @l-andorrà says though it's not very good. Good luck with dismantling what is given to make better.