ChartFirst() not working in mql4?

 

Hello

I'm trying to get a very basic ChartFirst() command working in mql4. But it always produces error 4211 - Chart Not Found.

There is a chart open in the trader. 3 charts open, in fact. My code simply says in OnInit():

Alert(ChartFirst());

Any idea why this is not returning a valid chart ID?

Thanks!

 
2ndTime:

Hello

I'm trying to get a very basic ChartFirst() command working in mql4. But it always produces error 4211 - Chart Not Found.

There is a chart open in the trader. 3 charts open, in fact. My code simply says in OnInit():

Alert(ChartFirst());

Any idea why this is not returning a valid chart ID?

Thanks!

It is working for me. Build 628.
 

OK, I'm using version 625. My understanding from the docs is that it should be working from 600+.

I must not be understanding something very basic to what it achieves / how to use it.

In metaTrader I have 4 charts open, of different timeframes. No scripts attached.

I want my EA, when it runs, to draw different graphical objects onto each (indicating positive trading signals unique to each timeframe).

This is a first for me, since I've previously run my EA's, then clicked 'Open chart' to open a single (testing) chart, from where objects plus trades (obviously) are visible. i.e. I've not before used already open charts for backtesting with my EAs.

Additionally I wish the backtested trades themselves to appear on these already opened charts.

(So I don't wish to click ' Open chart' at all to review the EA's tested behaviour) .

This is possible, right?

---

edit:

So to clarify, my code I mentioned in the first post actually returns '0' - but a GetLastError() call reveals that it is infact not a real result, but an error 4211. This is further proved by the fact that ChartNext(ChartFirst()) is also returning '0'.

 

Attached is my very simple EA code that does nothing except attempt to report the IDs of 2 already open charts in MetaTrader, via the journal alerts.

Could anybody please test, and tell me why I'm getting errors, and no true IDs.

Thank you.

Files:
findchart.mq4  2 kb
 
//+------------------------------------------------------------------+
//|                                                    FindChart.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   Alert("FirstChart ID: "+ChartFirst());
   Alert("GetLastError: "+GetLastError());
   
   Alert("Ssecond Chart ID: "+ChartNext(ChartFirst()));
   Alert("GetLastError: "+GetLastError());
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
//+------------------------------------------------------------------+
 

I put that code in a script and it works fine.

Platform is build 625

 
2ndTime:

Why are you searching for an error after CHartFirst ? There can't be an error, so don't use GetLastError(). ChartNext() return -1 when there is an error, so only check for GetLastError() when there is an error.

   long first=ChartFirst();
   Alert("FirstChart ID: "+first);

   long next=ChartNext(first);
   if(next==-1)
      Alert("ChartNext GetLastError: "+GetLastError());
   else
      Alert("Ssecond Chart ID: "+next);
 

Thanks

Ok, I've found that by dragging the EA onto a chart, rather than running it from the strategy tester, I can reveal the chart IDs.

However I don't seem to be able to address these charts (using their IDs) from an EA in the strategy tester, without errors. This I will need to do, (will I not?) in order to backtest the strategies, and execute draw commands (objectCreate() etc) to the various charts.

Reason: