//+------------------------------------------------------------------+
//|          Free and OpenSource code under MIT License              |
//|              (https://opensource.org/licenses/MIT)               |
//|                    ButtonTrading_2.05.mq4                        |
//|                 Copyright 2020 penyuperwira                      |
//|          (https://www.forexfactory.com/penyuperwira)             |
//|                  updated : 18.04.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.05"
#property strict
string versionName = "EA ButtonTradingV2.05";
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
 string    Use_MM_lots="Choose IF you want to decrease lot by";
 string    Set_Mode_Lots="[1]Fixed [2]%Equity [3]%Balance ";
 int      Mode_MM_Lots=3;
 string    Mode1="Fixed lot and Fixed TP/SL by pips";
 string info="If 'lots' is set to 0, it will try to use the 'ordersize'";
 double fixed_lot=0.01;
 double SLpips = 0;
 double TPpips = 13;
 int BreakEvenPips = 3;
extern string    Mode3="Percentage % lot/TP/SL by balance";
extern double    MM_Risk_Percent_Lots=0.03;
int    MM_Lot_Digits_Decimal=2;
extern bool      ProfitPercen =true;
extern double    ProfitPercent=1.10;
extern bool      StopPercen =false;
extern double    StopPercent=1.00;
extern bool      BreakEvenPercen=true;
extern double  BreakEvenPercent = 1.10;
extern string ExitBehaviour = "Choose how to manually exit trade";
extern bool CloseProfitOnly=true;
extern bool CloseOnlyCurrentSymbol=true;
int Slippage=5;
double pip;
int digits;
double Credit;
double Balance;
double GetoLots;
double account_type;
extern string    Setting="Buttons Arrangement";
extern bool ArrangeButtonVertical=true;
extern int buttonsHorizontalPosition = 120;
extern int buttonsVerticalPosition = 200;
extern int ButtonWidth=63;
extern int ButtonHeight=20;
int gap=5;
extern int ButtonFontSize=8;
extern color ButtonFontColor=White;
extern color BuyButtonColor=Green;
extern color HedgeButtonColor=Blue;
extern color SellButtonColor=Maroon;
extern color BuyNetTPButtonColor=DarkOliveGreen;
extern color HedgeNetTPButtonColor=SteelBlue;
extern color SellNetTPButtonColor=SaddleBrown;
extern color CloseButtonColor=Red;
extern color VersionButtonColor=Gray;
extern color ButtonOutlineColor=White;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
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.

//+---------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;
   int x4=Button_Horizontal;
   int y4=gap*3+ButtonHeight*3+Button_Vertical;
   int x5=Button_Horizontal;
   int y5=gap*4+ButtonHeight*4+Button_Vertical;
   int x6=Button_Horizontal;
   int y6=gap*5+ButtonHeight*5+Button_Vertical;
   int x7=Button_Horizontal;
   int y7=gap*6+ButtonHeight*6+Button_Vertical;
   int x8=Button_Horizontal;
   int y8=gap*7+ButtonHeight*7+Button_Vertical;

   if(!ArrangeButtonVertical)
     {
      CreateButton("button1","Buy Net",x,y,ButtonWidth,ButtonHeight,ButtonFontColor,BuyButtonColor,ButtonOutlineColor,ButtonFontSize);//Buy
      CreateButton("button2","Net % TP",x,y,ButtonWidth,ButtonHeight,ButtonFontColor,BuyNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);//Buy
      CreateButton("button3","Net % SL",x,y,ButtonWidth,ButtonHeight,ButtonFontColor,BuyNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);//Buy
      CreateButton("button4","Net % BE",x,y,ButtonWidth,ButtonHeight,ButtonFontColor,BuyNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);//Buy
      CreateButton("button5","Delete TP",x,y,ButtonWidth,ButtonHeight,ButtonFontColor,BuyNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);//Buy
      CreateButton("button6","Delete SL",x,y,ButtonWidth,ButtonHeight,ButtonFontColor,BuyNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);//Buy
      CreateButton("button7","Close",x,y,ButtonWidth,ButtonHeight,ButtonFontColor,CloseButtonColor,ButtonOutlineColor,ButtonFontSize);//Buy

      CreateButton("button8", "HedgeNet",x2,y2,ButtonWidth,ButtonHeight,ButtonFontColor,HedgeButtonColor,ButtonOutlineColor,ButtonFontSize);//hedge
      CreateButton("button9", "Net % TP",x2,y2,ButtonWidth,ButtonHeight,ButtonFontColor,HedgeNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);//hedge
      CreateButton("button10", "Net % SL",x2,y2,ButtonWidth,ButtonHeight,ButtonFontColor,HedgeNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);//hedge
      CreateButton("button11","Net % BE",x2,y2,ButtonWidth,ButtonHeight,ButtonFontColor,HedgeNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);//hedge
      CreateButton("button12","Delete TP",x2,y2,ButtonWidth,ButtonHeight,ButtonFontColor,HedgeNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);//hedge
      CreateButton("button13","Delete SL",x2,y2,ButtonWidth,ButtonHeight,ButtonFontColor,HedgeNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);//hedge
      CreateButton("button14","Close",x2,y2,ButtonWidth,ButtonHeight,ButtonFontColor,CloseButtonColor,ButtonOutlineColor,ButtonFontSize);//hedge

      CreateButton("button15","Sell Net",x3,y3,ButtonWidth,ButtonHeight,ButtonFontColor,SellButtonColor,ButtonOutlineColor,ButtonFontSize);
      CreateButton("button16","Net % TP",x3,y3,ButtonWidth,ButtonHeight,ButtonFontColor,SellNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);
      CreateButton("button17","Net % SL",x3,y3,ButtonWidth,ButtonHeight,ButtonFontColor,SellNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);
      CreateButton("button18","Net % BE",x3,y3,ButtonWidth,ButtonHeight,ButtonFontColor,SellNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);
      CreateButton("button19","Delete TP",x3,y3,ButtonWidth,ButtonHeight,ButtonFontColor,SellNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);
      CreateButton("button20","Delete SL",x3,y3,ButtonWidth,ButtonHeight,ButtonFontColor,SellNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);
      CreateButton("button21","Close",x3,y3,ButtonWidth,ButtonHeight,ButtonFontColor,CloseButtonColor,ButtonOutlineColor,ButtonFontSize);

      CreateButton("button22",versionName,x4,y4,ButtonWidth*7+gap*12,ButtonHeight,ButtonFontColor,VersionButtonColor,ButtonOutlineColor,ButtonFontSize);//title box
     }
   else
     {
      CreateButton("button1","Buy Net",x,y,ButtonWidth,ButtonHeight,ButtonFontColor,BuyButtonColor,ButtonOutlineColor,ButtonFontSize);//Buy
      CreateButton("button8","HedgeNet",x,y,ButtonWidth,ButtonHeight,ButtonFontColor,HedgeButtonColor,ButtonOutlineColor,ButtonFontSize);//hedge
      CreateButton("button15","Sell Net",x,y,ButtonWidth,ButtonHeight,ButtonFontColor,SellButtonColor,ButtonOutlineColor,ButtonFontSize);//sell

      CreateButton("button2","Net % TP",x2,y2,ButtonWidth,ButtonHeight,ButtonFontColor,BuyNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);//Buy;
      CreateButton("button9","Net % TP",x2,y2,ButtonWidth,ButtonHeight,ButtonFontColor,HedgeNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);//hedge
      CreateButton("button16","Net % TP",x2,y2,ButtonWidth,ButtonHeight,ButtonFontColor,SellNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);//sell

      CreateButton("button3","Net % SL",x3,y3,ButtonWidth,ButtonHeight,ButtonFontColor,BuyNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);//Buy;
      CreateButton("button10","Net % SL",x3,y3,ButtonWidth,ButtonHeight,ButtonFontColor,HedgeNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);//hedge
      CreateButton("button17","Net % SL",x3,y3,ButtonWidth,ButtonHeight,ButtonFontColor,SellNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);//sell

      CreateButton("button4","Net % BE",x4,y4,ButtonWidth,ButtonHeight,ButtonFontColor,BuyNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);//Buy
      CreateButton("button11","Net % BE",x4,y4,ButtonWidth,ButtonHeight,ButtonFontColor,HedgeNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);//hedge
      CreateButton("button18","Net % BE",x4,y4,ButtonWidth,ButtonHeight,ButtonFontColor,SellNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);//sell

      CreateButton("button5","Delete TP",x5,y5,ButtonWidth,ButtonHeight,ButtonFontColor,BuyNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);//Buy
      CreateButton("button12","Delete TP",x5,y5,ButtonWidth,ButtonHeight,ButtonFontColor,HedgeNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);//hedge
      CreateButton("button19","Delete TP",x5,y5,ButtonWidth,ButtonHeight,ButtonFontColor,SellNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);//sell

      CreateButton("button6","Delete SL",x6,y6,ButtonWidth,ButtonHeight,ButtonFontColor,BuyNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);//Buy
      CreateButton("button13","Delete SL",x6,y6,ButtonWidth,ButtonHeight,ButtonFontColor,HedgeNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);//hedge
      CreateButton("button20","Delete SL",x6,y6,ButtonWidth,ButtonHeight,ButtonFontColor,SellNetTPButtonColor,ButtonOutlineColor,ButtonFontSize);//sell

      CreateButton("button7","Close",x7,y7,ButtonWidth,ButtonHeight,ButtonFontColor,CloseButtonColor,ButtonOutlineColor,ButtonFontSize);//Buy
      CreateButton("button14","Close",x7,y7,ButtonWidth,ButtonHeight,ButtonFontColor,CloseButtonColor,ButtonOutlineColor,ButtonFontSize);//hedge
      CreateButton("button21","Close",x7,y7,ButtonWidth,ButtonHeight,ButtonFontColor,CloseButtonColor,ButtonOutlineColor,ButtonFontSize);//sell

      CreateButton("button22",versionName,x8,y8,ButtonWidth*3+gap*4,ButtonHeight,ButtonFontColor,VersionButtonColor,ButtonOutlineColor,ButtonFontSize);//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 Net
     {
      Action_Button1();
      ObjectSetInteger(0,"button1",OBJPROP_STATE,false);
     }
   else
      if(sparam=="button2") // Net %  TP (Buy)
        {
         Action_Button2();
         ObjectSetInteger(0,"button2",OBJPROP_STATE,false);
        }
      else
         if(sparam=="button3") //Net % Sl (Buy)
           {
            Action_Button3();
            ObjectSetInteger(0,"button3",OBJPROP_STATE,false);
           }
         else
            if(sparam=="button4") //BreakEven  Net (Buy)
              {
               Action_Button4();
               ObjectSetInteger(0,"button4",OBJPROP_STATE,false);
              }
            else
               if(sparam=="button5") // Delete TP (Buy)
                 {
                  Action_Button5();
                  ObjectSetInteger(0,"button5",OBJPROP_STATE,false);
                 }
               else
                  if(sparam=="button6") // Delete SL (Buy)
                    {
                     Action_Button6();
                     ObjectSetInteger(0,"button6",OBJPROP_STATE,false);
                    }
                  else
                     if(sparam=="button7") // Close All (Buy)
                       {
                        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 (Hedge)
                             {
                              Action_Button9();
                              ObjectSetInteger(0,"button9",OBJPROP_STATE,false);
                             }
                           else
                              if(sparam=="button10") //Net % Sl (Hedge)
                                {
                                 Action_Button10();
                                 ObjectSetInteger(0,"button10",OBJPROP_STATE,false);
                                }
                              else
                                 if(sparam=="button111") //BreakEven  Net TP/Sl (Hedge)
                                   {
                                    Action_Button11();
                                    ObjectSetInteger(0,"button11",OBJPROP_STATE,false);
                                   }
                                 else
                                    if(sparam=="button12") // Delete TP (Hedge)
                                      {
                                       Action_Button12();
                                       ObjectSetInteger(0,"button12",OBJPROP_STATE,false);
                                      }
                                    else
                                       if(sparam=="button13") // Delete SL (Hedge)
                                         {
                                          Action_Button13();
                                          ObjectSetInteger(0,"button13",OBJPROP_STATE,false);
                                         }
                                       else
                                          if(sparam=="button14") // Close All (Hedge)
                                            {
                                             Action_Button14();
                                             ObjectSetInteger(0,"button14",OBJPROP_STATE,false);
                                            }
                                          else
                                             if(sparam=="button15") // Sell Net
                                               {
                                                Action_Button15();
                                                ObjectSetInteger(0,"button15",OBJPROP_STATE,false);
                                               }
                                             else
                                                if(sparam=="button16") // Net %  TP (Sell)
                                                  {
                                                   Action_Button16();
                                                   ObjectSetInteger(0,"button16",OBJPROP_STATE,false);
                                                  }
                                                else
                                                   if(sparam=="button17") //Net %  Sl (Sell)
                                                     {
                                                      Action_Button17();
                                                      ObjectSetInteger(0,"button17",OBJPROP_STATE,false);
                                                     }
                                                   else
                                                      if(sparam=="button18") //BreakEven Net TP/Sl (Sell)
                                                        {
                                                         Action_Button18();
                                                         ObjectSetInteger(0,"button18",OBJPROP_STATE,false);
                                                        }
                                                      else
                                                         if(sparam=="button19") // Delete TP (Sell)
                                                           {
                                                            Action_Button19();
                                                            ObjectSetInteger(0,"button19",OBJPROP_STATE,false);
                                                           }
                                                         else
                                                            if(sparam=="button20") // Delete SL (Sell)
                                                              {
                                                               Action_Button20();
                                                               ObjectSetInteger(0,"button20",OBJPROP_STATE,false);
                                                              }
                                                            else
                                                               if(sparam=="button21") // Close All (Sell)
                                                                 {
                                                                  Action_Button21();
                                                                  ObjectSetInteger(0,"button21",OBJPROP_STATE,false);
                                                                 }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button1()
  {
   OrderBuyNet();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button2()
  {
   BuyNetTP();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button3()
  {
   BuyNetSL();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button4()
  {
   BuyNetBE();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button5()
  {
   BuyDeleteTP();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button6()
  {
   BuyDeleteSL();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button7()
  {
   CloseBuy();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button8()
  {
   OrderBuyNet();
   OrderSellNet();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button9()
  {
   BuyNetTP();
   SellNetTP();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button10()
  {
   BuyNetSL();
   SellNetSL();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button11()
  {
   BuyNetBE();
   SellNetBE();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button12()
  {
   BuyDeleteTP();
   SellDeleteTP();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button13()
  {
   BuyDeleteSL();
   SellDeleteSL();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button14()
  {
   CloseBuy();
   CloseSell();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button15()
  {
   OrderSellNet();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button16()
  {
   SellNetTP();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button17()
  {
   SellNetSL();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button18()
  {
   SellNetBE();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button19()
  {
   SellDeleteTP();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button20()
  {
   SellDeleteSL();
   PlaySound("ok.wav");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Action_Button21()
  {
   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 BuyNetTP()
  {
   int i,Total;
   Total=OrdersTotal();
   double 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)
         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)
              {
               SL = OrderStopLoss();
               double ProfitPercentage=(Balance+Credit) *(ProfitPercent/100);
               double ProfitPipsMath=NormalizeDouble((ProfitPercentage-(Bprofit))/((BLotsSymbol)/account_type),0);
               double Price=Bid;
               if(ProfitPercent>0)
                  TP=Price+ProfitPipsMath*pip;
               int ticket4=OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,OrderExpiration(),CLR_NONE);
              }
           }
        }
     }
   RefreshRates();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void BuyNetSL()
  {
   int i,Total;
   Total=OrdersTotal();
   double 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)
         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)
              {
               TP=OrderTakeProfit();
               double StopPercentage=(Balance+Credit) *(StopPercent/100);
               double StopPipsMath=NormalizeDouble((StopPercentage-(Bprofit))/((BLotsSymbol)/account_type),0);
               double Price=Bid;
               if(StopPercent  >0)
                  SL=Price-StopPipsMath*pip;
               int ticket4=OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,OrderExpiration(),CLR_NONE);
              }
           }
        }
     }
   RefreshRates();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SellNetSL()
  {
//double SL2,TP2;
   int i,Total;
   Total=OrdersTotal();
   double AE=AccountEquity();
//+------------------------------------------------------------------+
   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)
         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)
              {
               TP2 = OrderTakeProfit();
               double StopPercentage2=(Balance+Credit) *(StopPercent/100);
               double StopPipsMath2=NormalizeDouble((StopPercentage2-(Sprofit))/((SLotsSymbol)/account_type),0);
               double Price2=Ask;
               if(StopPercent  >0)
                  SL2 = Price2+StopPipsMath2*pip;
               int ticket5=OrderModify(OrderTicket(),OrderOpenPrice(),SL2,TP2,OrderExpiration(),CLR_NONE);
              }
           }
        }
     }
   RefreshRates();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SellNetTP()
  {
   int i,Total;
   Total=OrdersTotal();
   double AE=AccountEquity();
//+------------------------------------------------------------------+
   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)
         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)
              {
               SL2 = OrderStopLoss();
               double ProfitPercentage2=(Balance+Credit) *(ProfitPercent/100);
               double ProfitPipsMath2=NormalizeDouble((ProfitPercentage2-(Sprofit))/((SLotsSymbol)/account_type),0);
               double Price2=Ask;
               if(ProfitPercent>0)
                  TP2 = Price2-ProfitPipsMath2*pip;
               int ticket5=OrderModify(OrderTicket(),OrderOpenPrice(),SL2,TP2,OrderExpiration(),CLR_NONE);
              }
           }
        }
     }
   RefreshRates();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void BuyNetBE()
  {
   int i,Total;
   Total=OrdersTotal();
   double 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)
         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)
              {
               TP=OrderTakeProfit();
               double StopPercentage=(Balance+Credit) *(BreakEvenPercent/100);
               double StopPipsMath=NormalizeDouble((StopPercentage-(Bprofit))/((BLotsSymbol)/account_type),0);
               double Price=Bid;
               if(BreakEvenPercent  >0)
                  SL=Price+StopPipsMath*pip;
               int ticket4=OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,OrderExpiration(),CLR_NONE);
              }
           }
        }
     }
   RefreshRates();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SellNetBE()
  {
//double SL2,TP2;
   int i,Total;
   Total=OrdersTotal();
   double AE=AccountEquity();
//+------------------------------------------------------------------+
   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)
         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)
              {
               TP2 = OrderTakeProfit();
               double StopPercentage2=(Balance+Credit) *(BreakEvenPercent/100);
               double StopPipsMath2=NormalizeDouble((StopPercentage2-(Sprofit))/((SLotsSymbol)/account_type),0);
               double Price2=Ask;
               if(BreakEvenPercent  >0)
                  SL2 = Price2-StopPipsMath2*pip;
               int ticket5=OrderModify(OrderTicket(),OrderOpenPrice(),SL2,TP2,OrderExpiration(),CLR_NONE);
              }
           }
        }
     }
   RefreshRates();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void BuyDeleteTP()
  {
   int i,Total;
   Total=OrdersTotal();
//+------------------------------------------------------------------+
   double SL=0,TP=0;
//+------------------------------------------------------------------+
   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)
              {
               SL=OrderStopLoss();
               int ticket4=OrderModify(OrderTicket(),OrderOpenPrice(),SL,0,OrderExpiration(),CLR_NONE);
              }
           }
        }
     }
   return;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void BuyDeleteSL()
  {
   int i,Total;
   Total=OrdersTotal();
//+------------------------------------------------------------------+
   double SL=0,TP=0;
//+------------------------------------------------------------------+
   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)
              {
               TP=OrderTakeProfit();
               int ticket4=OrderModify(OrderTicket(),OrderOpenPrice(),0,TP,OrderExpiration(),CLR_NONE);
              }
           }
        }
     }
   return;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SellDeleteTP()
  {
//double SL,TP;//,SL2,TP2;
   int i,Total;
   Total=OrdersTotal();
//+------------------------------------------------------------------+
   double SL=0,TP=0;
//+------------------------------------------------------------------+
   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)
              {
               SL=OrderTakeProfit();
               int ticket5=OrderModify(OrderTicket(),OrderOpenPrice(),SL,0,OrderExpiration(),CLR_NONE);
              }
           }
        }
     }
   return;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SellDeleteSL()
  {
   int i,Total;
   Total=OrdersTotal();
//+------------------------------------------------------------------+
   double SL=0,TP=0;
//+------------------------------------------------------------------+
   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)
              {
               TP=OrderTakeProfit();
               int ticket5=OrderModify(OrderTicket(),OrderOpenPrice(),0,TP,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(!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);
              }
        }
     }
   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(!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);
              }
        }
     }
   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);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
