• No results found

WINDOW FUNCTIONS

In document MQL4_Documentation (Page 163-170)

A group of functions intended for working with the current chart window.

HIDETESTINDICATORS

void HideTestIndicators( bool hide)

The function sets a flag hiding indicators called by the Expert Advisor. After the expert has been tested and the appropriate chart opened, the flagged indicators will not be drawn in the testing chart. Every indicator called will first be flagged with the current hiding flag.

It must be noted that only those indicators can be drawn in the testing chart that are directly called from the expert under test.

Parameters:

hide - TRUE, if there is a need to hide indicators, or else FALSE.

Sample: HideTestIndicators(true); MaCurrent=iMA(NULL,0,56,0,MODE_EMA,PRICE_CLOSE,0); MaPrevious=iMA(NULL,0,56,0,MODE_EMA,PRICE_CLOSE,1); HideTestIndicators(false); PERIOD int Period( )

Returns the amount of minutes determining the used period (chart timeframe). Sample:

Print("Period is ", Period());

REFRESHRATES

bool RefreshRates( )

Refreshing of data in pre-defined variables and series arrays. This function is used when expert advisor has been calculating for a long time and needs data refreshing. Returns TRUE if data are refreshed, otherwise returns FALSE. The only reason for data cannot be refreshed is that they are the current data of the client terminal.

Experts and scripts operate with their own copy of history data. Data of the current symbol are copied at the first launch of the expert or script. At each subsequent launch of the expert (remember that script is executed only once and does not depend on incoming ticks), the initial copy will be updated. One or more new ticks can income while the expert or script is operating, and data can become out of date.

