How to start with MQL5 - page 34

 

Type of triggered pending order

Code 'TypePendingOrderTriggered.mq5'

//+------------------------------------------------------------------+
//|                                    TypePendingOrderTriggered.mq5 |
//|                         Copyright © 2017-2021, Vladimir Karputov |
//|                      https://www.mql5.com/en/users/barabashkakvn |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2017-2021, Vladimir Karputov"
#property link      "https://www.mql5.com/en/users/barabashkakvn"
#property version   "1.002"
#property description "What type of a pending order triggered?"
//---
bool bln_find_order=false;                // true -> you should look for a order
ulong ul_find_order=0;                    // order ticket
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(bln_find_order) // true -> you should look for a order
     {
      static long counter=0;
      PrintFormat("Attempt %d, looking for order number %",counter,ul_find_order);
      ResetLastError();
      if(HistoryOrderSelect(ul_find_order))
        {
         long type_order=HistoryOrderGetInteger(ul_find_order,ORDER_TYPE);
         if(type_order==ORDER_TYPE_BUY_LIMIT || type_order==ORDER_TYPE_BUY_STOP || type_order==ORDER_TYPE_SELL_LIMIT ||type_order==ORDER_TYPE_SELL_STOP)
           {
            Print("The pending order ",ul_find_order," is found! Type of order is ",
                  EnumToString((ENUM_ORDER_TYPE)HistoryOrderGetInteger(ul_find_order,ORDER_TYPE)));
            bln_find_order=false;         // true -> you should look for a order
            counter=0;
            return;
           }
         else
           {
            Print("The order ",ul_find_order," is not pending");
            bln_find_order=false;         // true -> you should look for a order
            return;
           }
        }
      else
        {
         Print("Order ",ul_find_order," is not find, error #",GetLastError());
        }
      counter++;
     }
  }
//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
//+------------------------------------------------------------------+
//| TRADE_TRANSACTION_DEAL_*                                         |
//| The following fields in MqlTradeTransaction structure            |
//| are filled for trade transactions related to deals handling      |
//| (TRADE_TRANSACTION_DEAL_ADD, TRADE_TRANSACTION_DEAL_UPDATE       |
//| and TRADE_TRANSACTION_DEAL_DELETE):                              |
//|  •deal - deal ticket;                                            |
//|  •order - order ticket, based on which a deal has been performed;|
//|  •symbol - deal symbol name;                                     |
//|  •type - trade transaction type;                                 |
//|  •deal_type - deal type;                                         |
//|  •price - deal price;                                            |
//|  •price_sl - Stop Loss price (filled, if specified in the order, |
//|  •based on which a deal has been performed);                     |
//|  •price_tp - Take Profit price (filled, if specified             |
//|   in the order, based on which a deal has been performed);       |
//|  •volume - deal volume in lots.                                  |
//|  •position - the ticket of the position that was opened,         |
//|   modified or closed as a result of deal execution.              |
//|  •position_by - the ticket of the opposite position.             |
//|   It is only filled for the out by deals                         |
//|   (closing a position by an opposite one).                       |
//+------------------------------------------------------------------+
//--- get transaction type as enumeration value 
   ENUM_TRADE_TRANSACTION_TYPE type=trans.type;
//--- if transaction is result of addition of the transaction in history
   if(type==TRADE_TRANSACTION_DEAL_ADD)
     {
      bln_find_order=true;                // true -> you should look for a order
      ul_find_order=trans.order;
     }
  }
//+------------------------------------------------------------------+
 
Vladimir Karputov #:

Type of triggered pending order

Code 'TypePendingOrderTriggered.mq5'

Thank you. This is going to be helpful in my current project itself.

 
daengrani # :

Dear Vladimir,


i want to ask about PostitionCloseBy.

Say BuyStop triggered, it closes Sell Position.. Thanks in advance..

Code example: Pending triggered and close opposite

 
Vladimir Karputov #:

Code example: Pending triggered and close opposite

Thank you very much sir. I started learning mql5... Thank you, thank you..

I saw some of your code about DEAL_REASON_TP, DEAL_REASON_SL.

Some not using DEAL_ENTRY_OUT, some use

 

Sir, i want to ask this function, inside Trade Library

and this is sample ea from mql5

 
daengrani #:

Sir, i want to ask this function, inside Trade Library

and this is sample ea from mql5

1. Please insert the code correctly: when editing a message, press the button         Codeand paste your code into the pop-up window.

2. Coloring is available for the inserted code:

   if(iTime(m_symbol.Name(),InpWorkingPeriod,0)==m_last_deal_in) // on one bar - only one deal
      return(true);
   double ma_high[],ma_low[],ma_median[];
   ArraySetAsSeries(ma_high,true);
   ArraySetAsSeries(ma_low,true);
   ArraySetAsSeries(ma_median,true);
   int start_pos=0,count=6;

Coloring is available for the inserted code

 
Vladimir Karputov #:

1. Please insert the code correctly: when editing a message, press the button         and paste your code into the pop-up window.

2. Coloring is available for the inserted code:


Sorry, my bad
 

Dear Vladimir,


I want to ask about using rectangle as level for pending orders. Here is my code :


It opens limit order, but still dont update (modify) when i drag that rectangle.



void OnChartEvent(const int id,         // Event identifier
                  const long& lparam,   // Event parameter of long type
                  const double& dparam, // Event parameter of double type
                  const string& sparam  // Event parameter of string type
                 )
{
//--- the left mouse button has been pressed on the chart
   if(id==CHARTEVENT_OBJECT_CREATE || id==CHARTEVENT_OBJECT_DRAG) {
      if(sparam == sDnRectangle || sparam == sUpRectangle) {
         HandleRect();
      }
      /*
      */
      if(sparam == sUpRectangle) {

         Comment ("Rectangle Low : ", DoubleToString(UpperRectLow, _Digits), "\n",
                  "Rectangle High : ", DoubleToString(UpperRectHigh, _Digits), "\n",
                  "SellLimit Price : ", DoubleToString(ObjectGetDouble(0, sUpRectLabelPrice, OBJPROP_PRICE, 0), _Digits), "\n",
                  "SellLimit SL : ", DoubleToString(ObjectGetDouble(0, sUpRectLabelSL, OBJPROP_PRICE, 0), _Digits)
                 );
      }
      if(sparam == sDnRectangle) {

         Comment ("Rectangle High : ", DoubleToString(LowerRectHigh, _Digits), "\n",
                  "Rectangle Low : ", DoubleToString(LowerRectLow, _Digits), "\n",
                  "BuyLimit Price : ", DoubleToString(ObjectGetDouble(0, sDnRectLabelPrice, OBJPROP_PRICE, 0), _Digits), "\n",
                  "BuyLimit SL : ", DoubleToString(ObjectGetDouble(0, sDnRectLabelSL, OBJPROP_PRICE, 0), _Digits)
                 );
      }
   }
}

If i put HandleRect() function at OnChartEvent, whenever i drag that rectangle, i can see all values update immediately. But OnTick, it doesn't update

Files:
 
daengrani # :

Dear Vladimir,

I want to ask about using rectangle as level for pending orders. Here is my code :

It opens limit order, but still dont update (modify) when i drag that rectangle.


What principle do you use to place a pending order? Which bar do you check?

 
daengrani # :

Dear Vladimir,


I want to ask about using rectangle as level for pending orders. Here is my code :


It opens limit order, but still dont update (modify) when i drag that rectangle.



If i put HandleRect() function at OnChartEvent, whenever i drag that rectangle, i can see all values update immediately. But OnTick, it doesn't update

Don't use 'OnChartEvent' - do your checks in OnTick.