The errors are occurring because MQL5 doesn't have the TIME_DAY_OF_WEEK_LONG, TIME_MONTH_LONG, and TIME_YEAR constants. Here's the corrected code with proper MQL5 date formatting:
mq5
//+------------------------------------------------------------------+
//| ConsistencyScore.mq5 |
//| Consistency Score Indicator |
//| |
//+------------------------------------------------------------------+
#property copyright "Consistency Score Indicator"
#property link ""
#property version "1.01"
//--- Indicator will now display in the main chart window
#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots 0
//--- Input parameters
input color TextColor = clrBlack; // Text Color
input int FontSize = 9; // Font Size
input string FontName = "Arial"; // Font Name
input int XOffset = 10; // X Offset
input int YOffset = 20; // Y Offset
input bool ShowOverall = true; // Show Overall Score
input bool ShowDaily = true; // Show Daily Scores
input bool ShowWeekly = true; // Show Weekly Scores
input bool ShowMonthly = true; // Show Monthly Scores
input bool ShowYearly = true; // Show Yearly Scores
//--- Global variables
string prefix = "CS_";
datetime firstTradeTime = 0;
//+------------------------------------------------------------------+
//| Structure to hold daily profit data |
//+------------------------------------------------------------------+
struct DailyProfit
{
datetime date;
double profit;
};
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
IndicatorSetString(INDICATOR_SHORTNAME, "Consistency Score");
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
ObjectsDeleteAll(0, prefix);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
// Clear previous objects
ObjectsDeleteAll(0, prefix);
// Get all historical deals
DailyProfit dailyProfits[];
if(!GetDailyProfits(dailyProfits))
{
DisplayText("No trading history found", XOffset, YOffset);
return(rates_total);
}
int yPos = YOffset;
// Display Active From date (Name of Day, Date Month Year format)
if(firstTradeTime > 0)
{
string dayName = GetDayName(firstTradeTime);
string formattedDate = GetFormattedDate(firstTradeTime);
DisplayText(StringFormat("Active From: %s, %s", dayName, formattedDate), XOffset, yPos);
yPos += 15;
}
// Overall Consistency Score
if(ShowOverall)
{
double overallScore = CalculateConsistencyScore(dailyProfits, 0, ArraySize(dailyProfits));
DisplayText("=== OVERALL ===", XOffset, yPos);
yPos += 15;
DisplayText(StringFormat("Overall Score: %.2f%%", overallScore), XOffset, yPos);
yPos += 20;
}
// Daily Scores (Current Week)
if(ShowDaily)
{
DisplayDailyScores(dailyProfits, yPos);
}
// Weekly Scores (Current Month)
if(ShowWeekly)
{
DisplayWeeklyScores(dailyProfits, yPos);
}
// Monthly Scores
if(ShowMonthly)
{
DisplayMonthlyScores(dailyProfits, yPos);
}
// Yearly Scores
if(ShowYearly)
{
DisplayYearlyScores(dailyProfits, yPos);
}
return(rates_total);
}
//+------------------------------------------------------------------+
//| Get day name from datetime |
//+------------------------------------------------------------------+
string GetDayName(datetime dt)
{
MqlDateTime mdt;
TimeToStruct(dt, mdt);
string dayNames[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
return dayNames[mdt.day_of_week];
}
//+------------------------------------------------------------------+
//| Get formatted date (DD Month YYYY) |
//+------------------------------------------------------------------+
string GetFormattedDate(datetime dt)
{
MqlDateTime mdt;
TimeToStruct(dt, mdt);
string monthNames[12] = {"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};
return StringFormat("%02d %s %d", mdt.day, monthNames[mdt.mon - 1], mdt.year);
}
//+------------------------------------------------------------------+
//| Get daily profits from history |
//+------------------------------------------------------------------+
bool GetDailyProfits(DailyProfit &profits[])
{
ArrayResize(profits, 0);
// Request deal history
if(!HistorySelect(0, TimeCurrent()))
return false;
int dealsTotal = HistoryDealsTotal();
if(dealsTotal == 0)
return false;
// Temporary array to store daily profits
double dayProfits[];
datetime dayDates[];
for(int i = 0; i < dealsTotal; i++)
{
ulong ticket = HistoryDealGetTicket(i);
if(ticket == 0)
continue;
// Skip balance operations
if(HistoryDealGetInteger(ticket, DEAL_TYPE) == DEAL_TYPE_BALANCE)
continue;
datetime dealTime = (datetime)HistoryDealGetInteger(ticket, DEAL_TIME);
double profit = HistoryDealGetDouble(ticket, DEAL_PROFIT);
double swap = HistoryDealGetDouble(ticket, DEAL_SWAP);
double commission = HistoryDealGetDouble(ticket, DEAL_COMMISSION);
double totalProfit = profit + swap + commission;
// Get date only (remove time component)
MqlDateTime dt;
TimeToStruct(dealTime, dt);
dt.hour = 0;
dt.min = 0;
dt.sec = 0;
datetime dayDate = StructToTime(dt);
// Find or add day
int dayIndex = -1;
for(int j = 0; j < ArraySize(dayDates); j++)
{
if(dayDates[j] == dayDate)
{
dayIndex = j;
break;
}
}
if(dayIndex == -1)
{
ArrayResize(dayDates, ArraySize(dayDates) + 1);
ArrayResize(dayProfits, ArraySize(dayProfits) + 1);
dayIndex = ArraySize(dayDates) - 1;
dayDates[dayIndex] = dayDate;
dayProfits[dayIndex] = 0;
}
dayProfits[dayIndex] += totalProfit;
}
// Convert to DailyProfit structure
ArrayResize(profits, ArraySize(dayDates));
for(int i = 0; i < ArraySize(dayDates); i++)
{
profits[i].date = dayDates[i];
profits[i].profit = dayProfits[i];
}
// Sort by date (oldest first)
for(int i = 0; i < ArraySize(profits) - 1; i++)
{
for(int j = i + 1; j < ArraySize(profits); j++)
{
if(profits[i].date > profits[j].date)
{
DailyProfit temp = profits[i];
profits[i] = profits[j];
profits[j] = temp;
}
}
}
// Set first trade time
if(ArraySize(profits) > 0)
{
firstTradeTime = profits[0].date;
return true;
}
return false;
}
//+------------------------------------------------------------------+
//| Calculate consistency score for a date range |
//+------------------------------------------------------------------+
double CalculateConsistencyScore(const DailyProfit &profits[], int startIdx, int endIdx)
{
if(startIdx >= endIdx || startIdx < 0 || endIdx > ArraySize(profits))
return 0;
// Handle single-day periods specially
if(endIdx - startIdx == 1)
return 100.0; // Full consistency for single day
double maxAbsProfit = 0;
double sumAbsProfit = 0;
for(int i = startIdx; i < endIdx; i++)
{
double absProfit = MathAbs(profits[i].profit);
sumAbsProfit += absProfit;
if(absProfit > maxAbsProfit)
maxAbsProfit = absProfit;
}
if(sumAbsProfit == 0)
return 0;
return (1.0 - (maxAbsProfit / sumAbsProfit)) * 100.0;
}
//+------------------------------------------------------------------+
//| Display daily scores for current week |
//+------------------------------------------------------------------+
void DisplayDailyScores(const DailyProfit &profits[], int &yPos)
{
DisplayText("=== DAILY (Current Week) ===", XOffset, yPos);
yPos += 15;
MqlDateTime currentTime;
TimeToStruct(TimeCurrent(), currentTime);
// Find Monday of current week
int dayOfWeek = currentTime.day_of_week;
if(dayOfWeek == 0) dayOfWeek = 7; // Sunday = 7
datetime monday = TimeCurrent() - (dayOfWeek - 1) * 86400;
MqlDateTime mondayDt;
TimeToStruct(monday, mondayDt);
mondayDt.hour = 0;
mondayDt.min = 0;
mondayDt.sec = 0;
monday = StructToTime(mondayDt);
string dayNames[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
for(int d = 0; d < 7; d++)
{
datetime dayDate = monday + d * 86400;
if(dayDate > TimeCurrent())
break; // Skip future days
// Check if any trades exist for this day
bool hasTrade = false;
for(int i = 0; i < ArraySize(profits); i++)
{
if(profits[i].date == dayDate)
{
hasTrade = true;
break;
}
}
if(hasTrade)
{
// Extract profits for this day
DailyProfit dayProfits[];
int count = 0;
for(int i = 0; i < ArraySize(profits); i++)
{
if(profits[i].date == dayDate)
{
ArrayResize(dayProfits, count+1);
dayProfits[count] = profits[i];
count++;
}
}
double score = CalculateConsistencyScore(dayProfits, 0, count);
DisplayText(StringFormat("%s: %.2f%%", dayNames[d], score), XOffset + 10, yPos);
yPos += 15;
}
else
{
DisplayText(StringFormat("%s: Inactive", dayNames[d]), XOffset + 10, yPos);
yPos += 15;
}
}
yPos += 10;
}
//+------------------------------------------------------------------+
//| Display weekly scores for current month |
//+------------------------------------------------------------------+
void DisplayWeeklyScores(const DailyProfit &profits[], int &yPos)
{
DisplayText("=== WEEKLY (Current Month) ===", XOffset, yPos);
yPos += 15;
MqlDateTime currentTime;
TimeToStruct(TimeCurrent(), currentTime);
// First day of current month
currentTime.day = 1;
currentTime.hour = 0;
currentTime.min = 0;
currentTime.sec = 0;
datetime monthStart = StructToTime(currentTime);
// Find first Monday of the month
MqlDateTime monthStartDt;
TimeToStruct(monthStart, monthStartDt);
int dayOfWeek = monthStartDt.day_of_week;
if(dayOfWeek == 0) dayOfWeek = 7;
datetime firstMonday = monthStart;
if(dayOfWeek != 1)
firstMonday = monthStart + (8 - dayOfWeek) * 86400;
for(int w = 0; w < 5; w++) // Show up to 5 weeks in the month
{
datetime weekStart = firstMonday + w * 7 * 86400;
datetime weekEnd = weekStart + 7 * 86400;
MqlDateTime weekStartDt;
TimeToStruct(weekStart, weekStartDt);
if(weekStartDt.mon != currentTime.mon)
break; // Stop if week is in next month
// Find profits for this week
int startIdx = -1, endIdx = -1;
for(int i = 0; i < ArraySize(profits); i++)
{
if(profits[i].date >= weekStart && profits[i].date < weekEnd)
{
if(startIdx == -1)
startIdx = i;
endIdx = i + 1;
}
}
if(startIdx != -1)
{
double score = CalculateConsistencyScore(profits, startIdx, endIdx);
DisplayText(StringFormat("Week %d: %.2f%%", w + 1, score), XOffset + 10, yPos);
yPos += 15;
}
else
{
DisplayText(StringFormat("Week %d: Inactive", w + 1), XOffset + 10, yPos);
yPos += 15;
}
}
yPos += 10;
}
//+------------------------------------------------------------------+
//| Display monthly scores |
//+------------------------------------------------------------------+
void DisplayMonthlyScores(const DailyProfit &profits[], int &yPos)
{
DisplayText("=== MONTHLY ===", XOffset, yPos);
yPos += 15;
datetime now = TimeCurrent();
// Define periods (excluded Last 12 Months)
struct Period
{
string name;
int months;
};
Period periods[];
ArrayResize(periods, 4);
periods[0].name = "Current Month"; periods[0].months = 0;
periods[1].name = "Last 1 Month"; periods[1].months = 1;
periods[2].name = "Last 3 Months"; periods[2].months = 3;
periods[3].name = "Last 6 Months"; periods[3].months = 6;
for(int p = 0; p < ArraySize(periods); p++)
{
datetime startDate;
if(periods[p].months == 0)
{
// Current month
MqlDateTime dt;
TimeToStruct(now, dt);
dt.day = 1;
dt.hour = 0;
dt.min = 0;
dt.sec = 0;
startDate = StructToTime(dt);
}
else
{
// Last X months
startDate = now - periods[p].months * 30 * 86400;
}
// Find profits in range
int startIdx = -1, endIdx = ArraySize(profits);
for(int i = 0; i < ArraySize(profits); i++)
{
if(profits[i].date >= startDate && startIdx == -1)
startIdx = i;
}
if(startIdx != -1)
{
double score = CalculateConsistencyScore(profits, startIdx, endIdx);
DisplayText(StringFormat("%s: %.2f%%", periods[p].name, score), XOffset + 10, yPos);
yPos += 15;
}
else
{
DisplayText(StringFormat("%s: Inactive", periods[p].name), XOffset + 10, yPos);
yPos += 15;
}
}
yPos += 10;
}
//+------------------------------------------------------------------+
//| Display yearly scores |
//+------------------------------------------------------------------+
void DisplayYearlyScores(const DailyProfit &profits[], int &yPos)
{
DisplayText("=== YEARLY ===", XOffset, yPos);
yPos += 15;
datetime now = TimeCurrent();
MqlDateTime nowDt;
TimeToStruct(now, nowDt);
// Current year
MqlDateTime yearStartDt = nowDt;
yearStartDt.mon = 1;
yearStartDt.day = 1;
yearStartDt.hour = 0;
yearStartDt.min = 0;
yearStartDt.sec = 0;
datetime currentYearStart = StructToTime(yearStartDt);
// Find profits for current year
int startIdx = -1;
for(int i = 0; i < ArraySize(profits); i++)
{
if(profits[i].date >= currentYearStart)
{
startIdx = i;
break;
}
}
if(startIdx != -1)
{
double score = CalculateConsistencyScore(profits, startIdx, ArraySize(profits));
DisplayText(StringFormat("Current Year: %.2f%%", score), XOffset + 10, yPos);
yPos += 15;
}
else
{
DisplayText("Current Year: Inactive", XOffset + 10, yPos);
yPos += 15;
}
// Last 1-5 years
for(int y = 1; y <= 5; y++)
{
datetime yearStart = now - y * 365 * 86400;
int yStartIdx = -1, yEndIdx = ArraySize(profits);
for(int i = 0; i < ArraySize(profits); i++)
{
if(profits[i].date >= yearStart && yStartIdx