Please help, My EA can't clost with profit,

 
please help, 

My ea can't close all position with profit or Stoploss
after 1st order is loss, it can't stuff and open new order when it enough condition

please help me make code correct, thank you

//+------------------------------------------------------------------+
//|                                                     RSI_AutoTrade.mq5 |
//|                        Copyright 2024, Your Name                |
//|                                       https://www.yourwebsite.com |
//+------------------------------------------------------------------+
input double InitialVolume = 0.1;         // Khối lượng lệnh ban đầu
input double MultiLot = 2.0;               // Hệ số khối lượng
input double Distance = 10;                 // Khoảng cách giữa các lệnh (tính theo điểm)
input double TakeProfitDCA = 50;           // Chốt lời DCA
input double StopLossDCA = 50;             // Chốt lỗ DCA
input int RsiPeriod = 14;                   // Thời gian của chỉ báo RSI
input double RsiOverbought = 80;            // Ngưỡng quá mua
input double RsiOversold = 20;              // Ngưỡng quá bán

//+------------------------------------------------------------------+
//| Hàm khởi tạo                                                       |
//+------------------------------------------------------------------+
int OnInit()
{
    return INIT_SUCCEEDED;
}

//+------------------------------------------------------------------+
//| Hàm xử lý tick                                                    |
//+------------------------------------------------------------------+
void OnTick()
{
    double rsiValue = iRSI(NULL, 0, RsiPeriod, PRICE_CLOSE);
    double totalProfit = 0.0;

    // Tính tổng lợi nhuận hiện tại từ tất cả các lệnh
    for (int i = 0; i < OrdersTotal(); i++)
    {
        if (OrderSelect(i)) // Chọn lệnh
        {
            double profit;
            if (OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_SELL)
            {
                double priceClose = SymbolInfoDouble(_Symbol, SYMBOL_BID);
                OrderCalcProfit(ORDER_TYPE_SELL, _Symbol, OrderGetDouble(ORDER_VOLUME_CURRENT), OrderGetDouble(ORDER_PRICE_OPEN), priceClose, profit);
            }
            else if (OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_BUY)
            {
                double priceClose = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
                OrderCalcProfit(ORDER_TYPE_BUY, _Symbol, OrderGetDouble(ORDER_VOLUME_CURRENT), OrderGetDouble(ORDER_PRICE_OPEN), priceClose, profit);
            }
            totalProfit += profit;
        }
    }

    // Mở lệnh sell đầu tiên khi RSI chạm ngưỡng quá mua
    if (rsiValue >= RsiOverbought && OrdersTotal() == 0)
    {
        OpenSellOrder(InitialVolume);
    }
    // Mở lệnh buy đầu tiên khi RSI chạm ngưỡng quá bán
    else if (rsiValue <= RsiOversold && OrdersTotal() == 0)
    {
        OpenBuyOrder(InitialVolume);
    }
    // Quản lý các lệnh sell
    else if (rsiValue >= RsiOverbought)
    {
        ManageSellOrders(totalProfit); // Truyền tổng lợi nhuận vào hàm quản lý
    }
    // Quản lý các lệnh buy
    else if (rsiValue <= RsiOversold)
    {
        ManageBuyOrders(totalProfit); // Truyền tổng lợi nhuận vào hàm quản lý
    }
}

//+------------------------------------------------------------------+
//| Mở lệnh sell đầu tiên                                             |
//+------------------------------------------------------------------+
void OpenSellOrder(double volume)
{
    MqlTradeRequest request;
    MqlTradeResult result;

    request.action = TRADE_ACTION_DEAL;
    request.symbol = _Symbol;
    request.volume = volume;
    request.type = ORDER_TYPE_SELL;
    request.price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
    request.tp = 0; // Chốt lời
    request.sl = 0; // Chốt lỗ
    request.magic = 0;
    request.comment = "First Sell Order";

    if (!OrderSend(request, result))
    {
        Print("Error opening first sell order: ", GetLastError());
    }
}

//+------------------------------------------------------------------+
//| Mở lệnh buy đầu tiên                                             |
//+------------------------------------------------------------------+
void OpenBuyOrder(double volume)
{
    MqlTradeRequest request;
    MqlTradeResult result;

    request.action = TRADE_ACTION_DEAL;
    request.symbol = _Symbol;
    request.volume = volume;
    request.type = ORDER_TYPE_BUY;
    request.price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
    request.tp = 0; // Chốt lời
    request.sl = 0; // Chốt lỗ
    request.magic = 0;
    request.comment = "First Buy Order";

    if (!OrderSend(request, result))
    {
        Print("Error opening first buy order: ", GetLastError());
    }
}

//+------------------------------------------------------------------+
//| Quản lý lệnh sell                                                |
//+------------------------------------------------------------------+
void ManageSellOrders(double totalProfit)
{
    double volume = InitialVolume;
    bool hasOpenSellOrder = false;

    for (int i = 0; i < OrdersTotal(); i++)
    {
        if (OrderSelect(i))
        {
            double profit;
            double priceClose = SymbolInfoDouble(_Symbol, SYMBOL_BID);
            OrderCalcProfit(ORDER_TYPE_SELL, _Symbol, OrderGetDouble(ORDER_VOLUME_CURRENT), OrderGetDouble(ORDER_PRICE_OPEN), priceClose, profit);

            if (OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_SELL)
            {
                hasOpenSellOrder = true;

                // Chốt lệnh nếu lợi nhuận đạt TakeProfitDCA
                if (profit >= TakeProfitDCA)
                {
                    CloseAllOrders(ORDER_TYPE_SELL);
                    return;
                }
            }
        }
    }

    // Chốt tất cả lệnh nếu tổng lợi nhuận đạt ngưỡng
    if (totalProfit >= TakeProfitDCA)
    {
        CloseAllOrders(ORDER_TYPE_SELL);
    }
    else if (totalProfit <= -StopLossDCA)
    {
        CloseAllOrders(ORDER_TYPE_SELL);
    }
}

//+------------------------------------------------------------------+
//| Quản lý lệnh buy                                                |
//+------------------------------------------------------------------+
void ManageBuyOrders(double totalProfit)
{
    double volume = InitialVolume;
    bool hasOpenBuyOrder = false;

    for (int i = 0; i < OrdersTotal(); i++)
    {
        if (OrderSelect(i))
        {
            double profit;
            double priceClose = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
            OrderCalcProfit(ORDER_TYPE_BUY, _Symbol, OrderGetDouble(ORDER_VOLUME_CURRENT), OrderGetDouble(ORDER_PRICE_OPEN), priceClose, profit);

            if (OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_BUY)
            {
                hasOpenBuyOrder = true;

                // Chốt lệnh nếu lợi nhuận đạt TakeProfitDCA
                if (profit >= TakeProfitDCA)
                {
                    CloseAllOrders(ORDER_TYPE_BUY);
                    return;
                }
            }
        }
    }

    // Chốt tất cả lệnh nếu tổng lợi nhuận đạt ngưỡng
    if (totalProfit >= TakeProfitDCA)
    {
        CloseAllOrders(ORDER_TYPE_BUY);
    }
    else if (totalProfit <= -StopLossDCA)
    {
        CloseAllOrders(ORDER_TYPE_BUY);
    }
}
//+------------------------------------------------------------------+
//| Đóng tất cả lệnh theo loại                                        |
//+------------------------------------------------------------------+
void CloseAllOrders(int orderType)
{
    for (int i = OrdersTotal() - 1; i >= 0; i--)
    {
        if (OrderSelect(i) && OrderGetInteger(ORDER_TYPE) == orderType) // Sử dụng đúng cách
        {
            ulong ticket = OrderGetTicket(OrderGetInteger(ORDER_TICKET));
            double volume = OrderGetDouble(ORDER_VOLUME_CURRENT);
            double price = (orderType == ORDER_TYPE_SELL) ? SymbolInfoDouble(_Symbol, SYMBOL_BID) : SymbolInfoDouble(_Symbol, SYMBOL_ASK);
            MqlTradeRequest request;
            MqlTradeResult result;

            request.action = TRADE_ACTION_DEAL;
            request.symbol = _Symbol;
            request.volume = volume;
            request.type = orderType;
            request.price = price;
            request.tp = 0; // Chốt lời
            request.sl = 0; // Chốt lỗ
            request.magic = 0;
            request.comment = "Close Order";

            if (!OrderSend(request, result))
            {
                Print("Error closing order: ", GetLastError());
            }
        }
    }
}

//+------------------------------------------------------------------+
 
Traders and coders are coding for free:
  • if it is interesting for them personally, or
  • if it is interesting for many members of this forum.

Freelance section of the forum should be used in most of the cases.

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • 2024.10.29
  • www.mql5.com
The largest freelance service with MQL5 application developers
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893