//+------------------------------------------------------------------+
//|                                                 OrderBalans_v3.4 |
//|                               Copyright © 2010, Vladimir Hlystov |
//|                                         http://cmillion.narod.ru |
//+------------------------------------------------------------------+
#property copyright "Хлыстов Владимир"
#property link      "cmillion@narod.ru"
#property indicator_chart_window
extern color   WhiteColor  = Silver;//цвет вывода информации
extern int     UGOL        = 3;     //если 4, то не выводить
extern color   NameColor   = Green;
extern color   BuyColor    = Blue;
extern color   SellColor   = Red;
//--------------------------------------------------------------------
double High_Win,Low_Win,balans,MaxLot,AFreeMargin,LINE [25][5],orders[25][10];
int    ORDER,UG;
string DayTek,DayWe;
//--------------------------------------------------------------------
int deinit()
{
   ObjectDelete("_TIME_");
   ObjectDelete("_SHAPKA_");
   ObjectDelete("_BALANS_");
   ObjectDelete("_MARGA_");
   del("ордер Bay");
   del("ордер Sell");
   del("ОРДЕР");
}
//--------------------------------------------------------------------
int del(string str)
{
   for(int n=ObjectsTotal()-1; n>=0; n--) 
     {
      string Obj_Name=ObjectName(n);
      if (StringFind(Obj_Name,str,0) != -1)
      {
         ObjectDelete(Obj_Name);
      }
   }
   return;                                      // Выход из deinit()
}
//--------------------------------------------------------------------
int init()
{
if (UGOL>3) return;
   ObjectCreate("_TIME_", OBJ_LABEL, 0, 0, 0);
      ObjectSet("_TIME_", OBJPROP_CORNER, UGOL);
      ObjectSet("_TIME_", OBJPROP_XDISTANCE, 10);
   ObjectCreate("_SHAPKA_", OBJ_LABEL, 0, 0, 0);
      ObjectSet("_SHAPKA_", OBJPROP_CORNER, UGOL);      
      ObjectSet("_SHAPKA_", OBJPROP_XDISTANCE, 10);      
   ObjectCreate("_BALANS_", OBJ_LABEL, 0, 0, 0);
      ObjectSet("_BALANS_", OBJPROP_CORNER, UGOL);      
      ObjectSet("_BALANS_", OBJPROP_XDISTANCE, 10 ); 
   ObjectCreate("_MARGA_", OBJ_LABEL, 0, 0, 0);
      ObjectSet("_MARGA_", OBJPROP_CORNER, UGOL);
      ObjectSet("_MARGA_", OBJPROP_XDISTANCE, 10 );
if (UGOL<2)
{  UG=+10;
      ObjectSet("_TIME_", OBJPROP_YDISTANCE, 13);
      ObjectSet("_BALANS_", OBJPROP_YDISTANCE, 25);
      ObjectSet("_MARGA_", OBJPROP_YDISTANCE, 35);
      ObjectSet("_SHAPKA_", OBJPROP_YDISTANCE, 60);
}
else
{  UG=-10;
      ObjectSet("_TIME_", OBJPROP_YDISTANCE, 5);
      ObjectSet("_BALANS_", OBJPROP_YDISTANCE, 15);
      ObjectSet("_MARGA_", OBJPROP_YDISTANCE, 25);
      ObjectSet("_SHAPKA_", OBJPROP_YDISTANCE, 100);
}
return;
}   
//------------------------------------------------------------------------------- НАЧАЛО ПРОГРАММЫ 
int start()                                  
{
   DayTek = TimeToStr(TimeCurrent(),TIME_DATE);
   DayWe = DayWeek(TimeDayOfWeek(TimeCurrent()));
   if (UGOL<4) ObjectSetText("_TIME_",StringConcatenate(DayWe," ",TimeToStr(TimeCurrent(),TIME_SECONDS),"  v3"),8,"Arial",WhiteColor);
   AFreeMargin = AccountFreeMargin();
   MaxLot = AFreeMargin/MarketInfo(Symbol(),MODE_MARGINREQUIRED);//Размер свободных средств, необходимых для открытия 1 лота на покупку
   //-------------------------------------------------------- назначение лотов end
   Terminal();
   ORDER = orders[0][0]; //количество_ордеров_этого_символа 
   if (ORDER > 0) 
   {
      string   NameLine;
      for (int n=1; n<=orders[0][0]; n++)
      {
         if (orders[n][6]==1) NameLine="ордер Bay  "+DoubleToStr(orders[n][4],0);
         if (orders[n][6]==-1) NameLine="ордер Sell "+DoubleToStr(orders[n][4],0);
         ObjectDelete(NameLine); //номер ордера
         ObjectDelete(NameLine+" з");
         if (orders[n][6]== 1) {ObjectCreate(NameLine,OBJ_TREND,0,orders[n][9],orders[n][1],Time[0],Bid);
         ObjectSet(NameLine, OBJPROP_COLOR,BuyColor); 
         ObjectCreate(NameLine+" з",OBJ_ARROW,0,Time[0],Bid,0,0,0,0);} //Значек на месте закрытия       
         if (orders[n][6]==-1) {ObjectCreate(NameLine,OBJ_TREND,0,orders[n][9],orders[n][1],Time[0],Ask);
         ObjectSet(NameLine, OBJPROP_COLOR,SellColor);
         ObjectCreate(NameLine+" з",OBJ_ARROW,0,Time[0],Ask,0,0,0,0);} //Значек на месте закрытия 
         ObjectSet(NameLine, OBJPROP_STYLE, STYLE_DOT);// Стиль   
         ObjectSet(NameLine, OBJPROP_RAY,   false);     // Луч  
         ObjectSet(NameLine+" з",OBJPROP_ARROWCODE,3);ObjectSet(NameLine+" з",OBJPROP_COLOR,WhiteColor);   
      }
   }
   //--------------------------------------------------------------------
   // Вывод_текстовой_информации
   if (UGOL<4) {
   string txt = StringConcatenate("  БАЛАНС = ",DoubleToStr(AccountBalance( ),2)," | своб ",DoubleToStr(AFreeMargin,2)," | ",DoubleToStr(MaxLot,2));
   ObjectSetText("_BALANS_",txt,8,"Arial",WhiteColor);
   if (balans <0) ObjectSetText("_MARGA_",StringConcatenate("  balans = ",DoubleToStr(balans,2)),8,"Arial",Red);
   else           ObjectSetText("_MARGA_",StringConcatenate("  balans = ",DoubleToStr(balans,2)),8,"Arial",Blue);
   if (ORDER== 0) ObjectSetText("_MARGA_","                ",8,"Arial",0);}
return;
}
//-------------------------------------------------------------------------------
string DayWeek(int day)
   {
      switch(day)
      {  case 1 : return("Понедельник ");      
         case 2 : return("Вторник ");      
         case 3 : return("Среда ");
         case 4 : return("Четверг ");      
         case 5 : return("Пятница ");      
         case 6 : return("Суббота ");
         case 7 : return("Воскресенье ");
         default: return("");}
   }
//-------------------------------------------------------------------------------
int Terminal()
{
   int Kol=0,n=0,X_order,Ticket,tip;
   double Y_order;
   string N_order,текст,txt;
          
   ArrayInitialize(orders,0);
   if (ObjectFind("_SHAPKA_")==0)
   {  X_order = ObjectGet("_SHAPKA_", OBJPROP_XDISTANCE);
      Y_order = ObjectGet("_SHAPKA_", OBJPROP_YDISTANCE);
   }
   else {X_order = 10;Y_order=60+10*Kol;}
   for (int i=1; i<=25; i++)
   {  N_order="ОРДЕР "+i;
      ObjectDelete(N_order);
   }
   balans=0;
   //double POINT,BID,ASK,DIG;
   //string SYMBOL;
   for ( i=0; i<OrdersTotal(); i++)
   {  if (OrderSelect(i, SELECT_BY_POS)==true)
      {  //SYMBOL=OrderSymbol();
         //DIG = MarketInfo(SYMBOL,MODE_DIGITS);
         //BID = MarketInfo(SYMBOL,MODE_BID);
         //ASK = MarketInfo(SYMBOL,MODE_ASK);
         //POINT = MarketInfo(SYMBOL,MODE_POINT);
         if (OrderSymbol()==Symbol())
         {  tip=OrderType();
            if (tip == OP_BUY  || tip == OP_SELL)
            {  Kol++;                  // Считаем общее количество ордеров
               orders[Kol][1] = OrderOpenPrice();  // Курс открытия ордеров
               orders[Kol][2] = OrderStopLoss();   //  SL 
               orders[Kol][3] = OrderTakeProfit(); //  ТР 
               orders[Kol][4] = OrderTicket();
               orders[Kol][5] = OrderLots();
               if (tip == OP_BUY) {orders[Kol][6] = 1; текст = "Buy "; }
               else               {orders[Kol][6] =-1; текст = "Sell ";}
               orders[Kol][7] = OrderMagicNumber();
               orders[Kol][9] = OrderOpenTime();
               //--------------------------------------------------------------------- Вывод текущих ордеров на экран
               if (UGOL<4){N_order="ОРДЕР "+Kol;//DoubleToStr(orders[Kol][4],0);
                  //Comment(BID,"  ",ASK,"  ",POINT,"  ",DIG);
                  if (orders[Kol][6]==1) orders[Kol][8] = NormalizeDouble((Bid-orders[Kol][1])/Point,0);// Прибыль в п Bay
                  else                   orders[Kol][8] = NormalizeDouble((orders[Kol][1]-Ask)/Point,0);// Прибыль в п Sell
                  balans=balans+OrderProfit();//(orders[i][8]*orders[i][5])*MarketInfo(SYMBOL,MODE_TICKVALUE);
                  ObjectDelete(N_order);
                  ObjectCreate(N_order, OBJ_LABEL, 0, 0, 0);
                  ObjectSet   (N_order, OBJPROP_CORNER, UGOL);
                  ObjectSet   (N_order, OBJPROP_XDISTANCE, X_order);
                  ObjectSet   (N_order, OBJPROP_YDISTANCE, Y_order+UG*Kol);
                  if (orders[Kol][2]!=0) txt = StringConcatenate("  ",DoubleToStr((Bid-orders[Kol][2])/Point,0)); 
                  else txt = "  __";
                  if (orders[Kol][3]!=0) txt = StringConcatenate(txt,"  " ,DoubleToStr((Bid-orders[Kol][3])/Point,0));  
                  else txt = StringConcatenate(txt,"  __   ");
                  ObjectSetText(N_order,StringConcatenate(
                     "  " ,DoubleToStr(orders[Kol][8],0),       //прибыль в п.
                     "  " ,            текст,                   //тип ордера
                     "  " ,            orders[Kol][4],          //номер ордера
                     "   ",DoubleToStr(orders[Kol][1],Digits),  //цена
                     "  " ,DoubleToStr(orders[Kol][5] ,2),      //лот
                     txt  ,TimeToStr  (TimeCurrent()-orders[Kol][9],TIME_MINUTES)),8,"Arial",WhiteColor);}
               //--------------------------------------------------------------------------------------------------------------------------------------------------
            }//if (tip<2)
         }
      }
   }
   orders[0][0] = Kol; // Сохраняем в нулевую ячеечку
   if (UGOL<4){
   if (orders[0][0]!=0)  ObjectSetText("_SHAPKA_","      п    тип      ОРДЕР     ц откр   лот  SL  ТР  время",8,"Arial",NameColor);
   else ObjectSetText("_SHAPKA_"," ",8,"Arial",Aquamarine);
   ObjectSet("_SHAPKA_", OBJPROP_CORNER, UGOL);}
return;
}
//-------------------------------------------------------------------------------

