//+------------------------------------------------------------------+
//|                                            Multi_Pair_Profit.mq4 |
//|                      Copyright © 2009, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "bobfourie"
#property link      ""

extern string sSymbols = "EURUSD,0,EURCHF,1,USDCHF,0";
extern datetime dStartTime = D'2009.08.24 04:00';
extern bool bUseVerticalLine = true;
extern string sObjName = "MPP";
extern bool bReverseSymbols = false;

#property indicator_separate_window
#property indicator_buffers 8
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_color3 Yellow
#property indicator_color4 Green
#property indicator_color5 White
#property indicator_color6 White
#property indicator_color7 Red
#property indicator_color8 Red
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];
double ExtMapBuffer6[];
double ExtMapBuffer7[];
double ExtMapBuffer8[];
  
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(3,ExtMapBuffer4);
   SetIndexStyle(4,DRAW_LINE);
   SetIndexBuffer(4,ExtMapBuffer5);
   SetIndexStyle(5,DRAW_LINE);
   SetIndexBuffer(5,ExtMapBuffer6);
   SetIndexStyle(6,DRAW_LINE);
   SetIndexBuffer(6,ExtMapBuffer7);
   SetIndexStyle(7,DRAW_LINE);
   SetIndexBuffer(7,ExtMapBuffer8);
   
   SetLevelValue( 1, 0) ;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
  if(bUseVerticalLine)
  {
      if(ObjectFind(sObjName) != -1)dStartTime = ObjectGet( sObjName,OBJPROP_TIME1) ;
      else 
      {
         ObjectCreate( sObjName, OBJ_VLINE, 0, Time[10],Close[10]);
         dStartTime = Time[10];
      }
   }

datetime d1 = dStartTime;
datetime d2 = Time[0];

    ArrayInitialize(ExtMapBuffer1, 0);
   for(int i = iBarShift(Symbol(),0, d1); i > iBarShift(Symbol(),0, d2) ; i-- )
   {  
      
      if(!bReverseSymbols)
      ExtMapBuffer1[i] =  TotalProfit(sSymbols, d1, iTime(Symbol(), 0,i));
      else 
      ExtMapBuffer1[i] =  -TotalProfit(sSymbols, d1, iTime(Symbol(), 0,i));
     
   }
//----
   return(0);
  }

//Get the Profit for more than 1 currencies from date d1 to d2, with a multiplier 1
// etc string sCurrEUR = "EURUSD,0,EURCHF,0,EURGBP,0,EURJPY,0";  
double TotalProfit(string s, datetime d1, datetime d2)
{
   string sSymbolsArray [0];    
   Split(s,sSymbolsArray,",");
   double dSum = 0;
   for(int i = 0; i < ArraySize(sSymbolsArray); i = i+2)
   {
     dSum = dSum + GetProfit(MiniCheck(sSymbolsArray[i]), StrToInteger(sSymbolsArray[i+1]), 1, d1 ,d2);
   }
   return (dSum);
}
//Get the Profit for 1 currencies from date d1 to d2, with a multiplier foreach lot size
double GetProfit(string s1, int  iType, double s1Multi, datetime t1, datetime t2)
{
s1 = MiniCheck(s1);
  int    barshift = 0;
  double Profit   = 0;
  
       barshift = iBarShift(s1,0,t1);
double Price1s1 = iClose(s1,0,barshift);
       barshift = iBarShift(s1,0,t2);
double Price2s1 = iClose(s1,0,barshift);

   if(iType == OP_BUY)  Profit += ((Price2s1 - Price1s1) / MarketInfo(s1,MODE_POINT)) * s1Multi * MarketInfo(s1, MODE_TICKVALUE);
   if(iType == OP_SELL) Profit += ((Price1s1 - Price2s1) / MarketInfo(s1,MODE_POINT)) * s1Multi * MarketInfo(s1, MODE_TICKVALUE);

return(Profit);
} 
//Check for prefix and adjust symbol if needed
string MiniCheck(string sSymbol)
{
  if(StringLen(sSymbol) > 6 && (StringLen(Symbol()) > 6) )return(sSymbol);
  if(StringLen(sSymbol) > 6 && !(StringLen(Symbol()) > 6) )return(StringSubstr(sSymbol, 0, 6));
  if( !(StringLen(sSymbol) > 6) && (StringLen(Symbol()) > 6) )return(sSymbol+""+StringSubstr(Symbol(), 6, StringLen(Symbol()) -6 ));
  return (sSymbol);
}
//to Split an string into an ref array
void Split(string s,string& array[],string  delimiter)
{
   int count = 0;
   int indexCurr = 0;
   int indexPrev = 0;  
   ArrayResize(array,0);
 if(delimiter != "")
 {
   while(indexCurr != -1)
   {
    count++;
    ArrayResize(array,ArraySize(array)+1);
    if(count >  StringLen(s)) break;
    indexCurr = StringFind(s, delimiter,indexPrev+1);
    array[count-1] = StringSubstr(s, indexPrev, indexCurr - indexPrev);
    indexPrev = indexCurr+1;
  }
 }
 else
 {
   for(int i = 0; i < StringLen(s); i++)
   {
      ArrayResize(array,ArraySize(array)+1);
      array[i] = StringSubstr(s, count, 1);
      count++;
   }
 } 
}
  /*/
int GetType(string s,int iTimeFrame,int iLag, int i)
{
   if(GetCurrStrenght(s,0,1, i) > 0)return (1);
   else return (-1);
}
*/

