StopLoss is beeing trigged immediately after order is placed, not when it´s reached. Any hint why?

 

Hi all

I place an order and stoploss is beeing trigged no matter i change it to a different value. It´s not been reached.

Any hint?


   MqlTradeRequest request={};   //https://www.mql5.com/en/docs/constants/structures/mqltraderequest
   MqlTradeResult  result={};
   
   ZeroMemory(request);
   ZeroMemory(result);
   
   request.action = TRADE_ACTION_DEAL;  // Trading action
   request.symbol = Symbol();           // Symbol to trade
   
   request.volume = volume;             // Trade volume
   request.type   = ORDER_TYPE_BUY;     // Order type (ENUM_ORDER_TYPE)
   
   request.price = price;               // Order price
   
   request.sl    = stopLoss;            // Stop Loss level
   request.tp    = takeProfit;          // Take Profit level
 
   request.type_filling = ORDER_FILLING_FOK;   //https://www.mql5.com/en/docs/constants/tradingconstants/orderproperties
 
   if(!OrderSend(request, result))
      //print error
 
mqlmaster4:

Hi all

I place an order and stoploss is beeing trigged no matter i change it to a different value. It´s not been reached.

Any hint?


Your details are insufficient. You need to show log from journal and expert tab as well. Also a screenshot of history can help, as well as chart. Symbol specs, maybe.
 

Also the code that calculates your stop loss, or is it just coming from an input?

Note that if it's just coming from an input, the request.sl value should be a price, not the # of points, or pips, or whatever your intent is. You have to calculate the price. See the sample code from here:

         //--- calculate the current price levels
         double price=PositionGetDouble(POSITION_PRICE_OPEN);
         double bid=SymbolInfoDouble(position_symbol,SYMBOL_BID);
         double ask=SymbolInfoDouble(position_symbol,SYMBOL_ASK);
         int    stop_level=(int)SymbolInfoInteger(position_symbol,SYMBOL_TRADE_STOPS_LEVEL);
         double price_level;
         //--- if the minimum allowed offset distance in points from the current close price is not set
         if(stop_level<=0)
            stop_level=150; // set the offset distance of 150 points from the current close price
         else
            stop_level+=50; // set the offset distance to (SYMBOL_TRADE_STOPS_LEVEL + 50) points for reliability
 
         //--- calculation and rounding of the Stop Loss and Take Profit values
         price_level=stop_level*SymbolInfoDouble(position_symbol,SYMBOL_POINT);
         if(type==POSITION_TYPE_BUY)
           {
            sl=NormalizeDouble(bid-price_level,digits);
            tp=NormalizeDouble(bid+price_level,digits);
           }
         else
           {
            sl=NormalizeDouble(ask+price_level,digits);
            tp=NormalizeDouble(ask-price_level,digits);
           }
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Trade Request Structure
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Trade Request Structure
  • www.mql5.com
Trade Request Structure - Data Structures - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Dominik Christian Egert #:
Your details are insufficient. You need to show log from journal and expert tab as well. Also a screenshot of history can help, as well as chart. Symbol specs, maybe.

I am using the strategy tester single.


Here goes the journal log:

