英文:
MQL5 PositionSelect(_Symbol) call freezes test
问题
我希望有人经历过这个问题。我是MQL5的新手(来自pinescript),正在熟悉这门语言。
根据文档的指导,建议在调用PositionGetInteger()
之前调用PositionSelect()
,例如:
int openPositions(ENUM_POSITION_TYPE type) {
int count = 0;
//if (PositionSelect(_Symbol)) {
for (long i = 0; i < PositionsTotal(); i++) {
if (PositionGetInteger(POSITION_TYPE, i) == type) {
count++;
}
}
//}
return count;
}
如果我启用PositionSelect(_Symbol)
条件,MT5的回测会在放置了一些交易后“冻结”,不再执行任何操作。然后我必须点击停止按钮才能恢复功能。
没有这个检查条件,代码似乎可以正常工作,但文档建议使用该调用,我也看到其他教程中使用了它... 所以是怎么回事?
对这里发生的情况有什么想法吗?
谢谢
英文:
I'm hoping someone else has experienced this issue. I'm new to MQL5 (coming from pinescript) and finding my way around the language.
Following the guidelines of the documentation, it's recommended to call PositionSelect()
before calling PositionGetInteger()
, eg:
int openPositions(ENUM_POSITION_TYPE type) {
int count = 0;
//if (PositionSelect(_Symbol)) {
for (long i = 0; i < PositionsTotal(); i++) {
if (PositionGetInteger(POSITION_TYPE, i) == type) {
count++;
}
}
//}
return count;
}
If I enable that PositionSelect(_Symbol)
condition, the MT5 back test "freezes" and does nothing after placing a few trades. I then have to click on Stop to restore functionality.
The code seems to work fine without that check, but the docs recommend that call and I've seen other tutorials using it too... so what the heck?
Any ideas on what's going on here?
Thanks
答案1
得分: 0
从MQL5.com论坛获得帮助。答案是:
int openPositions(ENUM_POSITION_TYPE type) {
int count = 0;
for (int i = 0; i < PositionsTotal(); i++) {
if (!PositionGetTicket(i)) {
Print("错误:PositionGetTicket() 失败,错误代码:", GetLastError());
continue;
}
if (PositionGetInteger(POSITION_TYPE) == type)
count++;
}
return count;
}
关于这一点,文档有点令人困惑:PositionGetTicket() 和 PositionSelect() 都应该在 PositionGetInteger() 之前使用,但一个会冻结,另一个可以正常工作。
嗯,这就是情况。
英文:
Got help via the MQL5.com forum. The answer is:
int openPositions(ENUM_POSITION_TYPE type) {
int count = 0;
for (int i = 0; i < PositionsTotal(); i++) {
if (!PositionGetTicket(i)) {
Print("ERROR: PositionGetTicket() failed, error code: ", GetLastError());
continue;
}
if (PositionGetInteger(POSITION_TYPE) == type)
count++;
}
return count;
}
The docs are confusing in this regard: both PositionGetTicket() and PositionSelect() are supposed to be used before PositionGetInteger(), but one freezes, and the other works.
oh well.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论