How to disable auto open orders when TP/SL triggered

 

Hi everyone, help me please.

When I ran the EA trading system(MT5), I found a message in the journal tab that said "take profit triggered #2 sell 0.02 XAUUSDm 2042.486 sl: 2058.181 tp: 2036.359 [#3 buy 0.02 XAUUSDm at 2036.359]"

This event represents the automatic opening of an order. When the system is TP/SL triggered.

If I don't want automatic orders to be opened when TP/SL is triggered.

What can I do to solve the problem?

Extract profit down to the last pip
Extract profit down to the last pip
  • www.mql5.com
The article describes an attempt to combine theory with practice in the algorithmic trading field. Most of discussions concerning the creation of Trading Systems is connected with the use of historic bars and various indicators applied thereon. This is the most well covered field and thus we will not consider it. Bars represent a very artificial entity; therefore we will work with something closer to proto-data, namely the price ticks.
 
Life Function:

Hi everyone, help me please.

When I ran the EA trading system(MT5), I found a message in the journal tab that said "take profit triggered #2 sell 0.02 XAUUSDm 2042.486 sl: 2058.181 tp: 2036.359 [#3 buy 0.02 XAUUSDm at 2036.359]"

This event represents the automatic opening of an order. When the system is TP/SL triggered.

If I don't want automatic orders to be opened when TP/SL is triggered.

What can I do to solve the problem?

It sounds like your EA has logic that opens a new position when a TP or SL is triggered. If you don't want this to happen, you'll need to modify the EA to prevent it from opening new orders automatically after closing positions.

Look for any part of the code where a new order is placed right after a TP or SL is hit and remove or comment out that logic. If you're not comfortable editing the code, you may want to ask a developer for help.

 

nothing.

If you bought the ea from marketplace, you can contact the author to make suggestion.

if you are the coder of the ea you can change the code so that a new trade does not happen right away.

But if you are not a coder and you do not have the source code, then, you can do nothing, but close that 2nd trade with a small loss, and do that manually, or buy a coder to make a 2nd ea that will do it for you.

 
Oleksandr Medviediev #:

It sounds like your EA has logic that opens a new position when a TP or SL is triggered. If you don't want this to happen, you'll need to modify the EA to prevent it from opening new orders automatically after closing positions.

Look for any part of the code where a new order is placed right after a TP or SL is hit and remove or comment out that logic. If you're not comfortable editing the code, you may want to ask a developer for help.

My coding only has basic commands for open order  /  OpenOrder (......some parameter.......) / and not have logic for opens a new position when a TP or SL is triggered.

When problem order opens a new position when a TP or SL is triggered. The order was closed almost immediately and the direction status was shown as 'out'.

This is my code.

//-------
double sl = CalculateSL(resistanceLevel * (1 + slBuffer), price);
double tp = price * (1 - tpBuffer);
OpenOrder(ORDER_TYPE_SELL, lotSize, price, sl, tp);
//-------
void OpenOrder(int type, double lot_size, double price, double sl, double tp)
{
    MqlTradeRequest request;
    MqlTradeResult result;
    ZeroMemory(request);
    ZeroMemory(result);
    request.action = TRADE_ACTION_DEAL;
    request.symbol = _Symbol;
    request.volume = lot_size;
    request.type = (ENUM_ORDER_TYPE)type;
    request.price = price;
    request.sl = sl;
    request.tp = tp;
    request.deviation = 10;
    request.magic = 123456;
    request.comment = "AutoTrade";
    if(CanOpenOrder()){
      if (OrderSend(request, result))
       {
           Print("Order opened successfully: ", result.order);
       }
       else
       {
           Print("OrderSend failed: ", result.retcode);
       }
    }    
}
//-------
bool CanOpenOrder()
{
    if (TimeCurrent() - lastTriggerTime == 0) 
    {
        return false;
    }
    return true;
}
 
Life Function #:

My coding only has basic commands for open order  /  OpenOrder (......some parameter.......) / and not have logic for opens a new position when a TP or SL is triggered.

When problem order opens a new position when a TP or SL is triggered. The order was closed almost immediately and the direction status was shown as 'out'.

This is my code.

CanOpenOrder is the cause. I recommend you look in codebase for eas with new candle checks for correct examples.

 
Michael Charles Schefe #:

CanOpenOrder is the cause. I recommend you look in codebase for eas with new candle checks for correct examples.

I have added some coding to check if there are open orders or not. I added this condition when calling the function OpenOrder(...............)

But it still can't solve the problem.

this is my code

..
if(!HasOpenOrders()){
        OpenOrder(ORDER_TYPE_SELL, lotSize, price, sl, tp);
}
..

bool HasOpenOrders()
  {
   int totalPositions = PositionsTotal();
   
  for(int i = 0; i < totalPositions; i++)
     {
      ulong ticket = PositionGetTicket(i);
      
      if(PositionGetSymbol(i) == _Symbol)
        {            
            return true;
        }
     }
   
   return false;
  }


Please give me an example of how to fix this problem.

 
Life Function #:

I have added some coding to check if there are open orders or not. I added this condition when calling the function OpenOrder(...............)

But it still can't solve the problem.

this is my code


Please give me an example of how to fix this problem.

describe when you want the ea to check for new signal? after new candle? when?

 
Life Function: When I ran the EA trading system(MT5), I found a message in the journal tab that said "take profit triggered #2 sell 0.02 XAUUSDm 2042.486 sl: 2058.181 tp: 2036.359 [#3 buy 0.02 XAUUSDm at 2036.359]"

This event represents the automatic opening of an order. When the system is TP/SL triggered.  If I don't want automatic orders to be opened when TP/SL is triggered. What can I do to solve the problem?

If it is from the "Journal" log, then I think that you may be mixing up a few things.

On MT5, when a position is closed (for example at a stop-loss or take-profit), a reverse order is placed to close it. It is not like MT4 where a position is simply closed.

On MT5, closing a "buy" position requires a "sell" order, and closing a "sell" position requires a "buy" order. To differentiate this, MT5 has an extra type of state for the deals, "in" or "out".

So when you open a position you have an "in" deal, and when you close it you have a "out" deal. Have a look at the trade history and enable the "deals" so that you can see it.

In the following image you can see one example of a position opening with a "sell" order and a "in" deal, and hitting a trailing stop-loss with an "out" deal of a "buy" order, and another example of a "buy" order, opening a position with an "in" deal and closing at the trailing stop-loss with a "out" deal of a "sell" order.


So, in conclusion, there is nothing wrong. It is just the mechanics of how the market works. There is no "automatic" order opening up a new position.
 
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