Moving average expert advisor

[Deleted]  

Where is the mistake, cause it didn't started.

[Deleted]  
//+------------------------------------------------------------------+
//|                                               Moving Average.mq4 |
//|                                            Copyright © 2011, BRO |
//+------------------------------------------------------------------+
#define BRO  20050610

extern double Lots               = 0.1;
extern double MovingPeriod       = 100;
extern double MinLot             = 0.1;
extern double MaxLot             = 5.0;
extern int LotPrecent            = 5;
extern bool DynamicLot           = false;
extern double LotStep            = 0.01;
extern double BalanceStep        = 500;

//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   int buys=0,sells=0;
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==BRO)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//---- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);
  }
//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
double GetLots()
  {
   double lot=Lots
   if (DynamicLot=true)
   {
    lots = NormalizeDouble(LotStep*AccountBalance()/BalanceStep, LotPrecent);  
   }
   lots = MathMax(lots, MinLot);
   lots = MathMin(lots, MaxLot);
   return (lots);
  }
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
   double ma;
   int    res;
//---- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//---- get Moving Average 
   ma=iMA(NULL,0,MovingPeriod,0,MODE_SMA,PRICE_CLOSE,0); 
//---- sell conditions
   if(Open[1]>ma && Close[1]<ma) continue;
   if(Open[2]<ma && Close[2]<ma);
     {
      res=OrderSend(Symbol(),OP_SELL, GetLots(),Bid,3,0,0,"",BROV,0,Red);
      return;
     }
//---- buy conditions
   if(Open[1]<ma && Close[1]>ma) continue;
   if(Open[2]>ma && Close[2]>ma);
     {
      res=OrderSend(Symbol(),OP_BUY,GetLots(),Ask,3,0,0,"",BRO,0,Blue);
      return;
     }
//----
  }
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
  {
   double ma;
//---- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//---- get Moving Average 
   ma=iMA(NULL,0,MovingPeriod,0,MODE_SMA,PRICE_CLOSE,0);
//----
   for(int pos=OrderTotal() -1; pos >=0; pos--)
     {
      if(OrderSelect(,SELECT_BY_POS,MODE_TRADES)==false)        break;
      if(OrderMagicNumber()==BRO || OrderSymbol()==Symbol()) continue;
      //---- check order type 
      if(OrderType()==OP_BUY)
        {
         if(Open[1]>ma && Close[1]<ma) continue;
         if(Open[2]<ma && Close[2]<ma);
         OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
         break;
        }
      if(OrderType()==OP_SELL)
        {
         if(Open[1]<ma && Close[1]>ma) continue;
         if(Open[2]>ma && Close[2]>ma); 
         OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
         break;
        }
     }
//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
  {
//---- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
   {
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else(CalculateCurrentOrders(Symbol())==0 CheckForClose();
   }
   return(0);
  }

 
you have to get the errors to find out what is going on
[Deleted]  

Lol at the amount of errors here. Like half the code was missing. The other half does some very weird stuff. Anyhow this is the error free version.

//|                                               Moving Average.mq4 |
//|                                            Copyright © 2011, BRO |
//+------------------------------------------------------------------+
#define BRO  20050610

extern double Lots               = 0.1;
extern double MovingPeriod       = 100;
extern double MinLot             = 0.1;
extern double MaxLot             = 5.0;
extern int LotPrecent            = 5;
extern bool DynamicLot           = false;
extern double LotStep            = 0.01;
extern double BalanceStep        = 500;

//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   int buys=0,sells=0;
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==BRO)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//---- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);
  }
//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
double GetLots()
  {
   double lot=Lots;
   
   if (DynamicLot==true)
   {
      lot = NormalizeDouble(LotStep*AccountBalance()/BalanceStep, LotPrecent);  
      lot = MathMax(lot, MinLot);
      lot = MathMin(lot, MaxLot);
   }

   return (lot);
  }
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
   double ma;
   int    res;
//---- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//---- get Moving Average 
   ma=iMA(NULL,0,MovingPeriod,0,MODE_SMA,PRICE_CLOSE,0); 
//---- sell conditions
   if(Open[1]>ma && Close[1]<ma) return;
   if(Open[2]<ma && Close[2]<ma)
     {
      res=OrderSend(Symbol(),OP_SELL, GetLots(),Bid,3,0,0,"",BRO,0,Red);
      return;
     }
//---- buy conditions
   if(Open[1]<ma && Close[1]>ma) return;
   if(Open[2]>ma && Close[2]>ma)
     {
      res=OrderSend(Symbol(),OP_BUY,GetLots(),Ask,3,0,0,"",BRO,0,Blue);
      return;
     }
//----
  }
  
  
  
  
  
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose(){
//---- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//---- get Moving Average 
   double ma=iMA(NULL,0,MovingPeriod,0,MODE_SMA,PRICE_CLOSE,0);
//----
   for(int pos=OrdersTotal() -1; pos >=0; pos--){
      if(OrderSelect(pos,SELECT_BY_POS,MODE_TRADES)==false)        break;
      if(OrderMagicNumber()==BRO || OrderSymbol()==Symbol()) continue;
      //---- check order type 
      if(OrderType()==OP_BUY){
         if(Open[1]>ma && Close[1]<ma) continue;
         if(Open[2]<ma && Close[2]<ma){
            OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
            break;
         }
      }
      if(OrderType()==OP_SELL){
         if(Open[1]<ma && Close[1]>ma) continue;
         if(Open[2]>ma && Close[2]>ma){
            OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
            break;
         }
      }
   }
}
//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
  {
//---- check for history and trading
   if(Bars<100 || !IsTradeAllowed()) return;
//---- calculate open orders by current symbol
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else if (CalculateCurrentOrders(Symbol())==0) CheckForClose();
   
   return(0);
}
[Deleted]  

What is missing ForexCoder.

EA based on simple moving average.

Buy conditions - when have 2 closing price above it, sell - when have 2 closing price below.

Lot size - percent of balance. Exmp. - 1000$x5%=50$ margin sum. No stop loss no limit - via ma.

Could you help me to do it.

Thx

[Deleted]  

I apologise, i thought you copy pasted this code from somebody. Didn't realize you were learning to code and coding your EA.

When I tested your code in my editor, it seemed like half of it were missing in places - unended if statements, for loops, functions that should be in loops (deduced by context) and weren't. That's why I also commented that something (as in some blocks of code) seemed missing.

Try it out, see if it works the way you planned it! :)