2012年10月16日

[MQL4]Lesson 1. K棒


我在學習MQL4時找到許多教程
其中最有印象的是Coders guru寫的一系列文章
接下來的一系列文章, 我將翻譯給有意開始學習MT4的朋友們



What’s a BAR?
K棒在英文中是BAR

The bar is the dawning unit on the chart which you can specify by choosing the period of
the timeframe.
K棒表達的是某一周期中的信息
For example: The 30 minutes timeframe will draw a bar every 30 minutes.
例如, 30分K即是30分鐘畫一根K棒
MetaTrader (and other platforms) uses the values of to high, low, open and close prices to
draw the bar start and end boundaries. (Figure 1)
MT4用開高低收來表達一根K棒
(翻譯未完待續)
Figure 1 - Bar chart dawning
How the bars indexed in MQL4?
MQL4 indexes the bars from 0 (for the current bar) then 1, 2, 3 etc to the count of the
bars.
So, if you want to work with the current bar you use the index 0.
And the index of the previous bar (of the current bar) is 1.
And the index 100 is the bar 100 in the history (100 bars ago).
And the index of last bar is the count of all bars on chart.
MQL4 BARS count functions:
In MQL4 there is a variety of functions working with the count of bars:
int Bars
This function returns the number of the bars of the current chart.
Note that, changing the timeframe will change this count.
int iBars( string symbol, int timeframe)
This function returns the number of bars on the specified currency pairs symbol and
timeframe.
Assume you are working on 30M timeframe and you want to get the count of Bars on 1H
time frame, you use this line:
iBars(NULL, PERIOD_H1));
Note: If you used it like this:
iBars(NULL,0));
It returns the same number as Bars function.
int IndicatorCounted()
When you are writing your indicator, you know now how to get the number of bars.
You use this number in your calculation and line dawning.
But it’s useful to know if you have counted the bar before or it’s the first time you count it.
That’s because, if you have counted it before you don’t want to count it again and want to
work only with the new bars.
In this case you use the function IndicatorCounted(), which returns the number of bars
have been counted by your indicator.
At the first run of your indicator this count will be zero, that’s because your indicator
didn’t count any bars yet.
Afterwards, it will equal to the count of Bars – 1. That’s because the last bar not counted
yet (Figure 2).
Figure 2 – Program output
Let’s write a small program to show you what’s going on.
//+------------------------------------------------------------------+
//| Bars.mq4 |
//| Codersguru |
//| http://www.forex-tsd.com |
//+------------------------------------------------------------------+
#property copyright "Codersguru"
#property link "http://www.forex-tsd.com"
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
//----
return(1);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
//----
Alert( "counted_bars = " , IndicatorCounted()); //The count of bars
have been counted
Alert( "Bars = " , Bars); //The count of the bars on the chart
Alert( "iBars = " , iBars(NULL,0)); //the same as Bars function
Alert( "First Bar Open = " , Open[Bars-1]); //Open price of the
first bar
Alert( "Last Bar Open = " , Open[0]); //Open price of the last bar
(current bar)
//----
return(0);
}
//+------------------------------------------------------------------+
Note: This program produces the image you have seen in figure 2
I hope the Bars count is clear now.
I welcome very much the questions and the suggestions.
See you
Coders’ Guru
21-11-2005

沒有留言:

張貼留言