//+------------------------------------------------------------------+
//|          Free and OpenSource code under MIT License              |
//|              (https://opensource.org/licenses/MIT)               |
//|                    ButtonTrading_2.02.mq4                        |
//|                 Copyright 2018 penyuperwira                      |
//|          (https://www.forexfactory.com/penyuperwira)             |
//|                  updated : 29.11.2018                            |
//+------------------------------------------------------------------+
#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.02"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
extern string info="If 'lots' is set to 0, it will try to use the 'ordersize'";
extern double fixed_lot=0.01;
extern double SLpips = 0;
extern double TPpips = 13;
extern string    Use_MM_lots="IF you want to decrease lot by";
extern string    Enable_MM_Percentage_Lots="set Mode_Lots = [1]Fixed [2]%Equity [3]%Balance ";
extern int      Mode_MM_Lots=3;
extern double    MM_Risk_Percent_Lots=0.25;
int    MM_Lot_Digits_Decimal=2;
extern bool      ProfitPercen=true;
extern double    ProfitPercent=1.00;
extern bool      StopPercen=false;
extern double    StopPercent=5.00;
extern int Button_Horizontal=120;
extern int Button_Vertical=220;
extern bool CloseProfitOnly=true;
extern bool ArrangeButtonVertical=true;
extern bool ButtonNameSimple=true;
int Slippage=5;
double pip;
int digits;
double Credit;
double Balance;
double GetoLots;
double account_type;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(SymbolInfoDouble(Symbol(), SYMBOL_TRADE_CONTRACT_SIZE)==1000) account_type=10; //micro acc.
   if(SymbolInfoDouble(Symbol(), SYMBOL_TRADE_CONTRACT_SIZE)==10000) account_type=1; //mini acc.
   if(SymbolInfoDouble(Symbol(),SYMBOL_TRADE_CONTRACT_SIZE)==100000) account_type=0.1;//standard acc.
//---
   int x=Button_Horizontal;
   int y=Button_Vertical;
   int x2=Button_Horizontal;
   int y2=0+Button_Vertical;
   int x3=Button_Horizontal;
   int y3=80+Button_Vertical;
   int x4=Button_Horizontal;
   int y4=120+Button_Vertical;
   int x5=Button_Horizontal;
   int y5=160+Button_Vertical;
   int x6=Button_Horizontal;
   int y6=200+Button_Vertical;
   int x7=Button_Horizontal;
   int y7=240+Button_Vertical;
//+------------------------------------------------------------------+
   if(!ButtonNameSimple)
     {
      if(!ArrangeButtonVertical)
        {
         CreateButton("button1","Buy",x,y,80,30,Yellow,Green,White,12);
         CreateButton("button2","Buy Net %",x,y,80,30,Yellow,Green,White,12);
         CreateButton("button3","Net%TP/SL(Buy)",x,y,160,30,White,Gray,White,12);
         CreateButton("button4","SingleTP/SL(Buy)",x,y,160,30,White,Gray,White,12);
         CreateButton("button5","DeleteTP/SL(Buy)",x,y,160,30,White,Gray,White,12);
         CreateButton("button6","CloseBuy",x,y,80,30,White,Gray,White,12);

         CreateButton("button7","Hedge",x2,y2+40,80,30,Yellow,Blue,White,12);
         CreateButton("button8","HedgeNet",x2,y2+40,80,30,Yellow,Blue,White,12);
         CreateButton("button9","Net%TP/SL(Hedge)",x2,y2+40,160,30,White,Gray,White,12);
         CreateButton("button10","SingleTP/SL(Hedge)",x2,y2+40,160,30,White,Gray,White,12);
         CreateButton("button11","DeleteTP/SL(Hedge)",x2,y2+40,160,30,White,Gray,White,12);
         CreateButton("button12","Close All",x2,y2+40,80,30,White,Gray,White,12);

         CreateButton("button13","Sell",x3,y3,80,30,Yellow,Maroon,White,12);
         CreateButton("button14","Sell Net %",x3,y3,80,30,Yellow,Maroon,White,12);
         CreateButton("button15","Net%TP/SL(Sell)",x3,y3,160,30,White,Gray,White,12);
         CreateButton("button16","SingleTP/SL(Sell)",x3,y3,160,30,White,Gray,White,12);
         CreateButton("button17","DeleteTP/SL(Sell)",x3,y3,160,30,White,Gray,White,12);
         CreateButton("button18","CloseSell",x3,y3,80,30,White,Gray,White,12);
        }
      else
        {
         CreateButton("button1","Buy",x,y,80,30,Yellow,Green,White,12);
         CreateButton("button7","Hedge",x,y,80,30,Yellow,Blue,White,12);
         CreateButton("button13","Sell",x,y,80,30,Yellow,Maroon,White,12);

         CreateButton("button2","Buy Net %",x2,y2+40,80,30,Yellow,Green,White,12);
         CreateButton("button8","Hedge Net",x2,y2+40,80,30,Yellow,Blue,White,12);
         CreateButton("button14","Sell Net %",x2,y2+40,80,30,Yellow,Maroon,White,12);

         CreateButton("button3","Net%TP/SL(Buy)",x3,y3+0,160,30,White,Gray,White,12);
         CreateButton("button9","Net%TP/SL(Hedge)",x3,y3+0,160,30,White,Gray,White,12);
         CreateButton("button15","Net%TP/SL(Sell)",x3,y3+0,160,30,White,Gray,White,12);

         CreateButton("button4","SingleTP/SL(Buy)",x4,y4+0,160,30,White,Gray,White,12);
         CreateButton("button10","SingleTP/SL(Hedge)",x4,y4+0,160,30,White,Gray,White,12);
         CreateButton("button16","SingleTP/SL(Sell)",x4,y4+0,160,30,White,Gray,White,12);

         CreateButton("button5","DeleteTP/SL(Buy)",x5,y5+0,160,30,White,Gray,White,12);
         CreateButton("button11","DeleteTP/SL(Hedge)",x5,y5+0,160,30,White,Gray,White,12);
         CreateButton("button17","DeleteTP/SL(Sell)",x5,y5+0,160,30,White,Gray,White,12);

         CreateButton("button6","CloseBuy",x6,y6+0,80,30,White,Gray,White,12);
         CreateButton("button12","Close All",x6,y6+0,80,30,White,Gray,White,12);
         CreateButton("button18","CloseSell",x6,y6+0,80,30,White,Gray,White,12);
        }
     }
