Questions from Beginners MQL5 MT5 MetaTrader 5 - page 799

 
Aleksey Vyazmikin:

Thank you! I searched and couldn't find it...

Then the next question, let's say the step is 25, then how to arrange rounding to a number divisible by 25, maybe there is a function?

https://www.mql5.com/ru/docs/standardlibrary/tradeclasses/csymbolinfo/csymbolinfonormalizeprice

Документация по MQL5: Стандартная библиотека / Торговые классы / CSymbolInfo / NormalizePrice
Документация по MQL5: Стандартная библиотека / Торговые классы / CSymbolInfo / NormalizePrice
  • www.mql5.com
Стандартная библиотека / Торговые классы / CSymbolInfo / NormalizePrice - справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 

Thanks, but I'm not using the standard trade library.

 
Vladislav Andruschenko:
No code, no comments.

Yeah, that's weird. Attached the file, but didn't check it. My bad. Now I am attaching both code and code excerpt where tester gives out a line with [Invalid stops]. I want to understand how MqlTradeRequest structure works when modifying position. In my case it's when change of stop on SAR. I can't get rid of [Invalid stops] message from the tester. RTS Splice Tool.

//+------------------------------------------------------------------+
//| Модификация Stop Loss открытой позиции                           |
//+------------------------------------------------------------------+
//--- объявление запроса и результата
   MqlTradeRequest request;
   MqlTradeResult  result;

   int total=PositionsTotal(); // количество открытых позиций  
   Print("total = ",total," ");
//--- перебор всех открытых позиций
   for(int i=0; i<total; i++)
     {
      if(PositionSelect(_Symbol)==true) // есть открытая позиция
        {
         //--- параметры ордера
         ulong  position_ticket=PositionGetTicket(i);// тикет позиции
         string position_symbol=PositionGetString(POSITION_SYMBOL); // символ 
         int    digits=(int)SymbolInfoInteger(position_symbol,SYMBOL_DIGITS); // количество знаков после запятой
         ulong  magic=PositionGetInteger(POSITION_MAGIC); // MagicNumber позиции

         if(PositionSelect(position_symbol)) // если позицию удалось выделить, значит - позиция существует
           {
            //--- обнуление значений запроса и результата
            ZeroMemory(request);
            ZeroMemory(result);
            //--- установка параметров операции
            request.action = TRADE_ACTION_SLTP;   // тип торговой операции
            request.position = position_ticket;   // тикет позиции
            request.symbol = position_symbol;     // символ 
            request.sl= STP;                      // Stop Loss позиции
            request.tp = 0;                       // Take Profit позиции

            //--- отсылаем ордер
            int number=OrderSend(request,result);
            // анализируем код возврата торгового сервера
            if(result.retcode==10009 || result.retcode==10008) //Request is completed or order placed
              {
               Alert("Ордер Sell успешно помещен, тикет ордера # ",result.order," ");
              }
            else
              {
               Alert("Запрос на установку ордера Sell не выполнен - код ошибки:",GetLastError());
               return;
              }
           }
        }
     }
Files:
SAR_SAR.mq5  27 kb
 
DCodec:

Yeah, that's weird. Attached the file, but didn't check it. My bad. Now I am attaching both code and excerpt from code where the tester gives out a line with [Invalid stops]. I want to understand how MqlTradeRequest structure works when modifying position. In my case it's when change of stop on SAR. I can't get rid of [Invalid stops] message from the tester. RTS Splice Tool.

This is not that code fragment where we should look for an error, at first sight. It is only triple selection of one and the same position that bothers me. It is redundant.

You have an error in incorrect stops, you should probably start your search from the place where these stops are calculated.

 
DCodec:

Yeah, that's weird. Attached the file, but didn't check it. My bad. Now I am attaching both code and code excerpt where tester gives out a line with [Invalid stops]. I want to understand how MqlTradeRequest structure works when modifying position. In my case it's when change of stop on SAR. I can't get rid of [Invalid stops] message from the tester. RTS Splice.

For full understanding it is necessary to specify what type of account hadge or netting

If the account type wasge

if(PositionSelect(_Symbol)==true) // есть открытая позиция