2023.08.03 18:30:31.917    Core 1    WIN$,M5: history begins from 2022.01.03 09:00
2023.08.03 18:30:31.917    Core 1    WIN$,M5 (Rico-DEMO): generating based on real ticks
2023.08.03 18:30:31.917    Core 1    WIN$,M5: testing of Experts\Order.ex5 from 2023.05.25 00:00 to 2023.05.26 00:00 started
2023.08.03 18:30:31.917    Core 1    WIN$ : 2023.05.25 23:59 - all the real ticks discarded within a day
2023.08.03 18:30:31.917    Core 1    WIN$ : 2023.05.25 23:59 - 1328287 tick prices mismatch for 535 minute bars
2023.08.03 18:30:31.917    Core 1    WIN$ : real ticks begin from 2023.04.17 00:00:00
2023.08.03 18:30:31.917    Core 1    WIN$ : 2023.05.25 00:00 - 2023.05.26 00:00  real ticks discarded for 535 minutes of 535 total minute bars, every tick generation used
2023.08.03 18:30:31.917    Core 1    WIN$ : 2023.05.25 00:00 - 2023.05.26 00:00  real ticks discarded for 1 whole days
2023.08.03 18:30:31.917    Core 1    WIN$ : 2023.05.25 00:00 - 2023.05.26 00:00  tick volumes not matched for 535 minute bars
2023.08.03 18:30:31.917    Core 1    WIN$ : 2023.05.25 00:00 - 2023.05.26 00:00  tick prices of 1328287 ticks not matched for 535 minute bars
2023.08.03 18:30:31.917    Core 1    2023.05.25 09:00:00   exchange buy 10 WIN$ at 112433 sl: 112383 tp: 112633 (112432 / 112433 / 109930)
2023.08.03 18:30:31.917    Core 1    2023.05.25 09:00:00   deal #2 buy 10 WIN$ at 112433 done (based on order #2)
2023.08.03 18:30:31.917    Core 1    2023.05.25 09:00:00   deal performed [#2 buy 10 WIN$ at 112433]
2023.08.03 18:30:31.917    Core 1    2023.05.25 09:00:00   order performed buy 10 at 112433 [#2 buy 10 WIN$ at 112433]
2023.08.03 18:30:31.917    Core 1    2023.05.25 09:00:00   BUY Order placed successfully. Order ticket: 2
2023.08.03 18:30:31.917    Core 1    2023.05.25 09:00:00   stop loss triggered #2 buy 10 WIN$ 112433 sl: 112383 tp: 112633 [#3 sell 10 WIN$ at 112383]
2023.08.03 18:30:31.917    Core 1    2023.05.25 09:00:00   deal #3 sell 10 WIN$ at 112433 done (based on order #3)
2023.08.03 18:30:31.917    Core 1    2023.05.25 09:00:00   deal performed [#3 sell 10 WIN$ at 112433]
2023.08.03 18:30:31.917    Core 1    2023.05.25 09:00:00   order performed sell 10 at 112433 [#3 sell 10 WIN$ at 112383]
2023.08.03 18:30:31.917    Core 1    final balance 10000.00 BRL

Files:
 
Scott Allen #:

Also the code that calculates your stop loss, or is it just coming from an input?

Note that if it's just coming from an input, the request.sl value should be a price, not the # of points, or pips, or whatever your intent is. You have to calculate the price. See the sample code from here:

Thanks for your reply.

I am using this code to get the price :


double price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);

It is working for the symbol "Win$" i am using on this test at strategy tester, but for other symbols like "Winq23" it does not, it return 0 for the price and when i try with a third variable "& output" it returns true, no error.

Code to calculate the stoploss is right after the price:

 double stopLoss = NormalizeDouble(price - 200 * _Point, _Digits);
 
mqlmaster4 #:

I am using the strategy tester single.


Here goes the journal log:

2023.08.03 18:30:31.917    Core 1    WIN$,M5: history begins from 2022.01.03 09:00
2023.08.03 18:30:31.917    Core 1    WIN$,M5 (Rico-DEMO): generating based on real ticks
2023.08.03 18:30:31.917    Core 1    WIN$,M5: testing of Experts\Order.ex5 from 2023.05.25 00:00 to 2023.05.26 00:00 started
2023.08.03 18:30:31.917    Core 1    WIN$ : 2023.05.25 23:59 - all the real ticks discarded within a day
2023.08.03 18:30:31.917    Core 1    WIN$ : 2023.05.25 23:59 - 1328287 tick prices mismatch for 535 minute bars
2023.08.03 18:30:31.917    Core 1    WIN$ : real ticks begin from 2023.04.17 00:00:00
2023.08.03 18:30:31.917    Core 1    WIN$ : 2023.05.25 00:00 - 2023.05.26 00:00  real ticks discarded for 535 minutes of 535 total minute bars, every tick generation used
2023.08.03 18:30:31.917    Core 1    WIN$ : 2023.05.25 00:00 - 2023.05.26 00:00  real ticks discarded for 1 whole days
2023.08.03 18:30:31.917    Core 1    WIN$ : 2023.05.25 00:00 - 2023.05.26 00:00  tick volumes not matched for 535 minute bars
2023.08.03 18:30:31.917    Core 1    WIN$ : 2023.05.25 00:00 - 2023.05.26 00:00  tick prices of 1328287 ticks not matched for 535 minute bars
2023.08.03 18:30:31.917    Core 1    2023.05.25 09:00:00   exchange buy 10 WIN$ at 112433 sl: 112383 tp: 112633 (112432 / 112433 / 109930)
2023.08.03 18:30:31.917    Core 1    2023.05.25 09:00:00   deal #2 buy 10 WIN$ at 112433 done (based on order #2)
2023.08.03 18:30:31.917    Core 1    2023.05.25 09:00:00   deal performed [#2 buy 10 WIN$ at 112433]
2023.08.03 18:30:31.917    Core 1    2023.05.25 09:00:00   order performed buy 10 at 112433 [#2 buy 10 WIN$ at 112433]
2023.08.03 18:30:31.917    Core 1    2023.05.25 09:00:00   BUY Order placed successfully. Order ticket: 2
2023.08.03 18:30:31.917    Core 1    2023.05.25 09:00:00   stop loss triggered #2 buy 10 WIN$ 112433 sl: 112383 tp: 112633 [#3 sell 10 WIN$ at 112383]
2023.08.03 18:30:31.917    Core 1    2023.05.25 09:00:00   deal #3 sell 10 WIN$ at 112433 done (based on order #3)
2023.08.03 18:30:31.917    Core 1    2023.05.25 09:00:00   deal performed [#3 sell 10 WIN$ at 112433]
2023.08.03 18:30:31.917    Core 1    2023.05.25 09:00:00   order performed sell 10 at 112433 [#3 sell 10 WIN$ at 112383]
2023.08.03 18:30:31.917    Core 1    final balance 10000.00 BRL

Something is wrong with the data.

For symbols with Exchange Stocks, Exchange Futures and Futures Forts calculation modes, Stop Loss and Take Profit orders are triggered according to the rules of the exchange where trading is performed. Usually, Last price (price of the last performed transaction) is applied. In other words, a stop-order triggers when the Last price touches the specified price. However note that buying or selling as a result of activation of a stop-order is always performed by the Bid and Ask prices.

The wrong last price (in red) triggered the stoploss.

Market Watch - Trading Operations - MetaTrader 5 Help
  • www.metatrader5.com
The Market Watch window provides an overview of price data of financial instruments: quotes, price statistics and tick chart. It also provides...