OnTradeTransaction - page 4

 
Andrey Dik:

I have never handled the OnTradeTransaction () event before, there was no need to do so as there was no need to consider what triggered in the trading logic. SL or TP. Are you suggesting that this is evidence of my trading experience?)))

I'm reading very carefully, but I don't see anyone giving a sensible answer in the form of a working code. Or is it a great fucking secret and a great wizardry - the secret knowledge and the ability to determine what works?

I do not write EAs to order, especially for FOREX.

You, now, have all the necessary knowledge to write a sensible code,

that meets your needs.

 
prostotrader:

I don't write EAs to order, especially for FOREX.

You now have all the knowledge you need to write a sensible code,

that meets your needs.

And who asked you to write to order?

To ask a question on this forum nowadays means to write to order?

Go to hell, all of you. You are sellers.

 
Andrey Dik:

...

And if you look at the account history to see the commentary of the last trade, would that be OK?

Something like that:

//+------------------------------------------------------------------+
//| Возвращает причину закрытия позиции по Take Profit               |
//+------------------------------------------------------------------+
bool CAccountHistory::IsClosedByTakeProfit(const string symbol)
  {
//--- Получим комментарий последней сделки на указанном символе
   string last_comment=LastDealComment(symbol);
//--- Если в комментарии есть строка "tp"
   if(StringFind(last_comment,"tp",0)>-1)
      return(true);
//--- Если нет строки "tp"
   return(false);
  }
//+------------------------------------------------------------------+
//| Возвращает причину закрытия позиции по Stop Loss                 |
//+------------------------------------------------------------------+
bool CAccountHistory::IsClosedByStopLoss(const string symbol)
  {
//--- Получим комментарий последней сделки на указанном символе
   string last_comment=LastDealComment(symbol);
//--- Если в комментарии есть строка "sl"
   if(StringFind(last_comment,"sl",0)>-1)
      return(true);
//--- Если нет строки "sl"
   return(false);
  }
//+------------------------------------------------------------------+
//| Возвращает комментарий последней сделки на указанном символе     |
//+------------------------------------------------------------------+
string CAccountHistory::LastDealComment(const string symbol)
  {
   int    total_deals  =0;  // Всего сделок в списке выбранной истории
   string deal_symbol  =""; // Символ сделки
   string deal_comment =""; // Комментарий сделки
//--- Если история сделок получена
   if(HistorySelect(0,TimeCurrent()))
     {
      //--- Получим количество сделок в полученном списке
      total_deals=HistoryDealsTotal();
      //--- Пройдемся по всем сделкам в полученном списке от последней сделки к первой
      for(int i=total_deals-1; i>=0; i--)
        {
         //--- Получим комментарий сделки
         deal_comment=HistoryDealGetString(HistoryDealGetTicket(i),DEAL_COMMENT);
         //--- Получим символ сделки
         deal_symbol=HistoryDealGetString(HistoryDealGetTicket(i),DEAL_SYMBOL);
         //--- Если символ сделки и текущий символ равны, остановим цикл
         if(deal_symbol==symbol)
            break;
        }
     }
//---
   return(deal_comment);
  }
 
Anatoli Kazharski:

And if you look at the account history to see the commentary of the last transaction, would that be OK?

Something like that:

Thank you kind man!

Probably the most reliable way, given that SL and TP can slip and comparing prices would be useless.

 
Anatoli Kazharski:

And if you look at the account history to see the commentary of the last transaction, would that be OK?

Something like that:


And if there is no commentary (which is entirely possible)?

 
Andrey Dik:

This is probably the most reliable way, given that SL and TP can slip and a price comparison would be useless.

Nah, this method is bad
   static int GetOrderType(const ulong OrderTicket)
     {
      int OrderType=(int)::HistoryOrderGetInteger(OrderTicket,ORDER_TYPE);

      if((OrderType==ORDER_TYPE_BUY) || (OrderType==ORDER_TYPE_SELL))
        {
         const string OrderComment=HistoryOrderGetString(OrderTicket,ORDER_COMMENT);
         const string OrderPrice=::DoubleToString(::HistoryOrderGetDouble(OrderTicket,ORDER_PRICE_OPEN),
                                                  (int)::SymbolInfoInteger(HistoryOrderGetString(OrderTicket,ORDER_SYMBOL),SYMBOL_DIGITS));

         if(OrderComment=="tp "+OrderPrice)
            OrderType=ORDER_TYPE_TAKEPROFIT;
         else if(OrderComment=="sl "+OrderPrice)
            OrderType=ORDER_TYPE_STOPLOSS;
        }

      return(OrderType);
     }
 
prostotrader:

What if there is no comment (which is entirely possible)?

Quite possible, of course. I use this one for analyzing on the history in the tester, as the easiest and fastest one.

In this case, I have to make and analyze tickets with pending orders. I do not have a ready example at hand.

 
An old problem

closing a position .


This is not indiscriminate accusation, but the result of hours of trying to find out (no HistorySelectByPosition and other stuff helps) how everything works. And I'm happy to apologise if I'm wrong. Not to be unfounded, I am showing an Expert Advisor for the tester (it is easier to understand) on the server RoboForexEU-MetaTrader 5, which opens a position, then puts SL and TP levels.

void OnTick()
{
  static bool Flag = true;

  if (Flag)
  {
    // Открываем SELL-позицию
    MqlTradeRequest Request = {0};

    Request.action = TRADE_ACTION_DEAL;

    Request.symbol = Symbol();
    Request.volume = 1;
    Request.price = SymbolInfoDouble(Symbol(), SYMBOL_BID);

    Request.type = ORDER_TYPE_SELL;

    MqlTradeResult Result;

    if (OrderSend(Request, Result))
    {
      // Устанавливаем SL и TP
      Request.position = Result.deal;

      Request.action = TRADE_ACTION_SLTP;

      Request.tp = Result.ask - 10 * _Point;
      Request.sl = Result.ask + 10 * _Point;

      if (OrderSend(Request, Result))
        Print("Сделка в тестере закроется либо по SL, TP, либо по окончании бэктеста")    ;

      Flag = false;
    }
  }
}

In this EA, SL and TP of a single closed position cannot be defined (in OnDeinit). Is it supposed to do that?

Who can solve - define SL and TP of a closed position?
 
Anatoli Kazharski:

Quite possible, of course. I use this one to analyse the history in the tester, as the easiest and fastest.

And so with pending orders you need to do and analyse tickets. I do not have a ready-made example at hand.

What did not suit you about this method?

case TRADE_TRANSACTION_DEAL_ADD:
  if (trans.order != my_order_ticket)
  {
   //Сработал SL или TP
  }
break;
 
prostotrader:

What's wrong with this method?

case TRADE_TRANSACTION_DEAL_ADD:
  if (trans.order != my_order_ticket)
  {
   //Сработал SL или TP
  }
break;
Yeah, it's more or less like that. Just haven't tested it that way yet.