What Reasons for a Trade to Close so quickly

 
The following is taken from an excel spreadsheet which I use to examine my 'trades'. [I have removed some details to fit 'better'].  The trade fulfilled my criteria - a breakout from consolidation.  The trade was entered and closed a few seconds later - my own logs confirm the details.  I check for any error codes.  The TP & SL are well away from the price.  I don't close any trades within code (so far) i.e. rely on TP & SL only.



















































The journal illustrates the Open & Close (approx 6 seconds apart)

2022.10.26 15:00:01.764    Trades    '709083': exchange buy 0.01 AUDCAD at market sl: 0.87593 tp: 0.88437
2022.10.26 15:00:01.803    Trades    '709083': accepted exchange buy 0.01 AUDCAD at market sl: 0.87593 tp: 0.88437
2022.10.26 15:00:01.809    Trades    '709083': deal #118727144 buy 0.01 AUDCAD at 0.88187 done (based on order #247901901)
2022.10.26 15:00:01.811    Trades    '709083': order #247901901 buy 0.01 / 0.01 AUDCAD at 0.88187 done in 46.989 ms
2022.10.26 15:00:07.076    Trades    '709083': exchange sell 0.01 AUDCAD at market, close #247901901 buy 0.01 AUDCAD 0.88187
2022.10.26 15:00:07.114    Trades    '709083': accepted exchange sell 0.01 AUDCAD at market, close #247901901 buy 0.01 AUDCAD 0.88187
2022.10.26 15:00:07.119    Trades    '709083': deal #118727193 sell 0.01 AUDCAD at 0.88081 done (based on order #247901952)
2022.10.26 15:00:07.121    Trades    '709083': order #247901952 sell 0.01 / 0.01 AUDCAD at 0.88081 done in 46.220 ms

Just running out of ideas - any pointers in the right direction appreciated.

I use the following code with input Volume (Lots)  & Comment set elsewhere. Report_StringError checks for last error and prints error decription  if error togther with my own string (No error found). [ires = true]

Price is calculated and passed

      double   buyPrice    = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
      double   sellPrice   = SymbolInfoDouble(_Symbol,SYMBOL_BID);   
      double   ClosePrice  = iClose(_Symbol,_Period,1);


