OnTradeTransaction processing - page 9

 
JRandomTrader:

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

I should add that the task in the first post was even easier - we needed to implement operation of a grid of limit orders where each node of the grid closes its SL/TP.

For example, only one such unidirectional EA works in an account and no one else does. But we are talking about imaginary positions here as well.


And the issue raised is not just about netting. The same can happen in hedging. There are several Expert Advisors in hedging, and someone just closed several positions by CloseBy. If Expert Advisors are implemented through imaginary positions, then such a collapsing will not break the logic.


Generally, this can be solved through a virtual trading environment. It can be solved easily. That's how algorithmic offices run their TS portfolios on the exchanges.

 
Alexey Viktorov:

To simplify things, yes. Agreed.

At this point, the "Transport Manager" hasn't completely clarified the problem and his actions.

What do you mean by stop orders? For the total position or just the part that this EA deals with???

Of course, for a part of a position with which our EA is working, the stop orders are pending sell/buy stop and sell/buy limit.

So far, I have realized that it would be better to move to OnTrade due to a great number of pitfalls of OnTradeTransactions.

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

Of course, stop orders are pending sell/buy stop and sell/buy limit for a part of a position that is handled by a specific EA.

So far, I have understood that it is better to go for OnTrade due to a great number of pitfalls of OnTradeTransactions.

Only pay attention that they work together. I don't remember which is the first and which is the second. But OnTradeTransactions has at least some information without any additional code, while OnTrade has to get everything by force. First of all we have to determine which event triggered the function...

 

I have written a transaction catcher - it may be useful to determine what and from where (although there is simple processing depending on transaction type) - there is no reference to order and transaction history (e.g. to print the Expert Advisor ID)

//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
//--- get transaction type as enumeration value 
   ENUM_TRADE_TRANSACTION_TYPE trans_type=trans.type;
   if(trans_type==TRADE_TRANSACTION_ORDER_ADD || trans_type==TRADE_TRANSACTION_ORDER_UPDATE || trans_type==TRADE_TRANSACTION_ORDER_DELETE ||
      trans_type==TRADE_TRANSACTION_HISTORY_ADD || trans_type==TRADE_TRANSACTION_HISTORY_UPDATE || trans_type==TRADE_TRANSACTION_HISTORY_DELETE)
     {
      Print("---");
      Print(EnumToString(trans_type));
      PrintFormat("%-15s: %d","order",trans.order);
      PrintFormat("%-15s: %s","symbol",trans.symbol);
      PrintFormat("%-15s: %s","order_type",EnumToString(trans.order_type));
      PrintFormat("%-15s: %s","orders_state",EnumToString(trans.order_state));
      PrintFormat("%-15s: %s","time_type",EnumToString(trans.time_type));
      if(trans.time_type==ORDER_TIME_SPECIFIED || trans.time_type==ORDER_TIME_SPECIFIED_DAY)
        {
         PrintFormat("%-15s: %s","time_expiration",TimeToString(trans.time_expiration,TIME_DATE|TIME_SECONDS));
        }
      else
        {
         PrintFormat("%-15s: %s","time_expiration","---");
        }
      PrintFormat("%-15s: %.8f","price",trans.price);
      if(trans.order_type==ORDER_TYPE_BUY_STOP_LIMIT || trans.order_type==ORDER_TYPE_SELL_STOP_LIMIT)
        {
         PrintFormat("%-15s: %.2f","price_trigger",trans.price_trigger);
        }
      else
        {
         PrintFormat("%-15s: %s","price_trigger","---");
        }
      PrintFormat("%-15s: %.8f","price_sl",trans.price_sl);
      PrintFormat("%-15s: %.8f","price_tp",trans.price_tp);
      PrintFormat("%-15s: %.2f","volume",trans.volume);
      if(trans_type!=TRADE_TRANSACTION_ORDER_ADD && (trans.order_type==ORDER_TYPE_BUY || trans.order_type==ORDER_TYPE_SELL))
        {
         PrintFormat("%-15s: %d","position",trans.position);
        }
      else
        {
         PrintFormat("%-15s: %s","position","---");
        }
      if(trans.order_type==ORDER_TYPE_CLOSE_BY)
        {
         PrintFormat("%-15s: %d","position_by",trans.position_by);
        }
      else
        {
         PrintFormat("%-15s: %s","position_by","---");
        }
     }
   else if(trans_type==TRADE_TRANSACTION_DEAL_ADD || trans_type==TRADE_TRANSACTION_DEAL_UPDATE || trans_type==TRADE_TRANSACTION_DEAL_DELETE)
     {
      Print("---");
      Print(EnumToString(trans_type));
      PrintFormat("%-15s: %d","deal",trans.deal);
      PrintFormat("%-15s: %d","order",trans.order);
      PrintFormat("%-15s: %s","symbol",trans.symbol);
      PrintFormat("%-15s: %s","deal_type",EnumToString(trans.deal_type));
      PrintFormat("%-15s: %.8f","price",trans.price);
      PrintFormat("%-15s: %.8f","price_sl",trans.price_sl);
      PrintFormat("%-15s: %.8f","price_tp",trans.price_tp);
      PrintFormat("%-15s: %.2f","volume",trans.volume);
      PrintFormat("%-15s: %d","position",trans.position);
      if(trans.order_type==ORDER_TYPE_CLOSE_BY)
        {
         PrintFormat("%-15s: %d","position_by",trans.position_by);
        }
      else
        {
         PrintFormat("%-15s: %s","position_by","---");
        }
     }
   else if(trans_type==TRADE_TRANSACTION_POSITION)
     {
      Print("---");
      Print(EnumToString(trans_type));
      PrintFormat("%-15s: %s","symbol",trans.symbol);
      PrintFormat("%-15s: %s","deal_type",EnumToString(trans.deal_type));
      PrintFormat("%-15s: %.8f","price",trans.price);
      PrintFormat("%-15s: %.8f","price_sl",trans.price_sl);
      PrintFormat("%-15s: %.8f","price_tp",trans.price_tp);
      PrintFormat("%-15s: %.2f","volume",trans.volume);
      PrintFormat("%-15s: %d","position",trans.position);
     }
   else if(trans_type==TRADE_TRANSACTION_REQUEST)
     {
      Print("---");
      Print(EnumToString(trans_type));
     }
  }
 
Илья Ребенок:

fxsaber thanks for the example!

Only it's not really an example, it's a complete solution to the original problem.

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

Of course, stop orders are pending sell/buy stop and sell/buy limit for a part of a position that is handled by a specific EA.

So far, I have understood that it is better to go for OnTrade due to a great number of pitfalls of OnTradeTransactions.

Keep up the good work :)

A little hint MqlTradeTransaction &trans - ONLY the fields are relevant

TRADE_TRANSACTION_DEAL_* FIELDS

For trade transactions related to trade processing (TRADE_TRANSACTION_DEAL_ADD, TRADE_TRANSACTION_DEAL_UPDATE and TRADE_TRANSACTION_DEAL_DELETE) the following fields are filled in MqlTradeTransaction structure

  • deal - trade ticket;
  • order - the order ticket, on the basis of which the trade was executed;
  • symbol - name of a financial instrument in the trade;
  • type - type of trade transaction;
  • deal_type - type of trade;
  • price - price at which the deal was executed;
  • price_sl - price Stop Loss (to be filled if it is specified in the order, on the basis of which the deal is executed);
  • price_tp - price of Take Profit (filled if specified in the order, on the basis of which the deal is executed);
  • volume - volume of a deal in lots.
  • position - ticket of a position opened, modified or closed as a result of execution of a trade.
  • position_by - ticket of the opposite position. Filled only for transactions for the closing of a position by the opposite one (out by).

And look at MqlTradeResult &result.

But now you have 1000 lines of code!

 
prostotrader:

Keep up the good work :)

A little hint MqlTradeTransaction &trans - ONLY the fields are relevant

TRADE_TRANSACTION_DEAL_*.

For trade transactions related to trade processing (TRADE_TRANSACTION_DEAL_ADD, TRADE_TRANSACTION_DEAL_UPDATE and TRADE_TRANSACTION_DEAL_DELETE) the following fields are filled in MqlTradeTransaction structure

  • deal - trade ticket;
  • order - the order ticket, on the basis of which the trade was executed;
  • symbol - name of a financial instrument in the trade;
  • type - type of trade transaction;
  • deal_type - type of trade;
  • price - price at which the deal was executed;
  • price_sl - price Stop Loss (to be filled if it is specified in the order, on the basis of which the deal is executed);
  • price_tp - price of Take Profit (filled if specified in the order, on the basis of which the deal is executed);
  • volume - volume of a deal in lots.
  • position - ticket of a position opened, modified or closed as a result of execution of a trade.
  • position_by - ticket of the opposite position. Filled only for transactions for the closing of a position by the opposite one (out by).

