外建指標(biāo)的數(shù)據(jù)緩沖
外建指標(biāo)的主要底層機(jī)制,是把指標(biāo)數(shù)組的數(shù)據(jù),送往終端窗口的緩沖,用于畫出指標(biāo)線。
緩沖是內(nèi)存區(qū),保存著指標(biāo)數(shù)據(jù)。
MQL4 規(guī)定,一個(gè)指標(biāo)最多可畫出8條指標(biāo)線。一個(gè)指標(biāo)數(shù)組,存儲(chǔ)一條指標(biāo)線的數(shù)據(jù),與一個(gè)緩沖關(guān)聯(lián)。8個(gè)緩沖的索引從0開(kāi)始,最后一個(gè)為 7。圖115表示外建指標(biāo)數(shù)據(jù),如何通過(guò)緩沖,傳入主圖畫出指標(biāo)線。

圖. 115. 把指標(biāo)數(shù)據(jù)從指標(biāo)數(shù)組通過(guò)緩沖傳入主圖
第一個(gè)示例
先看一個(gè)簡(jiǎn)單的外建指標(biāo)。它有2條線,一條顯示最高價(jià),另一條顯示最低價(jià)。
- //--------------------------------------------------------------------
- // userindicator.mq4
- // 本程序僅用于教學(xué)
- //--------------------------------------------------------------------
- #property indicator_chart_window // 在主圖畫出指標(biāo)
- #property indicator_buffers 2 // 緩沖的數(shù)目
- #property indicator_color1 Blue // 第1條指標(biāo)線的顏色
- #property indicator_color2 Red // 第2條指標(biāo)線的顏色
-
- double Buf_0[],Buf_1[]; // 以數(shù)組當(dāng)作指標(biāo)緩沖
- //--------------------------------------------------------------------
- int init() // 特別函數(shù) init()
- {
- SetIndexBuffer(0,Buf_0); // 第一個(gè)緩沖賦值
- SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2); // 第一條指標(biāo)線的特點(diǎn)
- SetIndexBuffer(1,Buf_1); // 第二個(gè)緩沖賦值
- SetIndexStyle (1,DRAW_LINE,STYLE_DOT,1);// 第二條指標(biāo)線的特點(diǎn)
- return; // 退出 init()
- }
- //--------------------------------------------------------------------
- int start() // 特別函數(shù) start()
- {
- int i, // 柱子 Bar 的索引
- Counted_bars; // 當(dāng)前柱子的數(shù)目
- //--------------------------------------------------------------------
- Counted_bars=IndicatorCounted(); // 歷史柱子的數(shù)目
- i=Bars-Counted_bars-1; // 第一個(gè)歷史柱的索引
- while(i>=0) // 歷史柱的循環(huán)
- {
- Buf_0[i]=High[i]; // 第一個(gè)緩沖第 i 個(gè)柱的數(shù)據(jù)值
- Buf_1[i]=Low[i]; // 第二個(gè)緩沖第 i 個(gè)柱的數(shù)據(jù)值
- i--; // 計(jì)算下一個(gè)柱
- }
- //--------------------------------------------------------------------
- return; // 退出 start()
- }
- //--------------------------------------------------------------------
圖120是以上外建指標(biāo)運(yùn)行的結(jié)果

圖. 120.
userindicator.mq4在主圖中畫出的指標(biāo)的2條線
第二個(gè)示例
 |