You have chosen a position for the symbol, but it may not be exactly what you wanted to see...

ulong  position_ticket=PositionGetTicket(i);// тикет позиции

But now we choose the exact position we wanted.

The function returns a ticket of a position by index in the list of open positions and automatically selects this position for further work with it using


Next, try again to choose the right position

if(PositionSelect(position_symbol)) // если позицию удалось выделить, значит - позиция существует

Again, the probability that you have chosen the wrong position is very high

When representing positions independently (ACCOUNT_MARGIN_MODE_RETAIL_HEDGING), several positions can be opened for each symbol simultaneously. In this case, PositionSelect will select the position with the smallest tick.


To check how the OrderSend() function behaves in this case, you should do so, but it is better to do it as it should be and if the error does not disappear, print not only the error code, but also the position ticket, the position price, the price of the new stop and take. This is the minimum.

 
Alexey Viktorov:

For a full understanding you need to specify what type of account hadge or netting

Thank you for your comments. Type of account is netting. RTS Splice. Is that the tricky part? One position on one symbol. You can choose any way you like, you'll still get to that position. These are not pending orders, are they? That's why I don't understand even more - what's wrong? I tried all the variants.

 
DCodec:

Thanks for the comments. Account type netting. RTS Splice instrument. Is that the tricky part? One position on one symbol. You can choose any way you like, you'll still get to that position. These are not pending orders, are they? That's why I don't understand even more - what's wrong? I tried all the variants.

Is there no answer to this?

Forum on Trading, Automated Trading Systems and Strategy Tests

Questions from Beginners MQL5 MT5 MetaTrader 5

Alexey Viktorov, 2017.11.22 09:29

To check how the OrderSend() function behaves in this case, you need to do so and check, but it is better to do as it should be and if the error is not gone print not only the error code but also the position ticket, position price, price of new stop and take. This is the minimum.


 
DCodec:

That's why I don't understand even more - WHAT'S WRONG??? Tried all the options.

Insert this line where the error occurs

Print(ToString(request) + ToString(result));

Forum on trading, automated trading systems and trading strategies testing

Peculiarities of mql5 language, subtleties and tricks

fxsaber, 2017.02.25 16:27

Translating MqlTrade-structures into a string
#define TOSTRING(A)  #A + " = " + (string)(A) + "\n"
#define TOSTRING2(A) #A + " = " + EnumToString(A) + " (" + (string)(A) + ")\n"

string ToString( const MqlTradeTransaction &Trans )
{
  return(TOSTRING(Trans.deal) + TOSTRING(Trans.order) + TOSTRING(Trans.symbol) +
         TOSTRING2(Trans.type) + TOSTRING2(Trans.order_type) + TOSTRING2(Trans.order_state) +
         TOSTRING2(Trans.deal_type) + TOSTRING2(Trans.time_type) +
         TOSTRING(Trans.time_expiration) + TOSTRING(Trans.price) + TOSTRING(Trans.price_trigger) +
         TOSTRING(Trans.price_sl) + TOSTRING(Trans.price_tp) + TOSTRING(Trans.volume) +
         TOSTRING(Trans.position) + TOSTRING(Trans.position_by));
}

string ToString( const MqlTradeRequest &Request )
{
  return(TOSTRING2(Request.action) + TOSTRING(Request.magic) + TOSTRING(Request.order) +
         TOSTRING(Request.symbol) + TOSTRING(Request.volume) + TOSTRING(Request.price) +
         TOSTRING(Request.stoplimit) + TOSTRING(Request.sl) +  TOSTRING(Request.tp) +
         TOSTRING(Request.deviation) + TOSTRING2(Request.type) + TOSTRING2(Request.type_filling) +
         TOSTRING2(Request.type_time) + TOSTRING(Request.expiration) + TOSTRING(Request.comment) +
         TOSTRING(Request.position) + TOSTRING(Request.position_by));
}

string ToString( const MqlTradeResult &Result )
{
  return(TOSTRING(Result.retcode) + TOSTRING(Result.deal) + TOSTRING(Result.order) +
         TOSTRING(Result.volume) + TOSTRING(Result.price) + TOSTRING(Result.bid) +  
         TOSTRING(Result.ask) + TOSTRING(Result.comment) + TOSTRING(Result.request_id) +  
         TOSTRING(Result.retcode_external));
}

