4.2 参数和返回值

第 21 节 / 共 36 节 课程:Mt4编程语言
函数的参数是输入,返回值是输出。

多个参数

double MA(int period, int shift) {
double sum = 0;
for (int i=shift; i<shift+period; i++) sum += Close[i];
return sum / period;
}

默认参数

int CountBars(int period, int shift=0) {
int c=0;
for (int i=shift; i<shift+100; i++)
if (Close[i] > iMA(NULL,0,period,0,MODE_SMA,PRICE_CLOSE,i)) c++;
return c;
}
void OnTick() {
int c1 = CountBars(14); // shift=0
int c2 = CountBars(14, 5);
}

引用参数

void GetHL(int bars, double &high, double &low) {
high = High[Highest(NULL,0,MODE_HIGH,bars,1)];
low = Low[Lowest(NULL,0,MODE_LOW,bars,1)];
}
void OnTick() {
double h,l; GetHL(20, h, l);
Print("最高:",h," 最低:",l);
}