Issues with stop loss at Exchange (NASDAQ) trading stocks using PositionClose() and CTrade -> Sell() function

 

Ciao, I have an EA which trades physical stocks at NASDAQ on a live account. Apart from the normal trading hours, there are pre and after hours. At these pre and after hours the spread between bid and ask is extremely high and often very far away from the latest price at normal opening hours. When I took my EA live I used PositionClose() and CTrade -> Sell() function to close an open position instead of placing the stop loss directly at the market using CTrade -> Buy() function with param SL.

I also realized that within the first 2-3 minutes, when the exchange opens, the price needs some time to  come back to "normal". So what happened when I was using PositionClose() is that my SL got triggered when the price was massively below the actual stop loss. It was not always the case but in maybe 2 out of 5 cases. It seems not to happen when I place the stop loss directly at the market using the Buy function as described. I do not know exactly why this happens but it looks like the EA code is already executed within the pre and after hours and thus PositionClose() is triggered at these extreme prices. I have improved my EA and I want to close an open position partially using either the CTrade -> Sell() function or PositionClosePartial(). To be on the safe side I will also place a stop loss at the market directly as well. So my idea to overcome these crazy pre and after market hour prices and the first 2-3 minutes before the price comes back to "normal" is to restrict my EA to the time when Exchange is really open plus adding 2-3 minutes on top. I looked at the following functions:

Print("TimeCurrent: ", TimeCurrent());
Print("TimeGMT: ", TimeGMT());
Print("TimeLocal: ", TimeLocal());
Print("TimeTradeServer: ", TimeTradeServer());

but then found the following post on this forum:

https://www.mql5.com/en/forum/231392

I looked at the functions Petr Nosek suggested but I am not sure what params to put into the SymbolInfoSessionTrade() function. In particular for the index and the from and to params. I thought this function would return the beginning and the end of the trading session? I am also not sure how to use these functions together as suggested by Petr Nosek to achieve this.

I am not sure if this is the way to go anyhow. Did anyone here experience the same behaviour trading physical stocks at an exchange and using PositionClose() and/or Sell() function? How to best proceed here?

Wishes Martina

How to check Market Open/Closed in MT5 ?
How to check Market Open/Closed in MT5 ?
  • 2018.03.09
  • www.mql5.com
Please, how I can check if market is open or closed in MT5, I cant find answer...
 

Forum on trading, automated trading systems and testing trading strategies

How to find the Friday's market closing time?

Alain Verleyen, 2016.12.22 18:44

And even the code I used :

   datetime from,to;
   uint session=0;

   while(SymbolInfoSessionQuote(_Symbol,FRIDAY,session,from,to))
     {
      printf("Quoting session %i start %02i:%02i to %02i:%02i",session,from/3600,(from%3600)/60,to/3600,(to%3600)/60);
      session++;
     }

   session=0;
   while(SymbolInfoSessionTrade(_Symbol,FRIDAY,session,from,to))
     {
      printf("Trading session %i start %02i:%02i to %02i:%02i",session,from/3600,(from%3600)/60,to/3600,(to%3600)/60);
      session++;
     }
So please don't come here talking about quality, when you have just posted rudeness and self-promoting.