//+------------------------------------------------------------------+
//|                                               EA_BXC_Arrow.mq5   |
//|                                Created with EABuilder.com       |
//|                                       https://www.eabuilder.com  |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024"
#property version   "1.00"
#property strict

//--- input parameters
input double TakeProfitMin = 20;   // Minimum Take Profit in pips
input double TakeProfitMax = 100;   // Maximum Take Profit in pips
input double TrailingStop = 20;     // Trailing stop in pips

//--- variables
double buyPrice, sellPrice;
ulong buyTicket = 0, sellTicket = 0; // Use ulong for ticket numbers

//+------------------------------------------------------------------+
//| Expert initialization function                                     |
//+------------------------------------------------------------------+
int OnInit() {
    return INIT_SUCCEEDED;
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                   |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
    // Close all orders when EA is removed
    if (buyTicket > 0) {
        CloseOrder(buyTicket);
    }
    if (sellTicket > 0) {
        CloseOrder(sellTicket);
    }
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick() {
    // Check for signals from the indicator
    double buySignal = iCustom(_Symbol, PERIOD_CURRENT, "BXC_Arrow_ICT_Zone", 0, 1);
    double sellSignal = iCustom(_Symbol, PERIOD_CURRENT, "BXC_Arrow_ICT_Zone", 1, 1);
    
    double currentPrice = GetCurrentPrice();

    // Check if there's an open order and manage trailing stop
    ManageTrailingStop();

    // Buy order conditions
    if (buySignal != EMPTY_VALUE && buyTicket == 0) { // Changed to check for 0
        double takeProfitPrice = currentPrice + (TakeProfitMin * GetPointSize());
        if (SendOrder(ORDER_BUY, 0.1, currentPrice, takeProfitPrice, buyTicket)) {
            buyPrice = currentPrice;
        }
    }
    
    // Sell order conditions
    if (sellSignal != EMPTY_VALUE && sellTicket == 0) { // Changed to check for 0
        double takeProfitPrice = currentPrice - (TakeProfitMin * GetPointSize());
        if (SendOrder(ORDER_SELL, 0.1, currentPrice, takeProfitPrice, sellTicket)) {
            sellPrice = currentPrice;
        }
    }
}

//+------------------------------------------------------------------+
//| Manage trailing stop                                             |
//+------------------------------------------------------------------+
void ManageTrailingStop() {
    if (buyTicket > 0) {
        double profit = (GetCurrentPrice() - buyPrice) / GetPointSize();
        if (profit >= TakeProfitMax) {
            double newStopLoss = GetCurrentPrice() - (TrailingStop * GetPointSize());
            ModifyOrder(buyTicket, newStopLoss);
        }
    }

    if (sellTicket > 0) {
        double profit = (sellPrice - GetCurrentPrice()) / GetPointSize();
        if (profit >= TakeProfitMax) {
            double newStopLoss = GetCurrentPrice() + (TrailingStop * GetPointSize());
            ModifyOrder(sellTicket, newStopLoss);
        }
    }
}

//+------------------------------------------------------------------+
//| Get current price                                                |
//+------------------------------------------------------------------+
double GetCurrentPrice() {
    return SymbolInfoDouble(_Symbol, SYMBOL_BID); // Use BID price
}

//+------------------------------------------------------------------+
//| Get point size                                                   |
//+------------------------------------------------------------------+
double GetPointSize() {
    return SymbolInfoDouble(_Symbol, SYMBOL_POINT); // Return point size
}

//+------------------------------------------------------------------+
//| Send order                                                       |
//+------------------------------------------------------------------+
bool SendOrder(int type, double volume, double price, double takeProfit, ulong &ticket) {
    MqlTradeRequest request;
    MqlTradeResult result;

    request.action = TRADE_ACTION_DEAL;
    request.symbol = _Symbol;
    request.volume = volume;
    request.type = type;
    request.price = price;
    request.tp = takeProfit;
    
    if (OrderSend(request, result)) {
        ticket = result.order; // Store the ticket number
        return true;
    }
    Print("OrderSend failed. Error: ", GetLastError()); // Print error if failed
    return false;
}

//+------------------------------------------------------------------+
//| Modify order                                                     |
//+------------------------------------------------------------------+
void ModifyOrder(ulong ticket, double stopLoss) {
    MqlTradeRequest request;
    MqlTradeResult result;

    request.action = TRADE_ACTION_SLTP;
    request.position = ticket; // Specify which order to modify
    request.sl = stopLoss; // Set new stop loss
    
    if (!OrderSend(request, result)) {
        Print("ModifyOrder failed. Error: ", GetLastError()); // Print error if failed
    }
}

//+------------------------------------------------------------------+
//| Close order                                                      |
//+------------------------------------------------------------------+
void CloseOrder(ulong ticket) {
    MqlTradeRequest request;
    MqlTradeResult result;

    request.action = TRADE_ACTION_DEAL;
    request.position = ticket; // Specify which order to close
    
    if (!OrderSend(request, result)) {
        Print("CloseOrder failed. Error: ", GetLastError()); // Print error if failed
    }
}
//+------------------------------------------------------------------+


erro:

'ORDER_BUY' - undeclared identifier	ICT_WEndel_001.mq5	55	23
'ORDER_SELL' - undeclared identifier	ICT_WEndel_001.mq5	63	23
implicit enum conversion	ICT_WEndel_001.mq5	114	20