TDM
Download scriptThis script is designed to perform a comprehensive historical analysis of trading behavior based on the specific day of the month. Instead of looking at dates individually, it groups data by the chronological order of trading days (e.g., analyzing what happens on the 1st trading day of every month vs. the 2nd trading day, and so on).
Core Functionality
The tool scans historical price data over a user-defined period to identify patterns that repeat across different months and years. It seeks to determine if certain "trading days" consistently show specific behaviors, such as being more bullish (upward), bearish (downward), or having higher volatility.
How the Logic Works
The script processes your historical charts using the following steps:
1. Data Filtering and Preparation
- Timeframe Check: The script ensures you are viewing a "Daily" chart. It will not run on smaller timeframes (like 1-minute or 5-minute charts) because it requires full daily candles to calculate accurate statistics.
- Date Range: You can specify the start and end years for the study. The script verifies that these dates are valid and contain enough historical data to produce a meaningful result.
2. Tracking Trading Days (TDM)
- The script doesn't just look at "October 1st." It identifies which trading day of the month it is. For example, if the 1st of the month falls on a weekend and the market is closed, the first Monday of that month becomes "Trading Day 1." This allows for more consistent statistical grouping across different months.
3. Statistical Collection For every trading day identified within your chosen years, the script captures several key metrics:
- Frequency: How many times that specific trading day occurred.
- Movement (Pips): The total distance moved from open to close.
- Volatility: The total range of price movement (High to Low).
- Directional Bias: It separates "Positive Days" (where the price closed higher than it opened) and "Negative Days" (where it closed lower or stayed equal).
- Percentages: It calculates what percentage of those days were positive and what percentage of the total movement was upward.
4. Yearly Aggregation and Comparison The logic follows a two-step accumulation process:
- Annual View: It records the stats for each year individually and saves these results to a file. This helps you see if "Trading Day 5" behaved differently in 2004 compared to 2008.
- Cumulative View: It then adds all those yearly figures together to create a "Grand Total" statistic. This provides a bird's-eye view of the behavior of each trading day across the entire multi-year period.
Output
Once the analysis is complete, the script generates a text file (e.g., TDM_Symbol_2000-2008.txt). This file contains a structured table showing:
- The count of each trading day.
- Average pips/movement for those days.
- The percentage of positive vs. negative moves.
- Final totals for the entire requested period.
This allows traders to identify if, for example, the middle of the month tends to be more volatile or if the beginning of the month consistently shows a specific directional bias.
//+------------------------------------------------------------------+
//| TDM.mq4 |
//| Copyright © 2008, BazilSoft |
//| vs-box@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, BazilSoft"
#property link "vs-box@mail.ru"
#property show_inputs
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
#define T 24
#define M 32
extern int BeginYear=2000; //Ãîä, ñ êîòîðîãî íà÷èíàåòñÿ èññëåäîâàíèå
extern int EndYear=2008; //Ãîä, ñ êîòîðîãî çàêàíè÷èâàåòñÿ èññëåäîâàíèå
//Ìàññèâ ïåðåìåííûõ ïðåäñòàâëåí èíäåêñåì â ìàññèâå. Ò.å. Åñòü åäèíàÿ ïåðåìåííàÿ â âèäå äâóõìåðíîãî ìàññèâà (òàáëèöû)
//23x10, ãäå â êà÷åñòâå ñòðîê(23) óêàçàíû òîðãîâûå äíè ìåñÿöà, à â êà÷åñòâå ñòîëáöîâ (10) óêàçàíû ñâîéñòâà ýòèõ äíåé.
//Ñâîéñòâà ïåðå÷èñëåíû íèæå:
int AllDaysOfMonth=0; //Âñåãî äíåé ìÿñÿöà â ãîäó. íàïðèìåð â ãîäó 12 ïåðâûõ òîðãîâûõ äíåé ìåñÿöà
int PositivelyDays=1;
int NegativelyDays=2;
int PercentPositivelyDays=3;
int RangeOfPrice=4;
int AllPipsMod=5;
int AllPips=6;
int PositivelyPips=7; //7. Ïîëîæèòåëüíûõ ïóíêòîâ (Close-Open, òîëüêî åñëè Close>Open)*
int NegativelyPips=8; //8. Îòðèöàòåëüíûõ ïóíêòîâ (Close-Open, òîëüêî åñëè Close<Open)*
int PercentPosityvelyPt=9;
int iYear; //Òåêóùèé ãîä
int index; //Òåêóùèé áàð
int TDM; //Ëèáî òîðãîâûé äåíü ìåñÿöà ëèáî ïðîñòî äåíü ìåñÿöà
int LTD; //Ïîñëåäíèé òîðãîâûé äåíü, ïðèíèìàåò çíà÷åíèå ëèáî 23 ëèáî 32 â çàâèñèìîñòè îò ðåæèìà èññëåäîâàíèÿ
int FileHandle; //Äèñêðèïòîð ôàéëà, â êîòîðûé ïèøåòñÿ ðåçóëüòàò
double TradeMonth[24][10]; //ìàññèâ äíåé.
double TradeMonthAll[24][10]; //Èòîãîâàÿ ñòàòèñòèêà
/*
Îñíîâíàÿ ôóíêöèÿ ñêðèïòà, âîçâðàùàåò òîðãîâûé äåíü ìåñÿöà. Íàïðèìåð â ìåñÿöå 22 òîðãîâûõ äíÿ, ò.å.
äåíü 31.10.08 áóäåò 23 òîðãîâûì äíåì
*/
int TradeDayOfMonth(int index){
int TDM; //òîðãîâûé äåíü ìåñÿöà, ïðèíèìàåò çíà÷åíèå îò 1 äî 22
int MonthYear; //ìåñÿö ãîäà, ïðèíèìàåò çíà÷åíèå îò 1 (ÿíâàðü) äî 12 (äåêàáðü)
MonthYear=TimeMonth(Time[index]); //óçíàåì ñ êàêèì ìåñÿöåì ìû èìååì äåëî
//Öèêë âûïîëíèòñÿ ïî êðàéíåé ìåðå îäèí ðàç, ïîýòîìó íåò íóæäû îò÷èòûâàòü TDM ñ 1
while(MonthYear==TimeMonth(Time[index])){
TDM++;
index++;
}
return(TDM);
}
int WriteResults(int FileHandle, double TradeM[24][10]){
double dYear;
double AverageDay;
dYear=iYear;
if(iYear>EndYear)
FileWriteString(FileHandle, "ÈÒÎÃÎ:", StringLen("ÈÒÎÃÎ:"));
else
FileWriteString(FileHandle, DoubleToStr(dYear,0), StringLen(DoubleToStr(dYear,0)));
FileWriteString(FileHandle, "\r\n", StringLen("\r\n"));
for(int i=1;i<24;i++){
//È ñíîâà ãîëü íà âûäóìêè õèòðà
double d=i;
FileWriteString(FileHandle, "TDM-", StringLen("TDM-"));
FileWriteString(FileHandle, DoubleToStr(d,0), StringLen(DoubleToStr(d,0)));
FileWriteString(FileHandle, "\t", StringLen("\t"));
for(int j=0;j<10;j++){
int len;
if(j==3||j==9)len=1;
else len=0;
if(j>3&&j<9){//Ñðåäíèè çíà÷åíèÿ äíåé
if(TradeM[i][AllDaysOfMonth]==0)AverageDay=0;
else AverageDay=TradeM[i][j]/TradeM[i][AllDaysOfMonth];
FileWriteString(FileHandle, (DoubleToStr(AverageDay, len)), StringLen(DoubleToStr(AverageDay, len)));
}
else FileWriteString(FileHandle, DoubleToStr(TradeM[i][j], len), StringLen(DoubleToStr(TradeM[i][j], len)));
if(j<9)FileWriteString(FileHandle, "\t", StringLen("\t"));
}
FileWriteString(FileHandle, "\r\n", StringLen("\r\n"));
}
FileWriteString(FileHandle, "\r\n", StringLen("\r\n"));
return(1);
}
int start()
{
//----
index=Bars-1;
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="TDM_"+Symbol()+BeginYear+"-"+EndYear+".txt";
FileHandle=FileOpen(FileName, FILE_BIN|FILE_WRITE);
if(FileHandle<1){
Print("Íå óäàëîñü îòêðûòü ôàéë, ïðè÷èíà:", GetLastError());
return;
}
while(BeginYear!=TimeYear(Time[index])){index--;}
for(iYear=BeginYear;iYear<=EndYear;iYear++){
while(iYear==TimeYear(Time[index])){
TDM=TradeDayOfMonth(index);
TradeMonth[TDM][AllDaysOfMonth]=TradeMonth[TDM][AllDaysOfMonth]+1;
TradeMonth[TDM][AllPips]=TradeMonth[TDM][AllPips]+(Close[index]-Open[index])/Point;
TradeMonth[TDM][RangeOfPrice]=TradeMonth[TDM][RangeOfPrice]+(High[index]-Low[index])/Point;
TradeMonth[TDM][AllPipsMod]=TradeMonth[TDM][AllPipsMod]+MathAbs((Close[index]-Open[index])/Point);
if(Close[index]>Open[index]) //Åñëè çàêðûòèå áîëüøå îòêðûòèÿ - äåíü ïîëîæèòåëüíûé
{
TradeMonth[TDM][PositivelyDays]=TradeMonth[TDM][PositivelyDays]+1;
TradeMonth[TDM][PositivelyPips]=TradeMonth[TDM][PositivelyPips]+(Close[index]-Open[index])/Point;
}
if(Close[index]<=Open[index]) //Åñëè çàêðûòèå ìåíüøå ëèáî ðàâíî îòêðûòèþ - äåíü îòðèöàòåëüíûé
{
TradeMonth[TDM][NegativelyDays]=TradeMonth[TDM][NegativelyDays]+1;
TradeMonth[TDM][NegativelyPips]=TradeMonth[TDM][NegativelyPips]+(Close[index]-Open[index])/Point;
}
if(TradeMonth[TDM][AllDaysOfMonth]!=0)
{TradeMonth[TDM][PercentPositivelyDays]=TradeMonth[TDM][PositivelyDays]*100/TradeMonth[TDM][AllDaysOfMonth];}
if(TradeMonth[TDM][AllPipsMod]!=0)
{TradeMonth[TDM][PercentPosityvelyPt]=TradeMonth[TDM][PositivelyPips]*100/TradeMonth[TDM][AllPipsMod];}
index--;
}
//îáúåäèíÿåì
for(int i=1;i<24;i++){
TradeMonthAll[i][AllDaysOfMonth]=TradeMonthAll[i][AllDaysOfMonth]+TradeMonth[i][AllDaysOfMonth];
TradeMonthAll[i][AllPips]=TradeMonthAll[i][AllPips]+TradeMonth[i][AllPips];
TradeMonthAll[i][RangeOfPrice]=TradeMonthAll[i][RangeOfPrice]+TradeMonth[i][RangeOfPrice];
TradeMonthAll[i][AllPipsMod]=TradeMonthAll[i][AllPipsMod]+TradeMonth[i][AllPipsMod];
TradeMonthAll[i][PositivelyDays]=TradeMonthAll[i][PositivelyDays]+TradeMonth[i][PositivelyDays];
TradeMonthAll[i][NegativelyDays]=TradeMonthAll[i][NegativelyDays]+TradeMonth[i][NegativelyDays];
TradeMonthAll[i][PositivelyPips]=TradeMonthAll[i][PositivelyPips]+TradeMonth[i][PositivelyPips];
TradeMonthAll[i][NegativelyPips]=TradeMonthAll[i][NegativelyPips]+TradeMonth[i][NegativelyPips];
if(TradeMonthAll[i][AllDaysOfMonth]!=0)
{TradeMonthAll[i][PercentPositivelyDays]=TradeMonthAll[i][PositivelyDays]*100/TradeMonthAll[i][AllDaysOfMonth];}
if(TradeMonthAll[i][AllPipsMod]!=0)
{TradeMonthAll[i][PercentPosityvelyPt]=TradeMonthAll[i][PositivelyPips]*100/TradeMonthAll[i][AllPipsMod];}
}
WriteResults(FileHandle, TradeMonth);
//îáíóëÿåì...
for(int a=1;a<24;a++){
for(int b=0;b<10;b++){
TradeMonth[a][b]=0;
}
}
}
//Èòîãîâàÿ ñòàòèñòêèêà
WriteResults(FileHandle, TradeMonthAll);
//----
return(0);
}
//+------------------------------------------------------------------+
Comments