The great and terrible MT4 forever (or how to strategise a transition) - page 27

 

Forum on trading, automated trading systems and testing trading strategies

OrderModify() in MQL5?

Edward Munroe, 2021.05.27 00:51

Ask a simple question, get a simple answer!? Why has almost everything in mql5 got to be so messy?

Good question.

 

Traditionally a simple task before the weekend.

An open position is 1 lot. After running the script we need to make the position become 0.9 lots.

It is clear that the script can be launched at any moment. That is why there are many situations that may occur.


As usually, I have tried to solve the problem in the classical way.

// Частичное закрытие позиции.

#include <Trade\Trade.mqh>

void OnStart()
{
  CTrade Trade;
  
  if (Trade.Buy(1)) // Пытаемся открыть позицию на 1 лот.
  {
    const ulong Ticket = Trade.ResultOrder();
    
    // Ждем, пока не появится.
    while (!IsStopped() && !PositionSelectByTicket(Ticket))
      Sleep(0);
      
    // А теперь просто закрываем 0.1 лота в случае, если объем позиции равен 1.
    // Цикл - для эмуляции запуска скрипта в любой момент.
    while (!IsStopped() && PositionSelectByTicket(Ticket) && (PositionGetDouble(POSITION_VOLUME) == 1))
      Trade.PositionClosePartial(Ticket, 0.1);
  }
}


This script shows a position at 0.8 lots. We need 0.9.

 
fxsaber:

Running this script gives a position of 0.8 lots. I need 0.9.

Tested a couple of times on 2 servers, I only got 0.9


The logic of the tests is clear - we are trying to get rid of asynchronous execution on one tick, the general scheme of this process (how to synchronize on one tick) is important to know

 
fxsaber:

Traditionally a simple task before the weekend.


Holy crap! And I have 0.8 (on MQDemo -0.9). How many more of these rakes are there going to be?

 
Igor Makanu:

Checked a couple of times on 2 servers, I have only 0.9

 
 
fxsaber:
Solved.

This is probably the most difficult task of all the easily formulated tasks in this thread at the moment.

A very strong test of mastery of the MQL5 trading part.

 

open a buy order with zero SL and TP, then set an SL and TP of 30 pips (if memory serves me correctly, this is called Market Execution account type - SL and TP cannot be set at once)

For MT4 this code works:

void OnStart()
{
   int ticket;
   if((ticket = OrderSend(_Symbol, OP_BUY, 0.1, Ask, 30, 0.0, 0.0)) < 0 ||
         !OrderSelect(ticket, SELECT_BY_TICKET) ||
         !OrderModify(ticket, OrderOpenPrice(), NormalizeDouble(OrderOpenPrice() - 300 * _Point, _Digits), NormalizeDouble(OrderOpenPrice() + 300 * _Point, _Digits), OrderExpiration()))
   {
      Print("Error Open order # ", GetLastError());
   }
}
 
Igor Makanu:

open a buy order with zero SL and TP, then set an SL and TP of 30 pips (if memory serves me correctly, this is called Market Execution account type - SL and TP cannot be set at once)

For MT4 this code works:

About two or three years ago the recognition of the execution type was introduced at terminal level. Now even with Market Execution you can set SL and TP and the terminal itself will split the order into two trade orders.

 
Igor Makanu:

Open a Buy order with zero SL and TP, then place a 30 pips SL and TP (if memory serves me correctly, this is called Market Execution account type - SL and TP cannot be placed simultaneously)

A common task.

Reason: