Pending triggered and close opposite

Author: Copyright © 2021, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
Pending triggered and close opposite
ÿþ//+------------------------------------------------------------------+

//|                         Pending triggered and close opposite.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

//|                      https://www.mql5.com/en/users/barabashkakvn |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2021, Vladimir Karputov"

#property link      "https://www.mql5.com/en/users/barabashkakvn"

#property version   "1.000"

//---

#include <Trade\PositionInfo.mqh>

#include <Trade\Trade.mqh>

//---

CPositionInfo  m_position;                   // object of CPositionInfo class

CTrade         m_trade;                      // object of CTrade class

//---

bool     bln_find_order    = false;    // true -> you should look for a order

ulong    ul_find_order     = 0;        // order ticket



bool     m_need_close_buy  = false;    // close all Buy positions

bool     m_need_close_sell = false;    // close all Sell positions

//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

int OnInit()

  {

//---

//---

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Expert deinitialization function                                 |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

//---

  }

//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

  {

   if(m_need_close_buy || m_need_close_sell)

     {

      int count_buys=0,count_sells=0;

      CalculateAllPositions(count_buys,count_sells);

      //--- close all Buy positions

      if(m_need_close_buy)

        {

         if(count_buys>0)

           {

            ClosePositions(POSITION_TYPE_BUY);

            return;

           }

         else

           {

            m_need_close_buy=false;

           }

        }

      //--- close all Sell positions

      if(m_need_close_sell)

        {

         if(count_sells>0)

           {

            ClosePositions(POSITION_TYPE_SELL);

            return;

           }

         else

           {

            m_need_close_sell=false;

           }

        }

     }

//---

   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;

            //---

            if(type_order==ORDER_TYPE_BUY_STOP)

               m_need_close_sell=true;

            if(type_order==ORDER_TYPE_SELL_STOP)

               m_need_close_buy=true;

            //---

            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 && trans.symbol==Symbol())

     {

      bln_find_order=true;                // true -> you should look for a order

      ul_find_order=trans.order;

     }

  }

//+------------------------------------------------------------------+

//| Calculate all positions Buy and Sell                             |

//+------------------------------------------------------------------+

void CalculateAllPositions(int &count_buys,int &count_sells)

  {

   count_buys=0;

   count_sells=0;

   for(int i=PositionsTotal()-1; i>=0; i--)

      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties

         if(m_position.Symbol()==Symbol())

           {

            if(m_position.PositionType()==POSITION_TYPE_BUY)

               count_buys++;

            if(m_position.PositionType()==POSITION_TYPE_SELL)

               count_sells++;

           }

//---

   return;

  }

//+------------------------------------------------------------------+

//| Close positions                                                  |

//+------------------------------------------------------------------+

void ClosePositions(const ENUM_POSITION_TYPE pos_type)

  {

   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions

      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties

         if(m_position.Symbol()==Symbol())

            if(m_position.PositionType()==pos_type)

               m_trade.PositionClose(m_position.Ticket()); // close a position by the specified m_symbol

  }

//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---