EA_PanelClosingOrders

Author: Developer: Andrey Minaev
Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
0 Views
0 Downloads
0 Favorites
EA_PanelClosingOrders
ÿþ//+-----------------------------------------------------------------------------------------------+

//| Developer:        Andrey Minaev                                                               |

//| Expert Advisor:   EA_PanelClosingOrders                                                       |

//| MQL5:             mql5.com/ru/users/id.scorpion                                               |

//| Mail:             id.scorpion@mail.ru                                                         |

//| Skype:            id.scorpion                                                                 |

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

#property copyright "Developer: Andrey Minaev"

#property link      "http://www.mql5.com/ru/users/id.scorpion"

#property strict



//--- 3;>10;L=K5 ?5@5<5==K5

long   chartID;          // 845=B8D8:0B>@ 3@0D8:0

string buttonName[8];    // =08<5=>20=85 :=>?>:

string buttonText[8];    // B5:AB =0 :=>?:0E

int    buttonYDist[8];   // 48AB0=F8O @0A?>;>65=8O :=>?>: ?> >A8 Y

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

int OnInit(void)

{

   chartID = ChartID();

   //--- =08<5=>20=85 :=>?>:

   buttonName[0] = "CloseAll";

   buttonName[1] = "CloseAllLoss";

   buttonName[2] = "CloseAllProfit";

   buttonName[3] = "DeleteAll";

   buttonName[4] = "DeleteBuyStop";

   buttonName[5] = "DeleteSellStop";

   buttonName[6] = "DeleteBuyLimit";

   buttonName[7] = "DeleteSellLimit";

   //--- B5:AB =0 :=>?:0E

   buttonText[0] = "0:@KBL 2A5 >@45@0";

   buttonText[1] = "0:@KBL 2A5 C1KB>G=K5";

   buttonText[2] = "0:@KBL 2A5 ?@81K;L=K5";

   buttonText[3] = "#40;8BL 2A5 >B;>65==K5";

   buttonText[4] = "#40;8BL BuyStop";

   buttonText[5] = "#40;8BL SellStop";

   buttonText[6] = "#40;8BL BuyLimit";

   buttonText[7] = "#40;8BL SellLimit";

   //--- 48AB0=F8O ?> >A8 Y

   buttonYDist[0] = 30;

   buttonYDist[1] = 65;

   buttonYDist[2] = 100;

   buttonYDist[3] = 145;

   buttonYDist[4] = 180;

   buttonYDist[5] = 215;

   buttonYDist[6] = 250;

   buttonYDist[7] = 285;

   //--- A>74048< :=>?:8

   for(int i = 0; i < 8; i++)

      CreateButtons(buttonName[i], buttonText[i], 200, buttonYDist[i]);

   //--- A>74048< B09<5@

   EventSetTimer(1);

   

   return INIT_SUCCEEDED;

}

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

void OnDeinit(const int reason)

{

   //--- C40;8< :=>?:8

   for(int i = 0; i < 8; i++)

      ObjectDelete(buttonName[i]);

   //--- C40;8< B09<5@

   EventKillTimer();

}

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

void OnTimer()

