EA will not enter or exit based on MA comparisons. Any help would be appreciated.

 

Hi I have what I thought was a simple routine but it has some issues.

#property copyright "Copyright 2019"
#property link      ""

// libraries included for coding routines
#include <Trade\Trade.mqh> 
//#include <MovingAverages.mqh>
#include <initmql4.mqh>

input double lowend = 0.25;
input double highend = 0.25;
input ENUM_TIMEFRAMES Aggregation1 = PERIOD_M1; 
input int buyperiod1=50;

input double buy_takeprofit= 6;
input double sell_takeprofit= 3;
input double buy_stoploss = 6;//set too 2
input double sell_stoploss = 6;

input double upperbandprofitplusbuy = 0.375;
input double lowerbandprofitplussell = 0.125;

input ENUM_MA_METHOD averageType = MODE_LWMA;

input bool close_on_ma_crossing = true;
input bool close_profit_in_pips = false;
input bool close_loss_in_pips = true;

input int contracts = 1;

input bool close_on_bar_end = false;
input int seconds_before_bar_end = 22;

input ENUM_TIMEFRAMES Aggregation = PERIOD_M1;

input bool daily_close = true;
input int close_hour = 20;
input int close_minute = 55;

input bool use_trailing = true;

input double sell_trailing_start = 5.0; 
input double sell_trailing_distance = 1.0; 
input double sell_trailing_step = 5.0; 

input double buy_trailing_start = 2.0; 
input double buy_trailing_distance = 8.0; 
input double buy_trailing_step = 7.0; 

input bool plot_lines = true;

// unique ID of trade
int EXPERT_MAGIC = 19812019;

// max acceptable slippage 
//int slippage = 3;


datetime last_bar = 0;
datetime last_Mbar = 0;

CTrade trade; 

double global_SL; // uses for "close_on_HL_and_stoploss" option

int cnt = 0; // counter for objects on chart

int last_position = 0;

