expert pro – EA MetaTrader 4

#property strict

extern double tpPoints = 50;          // Take profit points
extern double stepPoints = 100;       // Points between doubling orders
extern double initialLotSize = 0.01;  // Initial lot size
extern double lotStep = 0.01;         // Lot size step
extern bool useCustomLots = true;     // Use custom lot sizes
extern bool EnableFirstTrade = false; // Enable first trade by default


input double LotSize1 = 0.01;    // Lot size for the first trade
input double LotSize2 = 0.03;    // Lot size for the second trade
input double LotSize3 = 0.05;    // Lot size for the third trade
input double LotSize4 = 0.07;    // Lot size for the fourth trade
input double LotSize5 = 0.09;    // Lot size for the fifth trade
input double LotSize6 = 0.11;    // Lot size for the sixth trade
input double LotSize7 = 0.13;    // Lot size for the seventh trade
input double LotSize8 = 0.15;    // Lot size for the eighth trade
input double LotSize9 = 0.17;    // Lot size for the ninth trade
input double LotSize10 = 0.19;   // Lot size for the tenth trade
input double LotSize11 = 0.21;   // Lot size for the eleventh trade
input double LotSize12 = 0.23;   // Lot size for the twelfth trade
input double LotSize13 = 0.25;   // Lot size for the thirteenth trade
input double LotSize14 = 0.27;   // Lot size for the fourteenth trade
input double LotSize15 = 0.29;   // Lot size for the fifteenth trade
input double LotSize16 = 0.31;   // Lot size for the sixteenth trade
input double LotSize17 = 0.33;   // Lot size for the seventeenth trade
input double LotSize18 = 0.35;   // Lot size for the eighteenth trade
input double LotSize19 = 0.37;   // Lot size for the nineteenth trade
input double LotSize20 = 0.39;   // Lot size for the twentieth trade
input double LotSize21 = 0.41;   // Lot size for the twenty-first trade
input double LotSize22 = 0.43;   // Lot size for the twenty-second trade
input double LotSize23 = 0.45;   // Lot size for the twenty-third trade
input double LotSize24 = 0.47;   // Lot size for the twenty-fourth trade
input double LotSize25 = 0.49;   // Lot size for the twenty-fifth trade
input double LotSize26 = 0.51;   // Lot size for the twenty-sixth trade
input double LotSize27 = 0.53;   // Lot size for the twenty-seventh trade
input double LotSize28 = 0.55;   // Lot size for the twenty-eighth trade
input double LotSize29 = 0.57;   // Lot size for the twenty-ninth trade
input double LotSize30 = 0.59;   // Lot size for the thirtieth trade
input double LotSize31 = 0.61;   // Lot size for the thirty-first trade
input double LotSize32 = 0.63;   // Lot size for the thirty-second trade
input double LotSize33 = 0.65;   // Lot size for the thirty-third trade
input double LotSize34 = 0.67;   // Lot size for the thirty-fourth trade
input double LotSize35 = 0.69;   // Lot size for the thirty-fifth trade
input double LotSize36 = 0.71;   // Lot size for the thirty-sixth trade
input double LotSize37 = 0.73;   // Lot size for the thirty-seventh trade
input double LotSize38 = 0.75;   // Lot size for the thirty-eighth trade
input double LotSize39 = 0.77;   // Lot size for the thirty-ninth trade
input double LotSize40 = 0.79;   // Lot size for the fortieth trade

double LotSizes[40];

// Initialize the lot sizes array
void InitLotSizes() {
    LotSizes[0] = LotSize1;
    LotSizes[1] = LotSize2;
    LotSizes[2] = LotSize3;
    LotSizes[3] = LotSize4;
    LotSizes[4] = LotSize5;
    LotSizes[5] = LotSize6;
    LotSizes[6] = LotSize7;
    LotSizes[7] = LotSize8;
    LotSizes[8] = LotSize9;
    LotSizes[9] = LotSize10;
    LotSizes[10] = LotSize11;
    LotSizes[11] = LotSize12;
    LotSizes[12] = LotSize13;
    LotSizes[13] = LotSize14;
    LotSizes[14] = LotSize15;
    LotSizes[15] = LotSize16;
    LotSizes[16] = LotSize17;
    LotSizes[17] = LotSize18;
    LotSizes[18] = LotSize19;
    LotSizes[19] = LotSize20;
    LotSizes[20] = LotSize21;
    LotSizes[21] = LotSize22;
    LotSizes[22] = LotSize23;
    LotSizes[23] = LotSize24;
    LotSizes[24] = LotSize25;
    LotSizes[25] = LotSize26;
    LotSizes[26] = LotSize27;
    LotSizes[27] = LotSize28;
    LotSizes[28] = LotSize29;
    LotSizes[29] = LotSize30;
    LotSizes[30] = LotSize31;
    LotSizes[31] = LotSize32;
    LotSizes[32] = LotSize33;
    LotSizes[33] = LotSize34;
    LotSizes[34] = LotSize35;
    LotSizes[35] = LotSize36;
    LotSizes[36] = LotSize37;
    LotSizes[37] = LotSize38;
    LotSizes[38] = LotSize39;
    LotSizes[39] = LotSize40;
}

int buyTicket = -1;                  // Buy order ticket
int sellPendingTicket = -1;          // Sell pending order ticket
int buyPendingTicket = -1;           // Buy pending order ticket
double initialBuyPrice = 0;          // Initial buy price
double totalLots = initialLotSize;   // Total lots traded
int tradeCount = 0;                  // Counter for trades

//+——————————————————————+
//| Expert initialization function                                   |
//+——————————————————————+
int OnInit()
{
    if (!EnableFirstTrade) {
        return INIT_SUCCEEDED;
    }

    InitLotSizes();
    
    // Check if there are any open orders from the previous session
    if (OrdersTotal() > 0) {
        for (int i = 0; i < OrdersTotal(); i++) {
            if (OrderSelect(i, SELECT_BY_POS)) {
                if (OrderType() == OP_BUY) {
                    buyTicket = OrderTicket();
                    initialBuyPrice = OrderOpenPrice();
                    tradeCount = 1; // Start with the first trade
                }
            }
        }
    }

    return(INIT_SUCCEEDED);
}

//+——————————————————————+
//| Expert deinitialization function                                 |
//+——————————————————————+
void OnDeinit(const int reason)
{
}

//+——————————————————————+
//| Expert tick function                                             |
//+——————————————————————+
void OnTick()
{
    // Check the open buy order
    if (buyTicket > 0 && OrderSelect(buyTicket, SELECT_BY_TICKET))
    {
        double buyOpenPrice = OrderOpenPrice();
        
        // Check for take profit of the buy order
        if (Bid >= buyOpenPrice + tpPoints * Point)
        {
            if (OrderClose(buyTicket, OrderLots(), Bid, 3, White))
            {
                buyTicket = -1;

                // Cancel all pending orders
                if (sellPendingTicket > 0 && OrderSelect(sellPendingTicket, SELECT_BY_TICKET))
                {
                    if (!OrderDelete(sellPendingTicket)) {
                        Print(“Error deleting sell pending order: “, GetLastError());
                    } else {
                        sellPendingTicket = -1;
                    }
                }
                if (buyPendingTicket > 0 && OrderSelect(buyPendingTicket, SELECT_BY_TICKET))
                {
                    if (!OrderDelete(buyPendingTicket)) {
                        Print(“Error deleting buy pending order: “, GetLastError());
                    } else {
                        buyPendingTicket = -1;
                    }
                }

                tradeCount = 0; // Reset trade count
                totalLots = initialLotSize; // Reset total lots
            }
        }
    }

    // Check the sell pending order
    if (sellPendingTicket > 0 && OrderSelect(sellPendingTicket, SELECT_BY_TICKET))
    {
        double sellPrice = OrderOpenPrice();
        
        // If the sell pending order is activated
        if (Ask <= sellPrice)
        {
            // Determine lot size based on the trade count
            double lotSize;
            if (useCustomLots && tradeCount < 40) {
                lotSize = LotSizes[tradeCount];
            } else {
                lotSize = initialLotSize + totalLots * lotStep;
            }
            buyPendingTicket = OrderSend(Symbol(), OP_BUYLIMIT, lotSize, initialBuyPrice, 0, 0, 0, “Buy Pending Order”, 0, 0, Blue);
            if (buyPendingTicket < 0) {
                Print(“Error setting buy pending order: “, GetLastError());
            } else {
                if (!OrderDelete(sellPendingTicket)) {
                    Print(“Error deleting sell pending order: “, GetLastError());
                } else {
                    sellPendingTicket = -1;
                }
            }
            totalLots += lotSize;
            tradeCount++;
        }
    }

    // Check the open sell order
    for (int i = OrdersTotal() – 1; i >= 0; i–) {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderType() == OP_SELL)
        {
            double sellOpenPrice = OrderOpenPrice();
            
            // Check for take profit of the sell order
            if (Bid <= sellOpenPrice – tpPoints * Point)
            {
                if (OrderClose(OrderTicket(), OrderLots(), Bid, 3, White)) {
                    buyPendingTicket = -1;
                    sellPendingTicket = -1;
                } else {
                    Print(“Error closing sell order: “, GetLastError());
                }
            }
        }
    }
}

📈 ROBOTFX MetaTrader Expert Advisors and Indicators to maximize profits and minimize the risks