//+------------------------------------------------------------------+     
   else
     {
      if(!ArrangeButtonVertical)
        {
         CreateButton("button1","Buy",x,y,80,30,Yellow,Green,White,12);//Buy                
         CreateButton("button2","Buy Net %",x,y,80,30,Yellow,Green,White,12);//Buy
         CreateButton("button3","Net % TP",x,y,80,30,White,clrDarkOliveGreen,White,12);//Buy
         CreateButton("button4","Single TP",x,y,80,30,White,clrDarkOliveGreen,White,12);//Buy
         CreateButton("button5","Delete TP",x,y,80,30,White,clrDarkOliveGreen,White,12);//Buy
         CreateButton("button6","Close Buy",x,y,80,30,White,clrRed,White,12);//Buy

         CreateButton("button7","Hedge",x2,y2+40,80,30,Yellow,Blue,White,12);//hedge
         CreateButton("button8","HedgeNet",x2,y2+40,80,30,Yellow,Blue,White,12);//hedge
         CreateButton("button9","Net % TP",x2,y2+40,80,30,White,clrSteelBlue,White,12);//hedge
         CreateButton("button10","Single TP",x2,y2+40,80,30,White,clrSteelBlue,White,12);//hedge
         CreateButton("button11","Delete TP",x2,y2+40,80,30,White,clrSteelBlue,White,12);//hedge
         CreateButton("button12","Close All",x2,y2+40,80,30,White,clrRed,White,12);//hedge

         CreateButton("button13","Sell",x3,y3,80,30,Yellow,Maroon,White,12);
         CreateButton("button14","Sell Net %",x3,y3,80,30,Yellow,Maroon,White,12);
         CreateButton("button15","Net % TP",x3,y3,80,30,White,clrSaddleBrown,White,12);
         CreateButton("button16","Single TP",x3,y3,80,30,White,clrSaddleBrown,White,12);
         CreateButton("button17","Delete TP",x3,y3,80,30,White,clrSaddleBrown,White,12);
         CreateButton("button18","Close Sell",x3,y3,80,30,White,clrRed,White,12);

         CreateButton("button19","EA ButtonTradingV2.02",x4,y4+0,530,30,White,clrGray,White,12);//title box
        }
      else
        {
         CreateButton("button1","Buy",x,y,80,30,Yellow,Green,White,12);//Buy
         CreateButton("button7","Hedge",x,y,80,30,Yellow,Blue,White,12);//hedge
         CreateButton("button13","Sell",x,y,80,30,Yellow,Maroon,White,12);//sell

         CreateButton("button2","Buy Net %",x2,y2+40,80,30,Yellow,Green,White,12);//Buy
         CreateButton("button8","HedgeNet",x2,y2+40,80,30,Yellow,Blue,White,12);//hedge
         CreateButton("button14","Sell Net %",x2,y2+40,80,30,Yellow,Maroon,White,12);//sell

         CreateButton("button3","Net % TP",x3,y3+0,80,30,White,clrDarkOliveGreen,White,12);//Buy;
         CreateButton("button9","Net % TP",x3,y3+0,80,30,White,clrSteelBlue,White,12);//hedge
         CreateButton("button15","Net % TP",x3,y3+0,80,30,White,clrSaddleBrown,White,12);//sell

         CreateButton("button4","Single TP",x4,y4+0,80,30,White,clrDarkOliveGreen,White,12);//Buy
         CreateButton("button10","Single TP",x4,y4+0,80,30,White,clrSteelBlue,White,12);//hedge
         CreateButton("button16","Single TP",x4,y4+0,80,30,White,clrSaddleBrown,White,12);//sell

         CreateButton("button5","Delete TP",x5,y5+0,80,30,White,clrDarkOliveGreen,White,12);//Buy
         CreateButton("button11","Delete TP",x5,y5+0,80,30,White,clrSteelBlue,White,12);//hedge
         CreateButton("button17","Delete TP",x5,y5+0,80,30,White,clrSaddleBrown,White,12);//sell

         CreateButton("button6","Close Buy",x6,y6+0,80,30,White,clrRed,White,12);//Buy
         CreateButton("button12","Close All",x6,y6+0,80,30,White,clrRed,White,12);//hedge
         CreateButton("button18","Close Sell",x6,y6+0,80,30,White,clrRed,White,12);//sell

         CreateButton("button19","EA ButtonTradingV2.02",x7,y7+0,260,30,White,clrGray,White,12);//title box
        }
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   DeleteButtons();
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
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);
   x=x+w+10;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void DeleteButtons()
  {
   for(int 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") // Buy
     {
      Action_Button1();
      ObjectSetInteger(0,"button1",OBJPROP_STATE,false);
     }
   if(sparam=="button2") // Buy Net
     {
      Action_Button2();
      ObjectSetInteger(0,"button2",OBJPROP_STATE,false);
     }
   else if(sparam=="button3") // Net %  TP/Sl (Buy)
     {
      Action_Button3();
      ObjectSetInteger(0,"button3",OBJPROP_STATE,false);
     }
   else if(sparam=="button4") //Single  TP/Sl (Buy)
     {
      Action_Button4();
      ObjectSetInteger(0,"button4",OBJPROP_STATE,false);
     }
   else if(sparam=="button5") // Delete TP/Sl (Buy)
     {
      Action_Button5();
      ObjectSetInteger(0,"button5",OBJPROP_STATE,false);
     }
   else if(sparam=="button6") // Close All (Buy)
     {
      Action_Button6();
      ObjectSetInteger(0,"button6",OBJPROP_STATE,false);
     }

   else if(sparam=="button7") // Hedge
     {
      Action_Button7();
      ObjectSetInteger(0,"button7",OBJPROP_STATE,false);
     }
   else if(sparam=="button8") // Hedge Net
     {
      Action_Button8();
      ObjectSetInteger(0,"button8",OBJPROP_STATE,false);
     }
   else if(sparam=="button9") // Net %  TP/Sl (Hedge)
     {
      Action_Button9();
      ObjectSetInteger(0,"button9",OBJPROP_STATE,false);
     }
   else if(sparam=="button10") //Single  TP/Sl (Hedge)
     {
      Action_Button10();
      ObjectSetInteger(0,"button10",OBJPROP_STATE,false);
     }
   else if(sparam=="button11") // Delete TP/Sl (Hedge)
     {
      Action_Button11();
      ObjectSetInteger(0,"button11",OBJPROP_STATE,false);
     }
   else if(sparam=="button12") // Close All (Hedge)
     {
      Action_Button12();
      ObjectSetInteger(0,"button12",OBJPROP_STATE,false);
     }

   else if(sparam=="button13") // Sell
     {
      Action_Button13();
      ObjectSetInteger(0,"button13",OBJPROP_STATE,false);
     }
   else if(sparam=="button14") // Sell Net
     {
      Action_Button14();
      ObjectSetInteger(0,"button14",OBJPROP_STATE,false);
     }
   else if(sparam=="button15") // Net %  TP/Sl (Sell)
     {
      Action_Button15();
      ObjectSetInteger(0,"button15",OBJPROP_STATE,false);
     }
   else if(sparam=="button16") //Single  TP/Sl (Sell)
     {
      Action_Button16();
      ObjectSetInteger(0,"button16",OBJPROP_STATE,false);
     }

   else if(sparam=="button17") // Delete TP/Sl (Sell)
     {
      Action_Button17();
      ObjectSetInteger(0,"button17",OBJPROP_STATE,false);
     }
   else if(sparam=="button18") // Close All (Sell)
     {
      Action_Button18();
      ObjectSetInteger(0,"button18",OBJPROP_STATE,false);
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button1()
  {
   OrderBuy();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button2()
  {
   OrderBuyNet();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button3()
  {
   BuyNetTP();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button4()
  {
   BuySingleTP();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button5()
  {
   BuyDeleteTP();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button6()
  {
   CloseBuy();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button7()
  {
   OrderBuy();
   OrderSell();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button8()
  {
   OrderBuyNet();
   OrderSellNet();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button9()
  {
   BuyNetTP();
   SellNetTP();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button10()
  {
   BuySingleTP();
   SellSingleTP();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button11()
  {
   BuyDeleteTP();
   SellDeleteTP();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button12()
  {
   CloseBuy();
   CloseSell();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button13()
  {
   OrderSell();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button14()
  {
   OrderSellNet();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button15()
  {
   SellNetTP();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button16()
  {
   SellSingleTP();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button17()
  {
   SellDeleteTP();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button18()
  {
   CloseSell();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OrderBuyNet()
  {
   double SL,TP;
   int i,Total;
   Total=OrdersTotal();
   int err;
   double AE=AccountEquity();
//+------------------------------------------------------------------+
   double Bprofit=0,BLots=0,BLotsSymbol=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(OrderType()==0 && OrderSymbol()==Symbol()) Bprofit+=OrderProfit();
      if(OrderType()==0 && OrderSymbol()==Symbol()) BLotsSymbol+=OrderLots();
     }
//+------------------------------------------------------------------+
   getpip();
   Credit=AccountCredit();
   Balance=AccountBalance();
   GetoLots=GetLots();
   double StopPercentage=(Balance+Credit) *(StopPercent/100);
   double StopPipsMath=NormalizeDouble((StopPercentage-(Bprofit))/((BLotsSymbol+GetoLots)/account_type),0);
   double ProfitPercentage=(Balance+Credit) *(ProfitPercent/100);
   double ProfitPipsMath=NormalizeDouble((ProfitPercentage-(Bprofit))/((BLotsSymbol+GetoLots)/account_type),0);
   if(fixed_lot==0) fixed_lot = GlobalVariableGet("ordersize");
   if(fixed_lot==0)
     {
      Alert("!!! "+WindowExpertName()+" "+Symbol()+": 'ordersize' not defined !!!");
     }
   double SLprice=0;
   double TPprice=0;
   double Price=Bid;
   if(!StopPercen   && SLpips       !=0) SLprice=Price-SLpips*pip;
   if(StopPercen    && StopPercent  !=0) SLprice=Price-StopPipsMath*pip;
   if(!ProfitPercen && TPpips       !=0) TPprice=Price+TPpips*pip;
   if(ProfitPercen  && ProfitPercent!=0) TPprice=Price+ProfitPipsMath*pip;
   int ticket=OrderSend(Symbol(),OP_BUY,GetoLots,Price,30,SLprice,TPprice,"",0,0,Green);
//+------------------------------------------------------------------+
   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)
              {
               if(SLpips>0)SL=SLprice;else SL=0;
               if(TPpips>0)TP=TPprice;else TP=0;
               int ticket3=OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,OrderExpiration(),CLR_NONE);
              }
           }
        }
     }
   if(ticket<0)
     {
      err=GetLastError();

      Alert(Symbol()+":BUY FAILED : ErrorDescription("+IntegerToString(err)+")");
     }
   else
     {
      Alert(Symbol()+":BUY at "+DoubleToStr(Price,Digits)+" TP="+DoubleToStr(TPprice,Digits)+",SL="+DoubleToStr(SLprice,Digits));
     }
   Alert("--- END "+WindowExpertName()+" "+Symbol());
   RefreshRates();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OrderSellNet()
  {
   double SL,TP;
   int i,Total;
   Total=OrdersTotal();
   int err;
   double AE=AccountEquity();
//+------------------------------------------------------------------+
   double Sprofit=0,SLots=0,SLotsSymbol=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(OrderType()==1 && OrderSymbol()==Symbol())Sprofit+=OrderProfit();
      if(OrderType()==1 && OrderSymbol()==Symbol())SLotsSymbol+=OrderLots();
     }
//+------------------------------------------------------------------+
   getpip();
   Credit=AccountCredit();
   Balance=AccountBalance();
   GetoLots=GetLots();
   double StopPercentage=(Balance+Credit) *(StopPercent/100);
   double StopPipsMath=NormalizeDouble((StopPercentage-(Sprofit))/((SLotsSymbol+GetoLots)/account_type),0);
   double ProfitPercentage=(Balance+Credit) *(ProfitPercent/100);
   double ProfitPipsMath=NormalizeDouble((ProfitPercentage-(Sprofit))/((SLotsSymbol+GetoLots)/account_type),0);
   if(fixed_lot==0) fixed_lot = GlobalVariableGet("ordersize");
   if(fixed_lot==0)
     {
      Alert("!!! "+WindowExpertName()+" "+Symbol()+": 'ordersize' not defined !!!");
     }
   double SLprice=0;
   double TPprice=0;
   double Price=Ask;
   if(!StopPercen   && SLpips       !=0) SLprice = Price+SLpips*pip;
   if(StopPercen    && StopPercent  !=0) SLprice = Price+StopPipsMath*pip;
   if(!ProfitPercen && TPpips       !=0) TPprice = Price-TPpips*pip;
   if(ProfitPercen  && ProfitPercent!=0) TPprice = Price-ProfitPipsMath*pip;
   int ticket=OrderSend(Symbol(),OP_SELL,GetoLots,Price,30,SLprice,TPprice,"",0,0,Green);
//+------------------------------------------------------------------+
   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)
              {
               if(SLpips>0)SL=SLprice;else SL=0;
               if(TPpips>0)TP=TPprice;else TP=0;
               int ticket3=OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,OrderExpiration(),CLR_NONE);
              }
           }
        }
     }
//+------------------------------------------------------------------+
   if(ticket<0)
     {
      err=GetLastError();
      Alert(Symbol()+":SELL FAILED : ErrorDescription("+IntegerToString(err)+"");
     }
   else
     {
      Alert(Symbol()+":SELL at "+DoubleToStr(Price,Digits)+" TP="+DoubleToStr(TPprice,Digits)+",SL="+DoubleToStr(SLprice,Digits));
     }

   Alert("--- END "+WindowExpertName()+" "+Symbol());
   RefreshRates();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OrderBuy()
  {
   double SL,TP;
   int err;
   getpip();
   Credit=AccountCredit();
   double StopPercentage=(Balance+Credit) *(StopPercent/100);
   double StopPipsMath=NormalizeDouble((StopPercentage)/(GetLots()/account_type),0);
   double ProfitPercentage=(AccountBalance()+Credit) *(ProfitPercent/100);
   double ProfitPipsMath=NormalizeDouble((ProfitPercentage)/(GetLots()/account_type),0);
   if(fixed_lot==0) fixed_lot = GlobalVariableGet("ordersize");
   if(fixed_lot==0)
     {
      Alert("!!! "+WindowExpertName()+" "+Symbol()+": 'ordersize' not defined !!!");
     }
   double SLprice=0;
   double TPprice=0;
   double Price=Bid;
   if(!StopPercen   && SLpips       !=0) SLprice=Price-SLpips*pip;
   if(StopPercen    && StopPercent  !=0) SLprice=Price-StopPipsMath*pip;
   if(!ProfitPercen && TPpips       !=0) TPprice=Price+TPpips*pip;
   if(ProfitPercen  && ProfitPercent!=0) TPprice=Price+ProfitPipsMath*pip;
   if(StopPercent>0) SL=SLprice;  else SL=0; //buy trade
   if(ProfitPercent>0)TP=TPprice;  else TP=0;
   int ticket=OrderSend(Symbol(),OP_BUY,GetLots(),Price,30,SL,TP,"",0,0,Green);
   if(ticket<0)
     {
      err=GetLastError();
      Alert(Symbol()+":BUY FAILED : ErrorDescription("+IntegerToString(err)+"");
     }
   else
     {
      Alert(Symbol()+":BUY at "+DoubleToStr(Price,Digits)+" TP="+DoubleToStr(TP,Digits)+",SL="+DoubleToStr(SL,Digits));
     }
   Alert("--- END "+WindowExpertName()+" "+Symbol());
   RefreshRates();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OrderSell()
  {
   double SL2,TP2;
   int err;
   getpip();
   Credit=AccountCredit();
   double StopPercentage2=(Balance+Credit) *(StopPercent/100);
   double StopPipsMath2=NormalizeDouble((StopPercentage2)/(GetLots()/account_type),0);
   double ProfitPercentage2=(AccountBalance()+Credit) *(ProfitPercent/100);
   double ProfitPipsMath2=NormalizeDouble((ProfitPercentage2)/(GetLots()/account_type),0);
   if(fixed_lot==0) fixed_lot = GlobalVariableGet("ordersize");
   if(fixed_lot==0)
     {
      Alert("!!! "+WindowExpertName()+" "+Symbol()+": 'ordersize' not defined !!!");
     }
   double SLprice2=0;
   double TPprice2=0;
   double Price2=Ask;
   if(!StopPercen   && SLpips       !=0) SLprice2 = Price2+SLpips*pip;
   if(StopPercen    && StopPercent  !=0) SLprice2 = Price2+StopPipsMath2*pip;
   if(!ProfitPercen && TPpips       !=0) TPprice2 = Price2-TPpips*pip;
   if(ProfitPercen  && ProfitPercent!=0) TPprice2 = Price2-ProfitPipsMath2*pip;
   if(StopPercent>0) SL2=SLprice2; else SL2=0;//sell trade
   if(ProfitPercent>0)TP2=TPprice2; else TP2=0;
   int ticket=OrderSend(Symbol(),OP_SELL,GetLots(),Price2,30,SL2,TP2,"",0,0,Green);
   if(ticket<0)
     {
      err=GetLastError();
      Alert(Symbol()+":SELL FAILED : ErrorDescription("+IntegerToString(err)+"");
     }
   else
     {
      Alert(Symbol()+":SELL at "+DoubleToStr(Price2,Digits)+" TP="+DoubleToStr(TP2,Digits)+",SL="+DoubleToStr(SL2,Digits));
     }
   Alert("--- END "+WindowExpertName()+" "+Symbol());
   RefreshRates();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void BuyNetTP()
  {
   double SL,TP;//,SL2,TP2;
   int i,Total;
   Total=OrdersTotal();
   double AE=AccountEquity();
//+------------------------------------------------------------------+
   double Bprofit=0,Sprofit=0,BLotsSymbol=0,SLotsSymbol=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();
      if(OrderSymbol()==Symbol() && OrderType()==OP_BUY) BLotsSymbol+=OrderLots();
     }
   getpip();
   Credit=AccountCredit();
   Balance=AccountBalance();
//+------------------------------------------------------------------+
   if(fixed_lot==0) fixed_lot=GlobalVariableGet("ordersize");
   if(fixed_lot==0)
     {
      Alert("!!! "+WindowExpertName()+" "+Symbol()+": 'ordersize' not defined !!!");
     }
//+------------------------------------------------------------------+
   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)
              {
               double StopPercentage=(Balance+Credit) *(StopPercent/100);
               double StopPipsMath=NormalizeDouble((StopPercentage-(Bprofit))/((BLotsSymbol)/account_type),0);
               double ProfitPercentage=(Balance+Credit) *(ProfitPercent/100);
               double ProfitPipsMath=NormalizeDouble((ProfitPercentage-(Bprofit))/((BLotsSymbol)/account_type),0);
               double SLprice=0;
               double TPprice=0;
               double Price=Bid;
               if(!StopPercen   && SLpips       !=0) SLprice=Price-SLpips*pip;
               if(StopPercen    && StopPercent  !=0) SLprice=Price-StopPipsMath*pip;
               if(!ProfitPercen && TPpips       !=0) TPprice=Price+TPpips*pip;
               if(ProfitPercen  && ProfitPercent!=0) TPprice=Price+ProfitPipsMath*pip;
               if(StopPercent>0) SL=SLprice;  else SL=0; //buy trade
               if(ProfitPercent>0)TP=TPprice;  else TP=0;
               int ticket4=OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,OrderExpiration(),CLR_NONE);
              }
           }
        }
     }
   RefreshRates();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SellNetTP()
  {
   double SL2,TP2;
   int i,Total;
   Total=OrdersTotal();
   double AE=AccountEquity();
//+------------------------------------------------------------------+
   double Bprofit=0,Sprofit=0,BLotsSymbol=0,SLotsSymbol=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();
      if(OrderSymbol()==Symbol() && OrderType()==OP_SELL)SLotsSymbol+=OrderLots();
     }
   getpip();
   Credit=AccountCredit();
   Balance=AccountBalance();
//+------------------------------------------------------------------+
   if(fixed_lot==0) fixed_lot=GlobalVariableGet("ordersize");
   if(fixed_lot==0)
     {
      Alert("!!! "+WindowExpertName()+" "+Symbol()+": 'ordersize' not defined !!!");
     }
//+------------------------------------------------------------------+
   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)
              {
               double StopPercentage2=(Balance+Credit) *(StopPercent/100);
               double StopPipsMath2=NormalizeDouble((StopPercentage2-(Sprofit))/((SLotsSymbol)/account_type),0);
               double ProfitPercentage2=(Balance+Credit) *(ProfitPercent/100);
               double ProfitPipsMath2=NormalizeDouble((ProfitPercentage2-(Sprofit))/((SLotsSymbol)/account_type),0);
               double SLprice2=0;
               double TPprice2=0;
               double Price2=Ask;
               if(!StopPercen   && SLpips       !=0) SLprice2 = Price2+SLpips*pip;
               if(StopPercen    && StopPercent  !=0) SLprice2 = Price2+StopPipsMath2*pip;
               if(!ProfitPercen && TPpips       !=0) TPprice2 = Price2-TPpips*pip;
               if(ProfitPercen  && ProfitPercent!=0) TPprice2 = Price2-ProfitPipsMath2*pip;
               if(StopPercent>0) SL2=SLprice2; else SL2=0;//sell trade
               if(ProfitPercent>0)TP2=TPprice2; else TP2=0;
               int ticket5=OrderModify(OrderTicket(),OrderOpenPrice(),SL2,TP2,OrderExpiration(),CLR_NONE);
              }
           }
        }
     }
   RefreshRates();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+  
void BuySingleTP()
  {
   double SL,TP;//,SL2,TP2;
   int i,Total;
   Total=OrdersTotal();
//+------------------------------------------------------------------+
   double AE=AccountEquity();
//+------------------------------------------------------------------+
   double Bprofit=0,Sprofit=0,BLotsSymbol=0,SLotsSymbol=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(fixed_lot==0) fixed_lot=GlobalVariableGet("ordersize");
   if(fixed_lot==0)
     {
      Alert("!!! "+WindowExpertName()+" "+Symbol()+": 'ordersize' not defined !!!");
     }
//+------------------------------------------------------------------+
   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)
              {
               double StopPercentage=(Balance+Credit) *(StopPercent/100);
               double StopPipsMath=NormalizeDouble((StopPercentage)/(GetLots()/account_type),0);
               double ProfitPercentage=(Balance+Credit) *(ProfitPercent/100);
               double ProfitPipsMath=NormalizeDouble((ProfitPercentage)/(GetLots()/account_type),0);
               double SLprice=0;
               double TPprice=0;
               double Price=OrderOpenPrice();
               if(!StopPercen   && SLpips       !=0) SLprice=Price-SLpips*pip;
               if(StopPercen    && StopPercent  !=0) SLprice=Price-StopPipsMath*pip;
               if(!ProfitPercen && TPpips       !=0) TPprice=Price+TPpips*pip;
               if(ProfitPercen  && ProfitPercent!=0) TPprice=Price+ProfitPipsMath*pip;
               if(StopPercent>0) SL=SLprice;  else SL=0; //buy trade
               if(ProfitPercent>0)TP=TPprice;  else TP=0;
               int ticket4=OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,OrderExpiration(),CLR_NONE);
              }
           }
        }
     }
   RefreshRates();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+  
void SellSingleTP()
  {
   double SL2,TP2;
   int i,Total;
   Total=OrdersTotal();
   double AE=AccountEquity();
//+------------------------------------------------------------------+
   double Bprofit=0,Sprofit=0,BLotsSymbol=0,SLotsSymbol=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();
   Credit=AccountCredit();
   Balance=AccountBalance();
//+------------------------------------------------------------------+
   if(fixed_lot==0) fixed_lot=GlobalVariableGet("ordersize");
   if(fixed_lot==0)
     {
      Alert("!!! "+WindowExpertName()+" "+Symbol()+": 'ordersize' not defined !!!");
     }
//+------------------------------------------------------------------+
   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)
              {
               double StopPercentage2=(Balance+Credit) *(StopPercent/100);
               double StopPipsMath2=NormalizeDouble((StopPercentage2)/(GetLots()/account_type),0);
               double ProfitPercentage2=(Balance+Credit) *(ProfitPercent/100);
               double ProfitPipsMath2=NormalizeDouble((ProfitPercentage2)/(GetLots()/account_type),0);
               double SLprice2=0;
               double TPprice2=0;
               double Price2=OrderOpenPrice();
               if(!StopPercen   && SLpips       !=0) SLprice2 = Price2+SLpips*pip;
               if(StopPercen    && StopPercent  !=0) SLprice2 = Price2+StopPipsMath2*pip;
               if(!ProfitPercen && TPpips       !=0) TPprice2 = Price2-TPpips*pip;
               if(ProfitPercen  && ProfitPercent!=0) TPprice2 = Price2-ProfitPipsMath2*pip;
               if(StopPercent>0) SL2=SLprice2; else SL2=0;//sell trade
               if(ProfitPercent>0)TP2=TPprice2; else TP2=0;
               int ticket5=OrderModify(OrderTicket(),OrderOpenPrice(),SL2,TP2,OrderExpiration(),CLR_NONE);
              }
           }
        }
     }
   RefreshRates();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void BuyDeleteTP()
  {
   int i,Total;
   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 SellDeleteTP()
  {
   int i,Total;
   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(int i=OrdersTotal(); i>=0; i--)
     {
      int 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(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(),Ask,Slippage,White);
        }
     }
   BuyNetTP();
   return;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CloseSell()
  {
   bool closed=false;
   for(int i=OrdersTotal(); i>=0; i--)
     {
      int 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(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);
        }
     }
   SellNetTP();
   return;
  }
