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.