//+------------------------------------------------------------------+
//|                                                      returns.mq4 |
//|                                           Copyright © 2010, alsu |
//|                                 Upgrade to mql5 2011, MetaDriver |
//|                                                 alsufx@gmail.com |
//|                                                 alsufx@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, alsu; Upgrade to mql5 2011, MetaDriver"
#property link      "alsufx@gmail.com; MetaDriver@rambler.ru"

#property script_show_inputs

input int max_tf=300;
input int spread=18;
input double trade_intensity=1;
input int bars_per_trade=1;
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
#define count  900000
MqlRates data[count];
int OnStart()
  {
//----
   int h = FileOpen(Symbol()+"_cc_profit.csv",FILE_CSV|FILE_WRITE,";");
   FileWrite(h,
   Symbol(),
   "max_tf="+IntegerToString(max_tf),
   "spread="+IntegerToString(spread),
   "trade_intensity="+DoubleToString(trade_intensity),
   "bars_per_trade="+IntegerToString(bars_per_trade));
   int n,tf;
   double bars_per_hour, trades_per_hour;
   double D,d;
   double hD,hd;
//   ArraySetAsSeries(data,true);
   int err=CopyRates(NULL,PERIOD_M1,1,count,data);
   Print("CopyRates()=", err);
   if (err<0) 
   {
     Print("Данные не скопированы, ошибка ", GetLastError());
     return -1;
   }
   for(tf=1;tf<=max_tf;tf++)
   {
      D=0; hD=0;
      for(n=count-max_tf-3;n>=0&&!IsStopped();n--)
      {
         D+=MathAbs((data[n+tf].close-data[n].close)/_Point);
         hD+=fmax(MathAbs(data[n+tf].high-data[n].low), MathAbs(data[n+tf].low-data[n].high))/_Point;
      }   
      d = D / count;
      hd= hD / count;
      bars_per_hour = 60./tf;
      trades_per_hour = trade_intensity*bars_per_hour;
      d *= trades_per_hour*bars_per_trade;
      d -= spread*trades_per_hour;
      hd *= trades_per_hour*bars_per_trade;
      hd -= spread*trades_per_hour;

      FileWrite(h,tf,d,hd);
   }
   FileClose(h);
//----
   return(0);
  }
//+------------------------------------------------------------------+