{

   //--- ?@>25@8< A>AB>O=85 :=>?>:

   if(ObjectGetInteger(chartID, buttonName[0], OBJPROP_STATE))

   {

      CloseMarketOrders("All");

      ObjectSetInteger(chartID, buttonName[0], OBJPROP_STATE, false);

   }

   if(ObjectGetInteger(chartID, buttonName[1], OBJPROP_STATE))

   {

      CloseMarketOrders("Loss");

      ObjectSetInteger(chartID, buttonName[1], OBJPROP_STATE, false);

   }

   if(ObjectGetInteger(chartID, buttonName[2], OBJPROP_STATE))

   {

      CloseMarketOrders("Profit");

      ObjectSetInteger(chartID, buttonName[2], OBJPROP_STATE, false);

   }

   if(ObjectGetInteger(chartID, buttonName[3], OBJPROP_STATE))

   {

      DeletePendingOrders();

      ObjectSetInteger(chartID, buttonName[3], OBJPROP_STATE, false);

   }

   if(ObjectGetInteger(chartID, buttonName[4], OBJPROP_STATE))

   {

      DeletePendingOrders(OP_BUYSTOP);

      ObjectSetInteger(chartID, buttonName[4], OBJPROP_STATE, false);

   }

   if(ObjectGetInteger(chartID, buttonName[5], OBJPROP_STATE))

   {

      DeletePendingOrders(OP_SELLSTOP);

      ObjectSetInteger(chartID, buttonName[5], OBJPROP_STATE, false);

   }

   if(ObjectGetInteger(chartID, buttonName[6], OBJPROP_STATE))

   {

      DeletePendingOrders(OP_BUYLIMIT);

      ObjectSetInteger(chartID, buttonName[6], OBJPROP_STATE, false);

   }

   if(ObjectGetInteger(chartID, buttonName[7], OBJPROP_STATE))

   {

      DeletePendingOrders(OP_SELLLIMIT);

      ObjectSetInteger(chartID, buttonName[7], OBJPROP_STATE, false);

   }

}

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

//| $C=:F8O C40;O5B 2A5 >B;>65==K5 >@45@0                                                         |

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

void DeletePendingOrders(void)

{

   while(true)

   {

      for(int i = 0; i < OrdersTotal(); i++)

         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

            if(OrderSymbol() == _Symbol && OrderType() > 1)

               if(OrderDelete(OrderTicket())) continue;

      //--- ?@>25@8< =0 =0;8G85 >B;>65==KE >@45@>2

      if(!CheckPendingOrders()) return;

   }

}

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

//| $C=:F8O C40;O5B >B;>65==K5 >@45@0 ?> B8?C >@45@0                                              |

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

void DeletePendingOrders(int type)

{

   while(true)

   {

      for(int i = 0; i < OrdersTotal(); i++)

         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

            if(OrderSymbol() == _Symbol && OrderType() == type)

               if(OrderDelete(OrderTicket())) continue;

      //--- ?@>25@8< =0 =0;8G85 >B;>65==KE >@45@>2 ?> B8?C >@45@0

      if(!CheckPendingOrders(type)) return;

   }

}

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

//| $C=:F8O ?@>25@O5B =0 =0;8G85 >B;>65==KE >@45@>2                                               |

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

bool CheckPendingOrders(void)

{

   for(int i = 0; i < OrdersTotal(); i++)

      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

         if(OrderSymbol() == _Symbol && OrderType() > 1) return true;

   //--- =5B >B;>65==KE >@45@>2

   return false;

}

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

//| $C=:F8O ?@>25@O5B =0 =0;8G85 >B;>65==KE >@45@>2 ?> B8?C >@45@0                                |

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

bool CheckPendingOrders(int type)

{

   for(int i = 0; i < OrdersTotal(); i++)

      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

         if(OrderSymbol() == _Symbol && OrderType() > 1) return true;

   //--- =5B >B;>65==KE >@45@>2

   return false;

}

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

//| $C=:F8O 70:@K205B >B:@KBK5 ?>78F88                                                            |

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

void CloseMarketOrders(string type)

{

   while(true)

   {

      //--- 70:@>5< 2A5 >B:@KBK5 ?>78F88

      if(type == "All")

         for(int i = 0; i < OrdersTotal(); i++)

            if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

               if(OrderSymbol() == _Symbol && OrderType() < 2)

               {

                  if(OrderType() == OP_BUY)  if(OrderClose(OrderTicket(), OrderLots(), Bid, 0)) continue;

                  if(OrderType() == OP_SELL) if(OrderClose(OrderTicket(), OrderLots(), Ask, 0)) continue;

               }

      //--- ?@>25@8< =0 =0;8G85 >B:@KBKE ?>78F89

      if(!CheckOpenPosition()) return;

   }

   while(true)

   {

      //--- 70:@>5< 2A5 C1KB>G=K5 >B:@KBK5 ?>78F88

      if(type == "Loss")

         for(int i = 0; i < OrdersTotal(); i++)

            if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

               if(OrderSymbol() == _Symbol && OrderType() < 2 && OrderProfit() < 0)

               {

                  if(OrderType() == OP_BUY)  if(OrderClose(OrderTicket(), OrderLots(), Bid, 0)) continue;

                  if(OrderType() == OP_SELL) if(OrderClose(OrderTicket(), OrderLots(), Ask, 0)) continue;

               }

      //--- ?@>25@8< =0 =0;8G85 C1KB>G=KE >B:@KBKE ?>78F89

      if(!CheckOpenPosition(type)) return;

   }

   while(true)

   {

      //--- 70:@>5< 2A5 ?@81K;L=K5 >B:@KBK5 ?>78F88

      if(type == "Profit")

         for(int i = 0; i < OrdersTotal(); i++)

            if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

               if(OrderSymbol() == _Symbol && OrderType() < 2 && OrderProfit() >= 0)

               {

                  if(OrderType() == OP_BUY)  if(OrderClose(OrderTicket(), OrderLots(), Bid, 0)) continue;

                  if(OrderType() == OP_SELL) if(OrderClose(OrderTicket(), OrderLots(), Ask, 0)) continue;

               }

      //--- ?@>25@8< =0 =0;8G85 ?@81K;L=KE >B:@KBKE ?>78F89

      if(!CheckOpenPosition(type)) return;

   }

}

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

//| $C=:F8O ?@>25@O5B =0 =0;8G85 >B:@KBKE ?>78F89                                                 |

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

bool CheckOpenPosition(void)

{

   for(int i = 0; i < OrdersTotal(); i++)

      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

         if(OrderSymbol() == _Symbol && OrderType() < 2) return true;

   //--- =5B >B:@KBKE ?>78F89

   return false;

}

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

//| $C=:F8O ?@>25@O5B =0 =0;8G85 >B:@KBKE ?>78F89 ?> C1KB:C/?@81K;8                               |

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

bool CheckOpenPosition(string type)

{

   if(type == "Loss")

      for(int i = 0; i < OrdersTotal(); i++)

         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

            if(OrderSymbol() == _Symbol && OrderType() < 2 && OrderProfit() < 0) return true;

   if(type == "Profit")

   for(int i = 0; i < OrdersTotal(); i++)

      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

         if(OrderSymbol() == _Symbol && OrderType() < 2 && OrderProfit() >= 0) return true;

   //--- =5B >B:@KBKE ?>78F89

   return false;

}

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

//| $C=:F8O A>7405B :=>?:8 =0 3@0D8:5                                                             |

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

void CreateButtons(string name, string text, int xDist, int yDist)

{

   ObjectCreate    (chartID, name, OBJ_BUTTON,           0, 0, 0);

   ObjectSetInteger(chartID, name, OBJPROP_CORNER,       CORNER_RIGHT_UPPER);

   ObjectSetInteger(chartID, name, OBJPROP_XDISTANCE,    xDist);

   ObjectSetInteger(chartID, name, OBJPROP_YDISTANCE,    yDist);

   ObjectSetInteger(chartID, name, OBJPROP_XSIZE,        170);

   ObjectSetInteger(chartID, name, OBJPROP_YSIZE,        30);

   ObjectSetInteger(chartID, name, OBJPROP_BGCOLOR,      clrWhiteSmoke);

   ObjectSetInteger(chartID, name, OBJPROP_BORDER_COLOR, clrSteelBlue); 

   ObjectSetInteger(chartID, name, OBJPROP_STATE,        false);

   ObjectSetInteger(chartID, name, OBJPROP_FONTSIZE,     8);

   ObjectSetInteger(chartID, name, OBJPROP_COLOR,        clrDimGray);

   ObjectSetString (chartID, name, OBJPROP_TEXT,         text);

   ObjectSetString (chartID, name, OBJPROP_TOOLTIP,      "\n");

   ObjectSetInteger(chartID, name, OBJPROP_BACK,         false);

   ObjectSetInteger(chartID, name, OBJPROP_SELECTABLE,   false); 

   ObjectSetInteger(chartID, name, OBJPROP_SELECTED,     false); 

   ObjectSetInteger(chartID, name, OBJPROP_HIDDEN,       true);

}

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

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 ---