#property copyright "Copyright © 2010, InstaForex"
#property link      "http://instaforex.com"
#property indicator_minimum 0.0
#property indicator_maximum 0.1

extern color HeaderColor=Gold;
extern color PosColor=Lime;
extern color NegColor=Red;
extern color NulColor=Silver;
extern int FontSize=14;

#property indicator_separate_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
string ShortIndName="ShowOrders";

int init() {
	IndicatorShortName(ShortIndName);
   IndicatorDigits(0);
	SetIndexEmptyValue(0,0.0);	
  return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit() {
  int mwind=WindowFind(ShortIndName);
  if(mwind>0) ObjectsDeleteAll(mwind);   
  return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int find_from_array(string arr[][], string value, int index=0, int i=1000) {
  int n=0;
  string v="";
  for(n=0; n<=i; n++) {
    if(arr[n][index]==value)
      return(n);
  }
  return(-1);
}

double s_pl(int mode) {
  double result;
  int i;
    for(i=OrdersTotal()-1; i>=0; i--) {
    if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
    if((mode==1)&&((OrderProfit()+OrderCommission()+OrderSwap())>=0))
      result+=OrderProfit()+OrderCommission()+OrderSwap();
    if((mode==2)&&((OrderProfit()+OrderCommission()+OrderSwap())<0))
      result+=OrderProfit()+OrderCommission()+OrderSwap();        
  } 
  return(result);
}

double net_orders_by_lots(int type, double lots, string symbol) {
  int    i      = 0,
         n      = 0;
  double result = 0.0;
  for(i=OrdersTotal()-1; i>=0; i--) {
    if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
    if((OrderType()==type) && (OrderLots()==lots) && (OrderSymbol()==symbol)) {
      n++;
      result+=OrderOpenPrice();
    }  
  }
  return(result/n);
}

int net_orders(int type, string symbol) {
  int i      = 0,
      result = 0;
  for(i=OrdersTotal()-1; i>=0; i--) {
    if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
    if((OrderType()==type) && (OrderSymbol()==symbol))
      result++;
  }
  return(result);
}

double net_price(int type, string symbol, int orders) {
  int    i      = 0,
         n      = 0;
  double result = 0;
  for(i=OrdersTotal()-1; i>=0; i--) {
    if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
    if((OrderType()==type) && (OrderSymbol()==symbol))
      result+=net_orders_by_lots(OrderType(), OrderLots(), OrderSymbol());
  }
  return(result/net_orders(type, symbol));
}

double profit_by_symbol(string symb) {
  int    i      = 0,
         n      = 0;
  double result = 0;
  for(i=OrdersTotal()-1; i>=0; i--) {
    if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
    if((OrderType()==OP_BUY) || (OrderType()==OP_SELL) && (OrderSymbol()==symb))
    result+=OrderProfit()+OrderCommission();
  }
  return(result);
}


int start() {
  string o_arr[500][10];
  string IndHeaderFull[10]= {"Symbol   ",
                             "Buy Lots", 
                             "Sell Lots",
                             "Orders",
                             "Net Buy Price",
                             "Net Sell Price",
                             "Net Lots",
                             "Swap   ",
                             "Profit    ",
                             ""};
  int    type       = -1,
         xd         = 15,
         yd         = 15,
         mwind      = WindowFind(ShortIndName),
         n          = 0,
         i          = 0,
         i_true     = 0,
         digits     = 0;
  double lot        = 0.0,
         price      = 0.0,
         swap       = 0.0;
  string symb       = "",
         name       = "",
         net_symb   = "",
         font       = "Arial",
         temp_text  = "";
  color  color_show = CLR_NONE;  
//------------------
  if(mwind>0)
    ObjectsDeleteAll(mwind);
  else
    return;
  for(i=0; i<500; i++) { //простенькая инициализация массива
    o_arr[i][0]="--";
    o_arr[i][1]="--";
    o_arr[i][2]="--";
    o_arr[i][3]="--";
    o_arr[i][4]="--";
    o_arr[i][5]="--";
    o_arr[i][6]="--";
    o_arr[i][7]="--";
    o_arr[i][8]="--";    
    o_arr[i][9]="";

  }
  
  for(i=OrdersTotal()-1; i>=0; i--) {
    if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
    if((OrderType()!=OP_SELL) && (OrderType()!=OP_BUY)) continue;
    symb   = OrderSymbol();
    type   = OrderType();
    lot    = OrderLots();
    price  = OrderOpenPrice();
    digits = MarketInfo(OrderSymbol(),MODE_DIGITS);
    swap   = OrderSwap();
    i_true = find_from_array(o_arr,OrderSymbol());

    if(i_true==-1) i_true=i;
    o_arr[i_true][0]=symb;
    o_arr[i_true][3]=DoubleToStr(StrToInteger(o_arr[i_true][3])+1,0);
    if(type==0) {
      o_arr[i_true][1]=DoubleToStr(StrToDouble(o_arr[i_true][1])+lot,2);
      o_arr[i_true][4]=DoubleToStr(net_price(OrderType(), OrderSymbol(), StrToInteger(o_arr[i_true][3])),digits+1);
    }  
    if(type==1) {
      o_arr[i_true][2]=DoubleToStr(StrToDouble(o_arr[i_true][2])+lot,2);  
      o_arr[i_true][5]=DoubleToStr(net_price(OrderType(), OrderSymbol(), StrToInteger(o_arr[i_true][3])),digits+1);
    }

    o_arr[i_true][6]=DoubleToStr((StrToDouble(o_arr[i_true][1])+(-1)*StrToDouble(o_arr[i_true][2])),2);
    o_arr[i_true][8]=DoubleToStr(profit_by_symbol(symb),2);
    o_arr[i_true][7]=DoubleToStr(StrToDouble(o_arr[i_true][9])+swap,2);    
    if(NormalizeDouble(StrToDouble(o_arr[i_true][1]),2)>NormalizeDouble(StrToDouble(o_arr[i_true][2]),2)) 
      o_arr[i_true][9]=CharToStr(236);   
    if(NormalizeDouble(StrToDouble(o_arr[i_true][1]),2)<NormalizeDouble(StrToDouble(o_arr[i_true][2]),2)) 
      o_arr[i_true][9]=CharToStr(238);  
    if((NormalizeDouble(StrToDouble(o_arr[i_true][1]),2)==NormalizeDouble(StrToDouble(o_arr[i_true][2]),2))) 
      o_arr[i_true][9]=CharToStr(79);      
  } //end for(i=OrdersTotal()-1; i>=0; i--)
  font="Arial";
  color_show=HeaderColor;

    name="ShowOrders";         
    if(ObjectCreate(name,OBJ_LABEL,mwind,0,0)) {
      ObjectSet(name,OBJPROP_XDISTANCE,15);
      ObjectSet(name,OBJPROP_YDISTANCE,yd);
      ObjectSetText(name,"InstaForex MetaTrader Desk Panel",FontSize,font,color_show);
    }  
    yd+=FontSize+FontSize*1.3;

  
  if(OrdersTotal()==0) {
    name="error";         
    if(ObjectCreate(name,OBJ_LABEL,mwind,0,0)) {
      ObjectSet(name,OBJPROP_XDISTANCE,15);
      ObjectSet(name,OBJPROP_YDISTANCE,yd);
      ObjectSetText(name,"There is no opened trades",FontSize,font,color_show);
    }
    return(0);    
  } 
  for(n=0; n<11; n++) {
    name="ShowOrders_"+n;         
    if(ObjectCreate(name,OBJ_LABEL,mwind,0,0)) {
      ObjectSet(name,OBJPROP_XDISTANCE,xd);
      ObjectSet(name,OBJPROP_YDISTANCE,yd);
      ObjectSetText(name,IndHeaderFull[n],FontSize,font,color_show);
      xd+=0.7*FontSize*StringLen(IndHeaderFull[n])+FontSize;
    }
  }
  xd=15;
  yd+=FontSize+FontSize*0.8;
  for(i=0; i<500; i++) {
    if(o_arr[i][0]=="--") continue;  
    if(StrToDouble(o_arr[i][7])+StrToDouble(o_arr[i][8])>0)
      color_show=PosColor;
    else
      color_show=NegColor;
      
    if(StrToDouble(o_arr[i][7])+StrToDouble(o_arr[i][8])==0)
      color_show=NulColor;      
    
    for(n=0; n<10; n++) {
      if(n==9) font="WingDings";
      name="ShowOrders_"+o_arr[i][0]+n;
         
      if(ObjectCreate(name,OBJ_LABEL,mwind,0,0)) {
        ObjectSet(name,OBJPROP_XDISTANCE,xd);
        ObjectSet(name,OBJPROP_YDISTANCE,yd);
        ObjectSetText(name,o_arr[i][n],FontSize,font,color_show);      
        xd+=0.7*FontSize*StringLen(IndHeaderFull[n])+FontSize;
      }
    }  
    font="Arial";
    xd=15;
    yd+=FontSize+FontSize*0.7;
  }
  font="Arial";
  color_show=HeaderColor;
  
  yd+=FontSize+FontSize*0.2; 
  name="ShowOrders_a_1";      
  if(ObjectCreate(name,OBJ_LABEL,mwind,0,0)) {
    ObjectSet(name,OBJPROP_XDISTANCE,xd);
    ObjectSet(name,OBJPROP_YDISTANCE,yd);
    ObjectSetText(name,"Current Margin: "+DoubleToStr(AccountMargin(),2)
                       +" USD, Margin Level: "+DoubleToStr(AccountEquity()/AccountMargin()*100,2)+"%",
                  FontSize,font,color_show);      
  }       
  yd+=FontSize+FontSize*0.9;
  
  name="ShowOrders_a_2_0"; 
  temp_text="Summary profit: "+DoubleToStr(s_pl(1),2)+" USD, Summary loss: "+DoubleToStr(s_pl(2),2)+" USD.";     
  if(ObjectCreate(name,OBJ_LABEL,mwind,0,0)) {
    ObjectSet(name,OBJPROP_XDISTANCE,xd);
    ObjectSet(name,OBJPROP_YDISTANCE,yd);
    ObjectSetText(name,temp_text,FontSize,font,color_show);      
  }
  
  xd+=0.64*FontSize*StringLen(temp_text);
  temp_text="Saldo profit/loss: "+DoubleToStr(s_pl(1)+s_pl(2),2)+" USD";        
  name="ShowOrders_a_2_1"; 
        
  if(ObjectCreate(name,OBJ_LABEL,mwind,0,0)) {
    ObjectSet(name,OBJPROP_XDISTANCE,xd);
    ObjectSet(name,OBJPROP_YDISTANCE,yd);
    ObjectSetText(name,temp_text,FontSize,font,color_show);      
  }    
  return(0);
//  100 * {AccountBalance() - (Объем сделки в USD * (Цена открытия ордера - Текущая цена)/Текущая цена)}/Margin.
}
//+------------------------------------------------------------------+