I only read through your code as far as this and stopped as it doesn't make sense.
if (movingAverage7Prev < movingAverage18Prev && movingAverage7 > movingAverage18) { movingAverageDif = movingAverage7 - movingAverage18; crossing = 1; } if (movingAverage7Prev < movingAverage18Prev && movingAverage7 > movingAverage18) { movingAverageDif = movingAverage18 - movingAverage7; crossing = -1; }
The 2 if conditions are exactly the same, but with different actions based on the condition.
GBen: I still don't fully understand why I need 4 Ma's (https://www.mql5.com/en/forum/139415)
- How do you define cross? Fast WAS below slow and now fast IS above slow? Isn't that 4 values (not 4 Ma's?) Fast previous, fast current, slow previous, and slow current.
- Like GumRai said same condition. Or learn to use booleans correctly
bool wasBelow = movingAverage7Prev < movingAverage18Prev, isBelow = movingAverage7 < movingAverage18, isCrossed = wasBelow != isBelow; crossing = movingAverage7 - movingAverage18; // +/- crossing should be renamed direction movingAverageDif = MathAbs(crossing);
- What is the initial value going into the while loop?
Alert("Close condition met."); while (!isClosed)
1. When looking at a chart with just 2 MA's, just see one cross the other but don't really think about the values. Thanks for the explanation, makes sense.
2. My dumb mistake. Fixed. Your code here also helped me figure out where I went wrong, thanks :) Picking up crosses and entering at what looks like the right spot!
3. Missed that line, isClosed was set to true, I copied out the code I thought relevant but I guess I missed that line.
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi guys,
I'm fairly new to mql4, but not programming. Heres the code I've tried so far but it's not working. What I'm trying to do is enter a trade when the 2 MA's are a certain distance apart, and then exit when they cross.
I've got a lot of this from the book and these forums. I still don't fully understand why I need 4 Ma's (https://www.mql5.com/en/forum/139415) as well, if someone could explain that? Cheers :)