Advertisement 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: 7821
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.
Recommended broker for automated trading & scalping IC Markets
PTScalper
Site Admin
Posts: 7821
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: 7821
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: 7821
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.
LondonScalper
Posts: 965
Joined: Sat Sep 05, 2026 7:54 am

Re: My custom Xau/Xag indicator pro version

Post by LondonScalper »

PTScalper wrote:custom made Xau/Xag indicator... scalping small inbalances after trading News or volatile markets
Post-news imbalance scalps on gold/silver are a known niche — also a known way to donate spread.

Practical London filter: after a release I wait until spread and quote stability look like a market again, then decide. An indicator marking imbalances can help attention, but it will mark many that are simply residual thinness. Size should assume a worse fill than the chart mid, and “small” should mean small in risk %, not only small in lots.

Useful to share install notes; more useful later to share when you disable it (which sessions, which news tiers).

Do you trade the imbalance in the direction of the news impulse, or fade back into the pre-release range?
Shadow Trader
Posts: 5594
Joined: Wed Sep 09, 2026 5:19 pm

Re: My custom Xau/Xag indicator pro version

Post by Shadow Trader »

As it stands this panel shows the two bid prices, so the imbalance part still has to happen in the trader's head. Adding the gold to silver ratio as a third line would get closer to what the first post describes.

With made up prices, gold at 3,200 and silver at 40 gives a ratio of 80. If a release lifts gold 1 percent to 3,232 while silver stays at 40, the ratio moves to 80.8, and that gap is the thing being scalped.

A raw ratio does not say whether 0.8 is large, though. Comparing the current value with its average over the last hour or so, and showing the distance in standard deviations, would tell you whether the move is unusual for that session.

One practical snag on MT4 is that MarketInfo returns zero for a symbol that is not in Market Watch. The panel would then sit on the waiting for tick message forever, so the install steps could mention that both symbols need to be visible there.

Brokers that add a suffix to the symbol names are already covered by the inputs, which helps. It might be worth showing an example of what to type for a couple of common suffixes.
Shadow Trader
Posts: 5594
Joined: Wed Sep 09, 2026 5:19 pm

Re: My custom Xau/Xag indicator pro version

Post by Shadow Trader »

Instead of looking at price direction, some scalpers measure how fast ticks arrive. The number of price updates per second is a rough measure of activity. A spike in tick velocity often comes right before a price move.

A simple version counts ticks over the last 5 seconds and compares it with a 5-minute average. A ratio above 3 or 4 signals unusual activity. It doesn't say which way price will move, only that something is happening.

Used as a filter, it can keep a range-fading approach out of the market when conditions change. Used as a trigger, it can flag potential breakouts, though many of them fail.

The measure has issues. Different brokers produce different tick counts, so thresholds need calibrating for each. Spikes also happen because a liquidity provider refreshes quotes, with no market event.

Plotting tick velocity on a chart below price, with the spread as a second line, gives a quick view of the activity pattern. Many traders find that the combination of velocity and spread is more informative than most standard indicators.

Building it requires a custom indicator or EA, but the logic is easy to code.

What threshold of tick velocity has worked as an activity warning on your feed?
Post Reply