OnTradeTransaction processing - page 8

 
fxsaber:

Watch this video from 01:35 and 03:35.


Why do I need your homemade stuff? You amaze me. With such knowledge of programming you cannot understand the OnTradeTransaction handler

 
Alexey Viktorov:

Why do I need your homemade things? You amaze me. With such knowledge of programming you cannot understand the OnTradeTransaction handler

It's hard to talk to someone who can't even imagine the task.

 
Alexey Viktorov:

So are there no positions, or are they in inverted commas?

It is quite normal situation when in terms of netting - there is no position (i.e., total position == 0.0).

And from the point of view of two EAs - each has its own open position.

There may be more than one Expert Advisor on a single symbol. Plus, some trades may be done by hands.

 
JRandomTrader:

It is quite normal situation when in terms of netting - there is no position (i.e., total position == 0.0).

And from the point of view of two advisors - each has their own open position.

There may be more than one Expert Advisor on a single symbol. Plus, some trades may be done by hand.

The wife took the money in the nightstand and bought a TV. The husband took the TV, sold it and put the money in the nightstand. In netting terms, there is no TV. And according to your logic, the wife has the TV and the money in the nightstand. So they decide whether to buy another television or to spend all the money got from it on drinks.

Or do they each have a TV? After all, each of them has held it in their hands. Exaggerating.


If one advisor opened a position and the other closed it, it's gone... Forget about them... There is no position.

 
Alexey Viktorov:

The wife took the money in the nightstand, bought a television. Husband took the TV, sold it and put the money in the nightstand. In netting terms, there is no TV. And according to your logic, the wife has the TV and the money in the nightstand. So they decide whether to buy another television or to spend all the money got from it on drinks.

Or did each of them have a television set? After all, every one of them has held one in their hands. I exaggerate.


If one advisor opened a position and the other closed it, it's gone... Forget about them... There is no position.

There's no position.

But as part of their logic - each EA holds a different position. For example, one - "long term" will sit out the loss, while the other - "scalper" will take his own on the counter-trend at the same time.

 
JRandomTrader:

There is no position.

But within the limits of their logic - each EA holds its own position. For example, one - the "long term" will wait out losses, and the other - the "scalper" will go for a counter-trend.

Apparently you are trying to apply the logic of two hedge strategies to netting. The more logical sequence would be as follows.

A scalper on a counter-trend closes a position and sets a limit order at the expected TP price for his imaginary position. And if this limit switch works, the position will be restored in its entirety BUT!!! with a different ticket. So it is not absolutely correct to consider it as a continuation of that closed position, and long-term EA will not be able to determine it as theirs.

Another thing is if the scalper will work with a smaller volume. Then the ticket will remain the same, despite the fact that the opening price will change. In general, do not try to simply transfer the strategy to netting for hedge, nothing good will come out. The analogy will work, but the actions should be different. The specifics of netting should be taken into account.

 
Alexey Viktorov:

Apparently you are trying to apply the logic of two hedge strategies to netting. A more logical sequence would be as follows.

A scalper on a counter-trend will close the position and set a limit order at the expected TP price for his imaginary position. And if this limit switch works, the position will be restored in its entirety BUT!!! with a different ticket. So it is not absolutely correct to consider it as a continuation of that closed position and long-term EA will not be able to determine it as theirs.

It is quite another matter if the scalper works with smaller values. Then the ticket will remain the same, despite the fact that the opening price has changed. In general, do not try to simply transfer the netting strategy to hedge, nothing good will come out. The analogy will work, but the actions should be different. The specifics of netting should be taken into account.

This is just an example of the scalper and the long riser, there may be many EAs on the symbol, and they may have different lots.

"The scalper closes a position on a counter-trend and puts a limit stop at the price of the expected TP for his imaginary position. - But the scalper assumes for himself that he has an "imaginary position".

And the fact that the total position has changed the ticket - so the long term Advisor does not need it, he already knows that he has his own position, which is not related to the total position.

As a result, every EA works irrespectively of who else is working with it on this symbol, without knowing or wishing to know anything about it.

And yes, I'm not trying to transfer strategies for hedge, for I started with MT5, FORTS and netting right away. I just wanted several robots to be able to trade on the symbol and still be able to trade hands without interfering with them.

 

JRandomTrader:

But for himself, the squanderer thinks he has an "imaginary position".

That's what I'm saying, you have to build a different logic for netting. There shouldn't be imaginary positions. I only put it that way because I couldn't find another word to make you understand me. If there is a fill, then the total volume of the position and the new price of the open position is taken into account, if there is a partial close the same and the price and volume is taken into account.

 
Alexey Viktorov:

That's what I'm saying: you have to build a different logic for netting. There should be no imaginary positions. I only said that because I couldn't find another word to make you understand me. If there is a fill, then the total volume of the position and the new opening price is taken into account; if there is a partial close, then the same is true and the total price and volume is taken into account.

Then we will have to establish some interaction between robots and take into account the actions of "neighbours", who are the same today and may be different tomorrow. And it is unclear what benefits this will bring.

An imaginary position is needed from the point of view of the robot's algorithm: if it has opened it, it should close it.

 
JRandomTrader:

Then I have to create some kind of interaction between robots and take into account "neighbours" that are one today and maybe another tomorrow. And it is unclear what benefits this will bring.

An imaginary position is needed from the point of view of the robot's algorithm: if it opened it, it should close it.

To simplify things, yes. I agree.

Then the "Transport Manager" hasn't fully articulated the problem and his actions.

Forum on trading, automated trading systems and strategy testing

OnTradeTransaction processing

Ilya Child, 2019.02.07 20:08

Good evening.

Guys, please help to sort this out. The problem is probably not new, but I haven't found any unambiguous solution (neither in practice, nor on forums).

I am running 2 different robots in the terminal on 2 different instruments. The magics are different everywhere. The robot places pending limits and the procedure OnTradeTransaction allows me to detect a transaction and place pending stop orders using this transaction.

Below is the code for the trade 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;

This is the code of the function that checks whether the deal belongs to a robot

bool getIsDealOfExpert(ulong dealTicket)
     {
      if(HistoryDealSelect(dealTicket) && HistoryDealGetInteger(dealTicket,DEAL_MAGIC)==magic_number && HistoryDealGetString(dealTicket,DEAL_SYMBOL)==symbol)
         return true;
      else
         return false;
     }

This is the code of the procedure for pending stop orders

void analyzeFilledOrder(ulong orderTicket,double volume)
  {
   bool isFindOrder=false;
   string fullComment;
   ENUM_ORDER_TYPE orderType;
   if(getIsOrderOfExpert(orderTicket,true)) //Если ордер из сделки уже в истории
     {
      fullComment=HistoryOrderGetString(orderTicket,ORDER_COMMENT);
      orderType=ENUM_ORDER_TYPE(HistoryOrderGetInteger(orderTicket,ORDER_TYPE));
      isFindOrder=true; //локальная переменная, если нашли в истории
     }
   if(!isFindOrder && getIsOrderOfExpert(orderTicket,false)) //Если не нашли ордер в истории и ордер есть не в истории
     {
      fullComment=OrderGetString(ORDER_COMMENT); 
      orderType=ENUM_ORDER_TYPE(OrderGetInteger(ORDER_TYPE));
      isFindOrder=true; //локальная переменная, если нашли не в истории
     }
   if(isFindOrder) //если хоть где-то нашли, то выставляем отложенные стоп ордера
     {
     //выставляем стоп ордера

This is the code of the function for searching for an order in the history and out of the history

bool getIsOrderOfExpert(ulong OrderTicket,bool isHistory)
     {
      bool is_expert=false;
      //если ордер находится в истории
      if(isHistory)
        {
         if(HistoryOrderSelect(OrderTicket) && HistoryOrderGetInteger(OrderTicket,ORDER_MAGIC)==magic_number && HistoryOrderGetString(OrderTicket,ORDER_SYMBOL)==symbol)
            is_expert=true;
        }
      else
        {
         if(OrderSelect(OrderTicket) && OrderGetInteger(ORDER_MAGIC)==magic_number && OrderGetString(ORDER_SYMBOL)==symbol)
            is_expert=true;
        }
      return is_expert;
     }

I put the information about the incoming transactions in the log in the order they are received in the terminal. Now I have a problem that I have faced when trading on a demo account:

Sometimes transactions come in the following order TRADE_TRANSACTION_ORDER_DELETE, then TRADE_TRANSACTION_DEAL_ADD, then TRADE_TRANSACTION_HISTORY_ADD. In this case stop orders are often not placed after a trade has been executed. I suppose this happens because the order has already been deleted but has not yet been added to the history. It means that we cannot find the order from the deal either in the history or in the terminal. Although it is doubtful, the fact is that no stop order is placed because the robot does not find it after searching for the order in all dimensions(isFindOrder=false). The order of transactions may be correct, but the order is still nowhere to be found. In all cases, the robot detects the transaction correctly, but does not get to placing orders.However, it also occasionally works correctly and orders are placed.

I have tried different approaches, nothing works. I am now thinking of adding a 1-second interval at the beginning of pending order placing procedure. I do not know where else to dig.

Please share your experiences and ideas.

What do stop orders mean? For the total position or just for the part that this Expert Advisor is working with?
Reason: