//+------------------------------------------------------------------+
//|                                                      Breakeven.mq4|
//|                        Copyright 2023, Your Name                |
//|                               https://www.mql5.com               |
//+------------------------------------------------------------------+
#property strict

// Input parameters
input double BreakevenOffset = 1; // The offset in pips for breakeven + 1 pip
input double ProfitThreshold = 10; // Profit threshold in pips to trigger the action

// Function to check and set breakeven + offset
void CheckBreakevenPlus() {
    double breakevenPrice, newStopLoss;

    for(int i = OrdersTotal() - 1; i >= 0; i--) {
        if(OrderSelect(i, SELECT_BY_POS)) {
            // Only modify orders which are currently open
            if(OrderType() == OP_BUY || OrderType() == OP_SELL) {
                
                // Check if the order is in profit
                double profit = OrderProfit() + OrderSwap() + OrderCommission();
                
                // Convert profit to pips
                double profitInPips = profit / Point; // Scale profit to pips depending on the instrument precision
                
                // Check if the profit threshold has been reached
                if((OrderType() == OP_BUY && profitInPips >= ProfitThreshold) || 
                   (OrderType() == OP_SELL && profitInPips >= ProfitThreshold)) {
                    
                    // Set the breakeven price
                    if(OrderType() == OP_BUY) {
                        breakevenPrice = OrderOpenPrice();                  // Breakeven is Original open price
                        newStopLoss = breakevenPrice + (BreakevenOffset * Point);
                    }
                    else if(OrderType() == OP_SELL) {
                        breakevenPrice = OrderOpenPrice();                  // Breakeven is Original open price
                        newStopLoss = breakevenPrice - (BreakevenOffset * Point);
                    }

                    // Modify stop loss regardless of its current state
                    if((OrderType() == OP_BUY && (OrderStopLoss() < newStopLoss || OrderStopLoss() == 0)) || 
                       (OrderType() == OP_SELL && (OrderStopLoss() > newStopLoss || OrderStopLoss() == 0))) {
                        
                        if(OrderModify(OrderTicket(), OrderOpenPrice(), newStopLoss, OrderTakeProfit(), 0, clrNONE)) {
                            Print("Order #", OrderTicket(), ": Stop loss updated to breakeven + ", BreakevenOffset, " pips");
                        } 
                        else {
                            Print("Error modifying order #", OrderTicket(), ": ", GetLastError());
                        }
                    }
                }
            }
        }
    }
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick() {
    CheckBreakevenPlus(); // Check for breakeven + 1 pip on each tick
}

//+------------------------------------------------------------------+