ADATAwave59
Download scriptThis script is designed to automatically extract historical market data from your trading platform and save it into an organized file on your computer. It acts as a bridge between the live charts you see in MetaTrader and a permanent text record that can be used for outside analysis.
How the Process Works
1. Configuration and Setup The script begins by looking at two specific settings provided by the user: the total number of historical bars (candles) to scan and the starting year for the data collection. It then automatically identifies which currency pair and time frame you are currently viewing (for example, EURUSD on a 1-hour chart) to ensure the file it creates is labeled correctly.
2. File Creation The tool identifies a specific destination for your data by naming a new file based on the current asset and timeframe. It then opens this file in a "write" mode, essentially preparing a blank digital notebook ready to be filled with information.
3. Data Filtering and Scanning The script systematically works backward through your trading history. It examines every price candle one by one. During this process, it performs a check: if a candle's date falls before the "starting year" you defined, the script ignores it. This allows you to skip irrelevant historical data and focus only on the period you are interested in.
4. Data Extraction For every candle that meets your date criteria, the script gathers five specific pieces of information:
- The exact date and time.
- The opening price.
- The highest price reached.
- The lowest price reached.
- The closing price.
5. Exporting the Results The script then formats this information into a clean, comma-separated line of text. It writes this line into your file and moves on to the next candle until it has processed the entire requested range. Once all the data has been successfully transferred from the platform to the file, it closes the file to ensure all information is saved safely.
//+__________________________________________________________________+
//| AgetData.mq4 |
//| Copyright 2005,fxpars |
//|
//+__________________________________________________________________+
#property copyright "ooo"
#property link "aaa"
#property indicator_chart_window
extern int number_bars=2000;
extern int from_year = 1978;
int start()
{
int handle;
int cnt;
string strline;
string name_file;
name_file = Symbol()+ Period()+ ".prn";
handle = FileOpen(name_file , FILE_CSV|FILE_WRITE, "\t");
if(handle<1)
{
Print("cannot open file error_",GetLastError());
return(0);
}
else
{
for (cnt=number_bars;cnt>=0;cnt--)
if (from_year<TimeYear(Time[cnt]))
{
strline = TimeToStr(Time[cnt],TIME_DATE|TIME_SECONDS);
strline = StringSubstr(strline,5,2) + StringSubstr(strline,8,2) + StringSubstr(strline,0,4) +"," + StringSubstr(strline,11,5);
strline = strline + "," + DoubleToStr(Open[cnt],4) + "," + DoubleToStr(High[cnt],4) + "," + DoubleToStr(Low[cnt],4) + "," + DoubleToStr(Close[cnt],4);// + "," + DoubleToStr(Volume[cnt],0);
FileWrite(handle, strline);
}
FileClose(handle);
return(0);
}
}
Comments