Moving Average High/Low

 

Hi all,

I am trying to code a EA with two MAs; First will be a high and the second is a low, both with 20 periods. So the condition is when a candle crosses the upper MA it starts a buy order signal and this order closes when a candle crosses the lower MA. The same happens for a sell order; when a candle crosses the lower MA a sell order is issued and when an other candle crosses the upper MA it closes the order. It is a very simple. I've already coded it, but the part that contains the condition for the candle to cross the MA I think is not correct. It is the part shown below. It is actually not initiating any order. Can someone give me a tip?


//+------------------------------------------------------------------+
//| Performs system initialisation                                   |
//+------------------------------------------------------------------+
void InitSystem()
{
   Running = FALSE;

   PositionSize = Lots;

   RefreshRates();

   maHigh = iMA(Symbol(), 0, Period, 0, MODE_SMA, PRICE_HIGH, 0);
   maLow  = iMA(Symbol(), 0, Period, 0, MODE_SMA, PRICE_LOW, 0);
   
   //closePrice = iClose(Symbol(),0,0); // Close Price
   //openPrice  = iOpen(Symbol(),0,0); // Open Price
   
   if (( maHigh < Close[1] ) && ( maHigh > Open[1] ))
      {
         opBuy = TRUE; 
      }
      
   else if (( maLow > Close[1] ) && ( maLow < Open[1])) 
      {
         opSell = TRUE;
      } 

   //if (LastFast > LastSlow)
   //   Long = TRUE;

   Initialized = TRUE;
}

//+------------------------------------------------------------------+
//| Checks for entry to a trade - Exits previous trade also          |
//+------------------------------------------------------------------+
int CheckEntry(double Size)
{
   if (opSell) 
      {
      if (OrderNumber > 0)
       {
         CloseMarket(OrderNumber);  // Close previous short order
         opBuy = FALSE;
       }  
      OrderNumber = Order(Symbol(), OP_SELL, Bid, Size, 0, 0);
      
      //if (OrderNumber > 0) {
      //   Long = TRUE;
         //return(1);
      }
   
   else if (opBuy) {
      
       if (OrderNumber > 0)
       {
         CloseMarket(OrderNumber);  // Close previous short order
         opSell = FALSE;
       }  
      OrderNumber = Order(Symbol(), OP_BUY, Ask, Size, 0, 0);
      
      //if (OrderNumber > 0) {
      //   Long = FALSE;
        // return(1);
      //}
   }
   return(0);
}
 
Depends on how the functions are called. Where's your OnTick()?
 
Danilo Nascimento: ut the part that contains the condition for the candle to cross the MA I think is not correct.
   maHigh = iMA(Symbol(), 0, Period, 0, MODE_SMA, PRICE_HIGH, 0);
   maLow  = iMA(Symbol(), 0, Period, 0, MODE_SMA, PRICE_LOW, 0);
  1. a) Don't post code that won't even compile. Period is a function. b) Do you really want a SMA(1) on the M1 and a SMA(86000) on the D1?
  2. Assuming your InitSystem is called only by OnInit, those variables never update.