CTD
Download scriptThis script is designed to perform a historical statistical analysis of trading data. It specifically examines how market behavior correlates with the calendar day of the month (for example, observing if certain dates consistently show more upward or downward movement).
Core Logic and Workflow
1. Data Validation and Setup Before starting any calculations, the script performs several safety checks:
- It ensures you are viewing the correct timeframe (Daily charts only).
- It verifies that the start and end years provided by the user are valid and chronologically correct.
- It checks that there is enough historical data loaded into the platform to perform a meaningful analysis.
2. Data Processing Loop The script iterates through every trading day within your specified date range. For every single day, it identifies the specific calendar day (e.g., if the date is October 15th, it targets "Day 15"). It then gathers several key pieces of information for that specific day:
- Price Movement: The total change between the opening and closing prices.
- Volatility: The price range (the distance between the highest and lowest price reached during that day).
- Directional Statistics: It tracks how many "positive" days (where the close is higher than the open) and "negative" days (where the close is lower or equal to the open) occurred for each calendar date.
- Pips Analysis: It calculates the actual magnitude of these movements in pips, separating the gains from the losses.
3. Yearly and Lifetime Accumulation The logic operates on two levels of "memory":
- Yearly Tracking: For every year it processes, it compiles a report. It calculates averages for price changes and percentages for positive performance relative to the total volume of trades for that specific day of the month.
- Lifetime Tracking: It maintains a secondary "master" tally that adds up the data from every year. This allows you to see the total historical trend across your entire chosen period (e.g., "How does Day 15 behave over a 10-year span?").
4. Result Export Instead of showing this data on your trading chart, the script generates a dedicated text file. The filename automatically includes the currency pair and the years analyzed. This file provides a structured table containing:
- The cumulative totals for each day of the month.
- Average price movements (positive and negative).
- Price ranges.
- Percentages representing the frequency of positive moves.
Summary for the User
In plain terms, this tool tells you: "Historically, on the Xth day of the month, what is the average price movement, how volatile is it usually, and what percentage of those days ended in a profit?" It allows a trader to identify potential seasonal patterns or "day-of-the-month" biases in the market.
//+------------------------------------------------------------------+
//| CDM.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 32
#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 CDM; //Ëèáî òîðãîâûé äåíü ìåñÿöà ëèáî ïðîñòî äåíü ìåñÿöà
int LTD; //Ïîñëåäíèé òîðãîâûé äåíü, ïðèíèìàåò çíà÷åíèå ëèáî 23 ëèáî 32 â çàâèñèìîñòè îò ðåæèìà èññëåäîâàíèÿ
int FileHandle; //Äèñêðèïòîð ôàéëà, â êîòîðûé ïèøåòñÿ ðåçóëüòàò
double TradeMonth[32][10]; //ìàññèâ äíåé.
double TradeMonthAll[32][10]; //Èòîãîâàÿ ñòàòèñòèêà
/*
Îñíîâíàÿ ôóíêöèÿ ñêðèïòà, âîçâðàùàåò òîðãîâûé äåíü ìåñÿöà. Íàïðèìåð â ìåñÿöå 22 òîðãîâûõ äíÿ, ò.å.
äåíü 31.10.08 áóäåò 23 òîðãîâûì äíåì
*/
int TradeDayOfMonth(int index){
return(TimeDay(Time[index]));
}
int WriteResults(int FileHandle, double TradeM[32][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<32;i++){
//È ñíîâà ãîëü íà âûäóìêè õèòðà
double d=i;
FileWriteString(FileHandle, "CDM-", StringLen("CDM-"));
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="CDM_"+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])){
CDM=TradeDayOfMonth(index);
TradeMonth[CDM][AllDaysOfMonth]=TradeMonth[CDM][AllDaysOfMonth]+1;
TradeMonth[CDM][AllPips]=TradeMonth[CDM][AllPips]+(Close[index]-Open[index])/Point;
TradeMonth[CDM][RangeOfPrice]=TradeMonth[CDM][RangeOfPrice]+(High[index]-Low[index])/Point;
TradeMonth[CDM][AllPipsMod]=TradeMonth[CDM][AllPipsMod]+MathAbs((Close[index]-Open[index])/Point);
if(Close[index]>Open[index]) //Åñëè çàêðûòèå áîëüøå îòêðûòèÿ - äåíü ïîëîæèòåëüíûé
{
TradeMonth[CDM][PositivelyDays]=TradeMonth[CDM][PositivelyDays]+1;
TradeMonth[CDM][PositivelyPips]=TradeMonth[CDM][PositivelyPips]+(Close[index]-Open[index])/Point;
}
if(Close[index]<=Open[index]) //Åñëè çàêðûòèå ìåíüøå ëèáî ðàâíî îòêðûòèþ - äåíü îòðèöàòåëüíûé
{
TradeMonth[CDM][NegativelyDays]=TradeMonth[CDM][NegativelyDays]+1;
TradeMonth[CDM][NegativelyPips]=TradeMonth[CDM][NegativelyPips]+(Close[index]-Open[index])/Point;
}
if(TradeMonth[CDM][AllDaysOfMonth]!=0)
{TradeMonth[CDM][PercentPositivelyDays]=TradeMonth[CDM][PositivelyDays]*100/TradeMonth[CDM][AllDaysOfMonth];}
if(TradeMonth[CDM][AllPipsMod]!=0)
{TradeMonth[CDM][PercentPosityvelyPt]=TradeMonth[CDM][PositivelyPips]*100/TradeMonth[CDM][AllPipsMod];}
index--;
}
//îáúåäèíÿåì
for(int i=1;i<32;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<32;a++){
for(int b=0;b<10;b++){
TradeMonth[a][b]=0;
}
}
}
//Èòîãîâàÿ ñòàòèñòêèêà
WriteResults(FileHandle, TradeMonthAll);
//----
return(0);
}
//+------------------------------------------------------------------+
Comments