CloseAllSymbolsByOpposite

Author: Copyright © 2021, Andres
Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
Miscellaneous
It opens Message Boxes to the user
0 Views
0 Downloads
0 Favorites
CloseAllSymbolsByOpposite
ÿþ//+------------------------------------------------------------------+

//|                                    CloseAllSymbolsByOpposite.mq4 |

//|                                         Copyright © 2021, Andres |

//|                           https://www.mql5.com/ru/users/andres74 |

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

#property copyright "Copyright © 2021, Andres"

#property link      "https://www.mql5.com/ru/users/andres74"

#property version   "2.01"

#property strict

#property script_show_inputs



struct SOrder {

	int nTicket;

	int nType;

	double fLots;

	string sSymbol;

};



input int inp_magic  = -1;       // Magic of closed orders (-1 => do not check)



color    gl_clr_arrow[6]={clrBlue,clrRed,clrBlue,clrRed,clrBlue,clrRed};

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

//| Script program start function                                    |

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

void OnStart()

  {

   SOrder DataOrders[];

   int i,count_data=0,count_pairs=1;

   

   while(count_pairs) {

      RefreshRates();

      ArrayResize(DataOrders,OrdersTotal(),OrdersTotal());

      for(i=0,count_data=0; i<OrdersTotal(); i++) {                                          // !>18@05< 40==K5 >1 >@45@0E

         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) {

            if(OrderMagicNumber()!=inp_magic && inp_magic>=0) continue;

            if(OrderType()==OP_BUY || OrderType()==OP_SELL) {

               DataOrders[count_data].nType=OrderType();

               DataOrders[count_data].nTicket=OrderTicket();

               DataOrders[count_data].fLots=OrderLots();

               DataOrders[count_data].sSymbol=OrderSymbol();

               count_data++;

            }

         }

      }

      if(!MarketInfo(Symbol(),MODE_CLOSEBY_ALLOWED)) {

         PrintFormat("CloseBy on %s is not allowed",Symbol());

         break;

      }

      count_pairs=0;

      for(i=count_data-1; i>=0; i--) {

         for(int k=i-1; k>=0; k--) {

            if(DataOrders[i].nType!=DataOrders[k].nType && StringCompare(DataOrders[i].sSymbol,DataOrders[k].sSymbol)==0) {   // A;8 >@45@0 @07=>=0?@02;5==K5

               if(DataOrders[i].fLots <= DataOrders[k].fLots) {

                  if(OrderCloseBy(DataOrders[i].nTicket,DataOrders[k].nTicket,clrYellow)) {  // A;8 ;>B ?5@2>3> >@45@0 <5=LH5 8;8 @025= 2B>@><C

                     count_pairs++;

                     break;

                  } else printf("%s: The 1-st type of \"#%d was closed by #%d\" failed. Error: %d", __FUNCTION__, DataOrders[i].nTicket, DataOrders[k].nTicket, GetLastError());

               } else {

                  if(OrderCloseBy(DataOrders[k].nTicket,DataOrders[i].nTicket,clrYellow)) {  // A;8 ;>B 2B>@>3> >@45@0 <5=LH5 2B>@>3>

                     count_pairs++;

                     break;

                  } else printf("%s: The 2-st type of \"#%d was closed by #%d\" failed. Error: %d", __FUNCTION__, DataOrders[k].nTicket, DataOrders[i].nTicket, GetLastError());

               }   

               break;                                                                        // 0:@KBL ?0@C =5 C40;>AL, 84Q< 8A:0BL 4@C385   

            }

         }

         if(count_pairs) break;                                                              // A;8 70:@K;8 ?0@C, CE>48< =0 =>2K9 ?5@5AGQB >@45@>2

      }

   }   

   for(i=0; i<count_data; i++) ClosePosition(DataOrders[i].nTicket,DataOrders[i].fLots,DataOrders[i].sSymbol);     // >70:@K205< >AB02H85AO >@45@0

  }

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

//| 0:@KB85 ?>78F88                                                 |

//| 0@0<5B@K:                                                       |

//|   ticket  - B8:5B 70:@K205<>9 ?>78F88                            |

//|   lots    - >1JQ< 70:@K205<>9 ?>78F88                            |

//|   symb    - A8<2>; 70:@K205<>9 ?>78F88                           |

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

bool ClosePosition(int ticket, double lots, string symb) {

  double iBid, iAsk;

  bool ans=false;

  if (OrderSelect(ticket, SELECT_BY_TICKET)) {

    while(true) {                            // &8:; 70:@KB8O ?>7.

      if (OrderType()==OP_BUY) {             // B:@KB0 ?>78F8O Buy

        Print (">?KB:0 70:@KBL ",symb," Buy #",ticket,". 6840=85 >B25B0..");

        RefreshRates();                      // 1=>2;5=85 40==KE

        iBid = NormalizeDouble(SymbolInfoDouble(symb,SYMBOL_BID),(int)SymbolInfoInteger(symb,SYMBOL_DIGITS));

        ans=OrderClose(ticket,lots,iBid,10,gl_clr_arrow[0]);

        if (ans==true) {                     // >;CG8;>AL :)

          Print ("0:@KB >@45@ ",symb," Buy #",ticket);

          return(ans);                       // KE>4 87 DC=:F88

        }

        if (Fun_Error(GetLastError())==1)    // 1@01>B:0 >H81>:

          continue;                          // >2B>@=0O ?>?KB:0

        return(ans);                         // KE>4 87 DC=:F88

      }

      if (OrderType()==OP_SELL) {            // B:@KB0 ?>78F8O Sell

        Print (">?KB:0 70:@KBL ",symb," Sell #",ticket,". 6840=85 >B25B0..");

        RefreshRates();                      // 1=>2;5=85 40==KE

        iAsk = NormalizeDouble(SymbolInfoDouble(symb,SYMBOL_ASK),(int)SymbolInfoInteger(symb,SYMBOL_DIGITS));

        ans=OrderClose(ticket,lots,iAsk,10,gl_clr_arrow[1]);

        if (ans==true) {                     // >;CG8;>AL :)

          Print ("0:@KB >@45@ ",symb," Sell #",ticket);

          return(ans);                       // KE>4 87 DC=:F88

        }

        if (Fun_Error(GetLastError())==1)    // 1@01>B:0 >H81>:

          continue;                          // >2B>@=0O ?>?KB:0

        return(ans);                         // KE>4 87 DC=:F88

      }

      break;                                 // KE>4 87 while

    }

  }else return true;

  return(ans);

}

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

//| $C=:F8O >1@01>B:8 >H81>:                                         |

//| 0@0<5B@K:                                                       |

//|   Error     - :>4 >H81:8                                         |

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

int Fun_Error(int Error) {                       // $-8O >1@01>B >H81>:

  switch(Error) {                                // @5>4>;8<K5 >H81:8            

    case  4: Print("">@3>2K9 A5@25@ 70=OB. @>1C5< 5IQ @07..");

      Sleep(3000);                               // @>AB>5 @5H5=85

      return(1);                                 // KE>4 87 DC=:F88

    case  6: Print("5B A2O78 A B>@3>2K< A5@25@><. @>1C5< 5IQ @07..");

      Sleep(3000);                               // @>AB>5 @5H5=85

      return(1);                                 // KE>4 87 DC=:F88

    case 128:Print("AB5: A@>: >6840=8O A>25@H5=8O A45;:8..");

      return(1);                                 // KE>4 87 DC=:F88

    case 129:Print("5?@028;L=0O F5=0. @>1C5< 5IQ @07..");

      RefreshRates();                            // 1=>28< 40==K5

      return(1);                                 // KE>4 87 DC=:F88

    case 135:

    case 138:Print("&5=0 87<5=8;0AL. @>1C5< 5IQ @07..");

      RefreshRates();                            // 1=>28< 40==K5

      return(1);                                 // KE>4 87 DC=:F88

    case 136:Print("5B F5=. 4Q< =>2K9 B8:..");

      while(RefreshRates()==false)               // > =>2>3> B8:0

        Sleep(1);                                // 045@6:0 2 F8:;5

      return(1);                                 // KE>4 87 DC=:F88

    case 137:Print("@>:5@ 70=OB. @>1C5< 5IQ @07..");

      Sleep(3000);                               // @>AB>5 @5H5=85

      return(1);                                 // KE>4 87 DC=:F88

    case 146:Print(">4A8AB5<0 B>@3>2;8 70=OB0. @>1C5< 5IQ..");

      Sleep(500);                                // @>AB>5 @5H5=85

      return(1);                                 // KE>4 87 DC=:F88

      // @8B8G5A:85 >H81:8

    case  2: Print("1I0O >H81:0.");

      return(0);                                 // KE>4 87 DC=:F88

    case  5: Print("!B0@0O 25@A8O B5@<8=0;0.");

      ErrorBox("!B0@0O 25@A8O B5@<8=0;0.\n0;L=59H0O @01>B0 =52>7<>6=0.");

      return(0);                                 // KE>4 87 DC=:F88

    case 64: Print("!G5B 701;>:8@>20=.");

      ErrorBox("!G5B 701;>:8@>20=.\n0;L=59H0O @01>B0 =52>7<>6=0.");

      return(0);                                 // KE>4 87 DC=:F88

    case 133:Print("">@3>2;O 70?@5I5=0.");

      return(0);                                 // KE>4 87 DC=:F88

    case 134:Print("54>AB0B>G=> 45=53 4;O A>25@H5=8O >?5@0F88.");

      return(0);                                 // KE>4 87 DC=:F88

    default: Print(">7=8:;0 >H81:0 #",Error);   // @C385 20@80=BK   

      return(0);                                 // KE>4 87 DC=:F88

  }

}

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

//| K2>48B MessageBox A B@51C5<K< A>>1I5=85<                        |

//| 0@0<5B@K:                                                       |

//|   str   -  B5:AB A>>1I5=8O                                       |

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

void ErrorBox(string str) {

  MessageBox(str, "CloseAllByOpposite "+_Symbol, MB_OK|MB_ICONSTOP);

}

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

Comments