bool OpenTrade(double Price,ENUM_ORDER_TYPE type)
{  double   tp       = 0;
   double   sl       = 0;
   string   str_ires = "FALSE";
   bool     ires     = false;
   double   Spread   = SymbolInfoInteger( _Symbol, SYMBOL_SPREAD ) * _Point;
   //double   mult        = (type == ORDER_TYPE_BUY ) ? 1 : -1;
   if (type == ORDER_TYPE_BUY )
   {  tp = NormalizeDouble(Price + RangeSize,_Digits);
      sl = RangeLo;
   }
   if (type == ORDER_TYPE_SELL )
   {  tp = NormalizeDouble(Price - RangeSize,_Digits);
      sl = RangeHi;
   }
   ires = m_trade.PositionOpen(_Symbol,type,InpOrderSize,Price,sl,tp,InpTradeComment);
   if ( MTF == true ) Report_StringError("Error","str205108"); 
   MqlTradeResult result={};
   MqlTradeRequest request={};
 
   if (type == ORDER_TYPE_BUY )
   {  tp = NormalizeDouble(Price + RangeSize,_Digits);
      sl = RangeLo;
   }
   if (type == ORDER_TYPE_SELL )
   {  tp = NormalizeDouble(Price - RangeSize,_Digits);
      sl = RangeHi;

You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

  2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
              MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

  3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

    Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
    My GBPJPY shows average spread = 26 points, average maximum spread = 134.
    My EURCHF shows average spread = 18 points, average maximum spread = 106.
    (your broker will be similar).
              Is it reasonable to have such a huge spreads (20 pip spreads) in EURCHF? - General - MQL5 programming forum (2022)

 

William - many thanks for your response.

I understand your explanation about the spreads and how this would affect the triggered price but I don't see the BID/ASK being anywhere near the TP or SL. If the SL &TP are not trigger points then I just don't see what is triggering the close.

As previously stated I don't CLOSE any trades in the code.  (This EA is a first serious entry into MT5 - so early days)

Am I missing something? 

I don't understand why my close trade has been triggered - my SL of 0.87593 (593 points) & TP of  0.88437 (250 points) are  250+points away from the price of 0.88187. These figures are shown in the journal entries.

Just had 3 more simialr entries and all BUY orders - which could, of course, just be coincidence.

I perfectly understand that I should refine or be more consistence with the SL & TP calculations and thankyou for the explanation
 
Peter Williams #: Am I missing something?  I don't understand why my close trade has been triggered - my SL of 0.87593 (593 points) & TP of  0.88437 (250 points) are  250+points away from the price of 0.88187. These figures are shown in the journal entries.

I suspect that you have a "netting" account instead of "hedging" account. So when you place a sell order of the same volume of the previous buy order, you are effectively closing it.

EDIT: Analysing your log, it seems to be the case ...

2022.10.26 15:00:01.764    Trades    '709083': exchange buy 0.01 AUDCAD at market sl: 0.87593 tp: 0.88437
2022.10.26 15:00:01.803    Trades    '709083': accepted exchange buy 0.01 AUDCAD at market sl: 0.87593 tp: 0.88437
2022.10.26 15:00:01.809    Trades    '709083': deal #118727144 buy 0.01 AUDCAD at 0.88187 done (based on order #247901901)
2022.10.26 15:00:01.811    Trades    '709083': order #247901901 buy 0.01 / 0.01 AUDCAD at 0.88187 done in 46.989 ms
2022.10.26 15:00:07.076    Trades    '709083': exchange sell 0.01 AUDCAD at market, close #247901901 buy 0.01 AUDCAD 0.88187
2022.10.26 15:00:07.114    Trades    '709083': accepted exchange sell 0.01 AUDCAD at market, close #247901901 buy 0.01 AUDCAD 0.88187
2022.10.26 15:00:07.119    Trades    '709083': deal #118727193 sell 0.01 AUDCAD at 0.88081 done (based on order #247901952)
2022.10.26 15:00:07.121    Trades    '709083': order #247901952 sell 0.01 / 0.01 AUDCAD at 0.88081 done in 46.220 ms
 

Fernando - many thanks.

You got me looking at my code again and I had an ELSE where perhaps I shouldn't have. Not sure it solves the problem yet.

I suspect that you have a "netting" account instead of "hedging" - I'll take a look at this one too.

 
Peter Williams #: Fernando - many thanks. You got me looking at my code again and I had an ELSE where perhaps I shouldn't have. Not sure it solves the problem yet. I suspect that you have a "netting" account instead of "hedging" - I'll take a look at this one too.

This is one reason I strongly advocate that coders/traders should first learn to trade manually so that they properly understand the underlaying functionality before they attempt to code an EA.

 

Fernando,

Many thanks.  I believe I know what I want to do - quite simple OPEN a trade and not have it closed immediately.  Don't think the 'ELSE' statement made any difference. The ELSE was redundant.

Just set up a mini EA as follows

int OnInit()
  {
   ENUM_ACCOUNT_MARGIN_MODE m_margin_mode=(ENUM_ACCOUNT_MARGIN_MODE)AccountInfoInteger(ACCOUNT_MARGIN_MODE);
   ulong mymode = AccountInfoInteger(ACCOUNT_MARGIN_MODE);
   string msg = "mymode = "+IntegerToString(mymode);
   msg = msg + "\nACCOUNT_MARGIN_MODE_RETAIL_NETTING = "+IntegerToString(ACCOUNT_MARGIN_MODE_RETAIL_NETTING);
        msg = msg + "\nACCOUNT_MARGIN_MODE_EXCHANGE       = "+IntegerToString(ACCOUNT_MARGIN_MODE_EXCHANGE);
        msg = msg + "\nACCOUNT_MARGIN_MODE_RETAIL_HEDGING = "+IntegerToString(ACCOUNT_MARGIN_MODE_RETAIL_HEDGING);
   Comment(msg);
   return(INIT_SUCCEEDED);
  }

Results shown as below i.e. Hedging

Hedging_OR_Netting

Several more occurences- again within about 2 seconds for the EURGBP

2022.10.26 16:30:01.735 exchange buy 0.01 EURGBP at market sl: 0.86496 tp: 0.87027
2022.10.26 16:30:01.834 accepted exchange buy 0.01 EURGBP at market sl: 0.86496 tp: 0.87027
2022.10.26 16:30:01.905 deal #118730744 buy 0.01 EURGBP at 0.86777 done (based on order #247905594)
2022.10.26 16:30:01.953 order #247905594 buy 0.01 / 0.01 EURGBP at 0.86777 done in 238.481 ms
2022.10.26 16:30:03.334 exchange sell 0.01 EURGBP at market, close #247905594 buy 0.01 EURGBP 0.86777
2022.10.26 16:30:03.369 accepted exchange sell 0.01 EURGBP at market, close #247905594 buy 0.01 EURGBP 0.86777
2022.10.26 16:30:03.389 deal #118730747 sell 0.01 EURGBP at 0.86756 done (based on order #247905599)
2022.10.26 16:30:03.392 order #247905599 sell 0.01 / 0.01 EURGBP at 0.86756 done in 58.674 ms

So still running short on ideas

 
Peter Williams #:

Fernando,

Many thanks.  I believe I know what I want to do - quite simple OPEN a trade and not have it closed immediately.  Don't think the 'ELSE' statement made any difference. The ELSE was redundant.

Just set up a mini EA as follows

Results shown as below i.e. Hedging

Several more occurences- again within about 2 seconds for the EURGBP

So still running short on ideas

For exchange there also can be only 1 position.

Maybe also check your logic. If you get a buy signal expecting price to rise, and 2 seconds later you get a sell signal expecting price to fall, frankly your signals are no good.

 
Peter Williams #: Fernando, Many thanks.  I believe I know what I want to do - quite simple OPEN a trade and not have it closed immediately.  Don't think the 'ELSE' statement made any difference. The ELSE was redundant.

Just set up a mini EA as follows. Results shown as below i.e. Hedging. Several more occurences- again within about 2 seconds for the EURGBP. So still running short on ideas

Seems like "Exchange" account is equivalent to "Netting" account in that it only allows one position at a time.

So making changes to your EA will not help. You will have to rethink your entire strategy or change account types or broker.

I will repeat my statement. Trade your strategy manually before making it into an EA so that you can better understand how it will behave and to best code it.

 
Fernando Carreiro #:

Seems like "Exchange" account is equivalent to "Netting" account in that it only allows one position at a time.

So making changes to your EA will not help. You will have to rethink your entire strategy or change account types or broker.

I will repeat my statement. Trade your strategy manually before making it into an EA so that you can better understand how it will behave and to best code it.

Not exactly.  Any hedge strategy can run on a netting account. It does require a different internal architecture of the EA. 

Basically what is needed is a virtual trading environment, this is where the trading is conducted and a synchronizer which synchronizes all changes of the virtual environment to the real environment. The synchronizer for netting is extremely simple. It only has to increase/decrease the nett volume of the virtual to real.

It also can save on swap costs because for example there are 2 strategies running on the virtual environment. One strategy is long 1 lot, the other short 1 lot. Nett there is no position where as from hedge perspective there are 2 positions.

 
Enrique Dangeroux #: Not exactly.  Any hedge strategy can run on a netting account. It does require a different internal architecture of the EA. Basically what is needed is a virtual trading environment, this is where the trading is conducted and a synchronizer which synchronizes all changes of the virtual environment to the real environment. The synchronizer for netting is extremely simple. It only has to increase/decrease the nett volume of the virtual to real. It also can save on swap costs because for example there are 2 strategies running on the virtual environment. One strategy is long 1 lot, the other short 1 lot. Nett there is no position where as from hedge perspective there are 2 positions.

If the strategy were sound and valid then I would agree with you. But in this case the OP's strategy is not sound, so no amount of changes to his EA will fix the problem. He needs to rethink the strategy.

Also, the work required to create "virtual" environment is not worth the effort when it is much easier just to use a different account type or broker.

Reason: