//+------------------------------------------------------------------+ //| FXMacroData_MacroPanel.mq5 | //| Displays a live macroeconomic data panel on the MT5 chart. | //| Powered by the FXMacroData API — https://fxmacrodata.com | //+------------------------------------------------------------------+ // // SETUP // 1. Copy FXMacroData.mqh → \MQL5\Include\ // 2. Copy this file → \MQL5\Indicators\ // 3. In MT5: Tools > Options > Expert Advisors // Check "Allow WebRequest for listed URL" and add: // https://fxmacrodata.com // 4. Attach the indicator to any currency-pair chart. // 5. Enter your API key in the Inputs tab (leave blank to use the // free USD tier for testing). // // The panel refreshes every RefreshMinutes minutes. // Indicators shown: Policy Rate, CPI Inflation, Unemployment, GDP. // // DOCUMENTATION https://fxmacrodata.com/documentation/metatrader // GET AN API KEY https://fxmacrodata.com/subscribe //+------------------------------------------------------------------+ #property copyright "FXMacroData" #property link "https://fxmacrodata.com" #property version "1.00" #property indicator_chart_window #property indicator_plots 0 #include //--- Inputs ----------------------------------------------------------- input string ApiKey = ""; // API Key (blank = free USD tier) input string BaseOverride = ""; // Override base currency (e.g. EUR) input string QuoteOverride = ""; // Override quote currency (e.g. USD) input int RefreshMinutes = 5; // Refresh interval (minutes) input int PanelX = 10; // Panel X offset from top-left corner input int PanelY = 30; // Panel Y offset from top-left corner input int FontSize = 9; // Text size in points input color ColorHeader = C'100,149,237'; // Header colour input color ColorLabel = C'148,163,184'; // Label colour input color ColorValue = clrWhite; // Value colour //--- State ------------------------------------------------------------ string g_base = ""; string g_quote = ""; datetime g_last_refresh = 0; struct FmdRow { string label; double value; string unit; }; FmdRow g_rows[]; string INDICATORS[] = {"policy_rate","inflation","unemployment","gdp"}; string LABELS[] = {"Policy Rate","Inflation","Unemployment","GDP Growth"}; //+------------------------------------------------------------------+ int OnInit() { DetectCurrencies(); Refresh(); return INIT_SUCCEEDED; } //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const int begin, const double& price[]) { if(TimeCurrent() - g_last_refresh >= (datetime)(RefreshMinutes * 60)) Refresh(); DrawPanel(); return rates_total; } //+------------------------------------------------------------------+ void OnDeinit(const int reason) { ObjectsDeleteAll(0, "FXMD_"); ChartRedraw(0); } //+------------------------------------------------------------------+ void DetectCurrencies() { string sym = _Symbol; g_base = (StringLen(BaseOverride) > 0) ? BaseOverride : StringSubstr(sym, 0, 3); g_quote = (StringLen(QuoteOverride) > 0) ? QuoteOverride : StringSubstr(sym, 3, 3); StringToLower(g_base); StringToLower(g_quote); } //+------------------------------------------------------------------+ void Refresh() { int n = ArraySize(INDICATORS); ArrayResize(g_rows, n * 2); int idx = 0; for(int i = 0; i < n; i++) { string json_b = FMD_GetIndicator(g_base, INDICATORS[i], ApiKey); string json_q = FMD_GetIndicator(g_quote, INDICATORS[i], ApiKey); string base_upper = g_base; StringToUpper(base_upper); string quote_upper = g_quote; StringToUpper(quote_upper); g_rows[idx].label = base_upper + " " + LABELS[i]; g_rows[idx].value = FMD_LastValue(json_b); g_rows[idx].unit = "%"; idx++; g_rows[idx].label = quote_upper + " " + LABELS[i]; g_rows[idx].value = FMD_LastValue(json_q); g_rows[idx].unit = "%"; idx++; } g_last_refresh = TimeCurrent(); } //+------------------------------------------------------------------+ void DrawPanel() { int line_h = FontSize + 9; int x = PanelX; int y = PanelY; string base_upper = g_base; StringToUpper(base_upper); string quote_upper = g_quote; StringToUpper(quote_upper); // Header PutLabel("FXMD_HDR", base_upper + "/" + quote_upper + " Macro Snapshot", x, y, ColorHeader, FontSize + 1, true); y += line_h + 2; PutLabel("FXMD_SUB", "Powered by FXMacroData", x, y, ColorLabel, FontSize - 1, false); y += line_h + 4; int rows = ArraySize(g_rows); for(int i = 0; i < rows; i++) { string val_str = (g_rows[i].value == EMPTY_VALUE) ? "n/a" : DoubleToString(g_rows[i].value, 2) + g_rows[i].unit; PutLabel("FXMD_L" + IntegerToString(i), g_rows[i].label + ":", x, y, ColorLabel, FontSize, false); PutLabel("FXMD_V" + IntegerToString(i), val_str, x + 148, y, ColorValue, FontSize, false); y += line_h; } ChartRedraw(0); } //+------------------------------------------------------------------+ void PutLabel(const string name, const string text, const int x, const int y, const color clr, const int size, const bool bold) { if(ObjectFind(0, name) < 0) ObjectCreate(0, name, OBJ_LABEL, 0, 0, 0); ObjectSetInteger(0, name, OBJPROP_CORNER, CORNER_LEFT_UPPER); ObjectSetInteger(0, name, OBJPROP_XDISTANCE, x); ObjectSetInteger(0, name, OBJPROP_YDISTANCE, y); ObjectSetString(0, name, OBJPROP_TEXT, text); ObjectSetInteger(0, name, OBJPROP_COLOR, clr); ObjectSetInteger(0, name, OBJPROP_FONTSIZE, size); ObjectSetString(0, name, OBJPROP_FONT, bold ? "Arial Bold" : "Arial"); ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false); ObjectSetInteger(0, name, OBJPROP_HIDDEN, true); }