NavigationUser loginWho's new
Who's onlineThere are currently 0 users and 31 guests online.
|
Timeseries access functions - Part 1Hi folks, I've got a lot of messages asking me "How to deal with currency pairs which are different than the current chart currency (The chart that hosts the expert advisor)? For example, how to make an expert advisor that can open Buy position of GBPUSD or USDCHF while the expert advisor is hosted on EURUSD chart?" Good question, Huh? To reply these related questions we have to start with studding the Timeseries Access functions. Let's give these functions a brief look:
iBars
Syntax:
Description: The iBars function returns the number of Bars of the specified chart, you specify the chart by the symbol name and the timeframe. Note: If the chart of the symbol and timeframe you are trying to get its price data is not opened, MetaTrader will try to connect to the server to retrieve the request price data. In this case and with the all of the Timeseries access functions you have to check the the last was the error # 4066 - ERR_HISTORY_WILL_UPDATED (which means the requested data is under updating) to be sure that the price data is up-to-date. And retry your request for the price data. Note: If you want to get the number of bars of the current chart (symbol and timeframe) use Bars function.
Parameters: string symbol: The symbol (currency pair) of the chart you want to get its bars number. int timeframe: The timeframe (in integers, ex: 1,5,30,60 etc) of the chart you want to get its bars number. Note: You can use the integer representation of the timeframe (period in minutes) or you can use the timeframes constants:
Example:
iBarShift:Syntax:
Description: The iBarShift function takes the open time of the bar for a specified symbol and timeframe and searches for this bar and returns it if found otherwise it returns -1.
Parameters: string symbol: The symbol (currency pair) of the chart you want to get its bars number. int timeframe: The timeframe (in integers, ex: 1,5,30,60 etc) of the chart you want to get its bars number. datetime time: The open time of the bar you want to search for bool exact: The search mode, use exact =false (default) and and the function will return -1 if the open time not found. And use exact =true and the function will return the nearest bar to the given open time.
Example:
iClose:Syntax:
Description: The iClose function returns the Close price for the given bar (the shift parameter is the bar number) of the given symbol and timeframe. The function returns 0 if the history data not loaded. Note: It's very important (and always error's source) to know that the current bar is 0 and the previous bar is 1 etc. Note: If you want to get the Close price (of a bar) of the current chart (symbol and timeframe) use Close[int shift] function.
Parameters: string symbol: The symbol (currency pair) of the chart you want to get its bars number. int timeframe: The timeframe (in integers, ex: 1,5,30,60 etc) of the chart you want to get its bars number. int shift: The index of the bar you want to get its Close price.
Example:
iHigh:Syntax:
Description: The iHigh function returns the High price for the given bar (the shift parameter is the bar number) of the given symbol and timeframe. The function returns 0 if the history data not loaded. Note: If you want to get the High price (of a bar) of the current chart (symbol and timeframe) use High[int shift] function.
Parameters: string symbol: The symbol (currency pair) of the chart you want to get its bars number. int timeframe: The timeframe (in integers, ex: 1,5,30,60 etc) of the chart you want to get its bars number. int shift: The index of the bar you want to get its High price.
Example:
iLow:Syntax:
Description: The iLow function returns the Low price for the given bar (the shift parameter is the bar number) of the given symbol and timeframe. The function returns 0 if the history data not loaded. Note: If you want to get the Low price (of a bar) of the current chart (symbol and timeframe) use Low[int shift] function.
Parameters: string symbol: The symbol (currency pair) of the chart you want to get its bars number. int timeframe: The timeframe (in integers, ex: 1,5,30,60 etc) of the chart you want to get its bars number. int shift: The index of the bar you want to get its Low price. Example:
iOpen:Syntax:
Description: The iOpen function returns the Open price for the given bar (the shift parameter is the bar number) of the given symbol and timeframe. The function returns 0 if the history data not loaded. Note: If you want to get the Open price (of a bar) of the current chart (symbol and timeframe) use Open[int shift] function.
Parameters: string symbol: The symbol (currency pair) of the chart you want to get its bars number. int timeframe: The timeframe (in integers, ex: 1,5,30,60 etc) of the chart you want to get its bars number. int shift: The index of the bar you want to get its Open price.
Example:
iTime:Syntax:
Description: The iTime function returns the Open Time of the given bar (the shift parameter is the bar number) of the given symbol and timeframe. Note: If you want to get the bar Open Time (of a bar) of the current chart (symbol and timeframe) use Time[int shift] function.
Parameters: string symbol: The symbol (currency pair) of the chart you want to get its bars number. int timeframe: The timeframe (in integers, ex: 1,5,30,60 etc) of the chart you want to get its bars number. int shift: The index of the bar you want to get its Open Time.
Example:
iVolume:Syntax:
Description: The iVolume function returns the Tick Volume value of the given bar (the shift parameter is the bar number) of the given symbol and timeframe. Note: Volume is simply the number of shares (or contracts) traded during a specified time frame (e.g., hour, day, week, month, etc). Note: In MQL if you want to check if the tick is the first tick of the new bar you can check Volume[0] it will be 0 if it's the first tickof the new bar. Example:
Note: If you want to get the Tick Volume value (of a bar) of the current chart (symbol and timeframe) use Volume[int shift] function.
Parameters: string symbol: The symbol (currency pair) of the chart you want to get its bars number. int timeframe: The timeframe (in integers, ex: 1,5,30,60 etc) of the chart you want to get its bars number. int shift: The index of the bar you want to get its Tick Volume value.
Example:
Highest:Syntax:
Description: The Highest function calculate the Highest value of the specified type (Close price, Open price, High price etc) for a specified bars of the given symbol and timeframe. You use the type parameter to determine the type of the values to be calculated and the parameters count and start determine the bars to be calculated.
Parameters: string symbol: The symbol (currency pair) of the chart you want to get its bars number. int timeframe: The timeframe (in integers, ex: 1,5,30,60 etc) of the chart you want to get its bars number. int type: The type of values (Series array) to be calculated, it can be one of these types:
int count: How many bars you want to calculate its Highest value, use this parameter with start parameter to determine the range of bars to be calculated. The default value is WHOLE_ARRAY which means all the bars (values in the series array). int start: The bar to start the calculation from, the default value is 0 which means to start from the current bar.
Example:
Lowest:Syntax:
Description: The Lowest function calculate the Lowest value of the specified type (Close price, Open price, High price etc) for a specified bars of the given symbol and timeframe. You use the type parameter to determine the type of the values to be calculated and the parameters count and start determine the bars to be calculated.
Parameters: string symbol: The symbol (currency pair) of the chart you want to get its bars number. int timeframe: The timeframe (in integers, ex: 1,5,30,60 etc) of the chart you want to get its bars number. int type: The type of values (Series array) to be calculated. int count: How many bars you want to calculate its Highest value, use this parameter with start parameter to determine the range of bars to be calculated. The default value is WHOLE_ARRAY which means all the bars (values in the series array). int start: The bar to start the calculation from, the default value is 0 which means to start from the current bar. Example:
Now we have the tools to access the price data of the other charts, the question is: Is this enough to write our expert advisor that deals with other currency pairs? No! In the coming lesson we are going to know the most important function needed to write this kind of expert advisors; the MarketInfo() function. I hope you find it a useful lesson! |