Manage Orders

 

Hello guys...

I need help managing Orders, the code so far works fine except that it seems to sleep after opening orders...its like it doesn't opening again once the SL is Hit..but the idea was to close and open on the opposite direction obviously once the pattern is detected.


               if (ThreeCandlesSell() && numberOfOrders < 2 ) 
               {
                   patternDetected = "Three Candles SELL SIGNAL";
                   CloseAllBuyPositions();
                   
                   SendMarketSellOrdersWithTargetProfit();
                   Print("Three Candles SELL detected."); // Print detection message
               }



void SendMarketBuyOrdersWithTargetProfit() {
    // Define stop loss level
    double stopLossUSD = 10.0; // Set your desired stop loss in dollars here

    // Convert stop loss from USD to the symbol's base currency and points
    double stopLossBaseCurrency = stopLossUSD / SymbolInfoDouble(_Symbol, SYMBOL_TRADE_CONTRACT_SIZE);
    double stopLossPoints = stopLossBaseCurrency / SymbolInfoDouble(_Symbol, SYMBOL_POINT);

    // Check if the number of orders exceeds the limit
    if (numberOfOrders >= 2 ) 
    {
        Print("Cannot place more than 2 orders for this symbol.");
        return;  // Exit the function without sending new orders
    }

    // Initialize trade request
    MqlTradeRequest request;
    ZeroMemory(request);

    request.action = TRADE_ACTION_DEAL;  // Market order type
    request.symbol = _Symbol;             // Symbol
    request.volume = 0.5;                 // Initial lot size
    request.price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);  // Current Ask price
    request.type_filling = ORDER_FILLING_FOK;  // Order filling type
    request.type = ORDER_TYPE_BUY;        // Buy order type
    request.magic = 0;                    // Magic number (optional)
    request.comment = "MarketBuyOrder";   // Optional comment

    // Set stop loss level for each order
    request.sl = request.price - stopLossPoints; // Set stop loss level relative to current price

    // Initialize trade result
    MqlTradeResult result;
    ZeroMemory(result);

    // Send the order
    if (!OrderSend(request, result)) {
        Print("Failed to send order, Error code:", GetLastError());
    } else {
        Print("Order sent successfully, Ticket:", result.order);
        // Update the pattern age after sending the order
        UpdatePatternAge();
        // Increment the counter for total orders
        numberOfOrders++;
    }
}