示例二:簡(jiǎn)單的外建指標(biāo)
averagevalue.mq4. 指標(biāo)線的數(shù)據(jù),是N個(gè)柱的最小值和最大值的平均值。 |
- //--------------------------------------------------------------------
- // separatewindow.mq4
- // 僅用于教學(xué)
- //--------------------------------------------------------------------
- #property indicator_separate_window // 在副圖中畫線
- #property indicator_buffers 1 // 緩沖數(shù)目
- #property indicator_color1 Blue // 第一條線的顏色
- #property indicator_color2 Red // 第二條線的顏色
-
- extern int History =50; // 歷史柱的總數(shù)
- extern int Aver_Bars=5; // 取"平均"的柱的數(shù)目
-
- double Buf_0[]; // 指標(biāo)數(shù)組
- //--------------------------------------------------------------------
- int init() // 特別函數(shù) init()
- {
- SetIndexBuffer(0,Buf_0); // 把指標(biāo)數(shù)組的值賦予緩沖
- SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2);// 指標(biāo)線的特點(diǎn)
- return; // 退出 init()
- }
- //--------------------------------------------------------------------
- int start() // 特別函數(shù) start()
- {
- int i, // 柱的索引
- n, // 正常參數(shù)
- Counted_bars; // 歷史柱的數(shù)目
- double
- Sum_H, // 計(jì)算周期內(nèi)最高價(jià)的匯總
- Sum_L; // 計(jì)算周期內(nèi)最低價(jià)的匯總
- //--------------------------------------------------------------------
- Counted_bars=IndicatorCounted(); // 歷史柱的數(shù)目
- i=Bars-Counted_bars-1; // 歷史柱的第一個(gè)的索引
- if (i>History-1) // 若有太多的柱,計(jì)算指定的范圍
- i=History-1;
- while(i>=0) // 循環(huán)處理歷史柱
- {
- Sum_H=0; // 清空
- Sum_L=0; // 清空
- for(n=i;n<=i+Aver_Bars-1;n++) // 匯總過(guò)程的循環(huán)
- {
- Sum_H=Sum_H + High[n]; // 累計(jì)最高價(jià)
- Sum_L=Sum_L + Low[n]; // 累計(jì)最低價(jià)
- }
- Buf_0[i]=(Sum_H-Sum_L)/Aver_Bars;// 在第一個(gè)緩沖第 i 柱的值
- i--; // 下一個(gè)計(jì)算對(duì)象的索引
- }
- //--------------------------------------------------------------------
- return; // 退出 start()
- }
- //--------------------------------------------------------------------
如果把外建指標(biāo)
averagevalue.mq4 “粘”入平移指標(biāo)MA上,會(huì)看到3條指標(biāo)線。若這2個(gè)指標(biāo)的周期相同,MA指標(biāo)線會(huì)與外建指標(biāo)的一條線合并(重合),只看到2條線(見(jiàn)圖121)。

圖. 121.內(nèi)建指標(biāo)與外建指標(biāo)線的重合(紅線)
內(nèi)建指標(biāo)的選項(xiàng)
在副圖畫出指標(biāo)線
- #property indicator_separate_window // 這條指令確定指標(biāo)線在副圖畫出
歷史數(shù)據(jù)的數(shù)量限制
過(guò)多的歷史數(shù)據(jù),一是沒(méi)用,二是礙事(造成圖形過(guò)大)。用以下代碼,限制其數(shù)量。
- i=Bars-Counted_bars-1; // 第一個(gè)柱的索引
- if (i>History-1) // 若有過(guò)多的柱,
- i=History-1; // 則限定為預(yù)定數(shù)目
- //--------------------------------------------------------------------
- // separatewindow.mq4
- // 僅用于教學(xué)
- //--------------------------------------------------------------------
- #property indicator_separate_window // 在副圖畫線
- #property indicator_buffers 1 // 緩沖數(shù)目
- #property indicator_color1 Blue // 第1根線的顏色
- #property indicator_color2 Red // 第2根線的顏色
-
- extern int History =50; // 柱的歷史數(shù)據(jù)的數(shù)量
- extern int Aver_Bars=5; // 柱的總數(shù)
-
- double Buf_0[]; // 指標(biāo)數(shù)組
- //--------------------------------------------------------------------
- int init() // 特別函數(shù) init()
- {
- SetIndexBuffer(0,Buf_0); // 數(shù)組與緩沖建立關(guān)聯(lián)
- SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2);// 線的特點(diǎn)
- return; // 退出 init()
- }
- //--------------------------------------------------------------------
- int start() // 特別函數(shù) start()
- {
- int i, // 柱的索引
- n, // 一般變量
- Counted_bars; // 要計(jì)算的柱的量
- double
- Sum_H, // 周期內(nèi)最高價(jià)的匯總
- Sum_L; // 周期內(nèi)最低價(jià)的匯總
- //--------------------------------------------------------------------
- Counted_bars=IndicatorCounted(); // 要計(jì)算的柱的量
- i=Bars-Counted_bars-1; // 第1柱的索引
- if (i>History-1) // 柱太多了,
- i=History-1; // 于是,以預(yù)定數(shù)目為準(zhǔn)
- while(i>=0) // 逐一計(jì)算每個(gè)柱
- {
- Sum_H=0; // 清零
- Sum_L=0; // 清零
- for(n=i;n<=i+Aver_Bars-1;n++) // 匯總
- {
- Sum_H=Sum_H + High[n]; // 累計(jì)最高價(jià)
- Sum_L=Sum_L + Low[n]; // 累計(jì)最低價(jià)
- }
- Buf_0[i]=(Sum_H-Sum_L)/Aver_Bars;// 第1個(gè)緩沖的第 i 個(gè)柱的值
- i--; // 下個(gè)柱的索引
- }
- //--------------------------------------------------------------------
- return; // 退出 start()
- }
- //--------------------------------------------------------------------
圖122是指標(biāo)separatewindow.mq4 運(yùn)行的結(jié)果。

圖. 122. 在副圖畫出指標(biāo)線。
外建指標(biāo),僅畫出最新的50個(gè)柱。若要用更多歷史數(shù)據(jù),可在外建指標(biāo)設(shè)置窗口更改。
圖123是以直方圖表示的指標(biāo)線。為此,可對(duì)
separatewindow.mq4 做相應(yīng)修改:
- SetIndexStyle (0,DRAW_HISTOGRAM);// 指標(biāo)線的特點(diǎn)

圖. 123. 在副圖中畫出的指標(biāo)線直方圖
指標(biāo)線的垂直方向與水平方向的變換
- //--------------------------------------------------------------------
- // displacement.mq4
- // 僅用于教學(xué)
- //--------------------------------------------------------------------
- #property indicator_chart_window // 在主圖中畫線
- #property indicator_buffers 3 // 緩沖數(shù)目
- #property indicator_color1 Red // 第1條線的顏色
- #property indicator_color2 Blue // 第2條線的顏色
- #property indicator_color3 Green // 第3條線的顏色
-
- extern int History =500; // 歷史柱的數(shù)量
- extern int Aver_Bars=5; // 柱的總數(shù)
- extern int Left_Right= 5; // 柱的水平軸
- extern int Up_Down =25; // 點(diǎn)位的垂直軸
-
- double Line_0[],Line_1[],Line_2[]; // 緩沖
- //--------------------------------------------------------------------
- int init() // 特別函數(shù) init()
- {
- //--------------------------------------------------------------------
- SetIndexBuffer(0,Line_0); // 數(shù)組與緩沖建立關(guān)聯(lián)
- SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2);// 線的特點(diǎn)
- //--------------------------------------------------------------------
- SetIndexBuffer(1,Line_1); // 第1數(shù)組與第1緩沖建立關(guān)聯(lián)
- SetIndexStyle (1,DRAW_LINE,STYLE_DOT,1);// 線的特點(diǎn)
- //--------------------------------------------------------------------
- SetIndexBuffer(2,Line_2); // 第2數(shù)組與第2緩沖建立關(guān)聯(lián)
- SetIndexStyle (2,DRAW_LINE,STYLE_DOT,1);// 線的特點(diǎn)
- //--------------------------------------------------------------------
- return; // 退出 init()
- }
- //--------------------------------------------------------------------
- int start() // 特別函數(shù) start()
- {
- int i, // 柱的索引
- n, // 普通變量,表示索引值
- k, // 指標(biāo)數(shù)組索引
- Counted_bars; // 要計(jì)算的柱的數(shù)目
- double
- Sum; // 周期內(nèi)最高價(jià)與最低價(jià)的匯總
- //--------------------------------------------------------------------
- Counted_bars=IndicatorCounted(); // 要計(jì)算的柱的數(shù)目
- i=Bars-Counted_bars-1; // 第1柱的索引
- if (i>History-1) // 若柱太多,
- i=History-1; &n
|