//[i]Stochastic 漢譯KD
property indicator_separate_window
property indicator_minimum 0
property indicator_maximum 100
property indicator_buffers 2
property indicator_color1 LightSeaGreen
property indicator_color2 Red
property indicator_level1 20.0
property indicator_level2 80.0
property indicator_levelcolor clrSilver
property indicator_levelstyle STYLE_DOT
input int K周期 = 5; // (實(shí)為RSV周期)
input int D周期 = 3; // (實(shí)為K周期)
input int 慢周期 =3; // (實(shí)為D周期)
double 主線組[];
double 信線組[];
double 區(qū)頂序組[];
double 區(qū)底序組[];
//---
int 始繪處1 = 0;
int 始繪處2 = 0;
//-----------初始化---------------+
int OnInit(void)
{
//--用在計(jì)算中的兩組緩存
IndicatorBuffers(4);
SetIndexBuffer(2, 區(qū)頂序組);
SetIndexBuffer(3, 區(qū)底序組);
//--指標(biāo)線
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0, 主線組);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1, 信線組);
//--名稱
string 名稱="Sto("+IntegerToString(K周期)+","+IntegerToString(D周期)+","+IntegerToString(慢周期)+")";
IndicatorShortName(名稱);
SetIndexLabel(0,名稱);
SetIndexLabel(1,"Signal");
//--
始繪處1= K周期+慢周期;
始繪處2= 始繪處1+D周期;
SetIndexDrawBegin(0,始繪處1);
SetIndexDrawBegin(1,始繪處2);
//--初始化結(jié)束
return(INIT_SUCCEEDED);
}
//----------主函數(shù)---------------+
int OnCalculate(const int 總需棒數(shù),
const int 已計(jì),
const datetime &K時(shí)組[],
const double &K開組[],
const double &K高組[],
const double &K低組[],
const double &K收組[],
const long &tick_volume[],
const long &volume[],
const int &spread[]){
int i,
k,
pos;
//--檢查
if(總需棒數(shù)<=K周期+D周期+慢周期)
return(0);
//--設(shè)置為序列數(shù)組
ArraySetAsSeries(主線組,false);
ArraySetAsSeries(信線組,false);
ArraySetAsSeries(區(qū)頂序組,false);
ArraySetAsSeries(區(qū)底序組,false);
ArraySetAsSeries(K低組,false);
ArraySetAsSeries(K高組,false);
ArraySetAsSeries(K收組,false);
//--
pos= K周期-1;
if(pos+1<已計(jì))
pos= 已計(jì)-2;
else{
for(i=0; i<pos; i++){
區(qū)底序組[i]= 0.0;
區(qū)頂序組[i]= 0.0;
}
}
//--找到最小值與最大值 并賦于數(shù)組
for(i=pos; i<總需棒數(shù) && !IsStopped(); i++){
double 最小值= 1000000.0;
double 最大值= -1000000.0;
for(k=i-K周期+1; k<=i; k++){
if(最小值>K低組[k])
最小值= K低組[k];
if(最大值<K高組[k])
最大值= K高組[k];
}
區(qū)底序組[i]= 最小值;
區(qū)頂序組[i]= 最大值;
}
//--
pos= K周期-1 +慢周期-1;
if(pos+1<已計(jì))
pos= 已計(jì)-2;
else{
for(i=0; i<pos; i++)
主線組[i]= 0.0;
}
//--主線(%RSV)
for(i=pos; i<總需棒數(shù) && !IsStopped(); i++){
double 收低差和= 0.0;
double 高低差和= 0.0;
for(k=(i-慢周期+1); k<=i; k++){
收低差和 += (K收組[k]-區(qū)底序組[k]);
高低差和 += (區(qū)頂序組[k]-區(qū)底序組[k]);
}
if(高低差和==0.0)
主線組[i]= 100.0;
else
主線組[i]= 收低差和/高低差和*100.0;
}
//--信號(hào)線(%K)
pos= D周期-1;
if(pos+1<已計(jì))
pos= 已計(jì)-2;
else{
for(i=0; i<pos; i++)
信線組[i]=0.0;
}
for(i=pos; i<總需棒數(shù) && !IsStopped(); i++){
double sum= 0.0;
for(k=0; k<D周期; k++)
sum += 主線組[i-k];
信線組[i]= sum/D周期;
}
//--主循環(huán)結(jié)束,返回新的己計(jì)
return(總需棒數(shù));
}
//----------謝謝點(diǎn)贊-----------+