

#property indicator_chart_window


input int    Magic =0;
input string Symb_Dop_1 ="";
input string Symb_Dop_2 ="";
input string Symb_Dop_3 ="";
input string Symb_Dop_4 ="";
input string Symb_Dop_5 ="";
input string Symb_Dop_6 ="";
input string Symb_Dop_7 ="";
input string Symb_Dop_8 ="";
input string Symb_Dop_9 ="";
input string Symb_Dop_10 ="";


string sym [42];
void Symbol_baza (int cn) {
      switch (cn){
          case 1: sym [1] = "AUDCAD"; break; 
          case 2: sym [2] = "AUDCHF"; break; 
          case 3: sym [3]= "AUDJPY"; break; 
          case 4: sym [4]= "AUDNZD"; break; 
          case 5: sym [5]= "AUDUSD"; break; 
          case 6: sym [6] = "CADJPY"; break; 
          case 7: sym [7] = "CHFJPY"; break;  
          case 8: sym [8] = "EURAUD"; break; 
          case 9: sym [9] = "EURCAD"; break; 
          case 10: sym [10] = "EURGBP"; break; 
          case 11: sym [11] = "EURJPY"; break; 
          case 12: sym [12] = "EURNZD"; break; 
          case 13: sym [13] = "EURUSD"; break; 
          case 14: sym [14] = "GBPAUD"; break; 
          case 15: sym [15] = "GBPCAD"; break; 
          case 16: sym [16] = "GBPCHF"; break; 
          case 17: sym [17] = "GBPJPY"; break; 
          case 18: sym [18] = "GBPUSD"; break; 
          case 19: sym [19] = "NZDJPY"; break; 
          case 20: sym [20] = "NZDUSD"; break; 
          case 21: sym [21] = "USDCAD"; break; 
          case 22: sym [22] = "USDCHF"; break; 
          case 23: sym [23] = "USDJPY"; break; 
          case 24: sym [24] = "GBPNZD"; break; 
          case 25: sym [25] = "NZDCHF"; break; 
          case 26: sym [26] = "NZDCAD"; break; 
          case 27: sym [27] = "CADCHF"; break; 
          case 28: sym [28] = "XAUUSD"; break;
          case 29: sym [29] = "Gold";   break;
          case 30: sym [30] = "XAGUSD"; break;
          case 31: sym [31] = "Silver"; break;
          case 32: sym [32] = "USDRUB"; break;
          case 33: sym [33] = Symb_Dop_1; break;
          case 34: sym [34] = Symb_Dop_2; break;
          case 35: sym [35] = Symb_Dop_3; break;
          case 36: sym [36] = Symb_Dop_4; break;
          case 37: sym [37] = Symb_Dop_5; break;
          case 38: sym [38] = Symb_Dop_6; break;
          case 39: sym [39] = Symb_Dop_7; break;
          case 40: sym [40] = Symb_Dop_8; break;
          case 41: sym [41] = Symb_Dop_9; break;
          case 42: sym [42] = Symb_Dop_10; break;
 }
    return; 
}
    
// переменная, которая будет хранить количество ордеров, принадлежащих эксперту:
int _ExpertOrdersTotal = 0;

// массивы, в которых будут хранитсья характеристики ордеров:
int _OrderTicket[], _OrderType[];
double _OrderLots[], _OrderOpenPrice[], _OrderStopLoss[], _OrderTakeProfit[];
double _OrderProfit[], _OrderSwap[], _OrderCommission[];
datetime _OrderOpenTime[], _OrderExpiration[];
string _OrderSymbol[], _OrderComment[];


int init() {
   //string p = Information();
   //AllOrdersInit(); 
   //Comment(p);
   return (0);
}

int deinit() {
   return (0);
}

int start() {
   if (NewBar(1)){ 
   AllOrdersInit(); 
   string t = Information();
   Print (t);
   Comment("\n", "\n", "\n", t);
   }
   return (0);
   
}

