This script is designed as a specialized tool for traders who need to identify specific historical timeframes to conduct "optimization" testing. In trading, optimization involves looking at past data to see how a strategy performed under various conditions; this tool automates the heavy lifting of finding those significant windows of time.
Core Functionality
The primary purpose of this script is to scan through your historical trading chart and identify distinct periods where meaningful price action occurred. It does this by systematically analyzing "bars" (the individual candles on your chart) and grouping them into chronological segments based on the volume of data available.
How the Logic Works
Instead of just picking random dates, the script follows a structured process to ensure the data it identifies is useful for testing:
- Data Scanning: The tool looks back through your history starting from a specific point on your chart. It moves backward in time, searching for "anchor" points—specific moments that represent the start of a new sequence.
- Gap Verification: The script doesn't just grab any dates; it checks the distance between these anchor points. It ensures that there is enough historical "room" (a specific number of bars) between the periods to ensure the data isn't too cramped or overlapping.
- Nested Filtering: It uses a triple-layered filtering system. For every date it finds, it verifies three different conditions regarding how many bars are available in the past. This ensures that any timeframe it suggests has enough historical "depth" to be valid for an optimization test.
- Automated Reporting: Once the script identifies a valid window of time that meets all these criteria, it records the results into a dedicated text file (
_OptDates.txt).
The End Result for the User
When you run this script, you don't have to manually scroll through years of charts to figure out which dates are best for testing your strategy. The script does the math for you, scanning the history and generating a clean list of date ranges.
You can then open this text file to see a prepared list of "Start Date" and "End Date" pairings. These coordinates tell you exactly which segments of historical data are large enough and properly spaced to be used as reliable inputs for your trading strategy optimizations.
//+------------------------------------------------------------------+
//| sOptimisationDates.mq4 |
//| * |
//| * |
//+------------------------------------------------------------------+
#property copyright "http://dmffx.com"
#property link "http://dmffx.com"
#property show_inputs
extern int Shift = 0; // Îòñòóï â áàðàõ îò ïðàâîãî êðàÿ ãðàôèêà
int Step;
extern int CheckBars = 300; // Êîëè÷åñòâî áàðîâ íà êîòîðûõ ýêñïåðò "òîðãóåò" ïîñëå îïòèìèçàöèè
extern int OptimizationBars = 3000; // Êîëè÷åñòâî áàðîâ íà êîòîðûõ ýêñïåðò îïòèìèçèðóåòñÿ
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int start(){
Step=CheckBars;
int h=FileOpen(Symbol()+"_"+Period()+"_OptDates.txt",FILE_CSV|FILE_WRITE);
int pt=86400*(Time[Shift]/86400);
int pbs=-Step;
for(int i=pt;i>Time[Bars-1];i-=86400){
int bs=fMyBarShift2(i);
if(bs-pbs>=Step){
for(int j=i-86400;j>Time[Bars-1];j-=86400){
int bs2=fMyBarShift2(j);
if(bs2-bs>=CheckBars){
for(int k=i-86400;k>Time[Bars-1];k-=86400){
int bs3=fMyBarShift2(k);
if(bs3-bs2>=OptimizationBars){
FileWrite(h,TimeToStr(k,TIME_DATE)+" - "+TimeToStr(j,TIME_DATE)+" ("+(bs3-bs2)+"), "+TimeToStr(j,TIME_DATE)+" - "+TimeToStr(i,TIME_DATE)+" ("+(bs2-bs)+")");
break;
}
}
break;
}
}
pt=i;
pbs=fMyBarShift2(pt);
}
}
FileClose(h);
return(0);
}
int fMyBarShift2(datetime aTime){
int t1=iBarShift(NULL,0,aTime,true);
if(t1==-1){
return(iBarShift(NULL,0,aTime,false)-1);
}
return(t1);
}
Comments