小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

《MQL4實(shí)用編程》讀書(shū)筆記(8)

 分界交易 2016-05-08

外建指標(biāo)的數(shù)據(jù)緩沖

外建指標(biāo)的主要底層機(jī)制,是把指標(biāo)數(shù)組的數(shù)據(jù),送往終端窗口的緩沖,用于畫(huà)出指標(biāo)線。

緩沖是內(nèi)存區(qū),保存著指標(biāo)數(shù)據(jù)。

MQL4 規(guī)定,一個(gè)指標(biāo)最多可畫(huà)出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ò)緩沖,傳入主圖畫(huà)出指標(biāo)線。


圖. 115. 把指標(biāo)數(shù)據(jù)從指標(biāo)數(shù)組通過(guò)緩沖傳入主圖

第一個(gè)示例

先看一個(gè)簡(jiǎn)單的外建指標(biāo)。它有2條線,一條顯示最高價(jià),另一條顯示最低價(jià)。

示例一:簡(jiǎn)單的外建指標(biāo) userindicator.mq4
  1. //--------------------------------------------------------------------  
  2. // userindicator.mq4   
  3. // 本程序僅用于教學(xué)  
  4. //--------------------------------------------------------------------  
  5. #property indicator_chart_window    // 在主圖畫(huà)出指標(biāo)  
  6. #property indicator_buffers 2       // 緩沖的數(shù)目  
  7. #property indicator_color1 Blue     // 第1條指標(biāo)線的顏色  
  8. #property indicator_color2 Red      // 第2條指標(biāo)線的顏色  
  9.    
  10. double Buf_0[],Buf_1[];             // 以數(shù)組當(dāng)作指標(biāo)緩沖  
  11. //--------------------------------------------------------------------  
  12. int init()                          // 特別函數(shù) init()  
  13.   {  
  14.    SetIndexBuffer(0,Buf_0);         // 第一個(gè)緩沖賦值  
  15.    SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2); // 第一條指標(biāo)線的特點(diǎn)  
  16.    SetIndexBuffer(1,Buf_1);         // 第二個(gè)緩沖賦值  
  17.    SetIndexStyle (1,DRAW_LINE,STYLE_DOT,1);// 第二條指標(biāo)線的特點(diǎn)  
  18.    return;                          // 退出 init()  
  19.   }  
  20. //--------------------------------------------------------------------  
  21. int start()                         // 特別函數(shù) start()  
  22.   {  
  23.    int i,                           // 柱子 Bar 的索引  
  24.        Counted_bars;                // 當(dāng)前柱子的數(shù)目  
  25. //--------------------------------------------------------------------  
  26.    Counted_bars=IndicatorCounted(); // 歷史柱子的數(shù)目  
  27.    i=Bars-Counted_bars-1;           // 第一個(gè)歷史柱的索引  
  28.    while(i>=0)                      // 歷史柱的循環(huán)  
  29.      {  
  30.       Buf_0[i]=High[i];             // 第一個(gè)緩沖第 i 個(gè)柱的數(shù)據(jù)值  
  31.       Buf_1[i]=Low[i];              // 第二個(gè)緩沖第 i 個(gè)柱的數(shù)據(jù)值  
  32.       i--;                          // 計(jì)算下一個(gè)柱  
  33.      }  
  34. //--------------------------------------------------------------------  
  35.    return;                          // 退出 start()  
  36.   }  
  37. //--------------------------------------------------------------------  

圖120是以上外建指標(biāo)運(yùn)行的結(jié)果


圖. 120. userindicator.mq4在主圖中畫(huà)出的指標(biāo)的2條線

第二個(gè)示例

示例二:簡(jiǎn)單的外建指標(biāo) averagevalue.mq4. 指標(biāo)線的數(shù)據(jù),是N個(gè)柱的最小值和最大值的平均值。
  1. //--------------------------------------------------------------------  
  2. // separatewindow.mq4   
  3. // 僅用于教學(xué)  
  4. //--------------------------------------------------------------------  
  5. #property indicator_separate_window // 在副圖中畫(huà)線  
  6. #property indicator_buffers 1       // 緩沖數(shù)目  
  7. #property indicator_color1 Blue     // 第一條線的顏色  
  8. #property indicator_color2 Red      // 第二條線的顏色  
  9.    
  10. extern int History  =50;            // 歷史柱的總數(shù)  
  11. extern int Aver_Bars=5;             // 取"平均"的柱的數(shù)目  
  12.    
  13. double Buf_0[];                     // 指標(biāo)數(shù)組  
  14. //--------------------------------------------------------------------  
  15. int init()                          // 特別函數(shù) init()  
  16.   {  
  17.    SetIndexBuffer(0,Buf_0);         // 把指標(biāo)數(shù)組的值賦予緩沖  
  18.    SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2);// 指標(biāo)線的特點(diǎn)  
  19.    return;                          // 退出 init()  
  20.   }  
  21. //--------------------------------------------------------------------  
  22. int start()                         // 特別函數(shù) start()  
  23.   {  
  24.    int i,                           // 柱的索引  
  25.    n,                               // 正常參數(shù)  
  26.    Counted_bars;                    // 歷史柱的數(shù)目  
  27.    double  
  28.    Sum_H,                           // 計(jì)算周期內(nèi)最高價(jià)的匯總  
  29.    Sum_L;                           // 計(jì)算周期內(nèi)最低價(jià)的匯總  
  30. //--------------------------------------------------------------------  
  31.    Counted_bars=IndicatorCounted(); // 歷史柱的數(shù)目  
  32.    i=Bars-Counted_bars-1;           // 歷史柱的第一個(gè)的索引  
  33.    if (i>History-1)                 // 若有太多的柱,計(jì)算指定的范圍  
  34.       i=History-1;                    
  35.    while(i>=0)                      // 循環(huán)處理歷史柱  
  36.      {  
  37.       Sum_H=0;                      // 清空  
  38.       Sum_L=0;                      // 清空  
  39.       for(n=i;n<=i+Aver_Bars-1;n++) // 匯總過(guò)程的循環(huán)  
  40.         {  
  41.          Sum_H=Sum_H + High[n];     // 累計(jì)最高價(jià)  
  42.          Sum_L=Sum_L + Low[n];      // 累計(jì)最低價(jià)  
  43.         }  
  44.       Buf_0[i]=(Sum_H-Sum_L)/Aver_Bars;// 在第一個(gè)緩沖第 i 柱的值  
  45.       i--;                          // 下一個(gè)計(jì)算對(duì)象的索引  
  46.      }  
  47. //--------------------------------------------------------------------  
  48.    return;                          // 退出 start()  
  49.   }  
  50. //--------------------------------------------------------------------  
如果把外建指標(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)

在副圖畫(huà)出指標(biāo)線

  1. #property indicator_separate_window // 這條指令確定指標(biāo)線在副圖畫(huà)出  

歷史數(shù)據(jù)的數(shù)量限制

過(guò)多的歷史數(shù)據(jù),一是沒(méi)用,二是礙事(造成圖形過(guò)大)。用以下代碼,限制其數(shù)量。

  1. i=Bars-Counted_bars-1;           // 第一個(gè)柱的索引  
  2. if (i>History-1)                 // 若有過(guò)多的柱,  
  3.    i=History-1;                  // 則限定為預(yù)定數(shù)目  
示例:簡(jiǎn)單的外建指標(biāo) separatewindow.mq4. 指標(biāo)線畫(huà)在副圖。
  1. //--------------------------------------------------------------------  
  2. // separatewindow.mq4   
  3. // 僅用于教學(xué)  
  4. //--------------------------------------------------------------------  
  5. #property indicator_separate_window // 在副圖畫(huà)線  
  6. #property indicator_buffers 1       // 緩沖數(shù)目  
  7. #property indicator_color1 Blue     // 第1根線的顏色  
  8. #property indicator_color2 Red      // 第2根線的顏色  
  9.    
  10. extern int History  =50;            // 柱的歷史數(shù)據(jù)的數(shù)量  
  11. extern int Aver_Bars=5;             // 柱的總數(shù)  
  12.    
  13. double Buf_0[];                     // 指標(biāo)數(shù)組  
  14. //--------------------------------------------------------------------  
  15. int init()                          // 特別函數(shù) init()  
  16.   {  
  17.    SetIndexBuffer(0,Buf_0);         // 數(shù)組與緩沖建立關(guān)聯(lián)  
  18.    SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2);// 線的特點(diǎn)  
  19.    return;                          // 退出 init()  
  20.   }  
  21. //--------------------------------------------------------------------  
  22. int start()                         // 特別函數(shù) start()  
  23.   {  
  24.    int i,                           // 柱的索引  
  25.    n,                               // 一般變量  
  26.    Counted_bars;                    // 要計(jì)算的柱的量  
  27.    double  
  28.    Sum_H,                           // 周期內(nèi)最高價(jià)的匯總  
  29.    Sum_L;                           // 周期內(nèi)最低價(jià)的匯總  
  30. //--------------------------------------------------------------------  
  31.    Counted_bars=IndicatorCounted(); // 要計(jì)算的柱的量  
  32.    i=Bars-Counted_bars-1;           // 第1柱的索引  
  33.    if (i>History-1)                 // 柱太多了,  
  34.       i=History-1;                  // 于是,以預(yù)定數(shù)目為準(zhǔn)  
  35.    while(i>=0)                      // 逐一計(jì)算每個(gè)柱  
  36.      {  
  37.       Sum_H=0;                      // 清零  
  38.       Sum_L=0;                      // 清零  
  39.       for(n=i;n<=i+Aver_Bars-1;n++) // 匯總  
  40.         {  
  41.          Sum_H=Sum_H + High[n];     // 累計(jì)最高價(jià)  
  42.          Sum_L=Sum_L + Low[n];      // 累計(jì)最低價(jià)  
  43.         }  
  44.       Buf_0[i]=(Sum_H-Sum_L)/Aver_Bars;// 第1個(gè)緩沖的第 i 個(gè)柱的值  
  45.       i--;                          // 下個(gè)柱的索引  
  46.      }  
  47. //--------------------------------------------------------------------  
  48.    return;                          // 退出 start()  
  49.   }  
  50. //--------------------------------------------------------------------  

圖122是指標(biāo)separatewindow.mq4 運(yùn)行的結(jié)果。


圖. 122. 在副圖畫(huà)出指標(biāo)線。

外建指標(biāo),僅畫(huà)出最新的50個(gè)柱。若要用更多歷史數(shù)據(jù),可在外建指標(biāo)設(shè)置窗口更改。

圖123是以直方圖表示的指標(biāo)線。為此,可對(duì) separatewindow.mq4 做相應(yīng)修改:

   
  1. SetIndexStyle (0,DRAW_HISTOGRAM);// 指標(biāo)線的特點(diǎn)  


圖. 123. 在副圖中畫(huà)出的指標(biāo)線直方圖

指標(biāo)線的垂直方向與水平方向的變換