//--------------------------------------------------------------------------------------
// getpip
//--------------------------------------------------------------------------------------
void getpip()
  {
   if(Digits==2 || Digits==4) pip=Point;
   else if(Digits==3 || Digits==5) pip = 10*Point;
   else if(Digits==6) pip = 100*Point;
   if(Digits==3 || Digits==2) digits=2;
   else digits=4;
  }
//+------------------------------------------------------------------+
//|                       Money Management                           |
//+------------------------------------------------------------------+
double GetLots()
  {
   double MM_lots=0;
   Credit=AccountCredit();
   Balance=AccountBalance();
   double MM_minlot = MarketInfo(Symbol(), MODE_MINLOT);
   double MM_maxlot = MarketInfo(Symbol(), MODE_MAXLOT);
   double MM_leverage= AccountLeverage();
   double MM_lotsize = MarketInfo(Symbol(),MODE_LOTSIZE);
   double MM_stoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL);
   double xecn=1; if(Digits==5 || Digits==3){xecn=10;}
//+------------------------------------------------------------------+
   if(Mode_MM_Lots==1)
     {
      MM_lots=NormalizeDouble(fixed_lot,Digits);
     }
//+------------------------------------------------------------------+
   else if(Mode_MM_Lots==2)
     {
      MM_lots=NormalizeDouble((AccountFreeMargin()*MM_leverage*MM_Risk_Percent_Lots*Point*xecn*100/(Ask*MM_lotsize*MM_minlot)*MM_minlot),MM_Lot_Digits_Decimal);
      if(MM_lots < MM_minlot) MM_lots = MM_minlot;
      if(MM_lots > MM_maxlot) MM_lots = MM_maxlot;
      if(AccountFreeMargin()<Ask*MM_lots*MM_lotsize/MM_leverage)
        {
         Print("We have no money. Lots = ",MM_lots," , Free Margin = ",AccountFreeMargin());
         Comment("We have no money. Lots = ",MM_lots," , Free Margin = ",AccountFreeMargin());
        }
     }
//+------------------------------------------------------------------+
   else if(Mode_MM_Lots==3)
     {
      MM_lots=NormalizeDouble(((Balance+Credit)*MM_leverage*MM_Risk_Percent_Lots*Point*xecn*100/(Ask*MM_lotsize*MM_minlot)*MM_minlot),MM_Lot_Digits_Decimal);
      if(MM_lots < MM_minlot) MM_lots = MM_minlot;
      if(MM_lots > MM_maxlot) MM_lots = MM_maxlot;
      if(AccountFreeMargin()<Ask*MM_lots*MM_lotsize/MM_leverage)
        {
         Print("We have no money. Lots = ",MM_lots," , Free Margin = ",AccountFreeMargin());
         Comment("We have no money. Lots = ",MM_lots," , Free Margin = ",AccountFreeMargin());
        }
     }
   return(MM_lots);
  }
//+------------------------------------------------------------------+