#undef TOSTRING
#undef TOSTRING2

 
fxsaber:

Insert this line where the error occurs


Pasted. Here is the result of the tester:

2017.11.22 18:22:51.127 2016.11.09 12:00:00 exchange buy 1.00 RTS Splice at 98080 sl: 94130 (98070 / 98080 / 98070)
2017.11.22 18:22:51.127 2016.11.09 12:00:00 deal #4 buy 1.00 RTS Splice at 98080 done (based on order #4)
2017.11.22 18:22:51.127 2016.11.09 12:00:00 deal performed [#4 buy 1.00 RTS Splice at 98080]
2017.11.22 18:22:51.127 2016.11.09 12:00:00 order performed buy 1.00 at 98080 [#4 buy 1.00 RTS Splice at 98080]
2017.11.22 18:22:53.561 2016.11.09 14:05:00:00 failed modify #4 buy 1.00 RTS Splice sl: 94130, tp: 0 -> sl: 94316, tp: 0 [Invalid stops]
2017.11.22 18:22:53.576 2016.11.09 14:05:00 Request.action = TRADE_ACTION_SLTP (6)
2017.11.22 18:22:53.576 2016.11.09 14:05:00 Request.magic = 0
2017.11.22 18:22:53.576 2016.11.09 14:05:00 Request.order = 0
2017.11.22 18:22:53.576 2016.11.09 14:05:00 Request.symbol = RTS Splice
2017.11.22 18:22:53.576 2016.11.09 14:05:00 Request.volume = 0.0
2017.11.22 18:22:53.576 2016.11.09 14:05:00 Request.price = 0.0
2017.11.22 18:22:53.576 2016.11.09 14:05:00 Request.stoplimit = 0.0
2017.11.22 18:22:53.576 2016.11.09 14:05:00 Request.sl = 94316.0
2017.11.22 18:22:53.576 2016.11.09 14:05:00 Request.tp = 0.0
2017.11.22 18:22:53.576 2016.11.09 14:05:00 Request.deviation = 0
2017.11.22 18:22:53.576 2016.11.09 14:05:00 Request.type = ORDER_TYPE_BUY (0)
2017.11.22 18:22:53.576 2016.11.09 14:05:00 Request.type_filling = ORDER_FILLING_FOK (0)
2017.11.22 18:22:53.576 2016.11.09 14:05:00 Request.type_time = ORDER_TIME_GTC (0)
2017.11.22 18:22:53.576 2016.11.09 14:05:00 Request.expiration = 1970.01.01 00:00:00
2017.11.22 18:22:53.576 2016.11.09 14:05:00 Request.comment =
2017.11.22 18:22:53.576 2016.11.09 14:05:00 Request.position = 4
2017.11.22 18:22:53.576 2016.11.09 14:05:00 Request.position_by = 0
2017.11.22 18:22:53.576 2016.11.09 14:05:00 Result.retcode = 10016
2017.11.22 18:22:53.576 2016.11.09 14:05:00 Result.deal = 0
2017.11.22 18:22:53.576 2016.11.09 14:05:00 Result.order = 0
2017.11.22 18:22:53.576 2016.11.09 14:05:00 Result.volume = 0.0
2017.11.22 18:22:53.576 2016.11.09 14:05:00 Result.price = 0.0
2017.11.22 18:22:53.576 2016.11.09 14:05:00 Result.bid = 0.0
2017.11.22 18:22:53.576 2016.11.09 14:05:00 Result.ask = 0.0
2017.11.22 18:22:53.576 2016.11.09 14:05:00 Result.comment = Invalid stops
2017.11.22 18:22:53.576 2016.11.09 14:05:00 Result.request_id = 0
2017.11.22 18:22:53.576 2016.11.09 14:05:00 Result.retcode_external = 0
2017.11.22 18:22:53.576 2016.11.09 14:05:00 Alert: Position modification error by TP and SL error code: 4756

 
DCodec:

Put it in. Here's the result of the tester:

Server and tester mode?