【MT4学习】MQL4语言基础语法11-自定义指标函数 [Custom Indicat...

0
回复
6254
查看
[复制链接]

63

主题

36

回帖

1915

积分

管理员

积分
1915
来源: 2019-7-30 20:03:07 显示全部楼层 |阅读模式


void IndicatorBuffers(int count)
设置自定义指标缓存数
:: 输入参数
count - 缓存数量
示例:
#propertyindicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Silver
//---- indicator parameters
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;
//---- indicator buffers
double ind_buffer1[];
double ind_buffer2[];
double ind_buffer3[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- 2 additional buffers are used for counting.
IndicatorBuffers(3);
//---- drawing settings
SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,3);
SetIndexDrawBegin(0,SignalSMA);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2);
//---- 3 indicator buffers mapping
SetIndexBuffer(0,ind_buffer1);
SetIndexBuffer(1,ind_buffer2);
SetIndexBuffer(2,ind_buffer3);
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("OsMA("+FastEMA+","+SlowEMA+","+SignalSMA+")");
//---- initialization done
return(0);
}


int IndicatorCounted()
返回缓存数量
示例:
intstart()
{
int limit;
int counted_bars=IndicatorCounted();
//---- check for possible errors
if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
//---- main loop
for(int i=0; i

{
//---- ma_shift set to 0 because SetIndexShift called abowe
ExtBlueBuffer=iMA(NULL,0,JawsPeriod,0,MODE_SMMA,PRICE_MEDIAN,i);
ExtRedBuffer=iMA(NULL,0,TeethPeriod,0,MODE_SMMA,PRICE_MEDIAN,i);
ExtLimeBuffer=iMA(NULL,0,LipsPeriod,0,MODE_SMMA,PRICE_MEDIAN,i);
}
//---- done
return(0);
}


void IndicatorDigits(int digits)
设置指标精确度
:: 输入参数
digits - 小数点后的小数位数
示例:
#propertyindicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Silver
//---- indicator parameters
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;
//---- indicator buffers
double ind_buffer1[];
double ind_buffer2[];
double ind_buffer3[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- 2 additional buffers are used for counting.
IndicatorBuffers(3);
//---- drawing settings
SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,3);
SetIndexDrawBegin(0,SignalSMA);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2);
//---- 3 indicator buffers mapping
SetIndexBuffer(0,ind_buffer1);
SetIndexBuffer(1,ind_buffer2);
SetIndexBuffer(2,ind_buffer3);
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("OsMA("+FastEMA+","+SlowEMA+","+SignalSMA+")");
//---- initialization done
return(0);
}


void IndicatorShortName(string name)
设置指标的简称
:: 输入参数
name - 新的简称
示例:
#propertyindicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Silver
//---- indicator parameters
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;
//---- indicator buffers
double ind_buffer1[];
double ind_buffer2[];
double ind_buffer3[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- 2 additional buffers are used for counting.
IndicatorBuffers(3);
//---- drawing settings
SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,3);
SetIndexDrawBegin(0,SignalSMA);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2);
//---- 3 indicator buffers mapping
SetIndexBuffer(0,ind_buffer1);
SetIndexBuffer(1,ind_buffer2);
SetIndexBuffer(2,ind_buffer3);
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("OsMA("+FastEMA+","+SlowEMA+","+SignalSMA+")");
//---- initialization done
return(0);
}


void SetIndexArrow( intindex, int code)
在指标上设置一个箭头符号
:: 输入参数
index - 第几根指标线0-7
code - 符号的编码,参照Wingdings 字体
示例:
SetIndexArrow(0,217);


bool SetIndexBuffer( intindex, double array[])
设置指标线的缓存数组
:: 输入参数
index - 第几根指标线0-7
array[] - 缓存的数组
示例:
doubleExtBufferSilver[];
int init()
{
SetIndexBuffer(0, ExtBufferSilver); // set buffer for first line
// ...
}


void SetIndexDrawBegin(int index, int begin)
设置划线的开始点
:: 输入参数
index - 第几根指标线0-7
begin - 划线的开始点
示例:
#propertyindicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Silver
//---- indicator parameters
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;
//---- indicator buffers
double ind_buffer1[];
double ind_buffer2[];
double ind_buffer3[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- 2 additional buffers are used for counting.
IndicatorBuffers(3);
//---- drawing settings
SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,3);
SetIndexDrawBegin(0,SignalSMA);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2);
//---- 3 indicator buffers mapping
SetIndexBuffer(0,ind_buffer1);
SetIndexBuffer(1,ind_buffer2);
SetIndexBuffer(2,ind_buffer3);
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("OsMA("+FastEMA+","+SlowEMA+","+SignalSMA+")");
//---- initialization done
return(0);
}


void SetIndexEmptyValue(int index, double value)
设置划线的空值,空值不划在和出现在数据窗口
:: 输入参数
index - 第几根指标线0-7
value - 新的空值
示例:
SetIndexEmptyValue(6,0.0001);


void SetIndexLabel( intindex, string text)
设置指标线的名称
:: 输入参数
index - 第几根指标线0-7
text - 线的名称,Null不会显示在数据窗口中
示例:
//+------------------------------------------------------------------+
//| Ichimoku Kinko Hyo initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,Tenkan_Buffer);
SetIndexDrawBegin(0,Tenkan-1);
SetIndexLabel(0,"Tenkan Sen");
//----
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,Kijun_Buffer);
SetIndexDrawBegin(1,Kijun-1);
SetIndexLabel(1,"Kijun Sen");
//----
a_begin=Kijun; if(a_begin SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_DOT);
SetIndexBuffer(2,SpanA_Buffer);
SetIndexDrawBegin(2,Kijun+a_begin-1);
SetIndexShift(2,Kijun);
//---- Up Kumo bounding line does not show in the DataWindow
SetIndexLabel(2,NULL);
SetIndexStyle(5,DRAW_LINE,STYLE_DOT);
SetIndexBuffer(5,SpanA2_Buffer);
SetIndexDrawBegin(5,Kijun+a_begin-1);
SetIndexShift(5,Kijun);
SetIndexLabel(5,"Senkou Span A");
//----
SetIndexStyle(3,DRAW_HISTOGRAM,STYLE_DOT);
SetIndexBuffer(3,SpanB_Buffer);
SetIndexDrawBegin(3,Kijun+Senkou-1);
SetIndexShift(3,Kijun);
//---- Down Kumo bounding line does not show in the DataWindow
SetIndexLabel(3,NULL);
//----
SetIndexStyle(6,DRAW_LINE,STYLE_DOT);
SetIndexBuffer(6,SpanB2_Buffer);
SetIndexDrawBegin(6,Kijun+Senkou-1);
SetIndexShift(6,Kijun);
SetIndexLabel(6,"Senkou Span B");
//----
SetIndexStyle(4,DRAW_LINE);
SetIndexBuffer(4,Chinkou_Buffer);
SetIndexShift(4,-Kijun);
SetIndexLabel(4,"Chinkou Span");
//----
return(0);
}


void SetIndexShift( intindex, int shift)
设置指标线的位移数
:: 输入参数
index - 第几根指标线0-7
shift - 位移多少
示例:
//+------------------------------------------------------------------+
//| Alligator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- line shifts when drawing
SetIndexShift(0,JawsShift);
SetIndexShift(1,TeethShift);
SetIndexShift(2,LipsShift);
//---- first positions skipped when drawing
SetIndexDrawBegin(0,JawsShift+JawsPeriod);
SetIndexDrawBegin(1,TeethShift+TeethPeriod);
SetIndexDrawBegin(2,LipsShift+LipsPeriod);
//---- 3 indicator buffers mapping
SetIndexBuffer(0,ExtBlueBuffer);
SetIndexBuffer(1,ExtRedBuffer);
SetIndexBuffer(2,ExtLimeBuffer);
//---- drawing settings
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_LINE);
SetIndexStyle(2,DRAW_LINE);
//---- index labels
SetIndexLabel(0,"Gator Jaws");
SetIndexLabel(1,"Gator Teeth");
SetIndexLabel(2,"Gator Lips");
//---- initialization done
return(0);
}


void SetIndexStyle( intindex, int type, int style=EMPTY, int width=EMPTY, color clr=CLR_NONE)
设置指标线的样式
:: 输入参数
index - 第几根指标线0-7
type - 线形状的种类,详见线条种类
style - 划线的样式
width - 显得宽度(1,2,3,4,5)
clr - 线的颜色
示例:
SetIndexStyle(3,DRAW_LINE, EMPTY, 2, Red);

日期时间函数[Date & Time Functions]datetime CurTime( )
返回当前时间
示例:
if(CurTime()-OrderOpenTime()<360)return(0);


int Day()
返回当前日期
示例:
if(Day()<5)return(0);


int DayOfWeek( )
返回当前日期是星期几 0-星期天,1,2,3,4,5,6
示例:
// donot work on holidays.
if(DayOfWeek()==0 || DayOfWeek()==6) return(0);


int DayOfYear( )
返回当前日期在年内的第几天
示例:
if(DayOfYear()==245)
return(true);


int Hour()
返回当前的小时数 0-23
示例:
boolis_siesta=false;
if(Hour()>=12 || Hour()<17)
is_siesta=true;


datetime LocalTime()
返回当前电脑时间
示例:
if(LocalTime()-OrderOpenTime()<360)return(0);


int Minute()
返回当前分钟
示例:
if(Minute()<=15)
return("first quarter");


int Month()
返回当前月份
示例:
if(Month()<=5)
return("first half of year");


int Seconds()
返回当前秒数
示例:
if(Seconds()<=15)
return(0);


int TimeDay( datetimedate)
返回输入日期中的日期
:: 输入参数
date - 输入日期
示例:
intday=TimeDay(D'2003.12.31');
// day is 31


int TimeDayOfWeek(datetime date)
返回输入日期中的日期是星期几(0-6)
:: 输入参数
date - 输入日期
示例:
intweekday=TimeDayOfWeek(D'2004.11.2');
// day is 2 - tuesday


int TimeDayOfYear(datetime date)
返回输入日期中的日期在当年中的第几天
:: 输入参数
date - 输入日期
示例:
intday=TimeDayOfYear(CurTime());


int TimeHour( datetimetime)
返回输入日期中的小时
:: 输入参数
date - 输入日期
示例:
inth=TimeHour(CurTime());


int TimeMinute( datetimetime)
返回输入日期中的分钟
:: 输入参数
date - 输入日期
示例:
intm=TimeMinute(CurTime());


int TimeMonth( datetimetime)
返回输入日期中的月份
:: 输入参数
date - 输入日期
示例:
intm=TimeMonth(CurTime());


int TimeSeconds(datetime time)
返回输入日期中的秒钟
:: 输入参数
date - 输入日期
示例:
intm=TimeSeconds(CurTime());


int TimeYear( datetimetime)
返回输入日期中的年份
:: 输入参数
date - 输入日期
示例:
inty=TimeYear(CurTime());


int TimeYear( datetimetime)
返回当前年份
示例:
//return if date before 1 May 2002
if(Year()==2002 && Month()<5)
return(0);


MT4编程课程.png

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 免费注册
关注微信