Skip to content

"OBJECT" methods not working correctly in MT4 #291

@bukowa

Description

@bukowa

Take a look for yourself, the object methods are not working correctly (no object are found).

using MtApi;


var connectionTcs = new TaskCompletionSource();

void _mtapi_ConnectionStateChanged(object? sender, MtConnectionEventArgs e)
{
    Console.WriteLine($"{e.Status}");
    if (e.Status == MtConnectionState.Connected)
    {
        connectionTcs.TrySetResult();
    }
}

var mt = new MtApi.MtApiClient();
mt.ConnectionStateChanged += _mtapi_ConnectionStateChanged;
mt.BeginConnect(8222);
await connectionTcs.Task;
Console.WriteLine("Connected! Scanning ALL charts for objects...");

// Get the Broker Time
DateTime serverTime = mt.TimeCurrent();

// 1. Get the first chart open in MT4
long chartId = mt.ChartFirst();

// 2. Loop through all charts
while (chartId >= 0)
{
    // Get total objects on THIS specific chartId
    int totalObjects = mt.ObjectsTotal(chartId, -1, -1);
    
    // Optional: Get the symbol name to help you debug
    string symbol = mt.ChartSymbol(chartId);
    
    Console.WriteLine($"Checking Chart {chartId} ({symbol}): Found {totalObjects} objects.");

    if (totalObjects > 0)
    {
        for (int i = 0; i < totalObjects; i++)
        {
            // Note: We must pass 'chartId' here, not 0!
            string objName = mt.ObjectName(chartId, i);

            if (!string.IsNullOrEmpty(objName) && objName.StartsWith("Median_"))
            {
                // Get value using the specific Chart ID
                double val = mt.ObjectGetValueByTime(chartId, objName, serverTime, 0);
                
                // Get anchor using the specific Chart ID
                double anchor = mt.ObjectGetDouble(chartId, objName, EnumObjectPropertyDouble.OBJPROP_PRICE, 0);

                Console.WriteLine($"*** MATCH FOUND on {symbol} ***");
                Console.WriteLine($"   Name: {objName}");
                Console.WriteLine($"   Value Now: {val}");
                Console.WriteLine($"   Anchor: {anchor}");
            }
        }
    }

    // 3. Move to the next chart
    chartId = mt.ChartNext(chartId);
}

Console.WriteLine("Scan complete.");
Connecting
Connected
Connected! Scanning ALL charts for objects...
Checking Chart 133952468030226628 (US100.f): Found 0 objects.
Checking Chart 133952470563024394 (US500.f): Found 0 objects.
Checking Chart 133952470889578706 (EURUSD): Found 0 objects.
Checking Chart 133952471520915656 (GBPUSD): Found 0 objects.
Checking Chart 133952472226431993 (GOLD.f): Found 0 objects.
Checking Chart 133952472953815456 (SILVER.f): Found 0 objects.
Checking Chart 133952473353495386 (WTI.f): Found 0 objects.
Checking Chart 133952474347056110 (NATGAS.f): Found 0 objects.
Scan complete.

Meanwhile this one works:

//+------------------------------------------------------------------+
//|                                                   EAScanTest.mq4 |
//|                                                  Expert Advisor  |
//+------------------------------------------------------------------+
#property copyright "EATest"
#property strict

// Run the scan every 5 seconds to avoid freezing the terminal
input int ScanIntervalSeconds = 5;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   Print("EA Started. Will scan all charts every ", ScanIntervalSeconds, " seconds.");
   
   // Create a timer event
   EventSetTimer(ScanIntervalSeconds);
   
   // Run one scan immediately upon loading
   ScanCharts();
   
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   EventKillTimer();
   Print("EA Removed.");
}

//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
{
   ScanCharts();
}

//+------------------------------------------------------------------+
//| The Scanning Logic                                               |
//+------------------------------------------------------------------+
void ScanCharts()
{
   datetime serverTime = TimeCurrent();
   
   // 1. Get the first chart open in MT4
   long chartId = ChartFirst();

   // 2. Loop through all charts
   while(chartId >= 0)
   {
      string symbol = ChartSymbol(chartId);
      
      // Get objects on THAT SPECIFIC chart
      int totalObjects = ObjectsTotal(chartId, -1, -1);

      // Only print if objects exist to keep logs clean
      if(totalObjects > 0)
      {
         for(int i = 0; i < totalObjects; i++)
         {
            string objName = ObjectName(chartId, i);

            // Filter for Median_
            if(StringFind(objName, "Median_") == 0)
            {
               double val = ObjectGetValueByTime(chartId, objName, serverTime, 0);
               double anchor = ObjectGetDouble(chartId, objName, OBJPROP_PRICE, 0);

               string out = "EA SCAN [" + symbol + "]: Found " + objName + 
                            " | Value: " + DoubleToString(val, Digits) +
                            " | Anchor: " + DoubleToString(anchor, Digits);
               
               Print(out);
            }
         }
      }
      else 
      {
          // Uncomment this if you want to see empty charts too
          // Print("Checking ", symbol, " (ID: ", chartId, ") - 0 Objects found.");
      }

      // 3. Move to next chart
      chartId = ChartNext(chartId);
   }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions