OnTradeTransaction processing - page 3

 
Илья Ребенок:

In this case, I still need to store the ticket of the order from the requester somewhere to compare it with the ticket from the trade. And I just want to get away from all storing in local variables and get info solely from market/terminal to level out local infrastructure risks.

You're being a bit naive.

The mage is the same for all trades of this EA!

But the orders are different (unique)!

 
prostotrader:

I trade in Openvashka on real and am testing on demo, but I don't have multiple triggers.

Post your piece of code forTRADE_TRANSACTION_DEAL_ADD

I just had this happen today. Above posted a clipping from the log of 2 identical transactions for the same robot.

2019.02.08 10:55:29 [INFO]: ( PChBreak_RTS-3.19_22) TRADE_TRANSACTION_DEAL_ADD
TRADE_TRANSACTION_DEAL_ADD
Symbol: RTS-3.19
Deal ticket: 12674810
Deal type: DEAL_TYPE_BUY
Order ticket: 82646001
Order type: ORDER_TYPE_BUY
Order state: ORDER_STATE_STARTED
Order time type: ORDER_TIME_GTC
Order expiration: 1970.01.01 00:00
Price: 119700
Price trigger: 0
Stop Loss: 0
Take Profit: 0
Volume: 1
Position: 82646001
Position by: 0

2019.02.08 10:55:32 [INFO]: ( PChBreak_RTS-3.19_22 ) TRADE_TRANSACTION_DEAL_ADD
TRADE_TRANSACTION_DEAL_ADD
Symbol: RTS-3.19
Deal ticket: 12674810
Deal type: DEAL_TYPE_BUY
Order ticket: 82646001
Order type: ORDER_TYPE_BUY
Order state: ORDER_STATE_STARTED
Order time type: ORDER_TIME_GTC
Order expiration: 1970.01.01 00:00
Price: 119700
Price trigger: 0
Stop Loss: 0
Take Profit: 0
Volume: 1
Position: 82646001
Position by: 0

Code to deal_add

case TRADE_TRANSACTION_DEAL_ADD:
        {
         drop_info2("TRADE_TRANSACTION_DEAL_ADD\r\n"+TransactionDescription(trans));
         if((trans.deal_type==DEAL_TYPE_BUY || trans.deal_type==DEAL_TYPE_SELL) && trans.order!=0)
           {
            if(getIsDealOfExpert(trans.deal)) //функция проверки принадлежности сделки к роботу
              {
               drop_info2("Сделка наша");
               analyzeFilledOrder(trans.order,trans.volume); //процедура по выставлению отложенных стоп ордеров
              }
           }
        }
      break;

After a bug with two identical transactions, I added a check that the ticket of the current transaction is not equal to the previous one.

 
Илья Ребенок:

I just had one of those today. Above I posted a clipping from the log of 2 identical transactions for the same robot.

2019.02.08 10:55:29 [INFO]: ( PChBreak_RTS-3.19_22 ) TRADE_TRANSACTION_DEAL_ADD
TRADE_TRANSACTION_DEAL_ADD
Symbol: RTS-3.19
Deal ticket: 12674810
Deal type: DEAL_TYPE_BUY
Order ticket: 82646001
Order type: ORDER_TYPE_BUY
Order state: ORDER_STATE_STARTED
.........

Code to deal_add

After a bug with two identical transactions, I added a check that the ticket of the current transaction is not equal to the previous one.

Got it.

Order state: ORDER_STATE_STARTED - THIS CANNOT BE inTRADE_TRANSACTION_DEAL_ADD!

Added

I'm sure you don't (and neither doesIlya Baranov)

switch(trans.type)
  {
    case TRADE_TRANSACTION_DEAL_ADD:
    //Place you code here
    break;  
  }

Added

This forum has a "Exchange Trading" section you would be better off communicating there...

 
prostotrader:

I see.

Order state: ORDER_STATE_STARTED - THIS CANNOT BE in TRADE_TRANSACTION_DEAL_ADD!

Added

I'm sure you don't.

Added

This forum has a "Exchange Trading" section you would be better off communicating there...

I would have moved it long ago - but@Ilya Child never said - does he trade on the exchange or just netting forex. I'm sitting here waiting...

 
Vladimir Karputov:

I would have moved it a long time ago - but@Ilya Child never said whether he trades on the exchange or just netting forex. I'm sitting here waiting...

Symbol: RTS-3.19 is FORTS

 
prostotrader:

Symbol: RTS-3.19 is FORTS

Ido not understand. There is a stock exchange, there is forex. Everything else is out of the ballpark.

 
Vladimir Karputov:

I don't understand. There is a stock exchange, there is forex. Everything else is out of the ballpark.

FORTS is RTS Futures and Options - the derivatives market section of the Moscow Exchange:)

 
prostotrader:

I see.

Order state: ORDER_STATE_STARTED - THIS CANNOT BE inTRADE_TRANSACTION_DEAL_ADD!

Added

I'm sure you don't (and neither doesIlya Baranov)

Added

There is a "Exchange Trading" section on this forum you'd better communicate there...

I don't quite follow you. Here I have the processing of the transaction.

case TRADE_TRANSACTION_DEAL_ADD:
        {
         drop_info2("TRADE_TRANSACTION_DEAL_ADD\r\n"+TransactionDescription(trans));
         if((trans.deal_type==DEAL_TYPE_BUY || trans.deal_type==DEAL_TYPE_SELL) && trans.order!=0)
           {
            if(getIsDealOfExpert(trans.deal)) //функция проверки принадлежности сделки к роботу
              {
               drop_info2("Сделка наша");
               analyzeFilledOrder(trans.order,trans.volume); //процедура по выставлению отложенных стоп ордеров
              }
           }
        }
      break;

Regarding the status of the order in the transaction. You do realise that I'm not making it up myself. In all deal_add transactions this is the status of the order. Please note that it is a market order and it used to be a pending order.

Now we have another portion of confusion. A deal_add transaction flew in and no position appeared and pending on a non-existent position was placed.

Added.

A Deal_add transaction arrived, but the position did not appear and the pending orders on the non-existing position were placed. Transaction type is Sell, order type is Buy. Although initially the limit was Sell_limit

 
fxsaber:

Limiters themselves, which will require TP/SL, may be partially executed. The TP in the form of limiters is the same. For example, TP is executed by a third of the volume - SL must be reduced by the same amount.

All in all, quite unpleasant logic for catching all the tricks.


The task should be implemented in OnTrade. It should not be too difficult to implement.

Task

We have pending orders on Netting (may be differently directed and any number of each type). Each time the initial pending order triggers, we need to set its SL/TP as a Stop/Limit pending order. Then the SL/TP should be interdependent: once the order triggers, the second one is deleted. The initial and SL/TP pending orders can trigger partially. The Expert Advisor may be reloaded at any time, including transferring to another Terminal.


Solution

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

input int inTP = 100;
input int inSL = 200;
sinput MAGIC_TYPE inMagicNumber = 0;
sinput string inStrKey = "SLTP";

int GetAmountDeals()
{
  return(HistorySelect(0, INT_MAX) ? HistoryDealsTotal() : 0);
}

bool IsMyString( const string Str, const string Key )
{
  return(StringSubstr(Str, 0, StringLen(Key)) == Key);
}

string ToMyString( const string Str, const string Key )
{
  return(Key + Str);
}

struct ORDER
{
  int Type;
  TICKET_TYPE Ticket;
  double Lots;
  double OpenPrice;
  MAGIC_TYPE Magic;
  string Comment;
  
  ORDER() : Type(OrderType()), Ticket(OrderTicket()), Lots(OrderLots()),
            OpenPrice(OrderOpenPrice()), Magic(OrderMagicNumber()), Comment(OrderComment())
  {
  }
};

#define _CS(A) ((!::IsStopped()) && (A))

bool GetPairOrder()
{
  bool Res = false;

  ORDER Order;
  Order.Type = 6 - Order.Type + ((Order.Type & 1) << 1);
  
  for (int i = OrdersTotal() - 1; _CS((i >= 0) && (!Res)); i--)
    Res = OrderSelect(i, SELECT_BY_POS) && (OrderType() == Order.Type) &&
          (OrderMagicNumber() == Order.Magic) && (OrderComment() == Order.Comment);
    
  return(Res);
}

void CheckSLTP( const string Symb, const MAGIC_TYPE Magic, const string Key, const int Slip = 100 )
{    
  for (int i = OrdersTotal() - 1; _CS(i >= 0); i--)
    if (OrderSelect(i, SELECT_BY_POS) && (OrderType() > OP_SELL)  &&
        (OrderMagicNumber() == Magic) && (OrderSymbol() == Symb) && IsMyString(OrderComment(), Key))
    {
      const ORDER Order;      
      
      if (!_CS(GetPairOrder()))
      {
        OrderDelete(Order.Ticket);
        
        i = OrdersTotal();
      }
      else if (_CS(OrderLots() < Order.Lots))
      {
        if (OrderDelete(Order.Ticket))
          OrderSend(OrderSymbol(), Order.Type, OrderLots(), Order.OpenPrice, Slip, 0, 0, Order.Comment, Order.Magic);
          
        i = OrdersTotal();          
      }
    }
}

void CheckFill( const string Symb, const MAGIC_TYPE Magic, const string Key, const int SL, const int TP )
{    
  static int PrevDeals = GetAmountDeals();
  
  const double point = SymbolInfoDouble(Symb, SYMBOL_POINT);
  const int NewDeals = GetAmountDeals();
  
  while (_CS(PrevDeals < NewDeals))
  {
    const ulong Ticket = HistoryDealGetTicket(PrevDeals);
    
    if (Ticket && (HistoryDealGetInteger(Ticket, DEAL_MAGIC) == Magic) &&
                  (HistoryDealGetString(Ticket, DEAL_SYMBOL) == Symb) &&
                  !IsMyString(HistoryDealGetString(Ticket, DEAL_COMMENT), Key))
    {
      const double Lots = HistoryDealGetDouble(Ticket, DEAL_VOLUME);
      const double Price = HistoryDealGetDouble(Ticket, DEAL_PRICE);
      const int Type = 1 - (int)HistoryDealGetInteger(Ticket, DEAL_TYPE);
      const double Koef = Type ? -point : point;
      const string Comment = ToMyString((string)Ticket, Key);
      
      if (OrderSend(Symb, Type + OP_BUYLIMIT, Lots, Price - Koef * TP, 0, 0, 0, Comment))
        OrderSend(Symb, Type + OP_BUYSTOP, Lots, Price + Koef * SL, 0, 0, 0, Comment);
    }
    
    PrevDeals++;
  }
}

void System()
{
  CheckFill(_Symbol, inMagicNumber, inStrKey, inSL, inTP);
  CheckSLTP(_Symbol, inMagicNumber, inStrKey);
}

void OnTrade()
{
  System();
}

void OnInit()
{
  OnTrade();
}
 
Илья Ребенок:

I don't quite follow you. Here's my transaction processing


You do not have switch(trans.type)

void OnTradeTransaction( const MqlTradeTransaction &trans,
                         const MqlTradeRequest &request,
                         const MqlTradeResult &result )
{
  switch(trans.type) //<<---- ОТФИЛЬТРОВАТЬ ПО ТИПУ ТРАЗАКЦИИ!!!!!!!!!!!!!!!!!!!!!!
  {
   //А вот здесь уже TRADE_TRANSACTION_DEAL_ADD


  }
}
Reason: