The script defines the signals formed by the MACD indicator and saves them in the file. In script input parameters can be specified parameters for the MACD indicator calculation, the name of the currency pair and timeframe, as well as date from which we will analyze the data. The file with the indicator signals will be located in the “Data” subdirectory of the local terminal folder.
Code:
At first, we get the array of indicator values ​​and time array for the specified period:
//--- end time is the current time   date_finish=TimeCurrent(); //--- receive MACD indicator handle   ResetLastError();   int macd_handle=iMACD(InpSymbolName,InpSymbolPeriod,InpFastEMAPeriod,InpSlowEMAPeriod,InpSignalPeriod,InpAppliedPrice);   if(macd_handle==INVALID_HANDLE)     {       //--- failed to receive indicator handle       PrintFormat("Error when receiving indicator handle. Error code = %d",GetLastError());       return;     } //--- being in the loop until the indicator calculates all its values   while(BarsCalculated(macd_handle)==-1)       Sleep(10); // pause to allow the indicator to calculate all its values //--- copy the indicator values for a certain period of time   ResetLastError();   if(CopyBuffer(macd_handle,0,InpDateStart,date_finish,macd_buff)==-1)     {       PrintFormat("Failed to copy indicator values. Error code = %d",GetLastError());       return;     } //--- copy the appropriate time for the indicator values   ResetLastError();   if(CopyTime(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,date_buff)==-1)     {       PrintFormat("Failed to copy time values. Error code = %d",GetLastError());       return;     }
Then define the indicator signals and time of their formation:
//--- receive the buffer size   macd_size=ArraySize(macd_buff); //--- analyze the data and save the indicator signals to the arrays   ArrayResize(sign_buff,macd_size-1);   ArrayResize(time_buff,macd_size-1);   for(int i=1;i<macd_size;i++)     {       //--- buy signal       if(macd_buff[i-1]<0 && macd_buff[i]>=0)         {         sign_buff[sign_size]=true;         time_buff[sign_size]=date_buff[i];         sign_size++;         }       //--- sell signal       if(macd_buff[i-1]>0 && macd_buff[i]<=0)         {         sign_buff[sign_size]=false;         time_buff[sign_size]=date_buff[i];         sign_size++;         }     }
Finally, we write the values of obtained signals to the file using the FileWrite() function:
//--- open the file for writing the indicator values (if the file is absent, it will be created automatically)   ResetLastError();   int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_WRITE|FILE_CSV);   if(file_handle!=INVALID_HANDLE)     {       PrintFormat("%s file is available for writing",InpFileName);       PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));       //--- first, write the number of signals       FileWrite(file_handle,sign_size);       //--- write the time and values of signals to the file       for(int i=0;i<sign_size;i++)         FileWrite(file_handle,time_buff[i],sign_buff[i]);       //--- close the file       FileClose(file_handle);       PrintFormat("Data is written, %s file is closed",InpFileName);     }   else       PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());