MQL4:只有一半的 if/else if 循环导致当前执行
MQL4: Only half of my if/else if loop leads to current execution
我有这个小功能给我带来了麻烦,一次只有部分功能会符合我的条件。
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()
,这意味着它可以 return true
或 false
。在 else return(EMPTY_VALUE)
行中,常量 EMPTY_VALUE
(根据 MQL 文档其值为 0x7FFFFFFF
)隐式转换为 true
。这意味着如果没有上升趋势和下降趋势,您的函数将 return true
(发出您的买入信号)。
如果您遗漏了最后一行 else return(EMPTY_VALUE)
,您将缺少一个 return 语句。如果您尝试访问函数的 return 值,这会导致未定义的行为,您在 trending = trend()
.
行中这样做
总结一下:您的问题是 trend
函数只能 return 两个值之一,true
或 false
。但是您需要的是 return 三个值 uptrend
、downtrend
、no_trend
之一的函数。您可以使用这三个值声明一个枚举并相应地更改 return 类型:
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;
我有这个小功能给我带来了麻烦,一次只有部分功能会符合我的条件。
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()
,这意味着它可以 return true
或 false
。在 else return(EMPTY_VALUE)
行中,常量 EMPTY_VALUE
(根据 MQL 文档其值为 0x7FFFFFFF
)隐式转换为 true
。这意味着如果没有上升趋势和下降趋势,您的函数将 return true
(发出您的买入信号)。
如果您遗漏了最后一行 else return(EMPTY_VALUE)
,您将缺少一个 return 语句。如果您尝试访问函数的 return 值,这会导致未定义的行为,您在 trending = trend()
.
总结一下:您的问题是 trend
函数只能 return 两个值之一,true
或 false
。但是您需要的是 return 三个值 uptrend
、downtrend
、no_trend
之一的函数。您可以使用这三个值声明一个枚举并相应地更改 return 类型:
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;