validating certain entry points

 

I'm working on an EA and need help with validating the next entry point. For example, if the MAs cross then I want to enter an order with certain take profit and stop loss levels (I can accomplish that part just fine). However, I don't want to enter another order on the next MA cross unless the price has hit a certain level (say 100 pips above or below the first entry point). So I am working on some sort of code that can continually check the price and when it hits a certain point, then to report that or assign it a variable. I've tried writing a while loop, but the program keeps getting stuck in it. The problem is that even though the price may have reached above or below that level in the interim between MA crosses by the time the next MA cross actually occurs it may have fallen back within the level and therefore get stuck in the while loop. Does anyone have any other programming ideas to try and get around this in an easier fashion? Hopefully my question makes sense. Thanks.

 
There is no need for a while loop because the price changes on a each new tick and the EA is executed on each tick so just put the check where you want and it will be done on every price move. The fact that an MA will cross and uncross on the current bar is a different issue that's why it is common to make a decision on bar 1 the closed bar not bar 0.
 
I understand that it changes on very tick, but this is the scenario i need to figure out. In the last hour a cross occurred and a position is opened. My strategy would be to not change that position, close that position, or open another position until the price reached a certain level (say 100 pips above or below the entry price). In the current hour the price reached that threshold so now I want to change the position, close that position, or open another position at the next MA cross. Then say in two hours the MAs cross again (signaling me to change the position) but at that current time the price has fallen back within the original 100 pip range. Therefore, if i just did my price check then it would not be outside the 100 pip window and therefore wouldn't make the change. I'm trying to capture the price leaving the range and keep that as a variable or do something within the EA so that when the next cross came it would execute the parameters of the EA. However, I also don't want the second MA cross to come and induce a trade when the 100 pip level hasnt been broken by the original cross/entry point... is that a better explanation? I'm fairly new to the whole programming process, especially MQL4. I'm dealing with not the current MA crossing and uncrossing but a future cross that I'll call invalid based on my trading strategy. Thanks.
 

So create a Static Variable at the start of your program. Call it MaxDistMA.

Every tick, record the max movement from your trade entry price by doing....

if ( Ask > MaxDistMA ) MaxDistMA = Ask;

Then when you come to evaluate your next position (new trade at new cross etc) you can check if during the time elapsed the market moved your 100 points or not.

Then upon closing your trade, reset the MaxDistMA variable.

Does that help?

 
If you're talking about two MAs crossing just check the signs at bar start
DI = pDMId-nDMId;   
static double DI.prev=0;
if (DI*DI.prev <= 0) {              // Direction changed
    DI.prev = DI;
    //...
Reason: