Creating a for loop in MQL4 that counts the bars

Creating a for loop in MQL4 that counts the bars

Manage alerts

Loading saved threads...

Ludwig · External communityPost link
External question — Stack Overflow Stack Exchange Author: Ludwig Original post: https://stackoverflow.com/questions/66735297 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. So I am trying to make a for loop which counts 4 bars. Every bearish bar is negative and every bullish bar is positive. So the sum of all 4 bars should give me a number that is greater, less, or equal to 0. This I need to see the current price movement. This information is needed to enter a trade. What I want is after a trade is closed, that the for loop starts counting from this bar onwards. Because the problem I run into is that the program starts counting 5 bars after the trade closed giving false signals. How can I make the program count forward from the new bar? for(int i = 5; i>0; i--){ if(Open[i]>Close[i]){ z = z - 1; } else if(Open[i]<Close[i]){ z = z + 1; } else{ z = z; } }
Quote
Report
PaulB · External communityPost link
External answer — Stack Overflow Stack Exchange Author: PaulB Original post: https://stackoverflow.com/a/67099012 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 you want to count back 5 bars from and including the current bar, then you would use the following code (note, there is no need for the z=z statement, it achieves nothing). You would have to make a function that returns the bar number for the last closed trade. if(lastTradeClosed()==0) { for(int i=0; i<=4; i++) { if(Open[i]>Close[i]) z--; else if(Open[i]<Close[i]) z++; } }
Quote
Report

Post Reply

Checking account access…