我有这个给我带来麻烦的小功能,一次只有部分功能会符合我的条件。
bool trend()
{
//is there a trend?
close1 = iClose(NULL,0,1); //vars
close2 = iClose(NULL,0,2);
close3 = iClose(NULL,0,3);
open1 = iOpen(NULL,0,1);
open2 = iOpen(NULL,0,2);
open3 = iOpen(NULL,0,3);
if(close3 > open3 && close2 > open2 && close1 > open1)
{
return(true); //uptrend
}
else if(close3 < open3 && close2 < open2 && close1 < open1)
{
return(false); //downtrend
}
else return(EMPTY_VALUE);
}
这是在 int start() 下调用函数的方式
trending = trend();
if (trending == true) Order = SIGNAL_BUY; // Rule to ENTER a Long trade
if (trending == false) Order = SIGNAL_SELL; // Rule to ENTER a Short trade
如上所述,我的卖出信号将根据条件起作用,但买入信号不遵循条件,我无法弄清楚它们是如何触发的。
如果我删除“else return(EMPTY_VALUE);”然后买单开始遵循条件,但卖单不再遵循条件。破损的卖单看起来就像破损的买单。
知道为什么我的函数会这样吗?谢谢!
最佳答案
您的函数声明为 bool trend()
,这意味着它可以返回 true
或 false
。在 else return(EMPTY_VALUE)
行中,常量 EMPTY_VALUE
(根据 MQL 文档其值为 0x7FFFFFFF
)隐式转换为 真
。这意味着如果没有上升趋势和下降趋势,您的函数将返回 true
(发出您的买入信号)。
如果您遗漏了最后一行 else return(EMPTY_VALUE)
,您将缺少 return 语句。如果您尝试访问函数的返回值,这会导致未定义的行为,您在 trending = trend()
行中执行此操作。
总结一下:您的问题是 trend
函数只能返回两个值之一,true
或 false
。但您需要的是一个返回三个值之一的函数 uptrend
、downtrend
、no_trend
。您可以使用这三个值声明一个枚举并相应地更改返回类型:
enum Trend {
UPTREND,
DOWNTREND,
NONE
}
Trend trend() {
// check if there is a trend
// [...]
if (close3 > open3 && close2 > open2 && close1 > open1) {
return UPTREND;
}
else if (close3 < open3 && close2 < open2 && close1 < open1) {
return DOWNTREND;
}
else {
return NONE;
}
}
后来
Trend trending = trend();
if (trending == UPTREND) Order = SIGNAL_BUY;
if (trending == DOWNTREND) Order = SIGNAL_SELL;
关于c++ - MQL4:只有一半的 if/else if 循环导致当前执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42756459/