Sample: int ticket; while(true) { ticket=OrderSend(Symbol(),OP_BUY,1.0,Ask,3,0,0,"expert comment",255,0,CLR_NONE); if(ticket<=0) { int error=GetLastError(); //---- not enough money

164 if(error==134) break;

//---- 10 seconds wait Sleep(10000);

//---- refresh price data RefreshRates(); break; } else { OrderSelect(ticket,SELECT_BY_TICKET); OrderPrint(); break; } } SYMBOL string Symbol( )

Returns a text string with the name of the current financial instrument. Sample:

int total=OrdersTotal();

for(int pos=0;pos<total;pos++) {

// check selection result because the order may be closed or deleted at this time!

if(OrderSelect(pos, SELECT_BY_POS)==false) continue;

if(OrderType()>OP_SELL || OrderSymbol()!=Symbol()) continue; // performs some processing...

}

WINDOWBARSPERCHART

int WindowBarsPerChart( )

Function returns the amount of bars visible on the chart. Sample:

// work with visible bars.

int bars_count=WindowBarsPerChart(); int bar=WindowFirstVisibleBar(); for(int i=0; i<bars_count; i++,bar--) {

// ... }

WINDOWEXPERTNAME

string WindowExpertName( )

Returns name of the executed expert, script, custom indicator, or library, depending on the MQL4 program, from which this function has been called.

Sample:

string name=WindowExpertName(); GlobalVariablesDeleteAll(name);

WINDOWFIND

165

If indicator with name was found, the function returns the window index containing this specified indicator, otherwise it returns -1.

Note: WindowFind() returns -1 if custom indicator searches itself when init() function works.

Parameters:

name - Indicator short name.

Sample:

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

WINDOWFIRSTVISIBLEBAR

int WindowFirstVisibleBar( )

The function returns the first visible bar number in the current chart window. It must be taken into consideration that price bars are numbered in the reverse order, from the last to the first one. The current bar, the latest in the price array, is indexed as 0. The oldest bar is indexed as Bars-1. If the first visible bar number is 2 or more bars less than the amount of visible bars in the chart, it means that the chart window has not been fully filled out and there is a space to the left.

Sample:

// work with visible bars.

int bars_count=WindowBarsPerChart(); int bar=WindowFirstVisibleBar(); for(int i=0; i<bars_count; i++,bar--) {

// ... }

WINDOWHANDLE

int WindowHandle( string symbol, int timeframe)

Returns the system window handler containing the given chart. If the chart of symbol and timeframe has not been opened by the moment of function calling, 0 will be returned.

Parameters:

symbol - symbol name.

timeframe - Timeframe. It can be any of Timeframe enumeration values. 0 means

the current chart timeframe. Sample:

int win_handle=WindowHandle("USDX",PERIOD_H1); if(win_handle!=0)

Print("Window with USDX,H1 detected. Rates array will be copied immediately.");

WINDOWISVISIBLE

bool WindowIsVisible( int index)

Returns TRUE if the chart subwindow is visible, otherwise returns FALSE. The chart subwindow can be hidden due to the visibility properties of the indicator placed in it. Parameters:

166

Sample:

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

int WindowOnDropped( )

Returns window index where expert, custom indicator or script was dropped. This value is valid if the expert, custom indicator or script was dropped by mouse.

Note: For custom indicators being initialized (call from the init() function), this index is not defined.

The returned index is the number of window (0-chart main menu, subwindows of indicators are numbered starting from 1) where the custom indicator is working. A custom indicator can create its own new subwindow during its work, and the number of this subwindow will differ from that of the window where the indicator was really dropped in.

See also WindowXOnDropped(), WindowYOnDropped()

Sample:

if(WindowOnDropped()!=0) {

Print("Indicator 'MyIndicator' must be applied to main chart window!");

return(false); }

WINDOWPRICEMAX

double WindowPriceMax( int index=0)

Returns maximal value of the vertical scale of the specified subwindow of the current chart (0-main chart window, the indicators' subwindows are numbered starting from 1). If the subwindow index has not been specified, the maximal value of the price scale of the main chart window is returned.

See also WindowPriceMin(), WindowFirstVisibleBar(), WindowBarsPerChart()

Parameters:

index - Chart subwindow index (0 - main chart window).

Sample: double top=WindowPriceMax(); double bottom=WindowPriceMin(); datetime left=Time[WindowFirstVisibleBar()]; int right_bound=WindowFirstVisibleBar()-WindowBarsPerChart(); if(right_bound<0) right_bound=0; datetime right=Time[right_bound]+Period()*60; //---- ObjectCreate("Padding_rect",OBJ_RECTANGLE,0,left,top,right,bottom); ObjectSet("Padding_rect",OBJPROP_BACK,true); ObjectSet("Padding_rect",OBJPROP_COLOR,Blue); WindowRedraw(); WINDOWPRICEMIN

167 double WindowPriceMin( int index=0)

Returns minimal value of the vertical scale of the specified subwindow of the current chart (0-main chart window, the indicators' subwindows are numbered starting from 1). If the subwindow index has not been specified, the minimal value of the price scale of the main chart window is returned.

See also WindowPriceMax(), WindowFirstVisibleBar(), WindowBarsPerChart()

Parameters:

index - Chart subwindow index (0 - main chart window).

Sample: double top=WindowPriceMax(); double bottom=WindowPriceMin(); datetime left=Time[WindowFirstVisibleBar()]; int right_bound=WindowFirstVisibleBar()-WindowBarsPerChart(); if(right_bound<0) right_bound=0; datetime right=Time[right_bound]+Period()*60; //---- ObjectCreate("Padding_rect",OBJ_RECTANGLE,0,left,top,right,bottom); ObjectSet("Padding_rect",OBJPROP_BACK,true); ObjectSet("Padding_rect",OBJPROP_COLOR,Blue); WindowRedraw(); WINDOWPRICEONDROPPED double WindowPriceOnDropped( )

Returns the price part of the chart point where expert or script was dropped. This value is only valid if the expert or script was dropped by mouse.

Note: For custom indicators, this value is undefined. Sample:

double drop_price=WindowPriceOnDropped(); datetime drop_time=WindowTimeOnDropped(); //---- 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); }

WINDOWREDRAW

void WindowRedraw( )

Redraws the current chart forcedly. It is normally used after the objects properties have been changed.

Sample:

//---- set new properties for some objects

ObjectMove(object_name1, 0, Time[index], price); ObjectSet(object_name1, OBJPROP_ANGLE, angle*2); ObjectSet(object_name1, OBJPROP_FONTSIZE, fontsize); ObjectSet(line_name, OBJPROP_TIME2, time2);

ObjectSet(line_name, OBJPROP_ANGLE, line_angle); //---- now redraw all

WindowRedraw();

168 bool

WindowScreenShot(

string filename, int size_x, int size_y, int start_bar=-1,

int chart_scale=-1, int chart_mode=-1)

Saves current chart screen shot as a GIF file. Returns FALSE if it fails. To get the error code, one has to use the GetLastError() function.

The screen shot is saved in the terminal_dir\experts\files (terminal_dir\tester\files in case of testing) directory or its subdirectories.

Parameters:

filename - Screen shot file name.

size_x - Screen shot width in pixels.

size_y - Screen shot height in pixels.

start_bar - Index of the first visible bar in the screen shot. If 0 value is set, the

current first visible bar will be shot. If no value or negative value has been set, the end-of-chart screen shot will be produced, indent being taken into consideration.

chart_scale - Horizontal chart scale for screen shot. Can be in the range from 0 to 5.

If no value or negative value has been set, the current chart scale will be used.

chart_mode - Chart displaying mode. It can take the following values:

CHART_BAR (0 is a sequence of bars), CHART_CANDLE (1 is a sequence of candlesticks), CHART_LINE (2 is a close prices line). If no value or negative value has been set, the chart will be shown in its current mode.

Sample:

int lasterror=0;

//---- tester has closed one or more trades

if(IsTesting() && ExtTradesCounter<TradesTotal()) {

//---- make WindowScreenShot for further checking if(!WindowScreenShot("shots\\tester"+ExtShotsCounter+".gif",640,480)) lasterror=GetLastError(); else ExtShotsCounter++; ExtTradesCounter=TradesTotal(); } WINDOWTIMEONDROPPED datetime WindowTimeOnDropped( )

Returns the time part of the chart point where expert or script was dropped. This value is only valid if the expert or script was dropped by mouse.

Note: For custom indicators, this value is undefined. Sample:

double drop_price=WindowPriceOnDropped(); datetime drop_time=WindowTimeOnDropped(); //---- 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); }

169

WINDOWSTOTAL

int WindowsTotal( )

Returns count of indicator windows on the chart (including main chart). Sample:

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

WINDOWXONDROPPED

int WindowXOnDropped( )

Returns the value at X axis in pixels for the chart window client area point at which the expert or script was dropped. The value will be true only if the expert or script were moved with the mouse ("Drag'n'Drop") technique.

See also WindowYOnDropped(), WindowOnDropped()

Sample:

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

WINDOWYONDROPPED

int WindowYOnDropped( )

Returns the value at Y axis in pixels for the chart window client area point at which the expert or script was dropped. The value will be true only if the expert or script were moved with the mouse ("Drag'n'Drop") technique.

See also WindowXOnDropped(), WindowPriceOnDropped(), WindowOnDropped()

Sample:

Print"Expert was attached to the window in the point x=",WindowXOnDropped()," y=",WindowYOnDropped());

170

In document MQL4_Documentation (Page 163-170)