TDWv3
Download scriptThis script is designed to perform a comprehensive historical analysis of trading data to identify patterns based on the day of the week. It scans through past market history to calculate how specific days (Monday through Friday) typically perform, providing a statistical summary that helps traders understand weekly trends.
How the Logic Works
1. Data Filtering and Requirements Before beginning the analysis, the script performs several checks to ensure the data is valid:
- It ensures the chart timeframe is set to "Daily."
- It verifies that the start and end years provided by the user are logically correct and within the limits of the available historical data.
- It confirms there is enough data (at least 270 bars) to make a meaningful calculation.
2. Data Collection Process The script systematically loops through every single day of trading within your specified date range. It organizes its findings by day of the week, starting with Monday and moving through Friday. For each specific year and each day of the week, it calculates the following metrics:
- Frequency: How many times that specific day of the week occurred in the year.
- Price Movement (Points): The total movement from the opening price to the closing price.
- Volatility: The daily range (the difference between the highest and lowest prices reached during the day).
- Win/Loss Breakdown: It identifies whether a day ended "positively" (closed higher than it opened) or "negatively" (closed lower than it opened) and calculates the specific points gained or lost in those instances.
3. Statistical Calculations Once the raw data is collected for each year, the script converts those numbers into readable percentages:
- Success Rate: The percentage of days that ended in a positive close compared to the total number of occurrences for that day.
- Point Performance: The percentage of "positive points" relative to the total absolute movement of the market.
4. Output and Reporting The script generates two distinct types of reports:
- Yearly Breakdown: A detailed list showing the statistics for each year individually, allowing you to see how a specific day (e.g., "Mondays") performed in 2005 vs. 2006, etc.
- Cumulative Totals: A final "Summary" section that aggregates all data from your chosen date range into one grand total. This provides a "big picture" view of how the market behaves on specific days across several years combined.
The results are saved into a structured text file (.txt), which can be opened in any spreadsheet or text editor to review the final statistics.
//+------------------------------------------------------------------+
//| Trade Day Of Week.mq4 |
//| Copyright © 2008, Vasily Sokolov (C-4) |
//| e-mail: vs-box@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, BazilSoft"
#property link "vs-box@mail.ru"
#property show_inputs
extern int BeginYear=1998;
extern int EndYear=2008;
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
//ÏÅÐÅÌÅÍÍÛÅ ÂÛÂÎÄßÙÈÅÑß Â ÔÀÉË:
// ïåðâîé ÿ÷åéêå ìàññèâà õðàíèòñÿ ñòàòèñòèêà çà ãîä, âî âòîðîé ñîâîêóïíàÿ ñòàòèñòèêà çà âñå ãîäû
double AllDaysOfWeek[2]; //1. Âñåãî äíåé íåäåëè â ãîäó (íàïðèìåð â ãîäó 52 ïîíåäåëüíèêà)*
double PositivelyDays[2]; //2. Äíè çàêðûâøèåñÿ ïîëîæèòåëüíî*
double NegativelyDays[2]; //3. Äíè çàêðûâøèåñÿ îòðèöàòåëüíî*
double PercentPositivelyDays[2]; //4. Ïðîöåíò äíåé çàêðûâøèõñÿ ïîëîæèòåëüíî îò AllDaysOfWeek
double AllPtM[2]; //5. Âñåãî ïóíêòîâ ïî ìîäóëþ (Close-Open)*
double AllPt[2]; //6. Âñåãî ïóíêòîâ (Close-Open)*
double PositivelyPt[2]; //7. Ïîëîæèòåëüíûõ ïóíêòîâ (Close-Open, òîëüêî åñëè Close>Open)*
double NegativelyPt[2]; //8. Îòðèöàòåëüíûõ ïóíêòîâ (Close-Open, òîëüêî åñëè Close<Open)*
double PercentPosityvelyPt[2]; //9. Ïðîöåíò ïîëîæèòåëüíûõ ïóíêòîâ îò AllPtM
double RangeOfPrice[2]; //10. Äèàïàçîí öåí äíÿ, (High-Low)
int FileHandle;
int FileStat;
void WriteResults(int FileName, int BYear, int a){
string Y, N;
if(BYear==0){Y="Èòîãî";N="\n";}
else{Y=BYear;}
FileWrite(FileName, Y,"\t", DoubleToStr(AllDaysOfWeek[a], 0), DoubleToStr(PositivelyDays[a], 0),
DoubleToStr(NegativelyDays[a], 0), DoubleToStr(PercentPositivelyDays[a], 1), DoubleToStr((RangeOfPrice[a]/AllDaysOfWeek[a]), 0),
DoubleToStr(AllPtM[a]/AllDaysOfWeek[a], 0), DoubleToStr(AllPt[a]/AllDaysOfWeek[a], 0), DoubleToStr(PositivelyPt[a]/AllDaysOfWeek[a], 0),
DoubleToStr(NegativelyPt[a]/AllDaysOfWeek[a], 0), DoubleToStr(PercentPosityvelyPt[a], 1), N);
}
void WriteStatistic(int index){
string YY, MM, DD, YYMMDD;
double RangeOfPrice;
double AllPt;
YY=TimeYear(Time[index]);
MM=TimeMonth(Time[index]);
DD=TimeDay(Time[index]);
YYMMDD=YY+"."+MM+"."+DD;
RangeOfPrice=(High[index]-Low[index])/Point;
AllPt=(Close[index]-Open[index])/Point;
FileWrite(FileStat, YYMMDD, " ", RangeOfPrice, " ", AllPt);
}
int start()
{
//--------------------------------------------------------------------
//ÑËÓÆÅÁÍÛÅ ÏÅÐÅÌÅÍÍÛÅ:
int index; //Íîìåð èññëåäóåìîãî äíÿ
string DW; //Íàçâàíèå äíÿ íåäåëè
string FileName;
//--------------------------------------------------------------------
if(Period()<1440||Period()>1440){
Print("Ïîìåíÿéòå òàéìôðåéì íà äåíü");
return(0);}
if(BeginYear>Year()||EndYear>Year()){
Print("Íà÷àëüíûé èëè êîíå÷íûé ãîä íå ìîæåò áûòü áîëüøå òåêóùåãî");
return(0);}
if(BeginYear<TimeYear(Time[Bars-1])||EndYear<TimeYear(Time[Bars-1])){
Print("Íà÷àëüíûé èëè êîíå÷íûé ãîä íå ìîæåò áûòü ìåíüøå ñàìîé ñòàðîé èçâåñòíîé öåíû");
return(0);}
if(BeginYear>EndYear){
Print("Êîíå÷íûé ãîä íå ìîæåò áûòü ìåíüøå íà÷àëüíîãî");
return(0);}
if(Bars<270){
Print("Ñëèøêîì ìàëî äàííûõ. Çàãðóçèòå áîëüøå äàííûõ");
return(0);}
FileName="TDW_"+Symbol()+BeginYear+"-"+EndYear+".txt";
FileHandle=FileOpen(FileName, FILE_WRITE | FILE_CSV, "\t");
FileStat=FileOpen("_StatisticTDW.txt", FILE_WRITE | FILE_CSV, "\t");
if(FileHandle<1){
Print("Íå óäàëîñü îòêðûòü ôàéë, ïðè÷èíà:", GetLastError());
return;
}
for(int MDayOfWeek=1;MDayOfWeek<=5;MDayOfWeek++) //Íà÷èíàåì ñ ïîíåäåëüíèêà(1), çàêàí÷èâàåì ïÿòíèöåé(5)
{
index=Bars-1;
while(BeginYear!=TimeYear(Time[index])){index--;}
switch(MDayOfWeek)
{
case 1: DW="ÏÎÍÅÄÅËÜÍÈÊ"; break;
case 2: DW="ÂÒÎÐÍÈÊ"; break;
case 3: DW="ÑÐÅÄÀ\t"; break;
case 4: DW="×ÅÒÂÅÐÃ"; break;
case 5: DW="ÏßÒÍÈÖÎ\t";
}
FileWrite(FileHandle, DW);
for(int BYear=BeginYear; (BYear<=EndYear)&&(index>=0); BYear++)
{
Print(DW, " ", BYear);
while(BYear==TimeYear(Time[index])) //Ïðîñìàòðèâàåì âñå äíè ãîäà
{
if(TimeDayOfWeek(Time[index])==MDayOfWeek) //Âûáèðàåì èç íèõ òîëüêî MDayOfWeek (íàïð. ïîíåäåëüíèêè)
{
AllDaysOfWeek[0]++;
RangeOfPrice[0]=RangeOfPrice[0]+(High[index]-Low[index])/Point;
AllPt[0]=AllPt[0]+(Close[index]-Open[index])/Point;
if(Close[index]>Open[index]) //Åñëè çàêðûòèå áîëüøå îòêðûòèÿ - äåíü ïîëîæèòåëüíûé
{
PositivelyDays[0]++;
PositivelyPt[0]=PositivelyPt[0]+(Close[index]-Open[index])/Point;
AllPtM[0]=AllPtM[0]+(Close[index]-Open[index])/Point;
}
if(Close[index]<Open[index]) //Åñëè çàêðûòèå ìåíüøå îòêðûòèÿ - äåíü îòðèöàòåëüíûé
{
NegativelyDays[0]++;
NegativelyPt[0]=NegativelyPt[0]-(Close[index]-Open[index])/Point;
AllPtM[0]=AllPtM[0]+(-1*(Close[index]-Open[index])/Point);
}
//Äëÿ ïðîâåðêè ïðàâèëüíîñòè ïîäñ÷åòà èñïîëüçîâàëàñü âñïîìîãàòåëüíàÿ ôóíêöèÿ WriteStatistic
//îíà ïèøåò â îòäåëüíûé ôàéë äàòó êàæäîãî äíÿ íåäåëè, åãî äíåâíîé äèàïîçîí è äèàïîçîí Close-Open
//Ïðè æåëàíèè åå ìîæíî çàêîììåíòèðîâàòü.
WriteStatistic(index);
}
index--;
}
if(AllPtM[0]>0){
PercentPosityvelyPt[0]=PositivelyPt[0]*100/AllPtM[0];}
if(AllDaysOfWeek[0]>0){
PercentPositivelyDays[0]=PositivelyDays[0]*100/AllDaysOfWeek[0];}
AllDaysOfWeek[1]=AllDaysOfWeek[1]+AllDaysOfWeek[0];
AllPt[1]=AllPt[1]+AllPt[0];
AllPtM[1]=AllPtM[1]+AllPtM[0];
PositivelyDays[1]=PositivelyDays[1]+PositivelyDays[0];
NegativelyDays[1]=NegativelyDays[1]+NegativelyDays[0];
PositivelyPt[1]=PositivelyPt[1]+PositivelyPt[0];
NegativelyPt[1]=NegativelyPt[1]+NegativelyPt[0];
RangeOfPrice[1]=RangeOfPrice[1]+RangeOfPrice[0];
//Âûâîäèì ïåðåìåííûå â ôàéë
WriteResults(FileHandle, BYear, 0);
AllDaysOfWeek[0]=0;AllPt[0]=0;AllPtM[0]=0;PositivelyDays[0]=0;NegativelyDays[0]=0;RangeOfPrice[0]=0;
PositivelyPt[0]=0;NegativelyPt[0]=0;PercentPosityvelyPt[0]=0;PercentPositivelyDays[0]=0;
}
if(AllPtM[1]>0){
PercentPosityvelyPt[1]=PositivelyPt[1]*100/AllPtM[1];}
if(AllDaysOfWeek[1]>0){
PercentPositivelyDays[1]=PositivelyDays[1]*100/AllDaysOfWeek[1];}
//âûâîäèì èòîãîâóþ ñòàòèñòèêó â ôàéë
WriteResults(FileHandle, 0, 1);
AllDaysOfWeek[1]=0;AllPt[1]=0;AllPtM[1]=0;PositivelyDays[1]=0;NegativelyDays[1]=0;RangeOfPrice[1]=0;
PositivelyPt[1]=0;NegativelyPt[1]=0;PercentPosityvelyPt[1]=0;PercentPositivelyDays[1]=0;
}
return(0);
}
//+------------------------------------------------------------------+
Comments