//+------------------------------------------------------------------+
//|                                                  performance.mq4 |
//|                                                    A Sexy Trader |
//|                                  http://pipsmaker.wordpress.com/ |
//+------------------------------------------------------------------+
#property copyright "A Sexy Trader"
#property link      "http://pipsmaker.wordpress.com/"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Goldenrod
//---- input parameters
extern string    BaseSymbol="GBPJPY";
extern string    HedgeSymbol="EURJPY";
//---- buffers
double ExtMapBuffer1[]                 //this is the indicator buffer
      ,curve[8888888]                  //this array is for coolecting the result from the performance file
      ;

int handle;                            //maybe no need to explain anymore
string data;
int len=0
   ,i=0
   ,j=0
   ,p
   ,pv
   ,pter=0
   ;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
   
   IndicatorShortName(BaseSymbol+"~"+HedgeSymbol+" ");
//----
   
   p =FileOpen("p.csv",FILE_CSV|FILE_READ);                          //get how many result files were exported
   pv=StrToInteger(FileReadString(p));
   FileClose(p);
   
   for(int i=1;i<=pv;i++)                                            //the loop to get all exported result as a string
   {
      string name = BaseSymbol+"_"+HedgeSymbol+"_result"+p+".csv";   //get the name of the performance file
      handle=FileOpen(name,FILE_CSV|FILE_READ);                      //search for the file to open to read
      if(handle>0)                                                   //if the file is exist
      {   
          data=data+FileReadString(handle);                          //get the whole data
          FileClose(handle);                                         //close it
      }
   
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted(),i=0,s=-1;
   
//----
   len=StringLen(data);                                     //get the lenght of the data string
  
   pter=0;                                                  //set the pointer which use for searching the ","
                                                            /*please be informed that the file csv was collected 
                                                              the result as a string like this 
                                                              1000.54,1100.54,1200.54,1300.54,1400.54
                                                              but the fact is we want only the real data is like this
                                                              1000.54
                                                              1100.54
                                                              1200.54
                                                              1300.54
                                                              1400.54
                                                              so the "," is the key to get the data as above
                                                              and it can be done by finding out the data befor ","
                                                              and get rid off it from the whole data 
                                                              to shift the next data into the front
                                                              and insert each taken off data into the curve array 
                                                            */
   
   for(i=len;i>=0;i--)                                      /*the loop to define how many room this array should build
   {                                                          to contain the performance data
                                                            */
    if(StringFind(data,",",0)>0)                            //if there is a nearest "," from the front
    {
     s++;                                                   //indicate the room number  
     pter=StringFind(data,",",0);                           //get the point where the first "," exist
     curve[s]=StrToDouble(StringSubstr(data,0,pter));       //insert the first data of the whole string
     data=StringSubstr(data,pter+1,0);                      //cut the inserted data off
    }                           
    else          break;                                    //no data to count anymore , break the loop

   ArrayResize(curve,s+1);                                  //resize the curve array for furthur usage
//----
   for(i=0,j=s;i<=s;i++,j--)                                //the plotting process bigin
   {
    if(curve[j]>0)ExtMapBuffer1[i]=curve[j];                //plot the performance curve
   }
//----                                                      //all the things done.

   return(0);
  }
//+------------------------------------------------------------------+