//+------------------------------------------------------------------+
//|          Free and OpenSource code under MIT License              |
//|              (https://opensource.org/licenses/MIT)               |
//|                    ButtonTrading_2.08_tienpham005_a.mq4            |
//|                 Copyright 2020 penyuperwira                      |
//|          (https://www.forexfactory.com/penyuperwira)             |
//|                  updated : 26.05.2020                            |
//+------------------------------------------------------------------+
#property copyright "Created by PenyuPerwira https://www.forexfactory.com/showthread.php?t=785645"
#property link      "https://www.forexfactory.com/showthread.php?t=785645"
//#property version   "2.08_tienpham005_a"
#property strict
//string versionName = "EA ButtonTradingV2.08_tienpham005_a";
//+------------------------------------------------------------------+
//|             Expert initialization function                       |
//+------------------------------------------------------------------+
int       magic=0;
int       Slippage=5;
extern string    Mode3="Percentage % lot/TP/SL by balance";
extern bool      ProfitPercen =true;
extern double    ProfitPercent=2.00;
extern bool      StopPercen =true;
extern double    StopPercent=1.00;
extern string ExitBehaviour = "Choose how to manually exit trade";
extern bool CloseProfitOnly=false;
extern bool CloseOnlyCurrentSymbol=true;
//+------------------------------------------------------------------+
//|                Button General Settings                            |
//+------------------------------------------------------------------+
extern string    Setting="Buttons Arrangement";
bool    Corner_RIGHT_TOP=true;
extern  int buttonsHorizontalPosition = 75;
extern  int buttonsVerticalPosition = 20;
extern int ButtonWidth=70;
extern int ButtonHeight=20;
extern int ButtonFontSize=8;
extern color ButtonNet = LimeGreen;
extern color ButtonSingle = Maroon;
extern color ButtonDelete = Blue;
extern color ButtonCloseBuy = Red;
extern color ButtonCloseSell = Red;
extern color ButtonCloseAll = Red;
extern color ButtonFontColor=White;
extern color ButtonOutlineColor=White;
int gap=5; //gap for button
int i;
int ticket;
double Credit;
double Balance;
double balance;
double GetoLots;
double ProfitPercentage;
double StopPercentage;
double StopPipsMath;
double ProfitPipsMath;
double account_type;
int err,Total;
double AE;
double pip;
double bal_2=0;
double Price;
double pt;
//+------------------------------------------------------------------+
//|             Button Initialize                                    |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(magic > 999999999 || magic <0)
     {
      Alert("Magic number input must be between 0 ~ 999999999");
      magic=0;
     }
   account_type=1;
   if(SymbolInfoDouble(Symbol(), SYMBOL_TRADE_CONTRACT_SIZE)==1000)
      account_type=10;
   if(SymbolInfoDouble(Symbol(), SYMBOL_TRADE_CONTRACT_SIZE)==10000)
      account_type=1;
   if(SymbolInfoDouble(Symbol(), SYMBOL_TRADE_CONTRACT_SIZE)==100000)
      account_type=0.1;
//------Balance for Info indicator-------
   if(!GlobalVariableGet("bal_2"+Symbol()))
      GlobalVariableSet("bal_2"+Symbol(),0);
   if(GlobalVariableGet("bal_2"+Symbol())>0)
      bal_2=GlobalVariableGet("bal_2"+Symbol());
//+---------button arangement-------------------------+
   int Button_Horizontal=buttonsHorizontalPosition;
   int Button_Vertical=buttonsVerticalPosition;
   int x=Button_Horizontal;
   int y=Button_Vertical;
   int x2=Button_Horizontal;
   int y2=gap+ButtonHeight+Button_Vertical;
   int x3=Button_Horizontal;
   int y3=gap*2+ButtonHeight*2+Button_Vertical;

   CreateButton("button4","Close Buy",x,y,ButtonWidth,ButtonHeight,ButtonFontColor,ButtonCloseBuy,ButtonOutlineColor,ButtonFontSize);
   CreateButton("button5","Close Sell",x2,y2,ButtonWidth,ButtonHeight,ButtonFontColor,ButtonCloseSell,ButtonOutlineColor,ButtonFontSize);
   CreateButton("button6","Close All",x3,y3,ButtonWidth,ButtonHeight,ButtonFontColor,ButtonCloseAll,ButtonOutlineColor,ButtonFontSize);

   CreateButton("button1","Net % TP/SL",x,y,ButtonWidth,ButtonHeight,ButtonFontColor,ButtonNet,ButtonOutlineColor,ButtonFontSize);
   CreateButton("button2","Single TP/SL",x2,y2,ButtonWidth,ButtonHeight,ButtonFontColor,ButtonSingle,ButtonOutlineColor,ButtonFontSize);
   CreateButton("button3","Delete TP/SL",x3,y3,ButtonWidth,ButtonHeight,ButtonFontColor,ButtonDelete,ButtonOutlineColor,ButtonFontSize);
   if(Digits==3 || Digits==5)
      pt=10*Point;
   else
      pt=Point;
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   ObjectsDeleteAll(0,OBJ_LABEL);
//---
   DeleteButtons();
  }
//+------------------------------------------------------------------+
//| Expert tick function  (Indicator)                                |
//+------------------------------------------------------------------+
void OnTick()
  {

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CreateButton(string btnName,string btnText,int &x,int y,int w,int h,color clrText,color clrBg,color clrBorder,int fontSize)
  {
   ObjectCreate(0,btnName,OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,btnName,OBJPROP_XDISTANCE,x);
   ObjectSetInteger(0,btnName,OBJPROP_YDISTANCE,y);
   ObjectSetInteger(0,btnName,OBJPROP_XSIZE,w);
   ObjectSetInteger(0,btnName,OBJPROP_YSIZE,h);
   ObjectSetString(0,btnName,OBJPROP_TEXT,btnText);
   ObjectSetInteger(0,btnName,OBJPROP_COLOR,clrText);
   ObjectSetInteger(0,btnName,OBJPROP_BGCOLOR,clrBg);
   ObjectSetInteger(0,btnName,OBJPROP_BORDER_COLOR,clrBorder);
   ObjectSetInteger(0,btnName,OBJPROP_BORDER_TYPE,BORDER_FLAT);
   ObjectSetInteger(0,btnName,OBJPROP_HIDDEN,true);
   ObjectSetInteger(0,btnName,OBJPROP_STATE,false);
   ObjectSetInteger(0,btnName,OBJPROP_FONTSIZE,fontSize);
   ObjectSetInteger(0,btnName,OBJPROP_BACK,false);
   ObjectSetInteger(0,btnName,OBJPROP_CORNER,Corner_RIGHT_TOP);
   x=x+w+10;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void DeleteButtons()
  {
   for(i=ObjectsTotal()-1; i>-1; i--)
     {
      if(StringFind(ObjectName(i),"button")>=0)
         ObjectDelete(ObjectName(i));
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
  {
   if(sparam=="button1") //Net % TP/SL
     {
      Action_Button1();
      ObjectSetInteger(0,"button1",OBJPROP_STATE,false);
     }
   else
      if(sparam=="button2") //Single TP/SL
        {
         Action_Button2();
         ObjectSetInteger(0,"button2",OBJPROP_STATE,false);
        }
      else
         if(sparam=="button3") //Delete TP/SL
           {
            Action_Button3();
            ObjectSetInteger(0,"button3",OBJPROP_STATE,false);
           }
         else
            if(sparam=="button4") // Close All (Buy)
              {
               Action_Button4();
               ObjectSetInteger(0,"button4",OBJPROP_STATE,false);
              }
            else
               if(sparam=="button5") // Close All (Sell)
                 {
                  Action_Button5();
                  ObjectSetInteger(0,"button5",OBJPROP_STATE,false);
                 }
               else
                  if(sparam=="button6") // Close All
                    {
                     Action_Button6();
                     ObjectSetInteger(0,"button6",OBJPROP_STATE,false);
                    }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button1()//Net % TP/SL
  {
   BuyNetTP();
   BuyNetSL();
   SellNetTP();
   SellNetSL();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button2()//Single TP/SL
  {
   BuySingleTP();
   BuySingleSL();
   SellSingleTP();
   SellSingleSL();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button3()//Delete TP/SL
  {
   BuyDeleteTPSL();
   SellDeleteTPSL();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button4()// Close All (Buy)
  {
   CloseBuy();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button5()// Close All (Sell)
  {
   CloseSell();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button6()// Close All
  {
   CloseBuy();
   CloseSell();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void BuyNetTP()
  {
   Total=OrdersTotal();
//+------------------------------------------------------------------+
   double Bprofit=0,BLotsSymbol=0,SL=0,TP=0;
   for(int j=0; j<OrdersTotal(); j++)
     {
      int ticket2=OrderSelect(j,SELECT_BY_POS,MODE_TRADES);
      if(ticket2<0)//----added by tradercapaym
        {
         // Print("CL-849 , OrderSelect Error: ", GetLastError());
        }
      else
        {
         // Print("CL-853 , Order Select Successfully, Ticket # is : " + OrderTicket());
        }
      if(OrderSymbol()==Symbol() && OrderType()==OP_BUY)
        {
         Bprofit+=OrderProfit();
         BLotsSymbol+=OrderLots();
        }
     }
//+------------------------------------------------------------------+
   getpip();
   AE=AccountEquity();
   Credit=AccountCredit();
   Balance=AccountBalance();
   ProfitPercentage=(Balance+Credit) *(ProfitPercent/100);
   ProfitPipsMath=NormalizeDouble((ProfitPercentage-(Bprofit))/((BLotsSymbol)/account_type),0);
   Price=Bid;
   if(ProfitPercen && ProfitPercent>0)
      TP=Price+ProfitPipsMath*pip;
//+------------------------------------------------------------------+
   if(Total>0)
     {
      for(i=Total-1; i>=0; i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
           {
            if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && BLotsSymbol!=0)
              {
               SL = OrderStopLoss();
               int ticket4=OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,OrderExpiration(),CLR_NONE);
              }
           }
        }
     }
   RefreshRates();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void BuyNetSL()
  {
   Total=OrdersTotal();
//+------------------------------------------------------------------+
   double Bprofit=0,BLotsSymbol=0,SL=0,TP=0;
   for(int j=0; j<OrdersTotal(); j++)
     {
      int ticket2=OrderSelect(j,SELECT_BY_POS,MODE_TRADES);
      if(ticket2<0)//----added by tradercapaym
        {
         // Print("CL-849 , OrderSelect Error: ", GetLastError());
        }
      else
        {
         // Print("CL-853 , Order Select Successfully, Ticket # is : " + OrderTicket());
        }
      if(OrderSymbol()==Symbol() && OrderType()==OP_BUY)
        {
         Bprofit+=OrderProfit();
         BLotsSymbol+=OrderLots();
        }
     }
//+------------------------------------------------------------------+
   getpip();
   AE=AccountEquity();
   Credit=AccountCredit();
   Balance=AccountBalance();
   StopPercentage=(Balance+Credit) *(StopPercent/100);
   StopPipsMath=NormalizeDouble((StopPercentage-(Bprofit))/((BLotsSymbol)/account_type),0);
   Price=Bid;
   if(StopPercen && StopPercent  >0)
      SL=Price-StopPipsMath*pip;
//+------------------------------------------------------------------+
   if(Total>0)
     {
      for(i=Total-1; i>=0; i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
           {
            if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && BLotsSymbol!=0)
              {
               TP=OrderTakeProfit();
               int ticket4=OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,OrderExpiration(),CLR_NONE);
              }
           }
        }
     }
   RefreshRates();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SellNetSL()
  {
   Total=OrdersTotal();
//+------------------------------------------------------------------+
   double Sprofit=0,SLotsSymbol=0,SL2=0,TP2=0;
   for(int j=0; j<OrdersTotal(); j++)
     {
      int ticket2=OrderSelect(j,SELECT_BY_POS,MODE_TRADES);
      if(ticket2<0)//----added by tradercapaym
        {
         // Print("CL-849 , OrderSelect Error: ", GetLastError());
        }
      else
        {
         // Print("CL-853 , Order Select Successfully, Ticket # is : " + OrderTicket());
        }
      if(OrderSymbol()==Symbol() && OrderType()==OP_SELL)
        {
         Sprofit+=OrderProfit();
         SLotsSymbol+=OrderLots();
        }
     }
//+------------------------------------------------------------------+
   getpip();
   AE=AccountEquity();
   Credit=AccountCredit();
   Balance=AccountBalance();
   double StopPercentage2=(Balance+Credit) *(StopPercent/100);
   double StopPipsMath2=NormalizeDouble((StopPercentage2-(Sprofit))/((SLotsSymbol)/account_type),0);
   double Price2=Ask;
   if(StopPercen  && StopPercent  >0)
      SL2 = Price2+StopPipsMath2*pip;
//+------------------------------------------------------------------+
   if(Total>0)
     {
      for(i=Total-1; i>=0; i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
           {

            if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && SLotsSymbol!=0)
              {
               TP2 = OrderTakeProfit();
               int ticket5=OrderModify(OrderTicket(),OrderOpenPrice(),SL2,TP2,OrderExpiration(),CLR_NONE);
              }
           }
        }
     }
   RefreshRates();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SellNetTP()
  {
   double Sprofit=0,SLotsSymbol=0,SL2=0,TP2=0;
   Total=OrdersTotal();
//+------------------------------------------------------------------+
   for(int j=0; j<OrdersTotal(); j++)
     {
      int ticket2=OrderSelect(j,SELECT_BY_POS,MODE_TRADES);
      if(ticket2<0)//----added by tradercapaym
        {
         // Print("CL-849 , OrderSelect Error: ", GetLastError());
        }
      else
        {
         // Print("CL-853 , Order Select Successfully, Ticket # is : " + OrderTicket());
        }
      if(OrderSymbol()==Symbol() && OrderType()==OP_SELL)
        {
         Sprofit+=OrderProfit();
         SLotsSymbol+=OrderLots();
        }
     }
//+------------------------------------------------------------------+
   getpip();
   AE=AccountEquity();
   Credit=AccountCredit();
   Balance=AccountBalance();
   double ProfitPercentage2=(Balance+Credit) *(ProfitPercent/100);
   double ProfitPipsMath2=NormalizeDouble((ProfitPercentage2-(Sprofit))/((SLotsSymbol)/account_type),0);
   double Price2=Ask;
   if(ProfitPercen  && ProfitPercent>0)
      TP2 = Price2-ProfitPipsMath2*pip;
//+------------------------------------------------------------------+
   if(Total>0)
     {
      for(i=Total-1; i>=0; i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
           {

            if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && SLotsSymbol!=0)
              {
               SL2 = OrderStopLoss();
               int ticket5=OrderModify(OrderTicket(),OrderOpenPrice(),SL2,TP2,OrderExpiration(),CLR_NONE);
              }
           }
        }
     }
   RefreshRates();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void BuySingleTP()
  {
   Total=OrdersTotal();
   AE=AccountEquity();
//+------------------------------------------------------------------+
   double Bprofit=0,Sprofit=0,BLotsSymbol=0,SLotsSymbol=0,SL=0,TP=0;
   for(int j=0; j<OrdersTotal(); j++)
     {
      int ticket2=OrderSelect(j,SELECT_BY_POS,MODE_TRADES);
      if(ticket2<0)//----added by tradercapaym
        {
         // Print("CL-849 , OrderSelect Error: ", GetLastError());
        }
      else
        {
         // Print("CL-853 , Order Select Successfully, Ticket # is : " + OrderTicket());
        }

      if(OrderSymbol()==Symbol() && OrderType()==OP_BUY)
         BLotsSymbol+=OrderLots();
     }
//+------------------------------------------------------------------+
   getpip();
   Credit=AccountCredit();
   Balance=AccountBalance();
//+------------------------------------------------------------------+
   if(Total>0)
     {
      for(i=Total-1; i>=0; i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
           {
            if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && BLotsSymbol!=0)
              {
               SL=OrderStopLoss();
               ProfitPercentage=(Balance+Credit) *(ProfitPercent/100);
               ProfitPipsMath=NormalizeDouble((ProfitPercentage)/(OrderLots()/account_type),0);
               Price=OrderOpenPrice();
               if(ProfitPercen  && ProfitPercent>0)
                  TP=Price+ProfitPipsMath*pip;
               else
                  TP=0;
               if(Bid < TP)
                 {
                  int ticket4=OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,OrderExpiration(),CLR_NONE);
                 }
              }
           }
        }
     }
   RefreshRates();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void BuySingleSL()
  {
//double SL,TP;//,SL2,TP2;
   Total=OrdersTotal();
//+------------------------------------------------------------------+
   double Bprofit=0,Sprofit=0,BLotsSymbol=0,SLotsSymbol=0,SL=0,TP=0;
   for(int j=0; j<OrdersTotal(); j++)
     {
      int ticket2=OrderSelect(j,SELECT_BY_POS,MODE_TRADES);
      if(ticket2<0)//----added by tradercapaym
        {
         // Print("CL-849 , OrderSelect Error: ", GetLastError());
        }
      else
        {
         // Print("CL-853 , Order Select Successfully, Ticket # is : " + OrderTicket());
        }

      if(OrderSymbol()==Symbol() && OrderType()==OP_BUY)
         BLotsSymbol+=OrderLots();
     }
//+------------------------------------------------------------------+
   getpip();
   AE=AccountEquity();
   Credit=AccountCredit();
   Balance=AccountBalance();
//+------------------------------------------------------------------+
   if(Total>0)
     {
      for(i=Total-1; i>=0; i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
           {
            if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && BLotsSymbol!=0)
              {
               TP=OrderTakeProfit();
               StopPercentage=(Balance+Credit) *(StopPercent/100);
               StopPipsMath=NormalizeDouble((StopPercentage)/(OrderLots()/account_type),0);
               Price=OrderOpenPrice();
               if(StopPercen    && StopPercent  >0)
                  SL=Price-StopPipsMath*pip;
               else
                  SL=0; //buy trade
               if(Bid > SL)
                 {
                  int ticket4=OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,OrderExpiration(),CLR_NONE);
                 }
              }
           }
        }
     }
   RefreshRates();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SellSingleTP()
  {
   Total=OrdersTotal();
//+------------------------------------------------------------------+
   double Bprofit=0,Sprofit=0,BLotsSymbol=0,SLotsSymbol=0,SL2=0,TP2=0;
   for(int j=0; j<OrdersTotal(); j++)
     {
      int ticket2=OrderSelect(j,SELECT_BY_POS,MODE_TRADES);
      if(ticket2<0)//----added by tradercapaym
        {
         // Print("CL-849 , OrderSelect Error: ", GetLastError());
        }
      else
        {
         // Print("CL-853 , Order Select Successfully, Ticket # is : " + OrderTicket());
        }
      if(OrderSymbol()==Symbol() && OrderType()==OP_SELL)
         SLotsSymbol+=OrderLots();
     }
//+------------------------------------------------------------------+
   getpip();
   AE=AccountEquity();
   Credit=AccountCredit();
   Balance=AccountBalance();
//+------------------------------------------------------------------+
   if(Total>0)
     {
      for(i=Total-1; i>=0; i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
           {
            if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && SLotsSymbol!=0)
              {
               SL2=OrderStopLoss();
               double ProfitPercentage2=(Balance+Credit) *(ProfitPercent/100);
               double ProfitPipsMath2=NormalizeDouble((ProfitPercentage2)/(OrderLots()/account_type),0);
               double Price2=OrderOpenPrice();
               if(ProfitPercen  && ProfitPercent>0)
                 {
                  TP2 = Price2-ProfitPipsMath2*pip;
                 }
               if(Ask > TP2)
                 {
                  int ticket5=OrderModify(OrderTicket(),OrderOpenPrice(),SL2,TP2,OrderExpiration(),CLR_NONE);
                 }
              }
           }
        }
     }
   RefreshRates();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SellSingleSL()
  {
//double SL2,TP2;
   Total=OrdersTotal();
//+------------------------------------------------------------------+
   double Bprofit=0,Sprofit=0,BLotsSymbol=0,SLotsSymbol=0,SL2=0,TP2=0;
   for(int j=0; j<OrdersTotal(); j++)
     {
      int ticket2=OrderSelect(j,SELECT_BY_POS,MODE_TRADES);
      if(ticket2<0)//----added by tradercapaym
        {
         // Print("CL-849 , OrderSelect Error: ", GetLastError());
        }
      else
        {
         // Print("CL-853 , Order Select Successfully, Ticket # is : " + OrderTicket());
        }
      if(OrderSymbol()==Symbol() && OrderType()==OP_SELL)
         SLotsSymbol+=OrderLots();
     }
//+------------------------------------------------------------------+
   getpip();
   AE=AccountEquity();
   Credit=AccountCredit();
   Balance=AccountBalance();
//+------------------------------------------------------------------+
   if(Total>0)
     {
      for(i=Total-1; i>=0; i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
           {
            if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && SLotsSymbol!=0)
              {
               TP2=OrderTakeProfit();
               double StopPercentage2=(Balance+Credit) *(StopPercent/100);
               double StopPipsMath2=NormalizeDouble((StopPercentage2)/(OrderLots()/account_type),0);
               double Price2=OrderOpenPrice();
               if(StopPercen    && StopPercent  >0)
                 {
                  SL2 = Price2+StopPipsMath2*pip;
                 }
               if(Ask < SL2)
                 {
                  int ticket5=OrderModify(OrderTicket(),OrderOpenPrice(),SL2,TP2,OrderExpiration(),CLR_NONE);
                 }
              }
           }
        }
     }
   RefreshRates();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void BuyDeleteTPSL()
  {
   Total=OrdersTotal();
//+------------------------------------------------------------------+
   if(Total>0)
     {
      for(i=Total-1; i>=0; i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
           {
            if(OrderSymbol()==Symbol() && OrderType()==OP_BUY)
              {
               int ticket4=OrderModify(OrderTicket(),OrderOpenPrice(),0,0,OrderExpiration(),CLR_NONE);
              }
           }
        }
     }
   return;
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SellDeleteTPSL()
  {
   Total=OrdersTotal();
//+------------------------------------------------------------------+
   if(Total>0)
     {
      for(i=Total-1; i>=0; i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
           {
            if(OrderSymbol()==Symbol() && OrderType()==OP_SELL)
              {
               int ticket5=OrderModify(OrderTicket(),OrderOpenPrice(),0,0,OrderExpiration(),CLR_NONE);
              }
           }
        }
     }
   return;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CloseBuy()
  {
   bool closed=false;
   for(i=OrdersTotal(); i>=0; i--)
     {
      ticket=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(ticket<0)//----added by tradercapaym
        {
         // Print("CL-849 , OrderSelect Error: ", GetLastError());
        }
      else
        {
         // Print("CL-853 , Order Select Successfully, Ticket # is : " + OrderTicket());
        }
      while(IsTradeContextBusy())
         Sleep(100);
      RefreshRates();
      if(!CloseOnlyCurrentSymbol)
        {
         if(OrderType()==OP_BUY && !CloseProfitOnly)
           {
            closed=OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,White);
           }
         else
            if(OrderType()==OP_BUY && CloseProfitOnly && OrderProfit()>0)
              {
               closed=OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,White);
              }
        }
      else
        {
         if(OrderType()==OP_BUY && Symbol()==OrderSymbol() && !CloseProfitOnly)
           {
            closed=OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,White);
           }
         else
            if(OrderType()==OP_BUY && Symbol()==OrderSymbol() && CloseProfitOnly && OrderProfit()>0)
              {
               closed=OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,White);
              }
        }
     }
   return;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CloseSell()
  {
   bool closed=false;
   for(i=OrdersTotal(); i>=0; i--)
     {
      ticket=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(ticket<0)//----added by tradercapaym
        {
         // Print("CL-849 , OrderSelect Error: ", GetLastError());
        }
      else
        {
         // Print("CL-853 , Order Select Successfully, Ticket # is : " + OrderTicket());
        }
      while(IsTradeContextBusy())
         Sleep(100);
      RefreshRates();
      if(!CloseOnlyCurrentSymbol)
        {
         if(OrderType()==OP_SELL && !CloseProfitOnly)
           {
            closed=OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,White);
           }
         else
            if(OrderType()==OP_SELL && CloseProfitOnly && OrderProfit()>0)
              {
               closed=OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,White);
              }
        }
      else
        {
         if(OrderType()==OP_SELL && Symbol()==OrderSymbol() && !CloseProfitOnly)
           {
            closed=OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,White);
           }
         else
            if(OrderType()==OP_SELL && Symbol()==OrderSymbol() && CloseProfitOnly && OrderProfit()>0)
              {
               closed=OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,White);
              }
        }
     }
   return;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void getpip()
  {
   if(Digits==2 || Digits==4)
     {
      pip=Point;
     }
   if(Digits==3 || Digits==5)
     {
      pip = 10*Point;
     }
   if(Digits==6)
     {
      pip = 100*Point;
     }
  }
//+------------------------------------------------------------------+