示例:外建指標(biāo) displacement.mq4 。指標(biāo)線的垂直/水平變換。
  1. //--------------------------------------------------------------------  
  2. // displacement.mq4   
  3. // 僅用于教學(xué)  
  4. //--------------------------------------------------------------------  
  5. #property indicator_chart_window       // 在主圖中畫(huà)線  
  6. #property indicator_buffers 3          // 緩沖數(shù)目  
  7. #property indicator_color1 Red         // 第1條線的顏色  
  8. #property indicator_color2 Blue        // 第2條線的顏色  
  9. #property indicator_color3 Green       // 第3條線的顏色  
  10.    
  11. extern int History  =500;              // 歷史柱的數(shù)量  
  12. extern int Aver_Bars=5;                // 柱的總數(shù)  
  13. extern int Left_Right= 5;              // 柱的水平軸  
  14. extern int Up_Down  =25;               // 點(diǎn)位的垂直軸  
  15.    
  16. double Line_0[],Line_1[],Line_2[];     // 緩沖  
  17. //--------------------------------------------------------------------  
  18. int init()                             // 特別函數(shù) init()  
  19.   {  
  20. //--------------------------------------------------------------------  
  21.    SetIndexBuffer(0,Line_0);           // 數(shù)組與緩沖建立關(guān)聯(lián)  
  22.    SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2);// 線的特點(diǎn)  
  23. //--------------------------------------------------------------------  
  24.    SetIndexBuffer(1,Line_1);           // 第1數(shù)組與第1緩沖建立關(guān)聯(lián)  
  25.    SetIndexStyle (1,DRAW_LINE,STYLE_DOT,1);// 線的特點(diǎn)  
  26. //--------------------------------------------------------------------  
  27.    SetIndexBuffer(2,Line_2);           // 第2數(shù)組與第2緩沖建立關(guān)聯(lián)  
  28.    SetIndexStyle (2,DRAW_LINE,STYLE_DOT,1);// 線的特點(diǎn)  
  29. //--------------------------------------------------------------------  
  30.    return;                             // 退出 init()  
  31.   }  
  32. //--------------------------------------------------------------------  
  33. int start()                            // 特別函數(shù) start()  
  34.   {  
  35.    int i,                              // 柱的索引  
  36.        n,                              // 普通變量,表示索引值  
  37.        k,                              // 指標(biāo)數(shù)組索引  
  38.        Counted_bars;                   // 要計(jì)算的柱的數(shù)目  
  39.        double  
  40.        Sum;                            // 周期內(nèi)最高價(jià)與最低價(jià)的匯總  
  41. //--------------------------------------------------------------------  
  42.    Counted_bars=IndicatorCounted();    // 要計(jì)算的柱的數(shù)目  
  43.    i=Bars-Counted_bars-1;              // 第1柱的索引  
  44.    if (i>History-1)                    // 若柱太多,  
  45.       i=History-1;                     // 則以預(yù)定值為準(zhǔn)  
  46.    
  47.    while(i>=0)                         // 逐個(gè)柱進(jìn)行處理  
  48.      {  
  49.       Sum=0;                           // 清零  
  50.       for(n=i;n<=i+Aver_Bars-1;n++)    // 匯總  
  51.          Sum=Sum + High[n]+Low[n];     // 累計(jì)  
  52.       k=i+Left_Right;                  // 取得索引  
  53.       Line_0[k]= Sum/2/Aver_Bars;      // 第1個(gè)緩沖的第k個(gè)柱的數(shù)據(jù)值  
  54.       Line_1[k]= Line_0[k]+Up_Down*Point;// 第1個(gè)緩沖的值  
  55.       Line_2[k]= Line_0[k]-Up_Down*Point;// 第2個(gè)緩沖的值  
  56.    
  57.       i--;                             // 下一個(gè)柱的索引  
  58.      }  
  59. //--------------------------------------------------------------------  
  60.    return;                             // 退出 start()  
  61.   }  
  62. //--------------------------------------------------------------------  

指標(biāo)運(yùn)行結(jié)果見(jiàn)圖124。


圖. 124. 紅線向左 5 個(gè)柱,虛線與紅線間隔30點(diǎn)

外建指標(biāo)的限制

以下函數(shù)只能在指標(biāo)中使用,不能用于“操盤(pán)手”和腳本:IndicatorBuffers(),IndicatorCounted (), IndicatorDigits(), IndicatorShortName(), SetIndexArrow(),SetIndexBuffer(), SetIndexDrawBegin(), SetIndexEmptyValue(), SetIndexLabel(), SetIndexShift(),SetIndexStyle(), SetLevelStyle(), SetLevelValue()。

以下交易函數(shù)不能用于指標(biāo): OrderSend(),OrderClose(), OrderCloseBy(), OrderDelete() and OrderModify()。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類(lèi)似文章 更多