下面這個圖是AUD圖上,疊加了英磅的RSI指標。
(當然也可以不疊加,分兩個窗口)
從RSI指標圖上我們看到,英磅強勢,而澳元很弱
下面是指標源碼
-------------------------------------------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Aqua
#property indicator_level1 30
#property indicator_level2 70
extern int RSI=12;
extern string 商品="GBPUSD";
double ind_buffer[];
int init()
{
SetIndexBuffer(0,ind_buffer);
SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1);
IndicatorShortName("RSI("+商品+"," +RSI+")");
return(0);
}
int start()
{
int limit;
int counted_bars=IndicatorCounted();
if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
for(int i=0; i<limit; i++)
ind_buffer[i]=iRSI(商品,0,RSI,PRICE_CLOSE,i);
return(0);
}
-------------------------------------------------------------------------------------------------------
下面是指標疊加的操作方法:
當然這里用的是RSI指標,別的指標如KDJ、威廉等指標也可以類似操作,只要把上面源碼中的取值函數(shù)和參數(shù)換一個行了。
=============================================
語句簡要解釋如下:
=============================================
#property indicator_separate_window
指標放在副圖
#property indicator_buffers 1
設置指標線數(shù)組為1個
#property indicator_color1 Aqua
設置第一條指標線顏色值為Aqua,即介于藍綠之間的一種顏色
#property indicator_level1 30
在副圖中,30值位置上畫一條水平直線
#property indicator_level2 70
在副圖中,70值位置上畫一條水平直線
extern int RSI=12;
設立一個自定義變量,允許外部值修改,整數(shù)型,變量名為"RSI",默認值12
extern string 商品="GBPUSD";
設立一個自定義變量,允許外部值修改,字符串型,變量名為"商品",默認值"GBPUSD"
double ind_buffer[];
設立一個自定義數(shù)組,雙精度型
int init()
設立初始化函數(shù)init。init為系統(tǒng)規(guī)定函數(shù)名,函數(shù)內(nèi)容自定義。該函數(shù)在指標被加載時運行一次
{
SetIndexBuffer(0,ind_buffer);
設置第一條指標線的數(shù)組為ind_buffer
SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1);
設置第一條指標線的樣式,DRAW_LINE表示連續(xù)曲線,STYLE_SOLID表示實心線,1號粗線
IndicatorShortName("RSI("+商品+"," +RSI+")");
設置指標線的顯示簡稱
return(0);
初始化函數(shù)結束
}
int start()
設立觸發(fā)函數(shù)start。start為系統(tǒng)規(guī)定函數(shù)名,函數(shù)內(nèi)容自定義。當數(shù)據(jù)變動時,start函數(shù)被觸發(fā)
{
int limit;
設立自定義變量limit,整數(shù)型
int counted_bars=IndicatorCounted();
設立整數(shù)型自定義變量counted_bars,并將IndicatorCounted()的值賦給counted_bars
IndicatorCounted()為緩存數(shù)量,即已經(jīng)計算過值的燭柱數(shù)
(注:可能這里解釋得不是很準確,大致就這個意思)
if(counted_bars<0) return(-1);
如果counted_bars值小于零,start函數(shù)結束
if(counted_bars>0) counted_bars--;
如果counted_bars值大于零,則counted_bars值減掉1。這是為了配合下一句,以避免limit相差1而出錯
limit=Bars-counted_bars;
給limit賦值
Bars為圖表中的柱數(shù)
counted_bars為已經(jīng)賦值的柱數(shù)
這樣limit的值就是未賦值的燭柱數(shù)
這樣做的目的是避免重復運算,優(yōu)化程序
for(int i=0; i<limit; i++)
循環(huán)語句,括號中有三個語句:
第一句int i=0; 表示循環(huán)從i=0開始
第二句i<limit; 這是循環(huán)的條件,如果條件滿足則執(zhí)行大括號中的循環(huán)體,如果條件不滿足,則中止循環(huán),跳到大括號下面的語句執(zhí)行
第三句i++,這是循環(huán)步調(diào)控制語句,每循環(huán)一次后執(zhí)行一次此語句。
i++相當于i=i+1,即i值在原有數(shù)值上增加1
ind_buffer[i]=iRSI(商品,0,RSI,PRICE_CLOSE,i);
此語句為循環(huán)體,由于只有一個語句,所以省略花括號
i為圖表燭柱的序號,從0開始,右邊第1柱序號為0,從右向左遞增
iRSI為RSI指標的取值函數(shù)
return(0);
start函數(shù)結束
}