Save this file as an Indicator in MetaEditor (MQL5/Indicators/CostJournalTagger.mq5).
MQL5 directly provides SYMBOL_SPREAD, SYMBOL_ASK, and SYMBOL_BID, and processes series arrays forwards by default (so indices run from 0 to rates_total - 1).
Code: Select all
//+------------------------------------------------------------------+
//| CostJournalTagger.mq5 |
//| EURUSD Microstructure Cost Tagger |
//+------------------------------------------------------------------+
#property copyright "Execution Journal"
#property link ""
#property version "1.00"
#property indicator_chart_window
#property indicator_plots 0
//--- Input Parameters
input group "=== SPREAD THRESHOLDS (PIPS) ==="
input double InpCheapSpreadPips = 0.4; // Cheap spread ceiling
input double InpExpensiveSpreadPips = 1.1; // Expensive spread floor
input group "=== EXPENSIVE TIME WINDOWS (BROKER TIME) ==="
input bool InpUseTimeFilter = true; // Flag first 15 mins of sessions as Expensive
input int InpLonOpenHour = 8; // London Open Hour
input int InpLonOpenMinStart = 0; // London Open Start Minute
input int InpLonOpenMinEnd = 15; // London Open End Minute
input int InpNYOpenHour = 13; // NY Open Hour
input int InpNYOpenMinStart = 0; // NY Open Start Minute
input int InpNYOpenMinEnd = 15; // NY Open End Minute
input group "=== VISUAL SETTINGS ==="
input color InpColorCheap = clrLimeGreen;
input color InpColorNormal = clrDodgerBlue;
input color InpColorExpensive = clrCrimson;
input string InpPrefix = "CJ5_"; // Object Prefix
//--- Global Variables
double g_pipPoint = 0.0001;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
int digits = (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS);
double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
g_pipPoint = (digits == 3 || digits == 5) ? point * 10.0 : point;
CreateDashboard();
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
ObjectsDeleteAll(0, InpPrefix);
}
//+------------------------------------------------------------------+
//| Helper: Classify execution cost bucket |
//+------------------------------------------------------------------+
void EvaluateCost(datetime barTime, double spreadPips, string &outTag, color &outColor, bool &outLimitOnly)
{
bool isExpensiveTime = false;
if(InpUseTimeFilter)
{
MqlDateTime tm;
TimeToStruct(barTime, tm);
bool isLondonOpen = (tm.hour == InpLonOpenHour && tm.min >= InpLonOpenMinStart && tm.min < InpLonOpenMinEnd);
bool isNYOpen = (tm.hour == InpNYOpenHour && tm.min >= InpNYOpenMinStart && tm.min < InpNYOpenMinEnd);
if(isLondonOpen || isNYOpen)
isExpensiveTime = true;
}
if(spreadPips >= InpExpensiveSpreadPips || isExpensiveTime)
{
outTag = "EXPENSIVE";
outColor = InpColorExpensive;
outLimitOnly = true;
}
else if(spreadPips <= InpCheapSpreadPips)
{
outTag = "CHEAP";
outColor = InpColorCheap;
outLimitOnly = false;
}
else
{
outTag = "NORMAL";
outColor = InpColorNormal;
outLimitOnly = false;
}
}
//+------------------------------------------------------------------+
//| 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[])
{
if(rates_total < 3) return(0);
// 1. Calculate Live Spread & Update Dashboard
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double liveSpreadPips = (ask - bid) / g_pipPoint;
string liveTag = "";
color liveColor = clrWhite;
bool liveLimitOnly = false;
EvaluateCost(TimeCurrent(), liveSpreadPips, liveTag, liveColor, liveLimitOnly);
UpdateDashboard(liveSpreadPips, liveTag, liveColor, liveLimitOnly);
// 2. Iterate Historical Bars
int startIndex = (prev_calculated > 1) ? prev_calculated - 1 : 1;
for(int i = startIndex; i < rates_total - 1; i++)
{
double barSpreadPips = (spread[i] > 0) ? (spread[i] * SymbolInfoDouble(_Symbol, SYMBOL_POINT) / g_pipPoint) : liveSpreadPips;
string tag = "";
color tagColor = clrWhite;
bool limitOnly = false;
EvaluateCost(time[i], barSpreadPips, tag, tagColor, limitOnly);
// Example setup trigger: replace with your liquidity sweep / PA logic
bool bullEngulf = (close[i] > open[i] && close[i-1] < open[i-1] && close[i] > open[i-1] && open[i] < close[i-1]);
bool bearEngulf = (close[i] < open[i] && close[i-1] > open[i-1] && close[i] < open[i-1] && open[i] > close[i-1]);
string objName = InpPrefix + "Tag_" + TimeToString(time[i]);
if(bullEngulf && ObjectFind(0, objName) < 0)
{
string text = StringFormat("[BULL] %s | %s", tag, limitOnly ? "LIMIT ONLY" : "MARKET OK");
CreateChartLabel(objName, time[i], low[i] - (15 * g_pipPoint), text, tagColor, ANCHOR_TOP);
}
else if(bearEngulf && ObjectFind(0, objName) < 0)
{
string text = StringFormat("[BEAR] %s | %s", tag, limitOnly ? "LIMIT ONLY" : "MARKET OK");
CreateChartLabel(objName, time[i], high[i] + (15 * g_pipPoint), text, tagColor, ANCHOR_BOTTOM);
}
}
return(rates_total);
}
//+------------------------------------------------------------------+
//| Dashboard UI Helpers |
//+------------------------------------------------------------------+
void CreateDashboard()
{
string name = InpPrefix + "HUD";
if(ObjectFind(0, name) < 0)
{
ObjectCreate(0, name, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, name, OBJPROP_CORNER, CORNER_RIGHT_UPPER);
ObjectSetInteger(0, name, OBJPROP_XDISTANCE, 20);
ObjectSetInteger(0, name, OBJPROP_YDISTANCE, 25);
ObjectSetInteger(0, name, OBJPROP_FONTSIZE, 10);
ObjectSetString(0, name, OBJPROP_FONT, "Consolas");
}
}
void UpdateDashboard(double spreadPips, string tag, color tagColor, bool limitOnly)
{
string name = InpPrefix + "HUD";
string text = StringFormat("Spread: %.1f pips | Cost: %s (%s)",
spreadPips, tag, limitOnly ? "LIMIT REQUIRED" : "MARKET OK");
ObjectSetString(0, name, OBJPROP_TEXT, text);
ObjectSetInteger(0, name, OBJPROP_COLOR, tagColor);
}
void CreateChartLabel(string name, datetime t, double price, string text, color clr, ENUM_ANCHOR_POINT anchor)
{
ObjectCreate(0, name, OBJ_TEXT, 0, t, price);
ObjectSetString(0, name, OBJPROP_TEXT, text);
ObjectSetString(0, name, OBJPROP_FONT, "Arial Bold");
ObjectSetInteger(0, name, OBJPROP_FONTSIZE, 8);
ObjectSetInteger(0, name, OBJPROP_COLOR, clr);
ObjectSetInteger(0, name, OBJPROP_ANCHOR, anchor);
}