MQL5 PositionSelect(_Symbol) call freezes test
MQL5 PositionSelect(_Symbol) call freezes test
Loading saved threads...
Henry · External communityPost link
External question — Stack Overflow Stack Exchange
Author: Henry
Original post: https://stackoverflow.com/questions/76874135
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
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
Quote
Report
Henry · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: Henry
Original post: https://stackoverflow.com/a/76875132
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
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.
Quote
Report
Khizer imran · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: Khizer imran
Original post: https://stackoverflow.com/a/79021376
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
if (PositionSelect(i)) { //select this error
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External question — Stack Overflow Stack Exchange Author: Henry Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/questions/76874135 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. 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
Checking account access…