Grid stop order management function

 
I have a mql4 function that places a sell order based on a signal and if a sell trade is found, it places 5 numbers of buy stop orders in a grid starting at 10 pips away from the sell trade with 10 pips distance between buy stop orders.

I attempted to code a mql4 function that will scan to check:

1. If sell order is still open (bool condition)

2. If any of the buy stop orders has become buy order and has closed at stop loss (bool condition) 

If both conditions are true, 

The function will replace the individual closed buy trade with a buy stop trade with exact parameters of closed buy trade (same entry price and same stop loss)

The function will repeatedly scan to check for the above 2 conditions, it will continue to replace the closed buy trades with buy stop orders as long as the originating sell trade is open.

Once the originating sell trade is closed, the buy orders will be closed and buy stop orders will be deleted.

The function does not work kindly help me check what is wrong. Thanks
 

>> 2. If any of the buy stop orders has become buy order and has closed at stop loss (bool condition) 

if you want to do this, then correct me if i am wrong, dont you need to also check that there is a buystop open first -- before you check that the buy order has closed?

 
Revo Trades #:

>> 2. If any of the buy stop orders has become buy order and has closed at stop loss (bool condition) 

if you want to do this, then correct me if i am wrong, dont you need to also check that there is a buystop open first -- before you check that the buy order has closed?

Yes, you're very right, I have a different function that places the sell order and the buy stop orders, the function is not included in the file attached, it is a different file, because I want to have a separate function to manage buy stop orders I will just call the function immediately after the function that places the buy stop orders. I think you understand. Thanks
 
Jonathan Olalekan Toyese #:
Yes, you're very right, I have a different function that places the sell order and the buy stop orders, the function is not included in the file attached, it is a different file, because I want to have a separate function to manage buy stop orders I will just call the function immediately after the function that places the buy stop orders. I think you understand. Thanks

how can we help you if you do not include your full code? we do not know your logic if you do not include your full file.

Only thing that i can suggest is to use freelance service, or upload full file so we can analyse your full code.

 
Revo Trades #:

how can we help you if you do not include your full code? we do not know your logic if you do not include your full file.

Only thing that i can suggest is to use freelance service, or upload full file so we can analyse your full code.

The full code is attached. Thanks
Files:
code.mq4  7 kb
 

I have already replaced your attachments on two of your posts.

Please attach the source file directly, not a ZIP archive of the source file.

 
Fernando Carreiro #:

I have already replaced your attachments on two of your posts.

Please attach the source file directly, not a ZIP archive of the source file.

Ok. This is the source file. Thanks
Files:
code.mq4  7 kb
 
Jonathan Olalekan Toyese #:
Ok. This is the source file. Thanks
Hello, I'm still waiting.
Thanks a lot
 
Jonathan Olalekan Toyese #Hello, I'm still waiting. Thanks a lot

Then I suggest you please use the Freelance section. We are not here at your beck and call.

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • 2023.12.13
  • www.mql5.com
The largest freelance service with MQL5 application developers
 
Fernando Carreiro #:

Then I suggest you please use the Freelance section. We are not here at your beck and call.

I'll wait. Thanks
 
  1. Jonathan Olalekan Toyese: The function does not work kindly help me check what is wrong. Thanks

    Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

  2. Your code
        if (sellCount < maxSellPositions)
        return True;
             
             else
             
                 return False;
    
    Simplified
        return sellCount < maxSellPositions;
  3.  bool IsNewCandleSell(){
       static int BarsOnChart = Bars;
        if(Bars == BarsOnChart)
          return (false);
          BarsOnChart = Bars;
             return(true);
    

    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              MT4: New candle - MQL4 programming forum #3 (2014)
              MT5: Accessing variables - MQL4 programming forum #3 (2022)

    I disagree with making a new bar function, because it can only be called once per tick (second call returns false). A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum (2011)

  4.     int buyStopCounter = 0;
        if (OrdersTotal() > 0) {
            for (int i = OrdersTotal() - 1; i >= 0; i--) {

    The if statement is unnecessary.

  5. // Function to check if a sell order is still open
    bool IsSellOrderOpen() {
        int totalOrders = OrdersHistoryTotal();
        for (int i = totalOrders - 1; i >= 0; i--) {
            if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) && OrderType() == OP_SELL && OrderSymbol() == Symbol()) {
                return true; // Found an open sell order
    Your looking at history. That has nothing to do with checking for open orders.

  6. void ReplaceBuyStopOrder(int buyOrderTicket) {
        // Retrieve entry price and stop loss from the closed buy order
        double entryPrice = OrderOpenPrice();
        double stopLoss = OrderStopLoss();

    MT4: You can not use any Trade Functions until you first select an order.

Reason: