Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

Demo_FileWrite - MetaTrader 5 Script | Forex Indicator Download - MT4/MT5 Resources

author EAcpu | 2 reads | 0 comments |

The script defines the Moving Average Convergence and Divergence indicators and saves them in a file. In the script input parameters it is possible to specify the parameters used for the calculation of the MACD indicator, the name and time range of the currency pair, as well as the date for which we analyze the data. The file with the indicator signal will be located in the "Data" subdirectory of the local terminal folder.

First, we obtain the indicator value array and time array of the specified period:

 //--- The end time is the current time. Date completion = time current (); //--- Receive MACD indicator handle to reset the last error ();
Integer macd_handle = smoothed moving average of similarity and difference (InpSymbolName, InpSymbolPeriod, InpFastEMAPeriod, InpSlowEMAPeriod, InpSignalPeriod, InpAppliedPrice);
if (macd_handle == INVALID_HANDLE )
    {
//--- Failed to receive indicator handle printing format ( "Error receiving indicator handle. Error code = %d" , get the last error ());
return ;
    } //--- in a loop until the indicator calculates all its values ​​although ( number of calculated bars (macd_handle)==- 1 )
sleep ( 10 ); // Pause to allow the indicator to calculate all its values
//--- Copy indicator values ​​over a period of time to reset the last error ();
if ( copy buffer (macd_handle, 0 ,InpDateStart,date_finish,macd_buff)==- 1 )
    {
Print format ( "Unable to copy indicator value. Error code = %d" , get the last error ());
return ;
    } //--- Reset the last error at the appropriate time of copying the indicator value ();
if ( copy time (InpSymbolName, InpSymbolPeriod, InpDateStart, date_finish, date_buff)==- 1 )
    {
Print format ( "Unable to copy time value. Error code = %d" , get the last error ());
return ;
    }

Then define the indicator signal and its formation time:

 //--- Receive buffer size macd_size = array size (macd_buff); //--- Analyze data and save indicator signals to the array array resize (sign_buff,macd_size- 1 );
Array resize (time_buff,macd_size- 1 );
for ( integer i = 1 ;i //--- Buy signal if (macd_buff[i- 1 ]< 0 && macd_buff[i]>= 0 )
        {
        flags_buffer[flags_size] = true ;
        time_buff[sign_size]=date_buff[i];
        symbol size++;
        }
//--- Sell signal if (macd_buff[i- 1 ]> 0 && macd_buff[i]<= 0 )
        {
        flags_buffer[flags_size] = false ;
        time_buff[sign_size]=date_buff[i];
        symbol size++;
        }
    }

Finally, we write the obtained signal value to a file using the filewrite () function:


Attachment download

📎 demo_filewrite.mq5 (4.76 KB)

Source: MQL5 #1626

Verification code Refresh