string Information() {
   
   int    cntbuy [42]; 
   int    cntsell[42];
   double lotbuy [42];
   double lotsell[42];
   double profitbuy [42];
   double profitsell [42];
   string text = ""; 
   int    cnt = 0; 
   double lot = 0.0, profit = 0.0;
   
   ArrayInitialize(cntbuy,		   0 );
   ArrayInitialize(cntsell,		0 );
   ArrayInitialize(lotbuy,		   0 );
   ArrayInitialize(lotsell,		0 );
   ArrayInitialize(profitbuy,		0 );
   ArrayInitialize(profitsell,	0 );
    
   for (int i = 1; i <= 42; i++) {
        Symbol_baza (i);
        cntbuy [i] = CountTrades(sym [i], "buy");
        cntsell[i] = CountTrades(sym [i], "sell"); 

        if (cntbuy [i]+cntsell [i] >0) {
           lotbuy [i] = Balance( sym [i], "buy", "Lot"); 
           lotsell [i] = Balance( sym [i], "sell", "Lot"); 
           profitbuy [i] = Balance( sym [i], "buy", "Profit");
           profitsell [i] = Balance( sym [i], "sell", "Profit");
           //Print (i, "/", sym [i],  " / cntbuy = ",cntbuy [i],"/ cntsell = ",cntsell[i],
           //      "/ lotb = ",lotbuy [i], "/ lots = ",lotsell [i], "/ profb = ", profitbuy [i],
           //      "/ profs = ",  profitsell [i]);
           text=StringConcatenate(text,sym [i], ": Счет =", cntbuy [i]+cntsell[i], " / Лот = ", lotbuy [i]+lotsell [i],
                                  " / Профит = ", profitbuy [i]+profitsell [i], "\n");
           cnt = cnt+cntbuy [i]+cntsell [i];
           lot = lot + lotbuy [i]+lotsell [i];
           profit = profit + profitbuy [i]+profitsell [i];
        }
   }
   text=StringConcatenate(text,"---------------------------------------------------------------","\n");
   text=StringConcatenate(text,sym [i], " Всего сделок =", cnt, " / Сумарный лот = ", lot ,
                                  " / Итого Профит = ", profit, "\n");
   return (text);
}

//--------------------------------------------------------------------

double Balance(string symbol, string type, string pok) {
   double rez = 0;
   for (int i = _ExpertOrdersTotal - 1; i >= 0; i--) {
         if (_OrderSymbol [i] == symbol) {
            if (type == "buy" && _OrderType[i] == OP_BUY) {
                  if (pok == "Profit") rez = rez + _OrderProfit[i] + _OrderSwap[i] + _OrderCommission[i];
                  if (pok == "Lot") rez += _OrderLots [i];
               }
            if (type == "sell" && _OrderType [i] == OP_SELL) {
                  if (pok == "Profit") rez = rez + _OrderProfit[i] + _OrderSwap[i] + _OrderCommission[i];
                  if (pok == "Lot") rez += _OrderLots[i];
               }
         }
   }
   return (rez);
}

//--------------------------------------------------------------------

int CountTrades(string symbol, string type) {
   int rez = 0;
   for (int i =_ExpertOrdersTotal - 1; i >= 0; i--) {
      if (_OrderSymbol [i] == symbol) {
            if (type == "buy" && _OrderType[i] == OP_BUY) rez++;
            if (type == "sell" && _OrderType[i] == OP_SELL) rez++;
      }
   }
   return (rez);
}

//--------------------------------------------------------------------

bool NewBar(int tf)
  {
   static datetime newbar;
   if(newbar==0)newbar=iTime(Symbol (),tf,0);
   bool res=false;
   if(newbar==iTime(Symbol (),tf,0))return(res);
   newbar=iTime(Symbol (),tf,0);
   res=true;
   return(res);
}

//--------------------------------------------------------------------

void AllOrdersInit()
{
	int _GetLastError = 0, _OrdersTotal = OrdersTotal();
	_ExpertOrdersTotal = 0;

	// изменяем размеры массивов под текущее кол-во позиций
	// (если _OrdersTotal = 0, меняем размер массивов на 1)
	int temp_value = MathMax( _OrdersTotal, 1 );
	ArrayResize( _OrderSymbol,       temp_value );
	ArrayResize( _OrderTicket,			temp_value );
	ArrayResize( _OrderType,			temp_value );
	ArrayResize( _OrderLots,			temp_value );
	ArrayResize( _OrderOpenPrice,		temp_value );
	ArrayResize( _OrderStopLoss,		temp_value );
	ArrayResize( _OrderTakeProfit,	temp_value );
	ArrayResize( _OrderOpenTime,		temp_value );
	ArrayResize( _OrderProfit,			temp_value );
	ArrayResize( _OrderSwap,			temp_value );
	ArrayResize( _OrderCommission,	temp_value );
	ArrayResize( _OrderComment,		temp_value );
	ArrayResize( _OrderExpiration,	temp_value );

	// обнуляем массивы
	ArrayInitialize( _OrderTicket,		0 );
	ArrayInitialize( _OrderType,			0 );
	ArrayInitialize( _OrderLots,			0 );
	ArrayInitialize( _OrderOpenPrice,	0 );
	ArrayInitialize( _OrderStopLoss,		0 );
	ArrayInitialize( _OrderTakeProfit,	0 );
	ArrayInitialize( _OrderOpenTime,		0 );
	ArrayInitialize( _OrderProfit,		0 );
	ArrayInitialize( _OrderSwap,			0 );
	ArrayInitialize( _OrderCommission,	0 );
	ArrayInitialize( _OrderExpiration,	0 );

	for ( int z = _OrdersTotal - 1; z >= 0; z -- )
	{
		if ( !OrderSelect( z, SELECT_BY_POS ) )
		{
			_GetLastError = GetLastError();
			Print( "OrderSelect( ", z, ", SELECT_BY_POS ) - Error #", _GetLastError );
			continue;
		}
		if ( OrderMagicNumber() == Magic  )
		{
			// заполняем массивы
			_OrderSymbol    	[_ExpertOrdersTotal] = OrderSymbol();
			_OrderTicket		[_ExpertOrdersTotal] = OrderTicket();
			_OrderType			[_ExpertOrdersTotal] = OrderType();
			_OrderLots			[_ExpertOrdersTotal] = NormalizeDouble( OrderLots(),2 );
			_OrderOpenPrice	[_ExpertOrdersTotal] = NormalizeDouble( OrderOpenPrice(), Digits );
			_OrderStopLoss		[_ExpertOrdersTotal] = NormalizeDouble( OrderStopLoss(), Digits );
			_OrderTakeProfit	[_ExpertOrdersTotal] = NormalizeDouble( OrderTakeProfit(), Digits );
			_OrderOpenTime		[_ExpertOrdersTotal] = OrderOpenTime();
			_OrderProfit		[_ExpertOrdersTotal] = NormalizeDouble( OrderProfit(), 2 );
			_OrderSwap			[_ExpertOrdersTotal] = NormalizeDouble( OrderSwap(), 2 );
			_OrderCommission	[_ExpertOrdersTotal] = NormalizeDouble( OrderCommission(), 2 );
			_OrderComment		[_ExpertOrdersTotal] = OrderComment();
			_OrderExpiration	[_ExpertOrdersTotal] = OrderExpiration();
         //  Print (_ExpertOrdersTotal, " / ", _OrderType			[_ExpertOrdersTotal], 
         //  " / ", _OrderSymbol    	[_ExpertOrdersTotal] );
			_ExpertOrdersTotal ++;

			
		}
				
	}


	// изменяем размеры массивов под кол-во позиций, принадлежащих эксперту
	// (если _ExpertOrdersTotal = 0, меняем размер массивов на 1)
	temp_value = MathMax( _ExpertOrdersTotal, 1 );
	ArrayResize( _OrderSymbol,       temp_value );
	ArrayResize( _OrderTicket,			temp_value );
	ArrayResize( _OrderType,			temp_value );
	ArrayResize( _OrderLots,			temp_value );
	ArrayResize( _OrderOpenPrice,		temp_value );
	ArrayResize( _OrderStopLoss,		temp_value );
	ArrayResize( _OrderTakeProfit,	temp_value );
	ArrayResize( _OrderOpenTime,		temp_value );
	ArrayResize( _OrderProfit,			temp_value );
	ArrayResize( _OrderSwap,			temp_value );
	ArrayResize( _OrderCommission,	temp_value );
	ArrayResize( _OrderComment,		temp_value );
	ArrayResize( _OrderExpiration,	temp_value );
}

//--------------------------------------------------------------------