How to work correctly in MT5 with OrderSend - page 9

 
prostotrader:
https://www.mql5.com/ru/forum/97557/page4#comment_2891988
Документация по MQL5: Программы MQL5 / Выполнение программ
Документация по MQL5: Программы MQL5 / Выполнение программ
  • www.mql5.com
Программы MQL5 / Выполнение программ - справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
fxsaber:

Sorry, no desire to deal with your scripts

Added by

But there is a way out of it.

Since the OrderrSend() function is synchronous, after receiving the order we

make sure that the history is synchronized in OnTradeTransaction()

//+------------------------------------------------------------------+
// Expert TradeTransaction function                                  |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
    switch(trans.type)
     {
      case TRADE_TRANSACTION_ORDER_UPDATE:
         switch(trans.order_state)
           {
            case ORDER_STATE_PLACED:
               if(order_ticket==trans.order)
                 {
                  Print(__FUNCTION__," Order plased done. Ticket = ",trans.order);
                  if(order_ticket>0)
                    {
                     if(OrderSelect(order_ticket))
                       {
                        //Ордер синхронизирован с историей
                       }
                     else
                       {
                        Print(__FUNCTION__," Order not select! Ticket = ",trans.order);
                       }
                    }
                  else
                    {
                     Print(__FUNCTION__," Wrong order ticket = ",trans.order);
                    }
                 }
               break;
           }
         break;
     }
  }

And no dancing!

 
prostotrader:

No dancing!

Forum on trading, automated trading systems and strategy testing

How to work correctly in MT5 with OrderSend

fxsaber, 2016.11.10 10:00

Note that this is a script and there can't be any Event-overs. The only way out is a dumb Sleep.

If you rewrite this script with SB, nothing will change.

 
fxsaber:
YOU can get as twisted as you like with Sleep....
 
prostotrader:
You can screw with Sleep as you wish....

Why repeat the same thing that was already suggested and voiced before by others?

The thread says everything about possible problems with OnTradeTransaction. In particular, it concerns the operation of multiple Expert Advisors simultaneously.

People are making crutches, it does not mean that they are not familiar with the documentation and do not know how to use the features.

OnTrade and OnTradeTransaction have been developed exactly for this purpose. When they started it, the developers thought that it would be idiotic to handle more than one Expert Advisor simultaneously in an account.

Well, they were wrong about that.

Try to write the following function in your Expert Advisor

  1. i = 0.
  2. We open a position at symbol[i].
  3. If i++ >= 5, we exit.
  4. If there is no slippage, we return to step 2. If there is slippage, we exit.
 
fxsaber:

Why repeat the same thing that was already suggested and voiced before by others?

The thread says everything about possible problems with OnTradeTransaction. In particular, it concerns the operation of multiple Expert Advisors simultaneously.

People are making crutches, it does not mean that they are not familiar with the documentation and do not know how to use the features.

OnTrade and OnTradeTransaction have been developed exactly for this purpose. When they started it, the developers thought that it would be idiotic to handle more than one Expert Advisor simultaneously in an account.

Well, they were wrong about that.

Try to write the following function in your Expert Advisor

  1. i = 0.
  2. We open a position at symbol[i].
  3. If i++ >= 5, we exit.
  4. If there is no slippage, we return to step 2. If there is slippage, we exit.

I currently have 41 Expert Advisors working on a real account (in one terminal) that together during a trading day

They set 2000 orders open and close positions and I am not experiencing any difficulties at all!

Added

As for the functionality we have, it should be based on what we have and not on what we "need".

Developers, if possible, fix bugs and bugs, taking into account the wishes of

users (albeit very slowly).

I think they will soon deal with OederSend() too.

 
prostotrader:

I have 41 Expert Advisors on my real account (in one terminal) working all together during one trading day.

They set 2000 orders, open and close positions and I have no problems at all!

This is called getting off topic.

When it comes to even the simplest logic.

Forum on trading, automated trading systems and testing trading strategies

How to work correctly in MT5 with OrderSend

fxsaber, 2016.11.15 13:30

Try writing the following function in your EA

  1. i = 0.
  2. Open a position on symbol[i].
  3. If i++ >= 5, we exit.
  4. If the new position has no slippage, we return to step 2. If there is slippage, we exit.
Someone trades it immediately.
// MQL4&5-code

#property strict

#include <MT4Orders.mqh>    // https://www.mql5.com/ru/code/16006

void Func( const string &Symbols[] )
{
  const int Total = ArraySize(Symbols);
  
  for (int i = 0; i < Total; i++)
  {
    const double Price = SymbolInfoDouble(Symbols[i], SYMBOL_ASK);
    const int digits = (int)SymbolInfoInteger(Symbols[i], SYMBOL_DIGITS);
    
    if (!OrderSelect(OrderSend(Symbols[i], OP_BUY, 1, Price, 100, 0, 0, DoubleToString(Price, digits)), SELECT_BY_TICKET) ||
        (NormalizeDouble(Price - OrderOpenPrice(), digits) != 0)) // если не получилось открыть или есть проскальзывание - выходим
      break;
  }
}

void OnStart() // OnTick
{
  const string Symbols[] = {"EURUSD", "GBPUSD", "AUDUSD", "USDCAD", "USDJPY"};
  
  Func(Symbols);
}

And someone waits for mana from developers.
 

I have already said that you are free to twist it however you like (everyone has that right).

 
prostotrader:

I have already said that you are free to twist as you wish (everyone has that right).

it was a long time ago, but i remember this guy, well, thisfxsaber did wonders,

his codes are still stored in the MT4 codebase, his code for reading sites on WinInet

is still used by a lot of programmers...

and therefore, it seems to me, he is not a pervert...

 
Ripped the OrderSendSync from the library here
uint OrderSend_MaxPause = 1000000; // максимальное время на синхронизацию в мкс.

const bool IsTester = (::MQLInfoInteger(MQL_TESTER) || ::MQLInfoInteger(MQL_OPTIMIZATION) ||
                       ::MQLInfoInteger(MQL_VISUAL_MODE) || ::MQLInfoInteger(MQL_FRAME_MODE));
                      
                      

bool Waiting( const bool FlagInit = false )
{
  static ulong StartTime = 0;

  if (FlagInit)
    StartTime = ::GetMicrosecondCount();

  const bool Res = (::GetMicrosecondCount() - StartTime < OrderSend_MaxPause);

  if (Res)
    ::Sleep(0);

  return(Res);
}

bool EqualPrices( const double Price1, const double Price2, const int digits)
{
  return(::NormalizeDouble(Price1 - Price2, digits) == 0);
}

#define WHILE(A) while (!(Res = (A)) && Waiting())

bool OrderSendSync( const MqlTradeRequest &Request, MqlTradeResult &Result )
{
  bool Res = ::OrderSend(Request, Result);

  if (Res && !IsTester && (Result.retcode < TRADE_RETCODE_ERROR) && (OrderSend_MaxPause > 0))
  {
    Res = (Result.retcode == TRADE_RETCODE_DONE);
    Waiting(true);

    if (Request.action == TRADE_ACTION_DEAL)
    {
      WHILE(::HistoryOrderSelect(Result.order))
        ;

      Res = Res && (((ENUM_ORDER_STATE)::HistoryOrderGetInteger(Result.order, ORDER_STATE) == ORDER_STATE_FILLED) ||
                    ((ENUM_ORDER_STATE)::HistoryOrderGetInteger(Result.order, ORDER_STATE) == ORDER_STATE_PARTIAL));

      if (Res)
        WHILE(::HistoryDealSelect(Result.deal))
          ;
    }
    else if (Request.action == TRADE_ACTION_PENDING)
    {
      if (Res)
        WHILE(::OrderSelect(Result.order))
          ;
      else
      {
        WHILE(::HistoryOrderSelect(Result.order))
          ;

        Res = false;
      }
    }
    else if (Request.action == TRADE_ACTION_SLTP)
    {
      if (Res)
      {
        bool EqualSL = false;
        bool EqualTP = false;

        const int digits = (int)::SymbolInfoInteger(Request.symbol, SYMBOL_DIGITS);

        if ((Request.position == 0) ? ::PositionSelect(Request.symbol) : ::PositionSelectByTicket(Request.position))
        {
          EqualSL = EqualPrices(::PositionGetDouble(POSITION_SL), Request.sl, digits);
          EqualTP = EqualPrices(::PositionGetDouble(POSITION_TP), Request.tp, digits);
        }

        WHILE((EqualSL && EqualTP))
          if ((Request.position == 0) ? ::PositionSelect(Request.symbol) : ::PositionSelectByTicket(Request.position))
          {
            EqualSL = EqualPrices(::PositionGetDouble(POSITION_SL), Request.sl, digits);
            EqualTP = EqualPrices(::PositionGetDouble(POSITION_TP), Request.tp, digits);
          }
      }
    }
    else if (Request.action == TRADE_ACTION_MODIFY)
    {
      if (Res)
      {
        bool EqualSL = false;
        bool EqualTP = false;

        const int digits = (int)::SymbolInfoInteger(Request.symbol, SYMBOL_DIGITS);

        if (::OrderSelect(Result.order))
        {
          EqualSL = EqualPrices(::OrderGetDouble(ORDER_SL), Request.sl, digits);
          EqualTP = EqualPrices(::OrderGetDouble(ORDER_TP), Request.tp, digits);
        }

        WHILE((EqualSL && EqualTP))
          if (::OrderSelect(Result.order))
          {
            EqualSL = EqualPrices(::OrderGetDouble(ORDER_SL), Request.sl, digits);
            EqualTP = EqualPrices(::OrderGetDouble(ORDER_TP), Request.tp, digits);
          }
      }
    }
    else if (Request.action == TRADE_ACTION_REMOVE)
      if (Res)
        WHILE(::HistoryOrderSelect(Result.order))
          ;
  }

  return(Res);
}

#undef WHILE
The idea of the code should be clear. I may have overlooked something. I haven't noticed any errors in operation.