//+------------------------------------------------------------------+
// Initialization of empty values on start
int OnInit() {
   last_bar = TimeCurrent();
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
}
//+------------------------------------------------------------------+
void OnTick() {

   if (use_trailing) {
      trailingStop();
   }
   if (daily_close) {
      closeDaily();
   }
   // chech closing in time
   if (close_on_bar_end)
   if (TimeCurrent() > iTime(Symbol(),Aggregation,0) + Aggregation*60 - seconds_before_bar_end) {
      closePosition(POSITION_TYPE_SELL);
      closePosition(POSITION_TYPE_BUY);
   }
   datetime current_bar = iTime(Symbol(),Aggregation,0);  
   // if new bar comes
   if (current_bar > last_bar) {
      bool golong = false;
      bool goshort = false;
      //closing conditons
      //3 consecutive negative slopes
      bool firstnegmaslope =(iMA_(Symbol(), Aggregation1, buyperiod1, 0, MODE_LWMA, PRICE_CLOSE, 3)>iMA_(Symbol(), Aggregation1, buyperiod1, 0, MODE_LWMA, PRICE_CLOSE, 4));    
      bool secondnegmaslope =(iMA_(Symbol(), Aggregation1, buyperiod1, 0, MODE_LWMA, PRICE_CLOSE, 2)>iMA_(Symbol(), Aggregation1, buyperiod1, 0, MODE_LWMA, PRICE_CLOSE, 3));    
      bool thirdnegmaslope =(iMA_(Symbol(), Aggregation1, buyperiod1, 0, MODE_LWMA, PRICE_CLOSE, 1)>iMA_(Symbol(), Aggregation1, buyperiod1, 0, MODE_LWMA, PRICE_CLOSE, 2));    
      //potentially something here for the bollinger bands
          
      //  New routine for trendcheck
 //10 candlesticks with MA increasing 
 if (iMA_(Symbol(), Aggregation1, buyperiod1, 0, MODE_LWMA, PRICE_CLOSE, 1)>iMA_(Symbol(), Aggregation1, buyperiod1, 0, MODE_LWMA, PRICE_CLOSE, 2))
 if (iMA_(Symbol(), Aggregation1, buyperiod1, 0, MODE_LWMA, PRICE_CLOSE, 2)>iMA_(Symbol(), Aggregation1, buyperiod1, 0, MODE_LWMA, PRICE_CLOSE, 3))
 if (iMA_(Symbol(), Aggregation1, buyperiod1, 0, MODE_LWMA, PRICE_CLOSE, 3)>iMA_(Symbol(), Aggregation1, buyperiod1, 0, MODE_LWMA, PRICE_CLOSE, 4))
 if (iMA_(Symbol(), Aggregation1, buyperiod1, 0, MODE_LWMA, PRICE_CLOSE, 4)>iMA_(Symbol(), Aggregation1, buyperiod1, 0, MODE_LWMA, PRICE_CLOSE, 5))
 if (iMA_(Symbol(), Aggregation1, buyperiod1, 0, MODE_LWMA, PRICE_CLOSE, 5)>iMA_(Symbol(), Aggregation1, buyperiod1, 0, MODE_LWMA, PRICE_CLOSE, 6))
 if (iMA_(Symbol(), Aggregation1, buyperiod1, 0, MODE_LWMA, PRICE_CLOSE, 6)>iMA_(Symbol(), Aggregation1, buyperiod1, 0, MODE_LWMA, PRICE_CLOSE, 7))
 if (iMA_(Symbol(), Aggregation1, buyperiod1, 0, MODE_LWMA, PRICE_CLOSE, 7)>iMA_(Symbol(), Aggregation1, buyperiod1, 0, MODE_LWMA, PRICE_CLOSE, 8))
 if (iMA_(Symbol(), Aggregation1, buyperiod1, 0, MODE_LWMA, PRICE_CLOSE, 8)>iMA_(Symbol(), Aggregation1, buyperiod1, 0, MODE_LWMA, PRICE_CLOSE, 9))
 if (iMA_(Symbol(), Aggregation1, buyperiod1, 0, MODE_LWMA, PRICE_CLOSE, 9)>iMA_(Symbol(), Aggregation1, buyperiod1, 0, MODE_LWMA, PRICE_CLOSE, 10)) 
 //middle bar of candlestick near 50MA, in range
 if (((iClose(Symbol(),Aggregation1,1)+iOpen(Symbol(),Aggregation1,1))/2>=iMA_(Symbol(), Aggregation1, 50, 0, MODE_LWMA, PRICE_CLOSE, 1)-0.25)&&(iClose(Symbol(),Aggregation1,1)+iOpen(Symbol(),Aggregation1,1))/2<=iMA_(Symbol(), Aggregation1, 50, 0, MODE_LWMA, PRICE_CLOSE, 1)+0.25)
 //5MA > 50MA
 if (iMA_(Symbol(), Aggregation1, 5, 0, MODE_LWMA, PRICE_CLOSE, 1)>iMA_(Symbol(), Aggregation1, 50, 0, MODE_LWMA, PRICE_CLOSE, 1))
 //positive 50MA slope
 if (iMA_(Symbol(), Aggregation1, 5, 0, MODE_LWMA, PRICE_CLOSE, 1)>iMA_(Symbol(), Aggregation1, 5, 0, MODE_LWMA, PRICE_CLOSE, 2))
 
 golong = true; 
        
      if (golong) {
         Alert("golong");
         PlaySound("stops.wav");
         //if (close_on_reverse) closePosition(POSITION_TYPE_SELL);
         openBuy();
      }
 //closing conditions 
       if ((iMA_(Symbol(), Aggregation1, 5, 0, MODE_LWMA, PRICE_CLOSE, 1)<iMA_(Symbol(), Aggregation1, 50, 0, MODE_LWMA, PRICE_CLOSE, 1)) && (iMA_(Symbol(), Aggregation1, 5, 0, MODE_LWMA, PRICE_CLOSE, 2)>=iMA_(Symbol(), Aggregation1, 50, 0, MODE_LWMA, PRICE_CLOSE, 2)) )   
            closePosition(POSITION_TYPE_BUY);
      
      // update time of bar and waiting when new opens
      last_bar = current_bar;
   } 
   return;

1) it is will not restrict entry into a trade based on the logic  that holds the price close to the 50 moving average:

if (((iClose(Symbol(),Aggregation1,1)+iOpen(Symbol(),Aggregation1,1))/2>=iMA_(Symbol(), Aggregation1, 50, 0, MODE_LWMA, PRICE_CLOSE, 1)-0.25)&&(iClose(Symbol(),Aggregation1,1)+iOpen(Symbol(),Aggregation1,1))/2<=iMA_(Symbol(), Aggregation1, 50, 0, MODE_LWMA, PRICE_CLOSE, 1)+0.25)
 

2.) It will not exit a trade based on the logic to exit when 5 MA crosses 50 MA

if ((iMA_(Symbol(), Aggregation1, 5, 0, MODE_LWMA, PRICE_CLOSE, 1)<iMA_(Symbol(), Aggregation1, 50, 0, MODE_LWMA, PRICE_CLOSE, 1)) && (iMA_(Symbol(), Aggregation1, 5, 0, MODE_LWMA, PRICE_CLOSE, 2)>=iMA_(Symbol(), Aggregation1, 50, 0, MODE_LWMA, PRICE_CLOSE, 2)) )  
   ) 


The more complete code is included.  Any idea what I am doing wrong?

 
Looks ok. The error might be somewhere else. Note that Aggregation*60 doesn't work for H1 and greater in MQL5, use PeriodSeconds.
Reason: