//+------------------------------------------------------------------+ //| OnChartTargetDisplay_v2.mq5 | //| Copyright © 2025, Your Name Here | //| https://www.example.com | //+------------------------------------------------------------------+ #property copyright "Copyright © 2025, Your Name Here" #property link "https://www.example.com" #property version "2.01" // Updated version
//--- This indicator draws on the chart but has no plot lines #property indicator_chart_window #property indicator_buffers 0 #property indicator_plots 0
//--- User Inputs --- input group "Calculation Settings" input double InpProfitTargetPercent = 10.0; // Target profit % to calculate when trades close
input group "Display Settings" input color InpLabelColor = clrWhite; // Text color for the label input int InpLabelFontSize = 12; // Font size for the label input string InpFontName = "Arial"; // Font name input int InpLabelXOffset = 10; // X distance from top-left corner input int InpLabelYOffset = 30; // Y distance from top-left corner input bool InpShowBackground = true; // Show background for visibility input color InpBackgroundColor = clrBlack; // Background color
//--- Global variables --- string g_label_name = ""; // Unique name for our text label object string g_background_name = ""; // Unique name for background int g_last_positions_count = -1; // State variable to track position count
//+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- Generate unique names for objects int chart_id = (int)ChartID(); g_label_name = "TargetDisplay_Label_" + IntegerToString(chart_id); g_background_name = "TargetDisplay_BG_" + IntegerToString(chart_id);
//--- Clean up any existing objects ObjectDelete(ChartID(), g_label_name); ObjectDelete(ChartID(), g_background_name);
//--- Create background if enabled if(InpShowBackground) { ObjectCreate(ChartID(), g_background_name, OBJ_RECTANGLE_LABEL, 0, 0, 0); ObjectSetInteger(ChartID(), g_background_name, OBJPROP_CORNER, CORNER_LEFT_UPPER); ObjectSetInteger(ChartID(), g_background_name, OBJPROP_XDISTANCE, InpLabelXOffset - 5); ObjectSetInteger(ChartID(), g_background_name, OBJPROP_YDISTANCE, InpLabelYOffset - 5); ObjectSetInteger(ChartID(), g_background_name, OBJPROP_XSIZE, 300); // Will be adjusted dynamically ObjectSetInteger(ChartID(), g_background_name, OBJPROP_YSIZE, 30); // Will be adjusted dynamically ObjectSetInteger(ChartID(), g_background_name, OBJPROP_COLOR, InpBackgroundColor); ObjectSetInteger(ChartID(), g_background_name, OBJPROP_BACK, false); ObjectSetInteger(ChartID(), g_background_name, OBJPROP_SELECTABLE, false); ObjectSetInteger(ChartID(), g_background_name, OBJPROP_BORDER_TYPE, BORDER_FLAT); }
//--- Create the text label ObjectCreate(ChartID(), g_label_name, OBJ_LABEL, 0, 0, 0); ObjectSetInteger(ChartID(), g_label_name, OBJPROP_CORNER, CORNER_LEFT_UPPER); ObjectSetInteger(ChartID(), g_label_name, OBJPROP_XDISTANCE, InpLabelXOffset); ObjectSetInteger(ChartID(), g_label_name, OBJPROP_YDISTANCE, InpLabelYOffset); ObjectSetInteger(ChartID(), g_label_name, OBJPROP_COLOR, InpLabelColor); ObjectSetString(ChartID(), g_label_name, OBJPROP_FONT, InpFontName); ObjectSetInteger(ChartID(), g_label_name, OBJPROP_FONTSIZE, InpLabelFontSize); ObjectSetInteger(ChartID(), g_label_name, OBJPROP_SELECTABLE, false);
//--- Initialize state and update display g_last_positions_count = PositionsTotal(); UpdateTargetDisplay();
//--- Set timer for periodic updates EventSetMillisecondTimer(500);
return(INIT_SUCCEEDED); }
//+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- Kill timer EventKillTimer();
//--- Clean up objects ObjectDelete(ChartID(), g_label_name); ObjectDelete(ChartID(), g_background_name); }
//+------------------------------------------------------------------+ //| Timer event handler | //+------------------------------------------------------------------+ void OnTimer() { //--- The timer ensures the display is always up-to-date with equity changes UpdateTargetDisplay(); }
//+------------------------------------------------------------------+ //| 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[]) { //--- Get current position count int current_positions = PositionsTotal();
//--- Check if a position was opened or closed if(current_positions != g_last_positions_count) { UpdateTargetDisplay(); g_last_positions_count = current_positions; }
return(rates_total); }
//+------------------------------------------------------------------+ //| Update the display | //+------------------------------------------------------------------+ void UpdateTargetDisplay() { // --- Calculate required values first --- double current_balance = AccountInfoDouble(ACCOUNT_BALANCE); double target_equity = current_balance * (1.0 + InpProfitTargetPercent / 100.0); int positions = PositionsTotal(); string label_text;
// --- Determine the text to display based on position status --- if(positions > 0) { // --- Positions are open: Show current equity vs the target --- double current_equity = AccountInfoDouble(ACCOUNT_EQUITY); label_text = StringFormat("Equity: %.2f / Target: %.2f", current_equity, target_equity); } else { // --- No positions: Show the target for the next trade cycle --- label_text = StringFormat("Next Target Equity: %.2f", target_equity); }
//--- Update the label text ObjectSetString(ChartID(), g_label_name, OBJPROP_TEXT, label_text);
//--- Adjust background width if enabled if(InpShowBackground) { // A more robust way to estimate text width for variable-width fonts int text_width = (int)(StringLen(label_text) * InpLabelFontSize * 0.65); ObjectSetInteger(ChartID(), g_background_name, OBJPROP_XSIZE, text_width + 15); // Also adjust height in case font is very large ObjectSetInteger(ChartID(), g_background_name, OBJPROP_YSIZE, InpLabelFontSize + 18); }
//--- Force redraw ChartRedraw(); }
//+------------------------------------------------------------------+ //| Chart event handler | //+------------------------------------------------------------------+ void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { //--- Refresh on some chart events to ensure visibility if(id == CHARTEVENT_CHART_CHANGE) { // Re-initialize to handle template changes etc. OnInit(); } } //+------------------------------------------------------------------+
make the indi more efficient