
#property copyright "Copyright © 2009, BroCo Investments Inc."
#property link      "http://www.brocompany.com"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_minimum 0.0
#property indicator_maximum 0.1

#define SYMBOLS_MAX 1024

#define TYPE            0
#define SYMBOL          1
#define TICKET          0
#define VOLUME          1
#define OPEN_PRICE      2
#define SL              3
#define TP              4
#define CURRENT_PRICE   5
#define COMISSEE        6
#define SWAP            7
#define PROFIT          8
#define DEALS           9

extern color ExtColor=LightSeaGreen;

string ExtName="Broco Indicator";
string ExtSymbols[SYMBOLS_MAX];
double ExtTickets[SYMBOLS_MAX];
int    ExtSymbolsTotal=0;
double ExtSymbolsSummaries[SYMBOLS_MAX][10];
datetime ExtSymbolsDatetimes[SYMBOLS_MAX];
string ExtSymbolsStrings[SYMBOLS_MAX][2];
int    ExtLines=-1;
string ExtCols[12]={"Ордер",
                   "Время",
                   "Тип",
                   "Объем",
                   "Символ",
                   "Цена",
                   "S/L",
                   "T/P",
                   "Цена",
                   "Комиссия",
                   "Своп",
                   "Прибыль"};
int    ExtShifts[12]={ 10, 80, 200, 250, 300, 380, 450, 520,580, 650, 730, 780 };
int    ExtVertShift=14;
double ExtMapBuffer[];

void init()
  {
	IndicatorShortName(ExtName);
   SetIndexBuffer(0,ExtMapBuffer);
   SetIndexStyle(0,DRAW_NONE);
   IndicatorDigits(0);
	SetIndexEmptyValue(0,0.0);
  }


void deinit()
  {
   int windex=WindowFind(ExtName);
   if(windex>0) ObjectsDeleteAll(windex);
  }


