//±-----------------------------------------------------------------+
//|                                                     ZZ_EA.mq4   |
//|                              Copyright © 2023, Gabriele Bassi   |
//±-----------------------------------------------------------------+
#property strict

// External input parameters for the EA
input double TakeProfitPips = 400; // Take profit in pips
input double StopLossPips = 150; // Stop loss in pips
input double TrailingStopPips = 10; // Trailing stop in pips

// External input parameters for the indicator
input int CCI_Period = 12;
input int Momentum_Period = 21;
input int RSI_Period = 14;
input double SAR_Step = 0.01;
input double SAR_Maximum = 0.2;

// OnInit is called when the expert is loaded onto a chart
int OnInit() {
    // Initialization code here
    return INIT_SUCCEEDED;
}

// OnTick is called on every new tick
void OnTick() {
    // Get last bar index in the chart (0 is the currently forming bar, 1 is the last fully formed bar)
    int lastBarIndex = 1;

    // Retrieve the last known values for the indicator signals
    double arrowUp = iCustom(Symbol(), 0, "ZZZZZ", CCI_Period, Momentum_Period, RSI_Period, SAR_Step, SAR_Maximum, 0, lastBarIndex);
    double arrowDown = iCustom(Symbol(), 0, "ZZZZZ", CCI_Period, Momentum_Period, RSI_Period, SAR_Step, SAR_Maximum, 1, lastBarIndex);
    
    // If there are no current orders, check for a buy or sell signal
    if(OrdersTotal() == 0) {
        if(arrowUp != EMPTY_VALUE) {
            // If an up arrow (buy signal) is present, send a buy order.
            double sl = NormalizeDouble(Bid - StopLossPips * Point, Digits);
            double tp = NormalizeDouble(Bid + TakeProfitPips * Point, Digits);
            int ticket = OrderSend(Symbol(), OP_BUY, 0.1, Ask, 2, sl, tp, "Buy order based on ZZZZZ", 0, 0, clrGreen);
            if(ticket < 0) {
                Print("An error occurred while sending a buy order: ", GetLastError());
            }
        }
        if(arrowDown != EMPTY_VALUE) {
            // If a down arrow (sell signal) is present, send a sell order.
            double sl = NormalizeDouble(Ask + StopLossPips * Point, Digits);
            double tp = NormalizeDouble(Ask - TakeProfitPips * Point, Digits);
            int ticket = OrderSend(Symbol(), OP_SELL, 0.1, Bid, 2, sl, tp, "Sell order based on ZZZZZ", 0, 0, clrRed);
            if(ticket < 0) {
                Print("An error occurred while sending a sell order: ", GetLastError());
            }
        }
    } else {
        // Update the trailing stop for existing orders
        TrailingStopFunc();
    }
}

// TrailingStopFunc adjusts the stop loss to enable a trailing stop
void TrailingStopFunc() {
    for(int i = OrdersTotal() - 1; i >= 0; i--) {
        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
            // Verify that the selected order is for the current symbol
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == 0) {
                if(OrderType() == OP_BUY) {
                    // Current price is far enough from the opening price to move stop loss
                    if(Bid - OrderOpenPrice() > TrailingStopPips * Point) {
                        double newStopLoss = Bid - TrailingStopPips * Point;
                        // Update the stop loss if it is less than the current stop loss
                        if(newStopLoss > OrderStopLoss()) {
                            OrderModify(OrderTicket(), OrderOpenPrice(), newStopLoss, OrderTakeProfit(), 0, clrGreen);
                        }
                    }
                } else if(OrderType() == OP_SELL) {
                    // Current price is far enough from the opening price to move stop loss
                    if(OrderOpenPrice() - Ask > TrailingStopPips * Point) {
                        double newStopLoss = Ask + TrailingStopPips * Point;
                        // Update the stop loss if it is greater than the current stop loss
                        if(newStopLoss < OrderStopLoss()) {
                            OrderModify(OrderTicket(), OrderOpenPrice(), newStopLoss, OrderTakeProfit(), 0, clrRed);
                        }
                    }
                }
            }
        }
    }
}
//±-----------------------------------------------------------------+

aggiungi la gestione del lotto