Buy and sell orders do not work for BTCUSD

 

I have a robot in MQL5 that I use to trade assets on the Brazilian stock exchange. The robot works perfectly for opening buy and sell orders, but when testing it with BTCUSD, it fails on the sell orders. Looking at the logs in the Experts tab, it seems that the values are correct, and I don't understand why it's not working. I'm trading on a demo account with Deriv, using a M1 time frame.

Below are excerpts from the code that I consider important to demonstrate the functioning of the buy and sell functions, followed by a part of the log showing the error in the Experts tab.

#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>


CTrade trade;
CSymbolInfo symbolInfo; 

MqlTick currentTick;



void BuyOperation() {
   PRC = symbolInfo.NormalizePrice(currentTick.ask);
   STL = symbolInfo.NormalizePrice(PRC - (stopLoss));
   TKP = symbolInfo.NormalizePrice(PRC + (takeProfit));

   Print(">>>>>> BUY - lotSize: ", lotSize, " _Symbol: ", _Symbol, " PRC: ", PRC, ", STL: ", STL, ", TKP: ", TKP);
   
   if (trade.Buy(lotSize, _Symbol, PRC, STL, TKP, "Buy")) {
      Print("Ordem de Compra- sem falha. ResultRetcode: ", trade.ResultRetcode(), ", RetcodeDescription: ", trade.ResultRetcodeDescription());
   } else {
      Print("Ordem de Compra- com falha. ResultRetcode: ", trade.ResultRetcode(), ", RetcodeDescription: ", trade.ResultRetcodeDescription());
   }
}


void SellOperation() {
   PRC = symbolInfo.NormalizePrice(currentTick.bid);
   STL = symbolInfo.NormalizePrice(PRC + (stopLoss));
   TKP = symbolInfo.NormalizePrice(PRC - (takeProfit));

   Print(">>>>>> SELL - lotSize: ", lotSize, " _Symbol: ", _Symbol, " PRC: ", PRC, ", STL: ", STL, ", TKP: ", TKP);

   if (trade.Sell(lotSize, _Symbol, PRC, STL, TKP, "Sell")) {
      Print("Ordem de Venda - sem falha. ResultRetcode: ", trade.ResultRetcode(), ", RetcodeDescription: ", trade.ResultRetcodeDescription());
   } else {
      Print("Ordem de Venda - com falha. ResultRetcode: ", trade.ResultRetcode(), ", RetcodeDescription: ", trade.ResultRetcodeDescription());
   }
}


Tab Experts log:

2023.05.21 12:12:14.056 SAR (BTCUSD,M1)   >>>>>> SELL - lotSize: 0.01 _Symbol: BTCUSD PRC: 26921.0, STL: 26951.0, TKP: 26821.0

2023.05.21 12:12:14.056 SAR (BTCUSD,M1)   CTrade::OrderSend: market sell 0.01 BTCUSD sl: 26951.000 tp: 26821.000 [invalid stops]

2023.05.21 12:12:14.056 SAR (BTCUSD,M1)   Ordem de Venda - com falha. ResultRetcode: 10016, RetcodeDescription: invalid stops

Can anyone tell me what could be wrong? Could it be the price normalization section?

 

Apply the following to your code. Even though it is for Market products, these checks should also be applied to personal EAs too ...

Articles

The checks a trading robot must pass before publication in the Market

MetaQuotes, 2016.08.01 09:30

Before any product is published in the Market, it must undergo compulsory preliminary checks in order to ensure a uniform quality standard. This article considers the most frequent errors made by developers in their technical indicators and trading robots. An also shows how to self-test a product before sending it to the Market.
 
Fernando Carreiro #:

Apply the following to your code. Even though it is for Market products, these checks should also be applied to personal EAs too ...

Thank you for the link, I think it will be very helpful in solving my problem and some others that I hadn't even considered yet.
 
André Krebs:

I have a robot in MQL5 that I use to trade assets on the Brazilian stock exchange. The robot works perfectly for opening buy and sell orders, but when testing it with BTCUSD, it fails on the sell orders. Looking at the logs in the Experts tab, it seems that the values are correct, and I don't understand why it's not working. I'm trading on a demo account with Deriv, using a M1 time frame.

Below are excerpts from the code that I consider important to demonstrate the functioning of the buy and sell functions, followed by a part of the log showing the error in the Experts tab.


Tab Experts log:

2023.05.21 12:12:14.056 SAR (BTCUSD,M1)   >>>>>> SELL - lotSize: 0.01 _Symbol: BTCUSD PRC: 26921.0, STL: 26951.0, TKP: 26821.0

2023.05.21 12:12:14.056 SAR (BTCUSD,M1)   CTrade::OrderSend: market sell 0.01 BTCUSD sl: 26951.000 tp: 26821.000 [invalid stops]

2023.05.21 12:12:14.056 SAR (BTCUSD,M1)   Ordem de Venda - com falha. ResultRetcode: 10016, RetcodeDescription: invalid stops

Can anyone tell me what could be wrong? Could it be the price normalization section?


The error that appears indicates that your SL value is too small.

You can try to give SL a bigger value, then see the results.

Reason: