设为首页收藏本站

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

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 4157|回复: 0
打印 上一主题 下一主题

请教版主和高手,指标有源码如何编程

[复制链接]
跳转到指定楼层
1#
发表于 2009-9-15 21:17:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
请教版主和高手,指标有源码如何编程
本人使用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);


}

//+------------------------------------------------------------------+
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|手机版|小黑屋|外汇堂·专业外汇论坛    

GMT+8, 2024-11-15 07:44 , Processed in 0.170435 second(s), 25 queries .

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表