My First Expert

 
Hello guys,

Thank you for the great site; I am trying to write my first expert; I would appreciate if someone can take a look at it and get me a feedback about it; its not that wow thing; I am just trying to get myself familiar with the proper structure for writing expert advisors; my expert should be taking a position long in positive cross between a fast and a slow MA's; and to take a short position in the case of a negative cross; when I back-tested this expert -also trying it live on a demo account- it wasnt taking any positions; only it took ONE short position in the back test process; and BTW the backtest I did was a proper one using a tick by tick analysis for a history data that I downloaded from Alpari's web site; and used the period converter script to do the math for generating the other periods; anyway; I would appreciate the insight you guys can post about my expert as I am excited to get more deeply involved wirting EA's, Thanks.

P.S. you can notice those two paranthesis "}}" at the very end of the code; I dont know why when I use only ONE it gets me an error!!!!!!!!


//---- input parameters
extern double     lotsize=1;
extern double     int_risk=30;
extern double     int_target=120;
extern int        Trailingstops=40;
extern int        fast_ma_period=5;
extern int        slow_ma_period=25;
 
double  fastmareading, slowmareading;
int longflag=100, shortflag=100;
 
int init()
  {
   fastmareading= iMA(NULL, 0, fast_ma_period, 0, 1, 0, 0);
   slowmareading= iMA(NULL, 0, slow_ma_period, 0, 0, 0, 0); 
   
   if(fastmareading<slowmareading)
     {
      longflag=1;
      shortflag=0;
      return(0);
     }
    else if(fastmareading>slowmareading)
      {
       shortflag=1;
       longflag=0;
       return(0);
      }
   
   if(Bars<100)
     {
      Comment("bars less than 100");
      return(0);  
     }
   if(int_target<10)
     {
      Comment("TakeProfit less than 10");
      return(0);  // check TakeProfit
     }
   return(0);
  }
 
// STARTING POINT
 
int start()
  {
   int   total,ticket,cnt=0;       
   
   Comment("Hazem Expert advisor is running","  Long Flag Value = ",longflag,"  Short Flag Value = ",shortflag);
      
   fastmareading= iMA(NULL, 0, fast_ma_period, 0, 1, 0, 0);
   slowmareading= iMA(NULL, 0, slow_ma_period, 0, 0, 0, 0);
   
      
   // CHECK FOR EMPTY TRADING TERMINAL
   
   total= OrdersTotal();
   
   if(total<1)
     {
      
      // CHECK FOR FREE MARGINE
      
      if(AccountFreeMargin()<(1000*lotsize))
        {
         Comment("Equity below margine requirements, current equity=", AccountFreeMargin());
         return(0);
        }
       
      // CHECK FOR LONG ENTRY AND TAKING IT IF POSSIBLE
   
      if(fastmareading>=slowmareading && longflag==1)
        {
         ticket= OrderSend(Symbol(), OP_BUY, lotsize, Ask, 4, Bid-int_risk*Point, Ask+int_target*Point, "Simple MA CrossOver", 1177, 0, Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
              { 
               Comment("LONG position taken @  ",OrderOpenPrice());
               longflag=0;
               shortflag=1;
              }
            else Print("Error opening BUY order : ",GetLastError()); 
            return(0); 
           }
        }
   // CHECK FOR SHORT ENTRY AND TAKING IT IF POSSIBLE
 
   if(fastmareading<=slowmareading && shortflag==1)
     {
      ticket= OrderSend(Symbol(), OP_SELL, lotsize, Bid, 4, Ask-int_risk*Point, Bid+int_target*Point, "Simple MA CrossOver", 1177, 0, Red);
      if(ticket>0)
        {
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
         Comment("SHORT position taken @  ",OrderOpenPrice());
         shortflag=0;
         longflag=1;
        }
      else Print("Error opening SELL order : ",GetLastError()); 
      return(0); 
     }
      
   // CHECK FOR ALREADY OPENED POSITIONS
   
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
        {
         if(OrderType()==0)
           {
            
            // LONG POSITION FOUND, CHECK FOR CLOSE OR TRAILING STOPS
            
            // CHECK FOR CLOSING
            
            if(fastmareading<=slowmareading)
              {
               OrderClose(OrderTicket(), lotsize, Bid, 4);
               return(0);
              }
            
            // CHECK FOR TRAILING STOPS
            
            if(Trailingstops>0)
              {
               if(Bid-OrderOpenPrice()>Point*Trailingstops)
                 {
                  if(OrderStopLoss()<Bid-Point*Trailingstops)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*Trailingstops, OrderTakeProfit(),0, Red);
                     return(0);
                    }
                 }   
              }
           }
         else
           {
            // SHORT POSITION FOUND, CHECK FOR CLOSE OR TRAILING STOPS
            
            // CHECK FOR CLOSING
            
            if(fastmareading>=slowmareading)
              {
               OrderClose(OrderTicket(), lotsize, Bid, 4);
               return(0);
              }
             
            // CHECK FOR TRAILING STOPS
               
            if(Trailingstops>0)  
              {
               if((OrderOpenPrice()-Ask)>(Point*Trailingstops))
                 {
                  if((OrderStopLoss()>(Ask+Point*Trailingstops)))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*Trailingstops,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           }
        }
     }   
   return(0);
  }}
 
the missing paranthesis is for
   if(total<1)
     {


so your code is checking for already opened positions only if there's no opened position...
 

so your code is checking for already opened positions only if there's no opened position...

Thanks for the response; so would that affect the decision making for taking a position on not by the expert?!? my specific problem is that the EA is NOT taking positions; according to the rules of taking an order there should be a reasonable number of trades to be taken; specially during a consolidating market; as the MA's are usually close to each other during those conditions; and also I've tracked the market conditions using the visual mode working on a history data and there were alot of times that the EA failed to take positions!! I would really appreciate if you can help me finidng whats wrong with this simple EA as its going to be the basis for me to improve my skills for this hobby of mine and I am sure of so many others,



Thanks again,