『外汇堂』·专业外汇论坛

标题: 请教版主和高手,指标有源码如何编程 [打印本页]

作者: yoyo    时间: 2009-9-15 21:17
标题: 请教版主和高手,指标有源码如何编程
请教版主和高手,指标有源码如何编程
本人使用MT4平台中的MACDStochastic指标编程。MACD柱状线在0轴下做空,在0轴上为做多;Stochastic在高位死叉向下做空,在低位金叉向上做多,暂且不论效果如何;满足条件时利用M1分钟图表进场,随后M5分钟也满足条件时不平仓继续持仓。请教版主和高手如何在如下的源码中提取指标数据实现编程的买卖,在此多谢各位赐教。
MACD指标源码
//+------------------------------------------------------------------+
//|
Custom MACD.mq4 |

//|
Copyright ?2004, MetaQuotes Software Corp. |

//|
http://www.metaquotes.net/ |

//+------------------------------------------------------------------+
#property
copyright "Copyright ?2004, MetaQuotes Software Corp."

#property
link
"http://www.metaquotes.net/"

//---- indicator settings
#property
indicator_separate_window

#property
indicator_buffers 2

#property
indicator_color1
Silver

#property
indicator_color2
Red

#property
indicator_width1
2

//---- indicator parameters
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;
//---- indicator buffers
double
MacdBuffer[];

double
SignalBuffer[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function
|

//+------------------------------------------------------------------+
int init()

{

//---- drawing settings

SetIndexStyle(0,DRAW_HISTOGRAM);


SetIndexStyle(1,DRAW_LINE);


SetIndexDrawBegin(1,SignalSMA);


IndicatorDigits(Digits+1);

//---- indicator buffers mapping

SetIndexBuffer(0,MacdBuffer);


SetIndexBuffer(1,SignalBuffer);

//---- name for DataWindow and indicator subwindow label

IndicatorShortName("MACD("+FastEMA+","+SlowEMA+","+SignalSMA+")");


SetIndexLabel(0,"MACD");


SetIndexLabel(1,"Signal");

//---- initialization done

return(0);


}

//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence
|

//+------------------------------------------------------------------+
int start()

{


int limit;


int counted_bars=IndicatorCounted();

//---- last counted bar will be recounted

if(counted_bars>0) counted_bars--;


limit=Bars-counted_bars;

//---- macd counted in the 1-st buffer

for(int i=0; i<limit; i++)


MacdBuffer=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);

//---- signal line counted in the 2-nd buffer

for(i=0; i<limit; i++)


SignalBuffer=iMAOnArray(MacdBuffer,Bars,SignalSMA,0,MODE_SMA,i);

//---- done

return(0);


}

//+------------------------------------------------------------------+


Stochastic指标源码.
/+------------------------------------------------------------------+
//|
Stochastic.mq4 |

//|
Copyright ?2004, MetaQuotes Software Corp. |

//|
http://www.metaquotes.net/ |

//+------------------------------------------------------------------+
#property copyright "Copyright ?2004, MetaQuotes Software Corp."
#property link
"http://www.metaquotes.net/"


#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_buffers 2
#property indicator_color1 LightSeaGreen
#property indicator_color2 Red
//---- input parameters
extern int KPeriod=5;
extern int DPeriod=3;
extern int Slowing=3;
//---- buffers
double MainBuffer[];
double SignalBuffer[];
double HighesBuffer[];
double LowesBuffer[];
//----
int draw_begin1=0;
int draw_begin2=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function
|

//+------------------------------------------------------------------+
int init()

{


string short_name;

//---- 2 additional buffers are used for counting.

IndicatorBuffers(4);


SetIndexBuffer(2, HighesBuffer);


SetIndexBuffer(3, LowesBuffer);

//---- indicator lines

SetIndexStyle(0,DRAW_LINE);


SetIndexBuffer(0, MainBuffer);


SetIndexStyle(1,DRAW_LINE);


SetIndexBuffer(1, SignalBuffer);

//---- name for DataWindow and indicator subwindow label

short_name="Sto("+KPeriod+","+DPeriod+","+Slowing+")";


IndicatorShortName(short_name);


SetIndexLabel(0,short_name);


SetIndexLabel(1,"Signal");

//----

draw_begin1=KPeriod+Slowing;


draw_begin2=draw_begin1+DPeriod;


SetIndexDrawBegin(0,draw_begin1);


SetIndexDrawBegin(1,draw_begin2);

//----

return(0);


}

//+------------------------------------------------------------------+
//| Stochastic oscillator
|

//+------------------------------------------------------------------+
int start()

{


int
i,k;


int
counted_bars=IndicatorCounted();


double price;

//----

if(Bars<=draw_begin2) return(0);

//---- initial zero

if(counted_bars<1)


{


for(i=1;i<=draw_begin1;i++) MainBuffer[Bars-i]=0;


for(i=1;i<=draw_begin2;i++) SignalBuffer[Bars-i]=0;


}

//---- minimums counting

i=Bars-KPeriod;


if(counted_bars>KPeriod) i=Bars-counted_bars-1;


while(i>=0)


{


double min=1000000;


k=i+KPeriod-1;


while(k>=i)


{


price=Low[k];


if(min>price) min=price;


k--;


}


LowesBuffer=min;


i--;


}

//---- maximums counting

i=Bars-KPeriod;


if(counted_bars>KPeriod) i=Bars-counted_bars-1;


while(i>=0)


{


double max=-1000000;


k=i+KPeriod-1;


while(k>=i)


{


price=High[k];


if(max<price) max=price;


k--;


}


HighesBuffer=max;


i--;


}

//---- %K line

i=Bars-draw_begin1;


if(counted_bars>draw_begin1) i=Bars-counted_bars-1;


while(i>=0)


{


double sumlow=0.0;


double sumhigh=0.0;


for(k=(i+Slowing-1);k>=i;k--)


{


sumlow+=Close[k]-LowesBuffer[k];


sumhigh+=HighesBuffer[k]-LowesBuffer[k];


}


if(sumhigh==0.0) MainBuffer=100.0;


else MainBuffer=sumlow/sumhigh*100;


i--;


}

//---- last counted bar will be recounted

if(counted_bars>0) counted_bars--;


int limit=Bars-counted_bars;

//---- signal line is simple movimg average

for(i=0; i<limit; i++)


SignalBuffer=iMAOnArray(MainBuffer,Bars,DPeriod,0,MODE_SMA,i);

//----

return(0);


}

//+------------------------------------------------------------------+




欢迎光临 『外汇堂』·专业外汇论坛 (http://forex-town.com/) Powered by Discuz! X3.1