void start()
  {
  
  string sym = Symbol();
  //while(!IsStopped())     // До тех пор, пока пользователь..     
    {                    // ..не прекратит исполнение программы     
      RefreshRates();      // Обновление данных
   string name;
   int    i,col,line,windex=WindowFind(ExtName);
  //----
   if(windex>0) ObjectsDeleteAll(windex);
      if(windex<0) return;
   //---- header line
   ExtLines = -1;
   if(ExtLines<0)
     {
      for(col=0; col<12; col++)
        {
         name="Head_"+col;
         if(ObjectCreate(name,OBJ_LABEL,windex,0,0))
           {
            ObjectSet(name,OBJPROP_XDISTANCE,ExtShifts[col]);
            ObjectSet(name,OBJPROP_YDISTANCE,ExtVertShift);
            ObjectSetText(name,ExtCols[col],9,"Arial",ExtColor);
           }
        }
      ExtLines=0;
     }
//----
   ArrayInitialize(ExtSymbolsSummaries,0.0);
   for(int ii=0;ii<SYMBOLS_MAX;ii++)
   for(int jj=0;jj<10;jj++)
   ExtSymbolsSummaries[ii][jj] = 0.0;
   int total=Analyze();
  
  
   if(total>0)
     {
      line=0;
      for(i=0; i<total; i++)
        {
          if(ExtSymbolsSummaries[i][DEALS]<=0) 
          {
            continue;
            Print("index="+i);
          }
         line++;
      //   ---- add line
         if(line>ExtLines)
           {
            int y_dist=ExtVertShift*(line+1)+1;
            for(col=0; col<12; col++)
              {
               name="Line_"+line+"_"+col;
               if(ObjectCreate(name,OBJ_LABEL,windex,0,0))
                 {
                  ObjectSet(name,OBJPROP_XDISTANCE,ExtShifts[col]);
                  ObjectSet(name,OBJPROP_YDISTANCE,y_dist);
                 }
              }
            ExtLines++;
           }
           
           
           
          int    digits=MarketInfo(ExtSymbolsStrings[i][SYMBOL],MODE_DIGITS);  
          
          name="Line_"+line+"_0";
          ObjectSetText(name,DoubleToStr(ExtSymbolsSummaries[i][TICKET],0),9,"Arial",ExtColor);
          name="Line_"+line+"_1";
          ObjectSetText(name,TimeToStr(ExtSymbolsDatetimes[i],TIME_DATE|TIME_MINUTES),9,"Arial",ExtColor);
          name="Line_"+line+"_2";
          ObjectSetText(name,ExtSymbolsStrings[i][TYPE],9,"Arial",ExtColor);
          name="Line_"+line+"_3";
          ObjectSetText(name,DoubleToStr(ExtSymbolsSummaries[i][VOLUME],2),9,"Arial",ExtColor);
          name="Line_"+line+"_4";
          ObjectSetText(name,ExtSymbolsStrings[i][SYMBOL],9,"Arial",ExtColor);
          name="Line_"+line+"_5";
          ObjectSetText(name,DoubleToStr(ExtSymbolsSummaries[i][OPEN_PRICE],digits),9,"Arial",ExtColor);
          name="Line_"+line+"_6";
          ObjectSetText(name,DoubleToStr(ExtSymbolsSummaries[i][SL],digits),9,"Arial",ExtColor);
          name="Line_"+line+"_7";
          ObjectSetText(name,DoubleToStr(ExtSymbolsSummaries[i][TP],digits),9,"Arial",ExtColor);
          name="Line_"+line+"_8";
          ObjectSetText(name,DoubleToStr(ExtSymbolsSummaries[i][CURRENT_PRICE],digits),9,"Arial",ExtColor);
          name="Line_"+line+"_9";
          ObjectSetText(name,DoubleToStr(ExtSymbolsSummaries[i][COMISSEE],2),9,"Arial",ExtColor);
          name="Line_"+line+"_10";
          ObjectSetText(name,DoubleToStr(ExtSymbolsSummaries[i][SWAP],2),9,"Arial",ExtColor);
          name="Line_"+line+"_11";
          ObjectSetText(name,DoubleToStr(ExtSymbolsSummaries[i][PROFIT],2),9,"Arial",ExtColor);    
        }
        //Print(line);
     }
     
//---- remove lines
 //  if(total<ExtLines)
 //    {
 //     for(line=ExtLines; line>total; line--)
 //       {
 //        name="Line_"+line+"_0";
 //        ObjectSetText(name,"");
 //        name="Line_"+line+"_1";
 //        ObjectSetText(name,"");
 //        name="Line_"+line+"_2";
 //        ObjectSetText(name,"");
 //        name="Line_"+line+"_3";
  //       ObjectSetText(name,"");
   //      name="Line_"+line+"_4";
    //     ObjectSetText(name,"");
     //    name="Line_"+line+"_5";
      //   ObjectSetText(name,"");
       //  name="Line_"+line+"_6";
        // ObjectSetText(name,"");
         //name="Line_"+line+"_7";
 //        ObjectSetText(name,"");
  //       name="Line_"+line+"_8";
   //      ObjectSetText(name,"");
    //     name="Line_"+line+"_9";
     //    ObjectSetText(name,"");
      //   name="Line_"+line+"_10";
       ///  ObjectSetText(name,"");
        // name="Line_"+line+"_11";
         //ObjectSetText(name,"");
   //     }
    // }
//---- to avoid minimum==maximum
   ExtMapBuffer[Bars-1]=-1;
    
    
   //  Print(line);
      //string name;
    name="Line_"+(line+1)+"_0";
    double total_profit=0.0;
    for(i=0;i<line;i++)
    {
         total_profit+=ExtSymbolsSummaries[i][PROFIT]+ExtSymbolsSummaries[i][COMISSEE]+ExtSymbolsSummaries[i][SWAP];
    }
    line++;
    if(ObjectCreate(name,OBJ_LABEL,windex,0,0))
     {
     ExtLines++;
     string Total;
     Total = "Баланс: "+DoubleToStr(AccountBalance(),2)+
             " Средства: "+DoubleToStr(AccountBalance()+total_profit,2)+
             " Залог: "+DoubleToStr(AccountMargin(),2)+
             " Свободно: "+DoubleToStr(AccountFreeMargin(),2);
             //" Уровень: ";//+DoubleToStr( ( ( AccountBalance()+total_profit )/AccountMargin()*100 ),2 );
            ObjectSet(name,OBJPROP_XDISTANCE,ExtShifts[0]);
            ObjectSet(name,OBJPROP_YDISTANCE,ExtVertShift*(line+1));
            ObjectSetText(name,Total,9,"Arial",Red);   
     }
     //name="Line_"+line+"_5";
     //if(ObjectCreate(name,OBJ_LABEL,windex,0,0))
    // {
     //string Total2;
     //Total2 = " Уровень: "+DoubleToStr( ( ( AccountBalance()+total_profit )/AccountMargin()*100 ),2 )+"%";
     //       ObjectSet(name,OBJPROP_XDISTANCE,ExtShifts[6]);
     //       ObjectSet(name,OBJPROP_YDISTANCE,ExtVertShift*(line+1));
     //       ObjectSetText(name,Total2,9,"Arial",Red);   
    // }
    name="Line_"+line+"_11";
    if(ObjectCreate(name,OBJ_LABEL,windex,0,0))
     {
            ObjectSet(name,OBJPROP_XDISTANCE,ExtShifts[11]);
            ObjectSet(name,OBJPROP_YDISTANCE,ExtVertShift*(line+1));
            ObjectSetText(name,DoubleToStr(total_profit,2),9,"Arial",Red);   
     }
     
     if(sym!="SYNTENIC1")
      {
         line++;
         if(line>ExtLines)
           {
            y_dist=ExtVertShift*(line+1)+1;
            //for(col=0; col<12; col++)
             // {
               name="Line_"+line+"_"+0;
               if(ObjectCreate(name,OBJ_LABEL,windex,0,0))
                 {
                  ObjectSet(name,OBJPROP_XDISTANCE,ExtShifts[0]);
                  ObjectSet(name,OBJPROP_YDISTANCE,y_dist);
                 }
            //  }
            ExtLines++;
           }
           name="Line_"+line+"_0";
            ObjectSetText(name,"Выбран неверный символ. Правильный символ SYNTENIC1",9,"Arial",Red);
         
      }
    // Sleep(500);
     }
    // return;
//----
  }


int Analyze()
  {
   double profit;
   int    i,index,type,total=OrdersTotal();
   int retval=0;
   //insertion by M.Ten
   // 29.09.09 18-54
   //----------
   //Print("orderstotal = "+total);
   int my_temp_index=0;
   int j=0;
   string name;
   string name_of_prices;
   double close_price,open_price;
   double tick_size,tick_price,lots;
   //----------m.ten-----
//----
ExtSymbolsTotal = 0;
//ExtSymbolsTotal = total;
   for(i=0; i<total; i++)
     {
      if(!OrderSelect(i,SELECT_BY_POS)) continue;
      type=OrderType();
      if(type!=OP_BUY && type!=OP_SELL) continue;
      index=SymbolsIndex(OrderTicket());
      if(index<0 || index>=SYMBOLS_MAX) continue;

      ExtSymbolsSummaries[index][VOLUME]+=OrderLots();
      ExtSymbolsSummaries[index][OPEN_PRICE]+=OrderOpenPrice();
      
      name = StringSetChar(name,0,0);
      name = StringConcatenate( OrderSymbol(),"#I");

      double bid   =MarketInfo(name,MODE_BID);             
      double ask   =MarketInfo(name,MODE_ASK);

      double bid2   =MarketInfo(OrderSymbol(),MODE_BID);             
      double ask2   =MarketInfo(OrderSymbol(),MODE_ASK);

      if(type==OP_BUY)
      {
          if(bid!=0) ExtSymbolsSummaries[index][CURRENT_PRICE]+=bid;               
          else ExtSymbolsSummaries[index][CURRENT_PRICE]+=bid2;
      }
      else
      {
          if(ask!=0) ExtSymbolsSummaries[index][CURRENT_PRICE]+=ask;
          else ExtSymbolsSummaries[index][CURRENT_PRICE]+=ask2;
      }
      
      ExtSymbolsSummaries[index][DEALS]++;
      
      //Print("index=",index);
      
      if(type==OP_BUY)
      {
         if(bid==0) close_price = bid2;
         else close_price = bid;
      }
      else
      {
         if(ask==0) close_price = ask2;
         else close_price = ask;
      }
      
      tick_size = MarketInfo(name,MODE_TICKSIZE);
      tick_price = MarketInfo(name,MODE_TICKVALUE);
      
      if(tick_size==0) tick_size = MarketInfo(OrderSymbol(),MODE_TICKSIZE);
      if(tick_price==0) tick_price = MarketInfo(OrderSymbol(),MODE_TICKVALUE);      
      if(type==OP_BUY) profit =( ( close_price - ExtSymbolsSummaries[index][OPEN_PRICE] )/tick_size*tick_price*OrderLots()) + OrderCommission()+OrderSwap();
      else profit =( ( ExtSymbolsSummaries[index][OPEN_PRICE] - close_price )/tick_size*tick_price*OrderLots()) + OrderCommission()+OrderSwap();
      //ExtSymbolsSummaries[index][PROFIT]+=profit;
    
    
      if((OrderClosePrice()-ExtSymbolsSummaries[index][OPEN_PRICE])==0) profit = OrderProfit();
      else profit = ( close_price - ExtSymbolsSummaries[index][OPEN_PRICE] )*OrderProfit()/(OrderClosePrice()-ExtSymbolsSummaries[index][OPEN_PRICE]);
      ExtSymbolsSummaries[index][PROFIT]+=profit;
      
      
      ExtSymbolsSummaries[index][TICKET] = OrderTicket();
      ExtSymbolsSummaries[index][COMISSEE] = OrderCommission();
      ExtSymbolsSummaries[index][SWAP] = OrderSwap();
      ExtSymbolsSummaries[index][SL] = OrderStopLoss();
      ExtSymbolsSummaries[index][TP] = OrderTakeProfit();
      ExtSymbolsDatetimes[index] = OrderOpenTime();
      
      if(OrderType() == 0 ) ExtSymbolsStrings[index][TYPE] = "Buy";
      if(OrderType() == 1 ) ExtSymbolsStrings[index][TYPE] = "Sell";
      ExtSymbolsStrings[index][SYMBOL] = OrderSymbol();    
      
     }
//----
   total=0;
   for(i=0; i<ExtSymbolsTotal; i++)
     {
      if(ExtSymbolsSummaries[i][DEALS]>0) total++;
     }
//----
   return(total);
  }


int SymbolsIndex(int Ticket)
  {
  bool found=false;
//----
   for(int i=0; i<ExtSymbolsTotal; i++)
     {
     //Print("ticket=",Ticket,"exttiket=",ExtTickets[i]);
      if(Ticket==ExtTickets[i])
        {
         found=true;
         break;
        }
     }
//----
   //if(found) return(i);
   if(ExtSymbolsTotal>=SYMBOLS_MAX) return(-1);
   i=ExtSymbolsTotal;
   ExtSymbolsTotal++;
  //ExtSymbols[i]=SymbolName;
   ExtTickets[i]=Ticket;
   ExtSymbolsSummaries[i][TICKET]=0;
   ExtSymbolsSummaries[i][VOLUME]=0;
   ExtSymbolsSummaries[i][OPEN_PRICE]=0;
   ExtSymbolsSummaries[i][SL]=0;
   ExtSymbolsSummaries[i][TP]=0;
   ExtSymbolsSummaries[i][CURRENT_PRICE]=0;
   ExtSymbolsSummaries[i][COMISSEE]=0;
   ExtSymbolsSummaries[i][SWAP]=0;
   ExtSymbolsSummaries[i][PROFIT]=0;
   ExtSymbolsDatetimes[i] = 0;
   ExtSymbolsStrings[i][TYPE] = 0;
   ExtSymbolsStrings[i][SYMBOL] = 0;
//----
   return(i);
  }
//+------------------------------------------------------------------+