trade triggers way above the cross-over point

 
I have a simple EA that triggers on a moving average crossover plus or minus a filter increment. For example, if the 7 day moving average crosses above the 33 moving average, I test to see if the crossover is above a threshold amount, such as 10 points. However, the trade always activates higher than this threshold amount, sometimes by doulbe the threshold amount. Why?
 
No mind readers here, you'll have to post the code.
 
WHRoeder wrote >>
No mind readers here, you'll have to post the code.

Here is the code. Thanks for your help.


extern int fast_moving_average = 7;
extern int slow_moving_average = 13;
extern int stop_loss_points = 100;
extern int profit_target = 100000;
extern double lot_size = 0.01;
extern double open_filter = 0.0006;/ here is where I initialize the threshhold amount
extern double close_filter = 0.0006;

int trading_enabled = 1;
int ticket = 0;
int test_for_crossover = 1;
int test_for_trade = 1;
int pips = 0;
int buy_enable = 1;


double amount_per_pip = 0.0;
double trade_profit = 0.0;
double fast = 0.0;
double slow = 0.0;
double lot_profit = 0.0;
double cross_over = 0.0;

int init()
{

return(0);
}

int deinit()
{

return(0);
}

int start()

/This block is invoked after trade closes
{
if (trading_enabled != 1)
{
amount_per_pip = 10 * lot_size;
pips = trade_profit/amount_per_pip;
lot_profit = 10 * pips;
Comment("Total profit ",trade_profit, " Profit for one lot ", lot_profit);
return(0);
}

/This block tests for cross-over of the moving averages. It saves the price at the crossover point in the variable cross_over.

if (test_for_crossover == 1)
{
RefreshRates();
fast = iMA(NULL,0,fast_moving_average,0,1,0,0);
slow = iMA(NULL,0,slow_moving_average,0,1,0,0);
if (fast > slow )
{
cross_over = fast;
test_for_crossover = 0;
return (0);
}
else
return (0);
}

/Here the program tests again if the faster moving average is higher than the slower moving average. That is, the faster moving average hasn't dipped below the slower moving average. If so, the above process is started over again to establish a new value for the variable cross_over. If the faster moving average continues to be higher than the slower moving average, then this block tests to see if the faster is higher than the slower by the threshold amount found in the variable open_filter. It is in this purple block I suspect where the problem lies, but I am not sure what it is.

if (test_for_trade == 1)
{
RefreshRates();
fast = iMA(NULL,0,fast_moving_average,0,1,0,0);
slow = iMA(NULL,0,slow_moving_average,0,1,0,0);
if (fast < slow)
{
test_for_crossover = 1;
cross_over = 0.0;
return (0);
}

/At this point the faster moving average is still higher than the slower moving average and the program tests to see if the higher moving average is greater than the slower moving average by the threshold amount found in the variable open_filter.
if (fast > cross_over + open_filter)
{
test_for_trade = 0;
return (0);
}
else
return (0);
}

if (buy_enable == 1)
{

ticket = OrderSend(Symbol(),OP_BUY,lot_size,Ask,3,Ask - stop_loss_points * Point,Ask + 1000 * Point,NULL,0,0,Green);
OrderSelect(ticket, SELECT_BY_TICKET);
trade_profit = OrderProfit();
Comment("Current profit bagged ", trade_profit);
buy_enable = 0;

}

RefreshRates();
fast = iMA(NULL,0,fast_moving_average,0,1,0,0);
slow = iMA(NULL,0,slow_moving_average,0,1,0,0);

if (fast < slow - close_filter)

{
while(OrdersTotal()>0)
{
if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
{
OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,Gray);
OrderSelect(ticket, SELECT_BY_TICKET);
trade_profit = OrderProfit();
trading_enabled = 0;
}
}
}


if (trade_profit > profit_target)

{
while(OrdersTotal()>0)
{
if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
{
OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,Gray);
OrderSelect(ticket, SELECT_BY_TICKET);
trade_profit = OrderProfit();
trading_enabled = 0;
}
}
}

return(0);
}