MT編程入門教程(2)----指標畫哪里?價格窗口還是獨立窗口?
作者:MT4 來源:cxh99.com 發布時間:2012年05月04日
- 其實,都可以,不過是坐標的規格化,使得它們在相近的數據范圍,就可以畫在一起。
看你自己的習慣。畫在價格窗口,方便和價格同時感受。
//+本例展示了如何把MA轉換后畫到獨立窗口,同樣也可以把獨立窗口的指標改畫在價格窗口
//要達到這些目的,需要考慮兩個方面 1 估計數據的范圍,以確定以什么為單位? 2 確定坐標偏移,也就是確定坐標零點。
//目的就是讓你要畫的多條線在同樣的數值范圍,這樣才能同時畫出,達到參考目的
//比如-1000到+1000和0到+4這樣兩套數據要變換后才能在同圖畫出,
//轉換方法復習中學數學直角坐標系的縮放和平移部分-------------------------+
// 共畫2條線
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Yellow
// Parameter
// MA-Properties
extern double MovingFast = 7;
extern double MovingSlow = 13;
extern double MovingSlow02 = 13;
extern int MaM=2;
extern int MaP=2;
extern int MaM0=3;
extern int MaP0=3;如改
extern int range1 = 11;
extern int range2 = 4;
int i;
// Buffer
//// 共畫2條線,用2個數組存儲線數據
double bufferMA1[];
double bufferMA2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//// 設置2條線的順序編號,畫法,對應數組,
SetIndexStyle( 0, DRAW_LINE );
SetIndexBuffer( 0, bufferMA1 );
SetIndexStyle( 1, DRAW_LINE );
SetIndexBuffer( 1, bufferMA2 );
return(0);
}
int deinit() { return(0);}
int start()
{
double Buffer10,Buffer11,Buffer20,Buffer21,Buffer30, Buffer31;
int countedBars = IndicatorCounted();
//---- check for possible errors
if ( countedBars < 0 ) { return(-1); }
if ( countedBars > 0 ) { countedBars--; }
int barsToCount = Bars - countedBars;
for ( int i = barsToCount; i >= 0; i-- )
{
//// 開始計算
Buffer20 = iMA(NULL,0,MovingFast,0,MaM,MaP, i) ;//MovingFast
Buffer30 = iMA(NULL,0,MovingSlow,0,MaM,MaP, i) ;//MovingSlow
Buffer21 = iMA(NULL,0,MovingFast,0,MaM,MaP, i+range1) ;
Buffer31 = iMA(NULL,0,MovingSlow02,0,MaM,MaP, i+range2) ;
//賦值給數組以便畫線,使用Point使得坐標以點為單位,保證本指標對不同貨幣的一致y軸單位和視覺效果
bufferMA1 = MathRound((Buffer20-Buffer21)/Point);
bufferMA2 = MathRound((Buffer30-Buffer31)/Point);
}
return(0);
}上面把兩個MA的差值畫在獨立窗口,當然若把差值再加上一個MA,就可以畫在價格窗口。MA及其各種變化是最常用的指標基礎。MA一般常用來替代價格本身 再做進一步計算。#property indicator_chart_window
//......
Buffer10 = iMA(NULL,0,MovingFast*6,0,MaM,MaP, i)
bufferMA1 = MathRound(Buffer20-Buffer21)+Buffer10 ;
bufferMA2 = MathRound(Buffer30-Buffer31)+Buffer10 ;
//.........就可以把它們跌加到價格上畫了。你只需修改Buffer10 ,Buffer20, Buffer21 Buffer30, Buffer31為你需要的,在考慮一下數據范圍坐標問題,你可以畫任意的指標了。