IC Markets

My custom Xau/Xag indicator pro version

Optimize MetaTrader 4, MetaTrader 5, cTrader, and TradingView for speed. Discuss Level II Market Depth (DOM), custom hotkeys, and volume indicators.
Post Reply
PTScalper
Site Admin
Posts: 210
Joined: Mon Jul 20, 2026 1:28 pm

My custom Xau/Xag indicator pro version

Post by PTScalper »

Hi guys,

I would like to share with you my custom made Xau/Xag indicator. I use it for scalping small inbalances after trading News or volatile markets. (Gold/Silver indicator)

Here it is for MT4:

Code: Select all

 //+------------------------------------------------------------------+
//|                                              XAU_XAG_Tracker.mq4 |
//|                                            Professional Tracking |
//+------------------------------------------------------------------+
#property copyright "Your Name"
#property link      ""
#property version   "1.00"
#property indicator_chart_window
#property indicator_plots 0

//--- Professional Input Parameters
input string   GoldSymbol     = "XAUUSD";             // Gold Symbol Name (Match your broker)
input string   SilverSymbol   = "XAGUSD";             // Silver Symbol Name (Match your broker)
input color    GoldColor      = clrGold;              // Gold Text Color
input color    SilverColor    = clrSilver;            // Silver Text Color
input int      FontSize       = 14;                   // Font Size
input string   FontName       = "Trebuchet MS";       // Font Type (Clean & Professional)
input ENUM_BASE_CORNER Corner = CORNER_RIGHT_UPPER;   // Panel Corner Anchor
input int      X_Offset       = 20;                   // X-Axis Distance from corner
input int      Y_Offset       = 20;                   // Y-Axis Distance from corner
input int      LineSpacing    = 25;                   // Space between lines

//--- Internal Object Names
string obj_gold = "Label_Gold_Price";
string obj_silver = "Label_Silver_Price";

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   // Initialize and create the text labels on the chart
   CreateLabel(obj_gold, Corner, X_Offset, Y_Offset, GoldColor);
   CreateLabel(obj_silver, Corner, X_Offset, Y_Offset + LineSpacing, SilverColor);

   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   // Clean up the chart when the indicator is removed
   ObjectDelete(0, obj_gold);
   ObjectDelete(0, obj_silver);
  }

//+------------------------------------------------------------------+
//| 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[])
  {
   // Fetch latest Bid prices and the specific broker's digit formatting
   double gold_price = MarketInfo(GoldSymbol, MODE_BID);
   int gold_digits = (int)MarketInfo(GoldSymbol, MODE_DIGITS);
   
   double silver_price = MarketInfo(SilverSymbol, MODE_BID);
   int silver_digits = (int)MarketInfo(SilverSymbol, MODE_DIGITS);

   // Update Gold text dynamically
   if(gold_price > 0)
     {
      string gold_text = GoldSymbol + ": " + DoubleToStr(gold_price, gold_digits);
      ObjectSetString(0, obj_gold, OBJPROP_TEXT, gold_text);
     }
   else
     {
      ObjectSetString(0, obj_gold, OBJPROP_TEXT, GoldSymbol + ": Waiting for tick...");
     }

   // Update Silver text dynamically
   if(silver_price > 0)
     {
      string silver_text = SilverSymbol + ": " + DoubleToStr(silver_price, silver_digits);
      ObjectSetString(0, obj_silver, OBJPROP_TEXT, silver_text);
     }
   else
     {
      ObjectSetString(0, obj_silver, OBJPROP_TEXT, SilverSymbol + ": Waiting for tick...");
     }

   return(rates_total);
  }

//+------------------------------------------------------------------+
//| Helper function to create graphical text labels                  |
//+------------------------------------------------------------------+
void CreateLabel(string name, ENUM_BASE_CORNER corner, int x, int y, color clr)
  {
   // Only create if it doesn't already exist
   if(ObjectFind(0, name) < 0)
     {
      ObjectCreate(0, name, OBJ_LABEL, 0, 0, 0);
      ObjectSetInteger(0, name, OBJPROP_CORNER, corner);
      ObjectSetInteger(0, name, OBJPROP_XDISTANCE, x);
      ObjectSetInteger(0, name, OBJPROP_YDISTANCE, y);
      ObjectSetString(0, name, OBJPROP_FONT, FontName);
      ObjectSetInteger(0, name, OBJPROP_FONTSIZE, FontSize);
      ObjectSetInteger(0, name, OBJPROP_COLOR, clr);
      ObjectSetInteger(0, name, OBJPROP_BACK, false);
      ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false); // Prevent accidental clicking/dragging
      ObjectSetInteger(0, name, OBJPROP_HIDDEN, true);      // Hide from object list to keep UI clean
     }
  }
//+------------------------------------------------------------------+
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
PTScalper
Site Admin
Posts: 210
Joined: Mon Jul 20, 2026 1:28 pm

Re: My custom Xau/Xag indicator pro version

Post by PTScalper »

Here it is for MT5, pro grade indicator :-)

Code: Select all

 //+------------------------------------------------------------------+
//|                                              XAU_XAG_Tracker.mq5 |
//|                                            Professional Tracking |
//+------------------------------------------------------------------+
#property copyright "Your Name"
#property link      ""
#property version   "1.00"
#property indicator_chart_window
#property indicator_plots 0

//--- Professional Input Parameters
input string   GoldSymbol     = "XAUUSD";             // Gold Symbol Name (Match your broker)
input string   SilverSymbol   = "XAGUSD";             // Silver Symbol Name (Match your broker)
input color    GoldColor      = clrGold;              // Gold Text Color
input color    SilverColor    = clrSilver;            // Silver Text Color
input int      FontSize       = 14;                   // Font Size
input string   FontName       = "Trebuchet MS";       // Font Type (Clean & Professional)
input ENUM_BASE_CORNER Corner = CORNER_RIGHT_UPPER;   // Panel Corner Anchor
input int      X_Offset       = 20;                   // X-Axis Distance from corner
input int      Y_Offset       = 20;                   // Y-Axis Distance from corner
input int      LineSpacing    = 25;                   // Space between lines

//--- Internal Object Names
string obj_gold = "Label_Gold_Price_MT5";
string obj_silver = "Label_Silver_Price_MT5";

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   // Ensure symbols are visible in Market Watch (required in MT5)
   SymbolSelect(GoldSymbol, true);
   SymbolSelect(SilverSymbol, true);

   // Create the text labels on the chart
   CreateLabel(obj_gold, Corner, X_Offset, Y_Offset, GoldColor);
   CreateLabel(obj_silver, Corner, X_Offset, Y_Offset + LineSpacing, SilverColor);

   // Set a 1-second timer. This forces the dashboard to update 
   // even if the chart it is attached to isn't receiving ticks!
   EventSetTimer(1);

   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   // Kill the timer and clean up the objects
   EventKillTimer();
   ObjectDelete(0, obj_gold);
   ObjectDelete(0, obj_silver);
   ChartRedraw(0);
  }

//+------------------------------------------------------------------+
//| Timer function (Updates independently of chart ticks)            |
//+------------------------------------------------------------------+
void OnTimer()
  {
   UpdatePrices();
  }

//+------------------------------------------------------------------+
//| 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[])
  {
   // Also update on standard chart ticks for maximum responsiveness
   UpdatePrices();
   return(rates_total);
  }

//+------------------------------------------------------------------+
//| Main logic to fetch and display prices                           |
//+------------------------------------------------------------------+
void UpdatePrices()
  {
   // Fetch latest Bid prices and broker's digit formatting (MQL5 syntax)
   double gold_price = SymbolInfoDouble(GoldSymbol, SYMBOL_BID);
   int gold_digits = (int)SymbolInfoInteger(GoldSymbol, SYMBOL_DIGITS);
   
   double silver_price = SymbolInfoDouble(SilverSymbol, SYMBOL_BID);
   int silver_digits = (int)SymbolInfoInteger(SilverSymbol, SYMBOL_DIGITS);

   // Update Gold text dynamically
   if(gold_price > 0)
     {
      // DoubleToString is the MQL5 equivalent of DoubleToStr
      string gold_text = GoldSymbol + ": " + DoubleToString(gold_price, gold_digits);
      ObjectSetString(0, obj_gold, OBJPROP_TEXT, gold_text);
     }
   else
     {
      ObjectSetString(0, obj_gold, OBJPROP_TEXT, GoldSymbol + ": Awaiting Data...");
     }

   // Update Silver text dynamically
   if(silver_price > 0)
     {
      string silver_text = SilverSymbol + ": " + DoubleToString(silver_price, silver_digits);
      ObjectSetString(0, obj_silver, OBJPROP_TEXT, silver_text);
     }
   else
     {
      ObjectSetString(0, obj_silver, OBJPROP_TEXT, SilverSymbol + ": Awaiting Data...");
     }

   // Force MT5 to instantly redraw the updated text
   ChartRedraw(0);
  }

//+------------------------------------------------------------------+
//| Helper function to create graphical text labels                  |
//+------------------------------------------------------------------+
void CreateLabel(string name, ENUM_BASE_CORNER corner, int x, int y, color clr)
  {
   // Only create if it doesn't already exist
   if(ObjectFind(0, name) < 0)
     {
      ObjectCreate(0, name, OBJ_LABEL, 0, 0, 0);
      ObjectSetInteger(0, name, OBJPROP_CORNER, corner);
      ObjectSetInteger(0, name, OBJPROP_XDISTANCE, x);
      ObjectSetInteger(0, name, OBJPROP_YDISTANCE, y);
      ObjectSetString(0, name, OBJPROP_FONT, FontName);
      ObjectSetInteger(0, name, OBJPROP_FONTSIZE, FontSize);
      ObjectSetInteger(0, name, OBJPROP_COLOR, clr);
      ObjectSetInteger(0, name, OBJPROP_BACK, false);
      ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false); // Prevent accidental clicking
      ObjectSetInteger(0, name, OBJPROP_HIDDEN, true);      // Hide from object list
     }
  }
//+------------------------------------------------------------------+
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
PTScalper
Site Admin
Posts: 210
Joined: Mon Jul 20, 2026 1:28 pm

Re: My custom Xau/Xag indicator pro version

Post by PTScalper »

And this version i have made for IC trader:

Code: Select all

 using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class XAUXAGTracker : Indicator
    {
        //--- Professional Input Parameters
        [Parameter("Gold Symbol", DefaultValue = "XAUUSD")]
        public string GoldSymbolName { get; set; }

        [Parameter("Silver Symbol", DefaultValue = "XAGUSD")]
        public string SilverSymbolName { get; set; }

        //--- Internal UI Elements & Symbol Objects
        private Symbol _goldSymbol;
        private Symbol _silverSymbol;
        private TextBlock _goldTextBlock;
        private TextBlock _silverTextBlock;
        private StackPanel _mainPanel;

        protected override void Initialize()
        {
            // Fetch the external symbols from the broker's data feed
            _goldSymbol = Symbols.GetSymbol(GoldSymbolName);
            _silverSymbol = Symbols.GetSymbol(SilverSymbolName);

            // Create the Gold TextBlock
            _goldTextBlock = new TextBlock
            {
                Text = "Loading Gold...",
                ForegroundColor = Color.Gold,
                FontSize = 14,
                FontFamily = "Trebuchet MS",
                FontWeight = FontWeight.Bold,
                Margin = new Thickness(0, 0, 0, 5) // 5px bottom margin for spacing
            };

            // Create the Silver TextBlock
            _silverTextBlock = new TextBlock
            {
                Text = "Loading Silver...",
                ForegroundColor = Color.Silver,
                FontSize = 14,
                FontFamily = "Trebuchet MS",
                FontWeight = FontWeight.Bold
            };

            // Create a UI Panel to hold both texts cleanly
            _mainPanel = new StackPanel
            {
                HorizontalAlignment = HorizontalAlignment.Right,
                VerticalAlignment = VerticalAlignment.Top,
                BackgroundColor = Color.FromArgb(150, 0, 0, 0), // Semi-transparent black background
                Padding = new Thickness(10),
                Margin = new Thickness(20) // Distance from the edges of the chart
            };

            // Assemble the UI
            _mainPanel.AddChild(_goldTextBlock);
            _mainPanel.AddChild(_silverTextBlock);
            
            // Add the UI to the chart
            Chart.AddControl(_mainPanel);

            // Set a timer to update prices every 500 milliseconds for a buttery smooth feed
            Timer.Start(TimeSpan.FromMilliseconds(500));
        }

        protected override void OnTimer()
        {
            // Timer ensures updates even if the current chart is dead/slow
            UpdatePrices();
        }

        public override void Calculate(int index)
        {
            // Standard tick update
            if (IsLastBar)
            {
                UpdatePrices();
            }
        }

        private void UpdatePrices()
        {
            // Format Gold Price dynamically based on broker digits
            if (_goldSymbol != null)
            {
                _goldTextBlock.Text = $"{_goldSymbol.Name}: {_goldSymbol.Bid.ToString("F" + _goldSymbol.Digits)}";
            }
            else
            {
                _goldTextBlock.Text = $"{GoldSymbolName}: Symbol Not Found";
            }

            // Format Silver Price dynamically based on broker digits
            if (_silverSymbol != null)
            {
                _silverTextBlock.Text = $"{_silverSymbol.Name}: {_silverSymbol.Bid.ToString("F" + _silverSymbol.Digits)}";
            }
            else
            {
                _silverTextBlock.Text = $"{SilverSymbolName}: Symbol Not Found";
            }
        }
    }
}
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
PTScalper
Site Admin
Posts: 210
Joined: Mon Jul 20, 2026 1:28 pm

Re: My custom Xau/Xag indicator pro version

Post by PTScalper »

And please let me know, if you like it ;-)

If you need help with instalation, feel free to ask for any help.
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
Post Reply