How to determine the most recent order type that hit its target profit

 

I have written an EA (in MQL4) to test a strategy which involves placing a buy and sell pending order at the same price on a grid, with target profit, and once the orders are triggered, another pending order of buy and sell is placed at the next level in the direction of travel. I am not a proficient coder, but trying my best, and the EA seems to be doing what I want it to do to an extent. The one issue I am stuck on is this. I want it that if an order does not hit its target profit, I don't want the next next pending order for that order type to have a target profit, but I don't know how to code to identify and establish which order (buy or sell) just hit the target profit. I will give an example to clarify:

GBPUSD, buy and sell orders triggered at 1.24. Buy TP at 1.25, Sell TP at 1.23. Price moves up and hits 1.25, buy order TP hit and closed. Now I want next buy and sell pending order placed at 1.26, with buy TP at 1.27, but no TP for the sell order. How can I code to determine which order type (buy or sell) hit its TP last? Thank you

 

I think you can add a message in where you send your order.


int  OrderSend(
   string   symbol,              // symbol
   int      cmd,                 // operation
   double   volume,              // volume
   double   price,               // price
   int      slippage,            // slippage
   double   stoploss,            // stop loss
   double   takeprofit,          // take profit
   string   comment=NULL,        // comment
   int      magic=0,             // magic number
   datetime expiration=0,        // pending order expiration
   color    arrow_color=clrNONE  // color
   );



see after the take profit parameter, you can add a comment, if you add no comment it will automatically be NULL

so make your comment something like "This was a sell stop order" or something like that

 

Thank you. Was thinking along those lines. After I have added the comment, say "buy order", will the following code work:


string comment;
  if(OrderSelect(0,SELECT_BY_POS)==false)
    {
     Print("OrderSelect failed error code is",GetLastError());
     return(0);
    }
  comment = OrderComment();
  if (comment == "buy order")
    {
      //do----
    }
 

You can't compare strings directly with == in C++ (or MQL5) so you have to use 


string comment;
if(OrderSelect(0, SELECT_BY_POS) == false)
{
    Print("OrderSelect failed, error code is ", GetLastError());
    return(0);
}

// Check if there is an order
if(OrderSymbol() != NULL)
{
    comment = OrderComment();
    
    // Compare the strings
    if(StringCompare(comment, "buy order") == 0)
    {
        // Do something
    }
}
 
Conor Mcnamara #:

You can't compare strings directly with == in C++ (or MQL5) so you have to use 


Thank you! 

 

Hi

Be careful while using order comments – sometimes they are changed automatically (especially when trades are closed partially or changed by the broker). Or  when closed the broker sometimes add some parts of comments – le [sl] or [tp] – so when you compare string they won’t be the same. You can use then StringFind (comment,”buy order” )>=0 – but still the commnent may be fully changed. 

But here you can just check in hstory what was the last closed order and if t was closed on their tp level (profitable) or not.

You can get the type of the trade as OrderType()==OP_BUY or OrderType()==OP_SELL

Besides that – this is question for mt4 – so it should be in MQ4 section on the forum.

Best Regards

 
Marzena Maria Szmit #:

Hi

Be careful while using order comments – sometimes they are changed automatically (especially when trades are closed partially or changed by the broker). Or  when closed the broker sometimes add some parts of comments – le [sl] or [tp] – so when you compare string they won’t be the same. You can use then StringFind (comment,”buy order” )>=0 – but still the commnent may be fully changed. 

But here you can just check in hstory what was the last closed order and if t was closed on their tp level (profitable) or not.

You can get the type of the trade as OrderType()==OP_BUY or OrderType()==OP_SELL

Besides that – this is question for mt4 – so it should be in MQ4 section on the forum.

Best Regards

Thank you!

 
You can use OnTradeTranzaction() event handler to check which orders are closed and opened and all other things you may need .

 
Also you don’t need pending orders … EA can monitor 24/7 the price , so can open market orders . You track your trade, and when your TP or SL is hit, you can detect it in OnTradeTransaction() and your can trigger anything you want 
Reason: