Script: Open 2nd Chart, take a screenshot, close the 2nd chart - SOLVED!

 

Hi,

I have only one single chart open. EURUSD M5.  When running the script below, it does open a Second chart (as planned)  - EURUSD  M15.  I would like the script to take a screenshot of the 2nd graph (M15), but instead it takes the snapshot of the 1st one (M5).

Also, it does not close that second (new) window.

What am I doing wrong?

 

int init()

{
  ChartOpen( "EURUSD", 15 );
  ChartScreenShot(1, "Screenshot of EURUSD M15.gif", 1280, 720);
  ChartClose(1);
      return;
}
 
tk9000: What am I doing wrong?
  1. Perhaps you should read the manual.
    Opens a new chart with the specified symbol and period. The command is added to chart message queue and executed only after all previous commands have been processed -- ChartOpen - Chart Operations - MQL4 Reference
    The call is not synchronous, it takes time to open a chart, download the bars, and paint it. Where is your wait?

    You would know this had you bothered to Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  2. Perhaps you should read the manual.

    chart_id

    [in]  Chart ID. 0 means the current chart.  ChartScreenShot - Chart Operations - MQL4 Reference

    zero may be the current chart, but no where does it say that a second chart has id one. Perhaps you should find the second id ChartNext - Chart Operations - MQL4 Reference



 
whroeder1:
  1. Perhaps you should read the manual.The call is not synchronous, it takes time to open a chart, download the bars, and paint it. Where is your wait?

    You would know this had you bothered to Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  2. Perhaps you should read the manual.zero may be the current chart, but no where does it say that a second chart has id one. Perhaps you should find the second id ChartNext - Chart Operations - MQL4 Reference



Thanks Whroeder1!

1. I have added the Sleep function now after ChartOpen to allow for the bars to load.
2. Added the ChartNext code to enumerate the ChartIDs , they now show up in the Output fields ("Experts" tab on the bottom of MT4).

Question I still have - how do I capture (assign the ChartIDs to a variable, i.e. ChartID0 = (output from the ChartNext code, "ChartFirst"?) and ChartID1 = CurrChart(1) ?) Tried many ways, looks like going in circles... :-(

 

int init()

{
  ChartOpen( "EURUSD", 15 );
  Sleep (5000);
  
  long currChart,prevChart=ChartFirst();
   int i=0,limit=100;
   Print("ChartFirst =",ChartSymbol(prevChart)," ID =",prevChart);
   while(i<limit)// We have certainly not more than 100 open charts
     {
      currChart=ChartNext(prevChart); // Get the new chart ID by using the previous chart ID
      if(currChart<0) break;          // Have reached the end of the chart list
      Print(i,ChartSymbol(currChart)," ID =",currChart);
      prevChart=currChart;// let's save the current chart ID for the ChartNext()
      i++;// Do not forget to increase the counter
     }
    
  ChartScreenShot(0, "Screenshot of EURUSD M15.gif", 1280, 720); // instead of the "0", I need to pass a captured variable for ChartId (1)? But how to capture it first?
  ChartClose(0);    //same story here... 
      return;
}

 

How about

long  ID = ChartID();

Or

int init()

{
  ChartOpen( "EURUSD", 15 );
  ChartScreenShot(ChartID(), "Screenshot of EURUSD M15.gif", 1280, 720);
  ChartClose(ChartID());
      return;
}

Or you can try like this

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   for(int i=0;i<SymbolsTotal(1);i++)
     {
      ChartOpen(SymbolName(i,1),PERIOD_M15);
      ChartScreenShot(ChartID(),"Screenshot of "+SymbolName(i,1)+" M15.gif",1280,720);
      ChartClose(ChartID());
     }
  }
//+------------------------------------------------------------------+
 
tk9000:

Question I still have - how do I capture (assign the ChartIDs to a variable,


You didn't read the documentation for ChartOpen() did you?

If successful, it returns the opened chart ID. Otherwise returns 0. To get error details use the GetLastError() function.


 

Thanks a lot, Marco, for suggestions. I got it to work, with a slight change though to the code. Posting for benefit of others. 

int init()

{
  ChartOpen( "EURUSD", 15 );
  Sleep (5000);
  ChartOpen( "GBPUSD", 15 );
  Sleep (5000);

long    Ch01    = ChartFirst();     // the ChartID() from the First (base) chart
long    ChId2   = ChartNext(Ch01);  // the ChartID() from the Second Chart
long    ChId3   = ChartNext(ChId1); // the ChartID() from the Third Chart

  ChartScreenShot(ChId2, "Screenshot of EURUSD M15.gif", 1280, 720);
  ChartScreenShot(ChId3, "Screenshot of GBPUSD M15.gif", 1280, 720);
  ChartClose(ChId2);
  ChartClose(ChId3);
      return;

}    


 

Reason: