//+------------------------------------------------------------------+
//|                                Moving_Average_history_script.mq4 |
//|                                                         hapalkos |
//|                                                       2007.02.03 |
//+------------------------------------------------------------------+
#property copyright "hapalkos"
#property link      ""
#property show_inputs
 
extern int MA_method1 = 1;
extern int Indicator_Period1 =34;

extern int MA_method2 = 1;
extern int Indicator_Period2 =10;

extern int MA_method3 = 1;
extern int Indicator_Period3 =15;

extern int NumberOfBars = 20;

string Indicator_Name;
string Indicator_Short_Name;
string Indicator_Short_Name1;
string Indicator_Short_Name2;
string Indicator_Short_Name3;
string DataFileName;

//---- arrays

double MA_array1[1000];
double MA_array2[1000];
double MA_array3[1000];

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
  
//---- Names for display ----

      if(MA_method1 == 0) Indicator_Short_Name1 = "SMA";   
      if(MA_method1 == 1) Indicator_Short_Name1 = "EMA";
      if(MA_method1 == 2) Indicator_Short_Name1 = "SMMA"; 
      if(MA_method1 == 3) Indicator_Short_Name1 = "LWMA";

      if(MA_method2 == 0) Indicator_Short_Name2 = "SMA";   
      if(MA_method2 == 1) Indicator_Short_Name2 = "EMA";
      if(MA_method2 == 2) Indicator_Short_Name2 = "SMMA"; 
      if(MA_method2 == 3) Indicator_Short_Name2 = "LWMA";

      if(MA_method3 == 0) Indicator_Short_Name3 = "SMA";   
      if(MA_method3 == 1) Indicator_Short_Name3 = "EMA";
      if(MA_method3 == 2) Indicator_Short_Name3 = "SMMA"; 
      if(MA_method3 == 3) Indicator_Short_Name3 = "LWMA";
      
      Indicator_Short_Name=Indicator_Short_Name1+" "+Indicator_Short_Name2+" "+Indicator_Short_Name3; 
      
      Indicator_Name = Indicator_Short_Name1+" "+Indicator_Short_Name2+" "+Indicator_Short_Name3+" History" ;

//---- Creation of unique file name ----
 
      DataFileName = ("MA_history_" + Symbol() + "_" + Period() + "_" + Indicator_Short_Name + " _ " + Indicator_Period1 +"  "+ Indicator_Period2 +"  "+ Indicator_Period3 + ".csv");
      
//---- 
//**********************************************************************
//*****
//********      Indicator for selected chart   *********************
//*****
//**********************************************************************

//------------- Calculation of Moving Average ---------------
//---- The technical indicator used is iMA(), Moving Average
//---- Substitute the desired indicator
//---- For Example:
//----      iATR() for Average True Range
//----      iRSI() for Relative Strength Index
//----         appropriate changes to supporting code will be necessary
//----
//----   Information about the indicators is in the MetaQuotes Editor dictionary - Technical Indicators
//----

     for(int j=0; j<NumberOfBars; j++) 
       {
         MA_array1[j]=iMA(NULL,Period(),Indicator_Period1,0,MA_method1,PRICE_CLOSE,j);   
          MA_array2[j]=iMA(NULL,Period(),Indicator_Period2,0,MA_method2,PRICE_CLOSE,j); 
           MA_array3[j]=iMA(NULL,Period(),Indicator_Period3,0,MA_method3,PRICE_CLOSE,j);                         
       }      

//---- Write to FILE    location: C:\\Program Files\StrategyBuilderFX4\experts\files

         int handle;
         handle=FileOpen(DataFileName, FILE_CSV|FILE_WRITE, ',');
          if(handle>0)
            {
             FileWrite(handle,"CHART: " + Symbol() + "  " + Period() + " minutes");
             FileWrite(handle,"Indicator: " + Indicator_Name + ",       Period1 : " + Indicator_Period1+"      Period2 : "+ Indicator_Period2 +"      Period3 : "+ Indicator_Period3);
             FileWrite(handle,"");             
             FileWrite(handle," ---- Time ---- ,   Indicator Value");

         for(int k=0; k<NumberOfBars; k++)             
             FileWrite(handle,TimeToStr(Time[k],TIME_DATE|TIME_MINUTES),"      " + MA_array1[k]+"       "+MA_array2[k]+"       "+MA_array3[k] );
             FileClose(handle);
            }
            
//----
   return(0);
  }
//+------------------------------------------------------------------+