And look at MqlTradeResult &result.

But now you have 1000 lines of code!

Of course, I understand that you are bursting with ego. Experience, everything...

But I came here not for a bit of sarcasm but for help. If you look a bit higher, you'll see that I agreed that I made a mistake in parsing fields that aren't filled in for deal_add.

I would be grateful to you for more constructive help, I can "shit" myself) I accept constructive criticism.

About"And look at MqlTradeResult &result".

According to help, trade transaction result is filled only withTRADE_TRANSACTION_REQUEST and it must be analyzed only with this type of transactions.

 

You have a strange perception.

I wouldn't dream of "shitting on anyone", that's probably your perception of life and you probably use it a lot yourself.

You probably use it a lot yourself.

It's not me, but fxsaber(a), because in almost every topic he pushes his

But he himself does not trade on the real Market, and certainly not on the FORTS!

There are simple and complex solutions for the same problem.

Judging by your question, you are a beginner.

But as the saying goes: "We do not look for easy ways"!

The OnTradeTransaction function has been specifically designed to simplify

"life for programmers in robot development, but the old-timers don't want to use it,

because they are used to OnTrade and have to write a bunch of code to make it work.

I've already told you that the order is the head, not the deal!

What will you do if the volume of the initial order is not 1, but 2, 10?

You set a pending order, and it means you cannot "strictly" control it, much less control the trades!

You cannot "tightly" control it, all the more so if you do any deals! There are a lot of pitfalls with pending orders.

But, you have chosen the easy way, by getting a dubious ready-made solution, instead of trying to

But you have chosen the easy way, instead of trying to figure it out yourself (with hints).

Good luck, I am no longer involved.

 
prostotrader:

You have a strange perception.

I wouldn't dream of "shitting on anyone", that's probably your perception of life and you probably use it a lot yourself.

You probably use it a lot yourself.

It's not me, but fxsaber(a), because in almost every topic he pushes his

But he himself does not trade on the real Market, and certainly not on the FORTS!

There are simple and complex solutions for the same problem.

Judging by your question, you are a beginner.

But as the saying goes: "We do not look for easy ways"!

The OnTradeTransaction function has been specifically designed to make

"life" for programmers in robot development, but it's the old-timers who don't want to use it,

because they are used to OnTrade and have to write a bunch of code to make it work.

I've already told you that the order is the head, not the deal!

What will you do if the volume of the initial order is not 1, but 2, 10?

You set a pending order, and it means you cannot "strictly" control it, much less control the trades!

You cannot "tightly" control it, all the more so if you do any deals! There are a lot of pitfalls with pending orders.

But, you have chosen the easy way, getting a dubious ready-made solution, instead of trying to

But you have chosen the easy way, instead of trying to figure it out yourself (with hints).

Good luck, I am no longer involved.

Let's start with the fact that I don't use ready-made solutions. I don't know what you've made up for me, but I haven't used any of the solutions in the thread (although I've extracted some useful knowledge) and I'm still experimenting and figuring things out. Generally, I've noticed from your posts that you like to second-guess others. No offence.

As for the order volume, it doesn't matter what it is. It is a deal that allows me to judge what volume is really executed. I think it is easier than catching the partial order execution by its appropriate type. In one of the topics, in addition, it was pointed out that only a deal makes you sure that the position has been executed.

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

To begin with, I don't use ready-made solutions. I don't know what you've guessed for me, but I haven't used any of the solutions in the thread (although I've learned some useful knowledge) and I'm still experimenting and figuring it out. Generally, I've noticed from your posts that you like to second-guess others. No offence.

As for the order volume, it doesn't matter what it is. It's a deal that allows me to judge what volume is really executed. I think it is easier than catching the partial order execution by its appropriate type. In one of the topics, it was pointed out that only a deal makes us sure that the position has been executed.

Have you forgotten how to read?

"Good luck, I'm no longer involved."