Skip navigation.
Home
Metatrader community - Forex Trading with Metatrader

Windows functions

Hi folks,

We are going to study today a set of functions that responsible of handling the chart windows. These functions are infrequently used (expect WindowsTotal(), WindowFind() and ScreenShot functions) but as a MQL4 programmer you have to know them.

These are the Windows functions set:

 

BarsPerWindow:

 

Syntax

int BarsPerWindow( )

 Description: 

The BarsPerWindow function returns the number of bars visible for the user on the chart.

Note: Resizing the chart window or changing the chart period changes this number.

Parameters:

The function doesn't take any parameters.

 Example

// work with visible bars.
int bars_count=BarsPerWindow();
int bar=FirstVisibleBar();
for(int i=0; i<bars_count; i++,bar--)
{
// ...
}

caption=FirstVisibleBar()
type=int

Function returns index of the first visible bar.

// work with visible bars.
int bars_count=BarsPerWindow();
int bar=FirstVisibleBar();
for(int i=0; i<bars_count; i++,bar--)
{
// ...
}

 

PriceOnDropped:

 

Syntax

double PriceOnDropped( )

 Description: 

The PriceOnDropped function returns the price of the currency of the chart from the point you dragged and release the expert advisor or the script at. When you click the expert advisor or the script in the navigator window and hold the left button mouse then drag it and release the left mouse buttons at any point on the chart; the PriceOnDropped  returns the price at this point.

This function works only with the expert advisors and script and doesn't work with the custom indicators.

Parameters:

The function doesn't take any parameters.

 Example

double drop_price=PriceOnDropped();
datetime drop_time=TimeOnDropped();
//---- may be undefined (zero)
if(drop_time>0)
{
ObjectCreate("Dropped price line", OBJ_HLINE, 0, drop_price);
ObjectCreate("Dropped time line", OBJ_VLINE, 0, drop_time);
}

 

TimeOnDropped:

 

Syntax

datetime TimeOnDropped( )

 Description: 

The TimeOnDropped function returns the time of on the chart where you dragged and release the expert advisor or the script at. This function too works only with the expert advisors and script and doesn't work with the custom indicators.

Parameters:

The function doesn't take any parameters.

 Example

double drop_price=PriceOnDropped();
datetime drop_time=TimeOnDropped();
//---- may be undefined (zero)
if(drop_time>0)
{
ObjectCreate("Dropped price line", OBJ_HLINE, 0, drop_price);
ObjectCreate("Dropped time line", OBJ_VLINE, 0, drop_time);
}

 

ScreenShot:

 

Syntax

bool ScreenShot( string filename, int size_x, int size_y, int start_bar=-1, int chart_scale=-1, int chart_mode=-1)

 Description: 

The ScreenShot function save a screen shot of the current chart. It saves the screen shot as GIF file in one of two folder:
If you used the function in the live mode it'll save the screen shot in  terminal_dir\experts\files and its subdirectories. If you used the function in the testing mode it'll save the screen shot in terminal_dir\tester\files and its subdirectories.

The function returns True if it successfully saved the screen shot and false if it failed.

Parameters:

The function takes 6 parameters.

string filename:

The file name that the function will save the screen shot to. You can combine the file name with a subdirectory to tell the function where to save the file.

int size_x:

The width of the screen shot.

int size_y:

The height of the screen shot.

int start_bar=-1:

The index of the first bar you want to include in your screen shot. 0 means the first visible bar on the chart. The default vaule is -1 which means the end-of-chart screen shot will be taken.

int chart_scale=-1:

The scale (Zoom In and Zoom Out) of the chart screen shot that will be taken. It ranges from 0 to 5. The default value is -1 which means the function will use the current scale of the chart.

int chart_mode=-1

The mode of the chart screen shot that will be taken. it can be one of these values:

CHART_BAR  (0)
CHART_CANDLE (1)
CHART_LINE (2)

The default value is -1 which means the function will use the chart mode.

 

 Example

int lasterror=0;
//---- tester has closed one or more trades
if(IsTesting() && ExtTradesCounter<TradesTotal())
{
//---- make screenshot for further checking
if(!ScreenShot("shots\\tester"+ExtShotsCounter+".gif",640,480))
lasterror=GetLastError();
else ExtShotsCounter++;
ExtTradesCounter=TradesTotal();
}

 

WindowFind:

 

Syntax

int WindowFind(string name)

 Description: 

The WindowFind function searches the chart sub windows for the indicator short name passed to it and returns the window index if found and -1 otherwise!

Note: The indicators name can be set using the function IndicatorShortName().

Note: If the indicator search for his name in the init() function using WindowFind() function it always returns -1.

Parameters:

The function takes only one parameter.

string name:

The name of the indicator short name you want to search for and return its window index.

 Example

int win_idx=WindowFind("MACD(12,26,9)");

 

WindowHandle:

 

Syntax

int WindowHandle(string symbol, int timeframe)

 Description: 

The WindowHandle function searches all the opened chart for the symbol (currency pairs) and timeframe passed to it and returns the window handle of the chart window if found and -1 otherwise.

Parameters:

The function takes two parameters:

string symbol:

The symbol name you want to search for and return its window index.

int timeframe:

The timeframe you want to search for and return its window index.

Note: The chart window you are searching must have the symbol name and the timeframe to return the window handle. if the one of the two conditions is found and other not found the return value is -1.

 

 Example

 int win_handle=WindowHandle("EURUSD",PERIOD_H1);
if(win_handle!=0)
Print("Window with EURUSD,H1 detected. Rates array will be copied immediately.");

 

WindowIsVisible:

 

Syntax

bool WindowIsVisible(int index)

 Description: 

The WindowIsVisible function returns true if the chart sub-window index passed to it is visible to the user and false otherwise.

Parameters:

The function takes only one parameter:

int index:

The index of the chart sub-window you want to check its visibility.

 Example

 int maywin=WindowFind("MyMACD");
if(maywin>-1 && WindowIsVisible(maywin)==true)
Print("window of MyMACD is visible");
else
Print("window of MyMACD not found or is not visible");

 

WindowOnDropped:

 

Syntax

int WindowOnDropped()

 Description: 

The WindowOnDropped  function returns the index of the window you dragged and released the expert advisor, indicator or the script on.

Parameters:

The function doesn't take any parameters.

 Example

if(WindowOnDropped()!=0)
{
Print("Indicator 'MyIndicator' must be applied to main chart window!");
return(false);
}

 

WindowsTotal:

 

Syntax

int WindowsTotal()

 Description: 

The WindowsTotal function returns the count of windows (sub-windows and main window) on the chart.

Parameters:

The function doesn't take any parameters.

 Example

Print("Windows count = ", WindowsTotal());

 

WindowXOnDropped:

 

Syntax

int WindowXOnDropped()

 Description: 

The WindowXOnDropped  function returns the x-axis coordinate in pixels of the point you dragged and released the expert advisor, indicator or the script on.

Parameters:

The function doesn't take any parameters.

 Example

Print("Expert dropped point x=",WindowXOnDropped()," y=",WindowYOnDropped());

 

WindowYOnDropped:

 

Syntax

int WindowYOnDropped()

 Description: 

The WindowYOnDropped  function returns the Y-axis coordinate in pixels of the point you dragged and released the expert advisor, indicator or the script on.

Parameters:

The function doesn't take any parameters.

 Example

Print("Expert dropped point x=",WindowXOnDropped()," y=",WindowYOnDropped());