Navigation:Home > Content >

cm-CloseLock.mq4

Time: 2011-11-17 | Download file:cm-CloseLock.mq4

//+------------------------------------------------------------------+
#property copyright "Copyright © 2015, http://cmillion.ru"
#property link      "cmillion@narod.ru"
//-------------------------------------------------------------------
extern int    CloseProfit  = 0;     //закрывать по суммарному профиту
extern int    Magic        = 0;     //-1 все ордера
//-------------------------------------------------------------------
string AC;
int font_size=10;
color text_color=Aqua;
//-------------------------------------------------------------------
int start()
{
   if (!IsTradeAllowed()) 
   {
      DrawLABEL("Торговля",0,0,0,Red,"Торговля запрещена");
      return(0);
   } 
   else DrawLABEL("Торговля",0,0,0,Lime,"Торговля разрешена");

   //---

   double MaxOrderBuy,MinOrderSell;
   double OOP,Profit,ProfitPlus,LossBuy,LossSell;
   int TicketB,TicketS,tip;
   for (int i=0; i0) ProfitPlus+=Profit;
            if (tip==OP_BUY)             
            {  
               if (MaxOrderBuy < OOP || MaxOrderBuy==0) {MaxOrderBuy = OOP; TicketB=OrderTicket(); LossBuy=Profit;}
            }                                         
            if (tip==OP_SELL)        
            {
               if (MinOrderSell > OOP || MinOrderSell==0) {MinOrderSell = OOP; TicketS=OrderTicket(); LossSell=Profit;}
            } 
         }
      }
   }

   //---
   
   if (ProfitPlus+LossBuy>=CloseProfit && LossBuy<0 && LossBuy=CloseProfit && LossSell<0 && LossBuy>LossSell) 
   {
      if (OrderSelect(TicketS, SELECT_BY_TICKET))
      {
         if (OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),30,Red))
         {
            CloseAll();
            Text(Lime,Low[0],StringConcatenate(DoubleToStr(ProfitPlus,2),DoubleToStr(LossSell,2)),0);
         }
      }
   }
   
   //---
   
   double AB = AccountBalance();
   
   //---
   
   DrawLABEL("Balance"        ,1,5,0,Aqua,StringConcatenate("Balance ",DoubleToStr(AB,2),AC));
   DrawLABEL("Equity"         ,1,5,0,Aqua,StringConcatenate("Equity ",DoubleToStr(AccountEquity(),2),AC));
   DrawLABEL("FreeMargin"     ,1,5,0,Aqua,StringConcatenate("FreeMargin ",DoubleToStr(AccountFreeMargin(),2),AC));
   DrawLABEL("Take"           ,1,5,0,Color(ProfitPlus>0,Lime,Red),StringConcatenate("Profit ",DoubleToStr(ProfitPlus,2),AC));

   DrawLABEL("Убыточный Buy"  ,1,5,0,Color(LossBuy>0,Lime,Red),StringConcatenate("Buy " ,TicketB," | ",DoubleToStr(LossBuy,2),AC));
   DrawLABEL("Убыточный Sell" ,1,5,0,Color(LossSell>0,Lime,Red),StringConcatenate("Sell ",TicketS," | ",DoubleToStr(LossSell,2),AC));
   
   


return(0);
}
//------------------------------------------------------------------
int deinit()
{
   if (!IsTesting()) 
   {
      ObjectsDeleteAll(0);
   }
   return(0);
}
//-------------------------------------------------------------------
void DrawLABEL(string name, int CORNER, int X, int Y, color clr, string Name)
{
   if (ObjectFind(name)==-1)
   {
      ObjectCreate(name, OBJ_LABEL, 0, 0, 0);
      ObjectSet(name, OBJPROP_CORNER, CORNER);
      ObjectSet(name, OBJPROP_XDISTANCE, X);
      ObjectSet(name, OBJPROP_YDISTANCE, Y);
   }
   ObjectSetText(name,Name,font_size,"Arial",clr);
}
//+------------------------------------------------------------------+
int init()
{
   AC = StringConcatenate(" ", AccountCurrency());
   
   int Y=15;
   DrawLABEL("Торговля"  ,1,5,Y,Red,"Торговля ");Y += font_size*2;
   DrawLABEL("Balance"   ,1,5,Y,text_color,StringConcatenate("Balance ",DoubleToStr(AccountBalance(),2),AC));Y += font_size*1.5;
   DrawLABEL("Equity"    ,1,5,Y,text_color,StringConcatenate("Equity ",DoubleToStr(AccountEquity(),2),AC));Y += font_size*1.5;
   DrawLABEL("FreeMargin",1,5,Y,text_color,StringConcatenate("FreeMargin ",DoubleToStr(AccountFreeMargin(),2),AC));Y += font_size*3;

   DrawLABEL("Убыточный Sell" ,1,5,Y,text_color,"Buy " );Y += font_size*1.5;
   DrawLABEL("Убыточный Buy"  ,1,5,Y,text_color,"Sell ");Y += font_size*3;

   DrawLABEL("Take"           ,1,5,Y,Lime,"Profit ");Y += font_size*2;
   
   return(0);
}
//+------------------------------------------------------------------+
void Text(color COLOR,double Price,string Name,int a)
{
   string name = StringConcatenate(TimeToStr(TimeCurrent(),TIME_SECONDS)," ",Name);
   ObjectDelete(name);
   ObjectCreate(name, OBJ_TEXT,0,Time[0],Price,0,0,0,0);
   ObjectSet(name, OBJPROP_ANGLE, a);
   ObjectSetText(name, Name,font_size, "Times New Roman", COLOR);
}
//+------------------------------------------------------------------+
color Color(bool P,color a,color b)
{
   if (P) return(a);
   else return(b);
}
//------------------------------------------------------------------
bool CloseAll()
{
   bool error=true;
   int err,nn,OT;
   double Profit;
   while(true)
   {
      for (int j = OrdersTotal()-1; j >= 0; j--)
      {
         if (OrderSelect(j, SELECT_BY_POS))
         {
            if (OrderSymbol() == Symbol() && (Magic==OrderMagicNumber() || Magic==-1))
            {
               Profit=OrderProfit()+OrderCommission()+OrderSwap();
               if (Profit<0) continue;
               OT = OrderType();
               if (OT>1) error=OrderDelete(OrderTicket());
               if (OT==OP_BUY) 
               {
                  error=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),30,Blue);
               }
               if (OT==OP_SELL) 
               {
                  error=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),30,Red);
               }
               if (!error) 
               {
                  err = GetLastError();
                  if (err<2) continue;
                  if (err==129) 
                  {
                     RefreshRates();
                     continue;
                  }
                  if (err==146) 
                  {
                     if (IsTradeContextBusy()) Sleep(2000);
                     continue;
                  }
                  Print("Ошибка ",err," закрытия ордера N ",OrderTicket(),"     ",TimeToStr(TimeCurrent(),TIME_SECONDS));
               }
            }
         }
      }
      int n=0;
      for (j = 0; j < OrdersTotal(); j++)
      {
         if (OrderSelect(j, SELECT_BY_POS))
         {
            if (OrderSymbol() == Symbol() && (Magic==OrderMagicNumber() || Magic==-1))
            {
               Profit=OrderProfit()+OrderCommission()+OrderSwap();
               if (Profit<0) continue;
               n++;
            }
         }  
      }
      if (n==0) break;
      nn++;
      if (nn>10) 
      {
         Alert(Symbol()," Не удалось закрыть все сделки, осталось еще ",n);
         return(0);
      }
      Sleep(1000);
      RefreshRates();
   }
   return(1);
}
//--------------------------------------------------------------------


Recommend