EA wont buy at specified price, please help..

 

Hi

I have modified an EA to suit my needs. This EA has to buy /sell at a specified price (in this case 134.42), the problem is I cant seem to get it to buy or sell t the specified price or even at all. Please note I am a novice coder, I have tired commenting out all other conditions to the buy sell prices are only tested, but still no trade:

extern double TakeProfit = 20;
extern double Lots = 0.1;
extern double TrailingStop = 10;
extern double StopLoss = 15;
extern double MATrendPeriodBlue=26;
extern double MATrendPeriodRed=13;

extern    int       RsiPeriod                       = 25;
extern    int       RsiBuyLevel                     = 68;
extern    int       RsiSellLevel                    = 32;

extern    int       BuyLevel                        = 134.42;
extern    int       SellLevel                       = 115.84;
extern    int       BarsToProcess                   = 12;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
  int BarsToProcess = 12;
   bool SignalSell = false, SignalBuy = false;
   double MacdCurrent[], SignalCurrent[];
   double MaRed[], MaBlue[];
   
   ArrayResize(MacdCurrent, BarsToProcess+3);
   ArrayResize(SignalCurrent, BarsToProcess+3);
   ArrayResize(MaRed, BarsToProcess+3);
   ArrayResize(MaBlue, BarsToProcess+3);

   int cnt, ticket, total;
   

   if(Bars<100)
     {
      Print("bars less than 100");
      return(0);  
     }
   if(TakeProfit<10)
     {
      Print("TakeProfit less than 10");
      return(0); 
     }
     
     double Rsi,Crossover;{
  
for(int i = BarsToProcess+2; i >= 0; i--){

   return(iRSI(Symbol(),Period(),RsiPeriod,0,i));
   
   if(MaRed[i] > MaBlue[i] && MacdCurrent[i] > SignalCurrent[i] && SignalCurrent[i] >= 0.1*Point) SignalBuy = true;
   if(MaRed[i] < MaBlue[i] && MacdCurrent[i] < SignalCurrent[i] && SignalCurrent[i] <= -0.1*Point) SignalSell = true;

}
  

   MacdCurrent[i]=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,i);

   SignalCurrent[i]=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,i);
   
   MaRed[i]=iMA(NULL,0,MATrendPeriodRed,0,MODE_EMA,PRICE_CLOSE,i);
   MaBlue[i]=iMA(NULL,0,MATrendPeriodBlue,0,MODE_EMA,PRICE_CLOSE,i);
}
double res;
NormalizeDouble(res,Digits);
   double SL;
   total=OrdersTotal();
   if(total<1) 
     {
      // no opened orders identified
     
      // check for long position (BUY) possibility
      if(MacdCurrent[i] > SignalCurrent[i] && SignalCurrent[i] >= 0.1*Point && Rsi <RsiBuyLevel &&
         MaRed[i] > MaBlue[i] && PRICE_CLOSE == BuyLevel  && SignalBuy == true)
        {
         SL = 0;
         if (StopLoss > 0) SL = Ask - StopLoss * Point;
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,SL,Ask+TakeProfit*Point,"macd sample",16384,0,Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
           }
         else Print("Error opening BUY order : ",GetLastError()); 
         return(0); 
        }
      // check for short position (SELL) possibility
       if(MacdCurrent[i] < SignalCurrent[i] && SignalCurrent[i] <= -0.1*Point && Rsi >RsiSellLevel &&
         MaRed[i] < MaBlue[i] && PRICE_CLOSE == SellLevel && SignalSell == true)
        {
         SL = 0;
         if (StopLoss > 0) SL = Bid + StopLoss * Point;
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"macd sample",16384,0,Red);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
           }
         else Print("Error opening SELL order : ",GetLastError()); 
         return(0); 
        }
      return(0);
     }
   // it is important to enter the market correctly, 
   // but it is more important to exit it correctly...   
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   // check for opened position 
         OrderSymbol()==Symbol())  // check for symbol
        {
         if(OrderType()==OP_BUY)   // long position is opened
           {
            // should it be closed?
            
            // check for trailing stop
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-Point*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
         else // go to short position
           {
           
            // check for trailing stop
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           }
        }
     }
    
   return(0);
  }

Thanks

Antony

 

what error u get ?

 

Hi

There is no error, it compiles fine but when the market reaches the desired buy or sell figure and all the other conditions are satisfied then there is just no position taken by the EA.

Thanks

Antony

 

The problem has to lie with the logic of

      if(MacdCurrent[i] > SignalCurrent[i] && SignalCurrent[i] >= 0.1*Point && Rsi <RsiBuyLevel &&
         MaRed[i] > MaBlue[i] && PRICE_CLOSE == BuyLevel  && SignalBuy == true)
        {

Add a comment/Print/Alert to check it is doing as you expect it to.

Alternatively break the if down so that you can see all conditions apply with

     if(MacdCurrent[i] > SignalCurrent[i] ) 
       {
       if (SignalCurrent[i] >= 0.1*Point) 
          {
          if (Rsi <RsiBuyLevel )
            {
            if (MaRed[i] > MaBlue[i] )
               {
               if( PRICE_CLOSE == BuyLevel )
                 {
                 if( SignalBuy == true)
                   {
                   }
                 }
               }
            }
          }
       }
Which is a way of debuging multiple condition if statements
 

2 problems at least:

if( PRICE_CLOSE == BuyLevel ) // is flawed as PRICE_CLOSE is not a price but enum

extern int BuyLevel = 134.42; // is flawed - assigning real/double value to int loses decimal places

 
  1. int start()
      {
    ...
         double Rsi,Crossover;{
    for(int i = BarsToProcess+2; i >= 0; i--){
       return(iRSI(Symbol(),Period(),RsiPeriod,0,i));
    
    Nothing below this line will be executed.
  2. On a 5 digit broker your TP= one pip, SL=1.5 pips. You must adjust TP, SL, and slippage
    //++++ These are adjusted for 5 digit brokers.
    double  pips2points,    // slippage  3 pips    3=points    30=points
            pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int     init(){
        if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    
    On an ECN broker you must open the order and THEN set the TP/SL
  3. if(MaRed[i] > MaBlue[i] && MacdCurrent[i] > SignalCurrent[i] && SignalCurrent[i] >= 0.1*Point) SignalBuy = true;
    
    SignalCurrent[] is set below the test. MACD is a number +/- 1 you don't want to multiply by Point
  4.    // but it is more important to exit it correctly...   
       for(cnt=0;cnt<total;cnt++)
         {
          OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
          if(OrderType()<=OP_SELL &&   // check for opened position 
             OrderSymbol()==Symbol())  // check for symbol
    
    OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
    
    Always count down when closing/deleting in the presence of multiple order (multiple charts.) Always test your return codes
        for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
            OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
        &&  OrderMagicNumber()  == magic.number             // my magic number
        &&  OrderSymbol()       == Symbol() ){              // and my pair.
    
    if (!OrderModify(...)
       Alert("OrderModify() failed: ",GetLastError());
    

  5. total=OrdersTotal();
       if(total<1) 
    
    This assumes only one chart and no manual orders, not good.
        for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
            OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
        &&  OrderMagicNumber()  == magic.number             // my magic number
        &&  OrderSymbol()       == Symbol() ){              // and my pair.
            // Have open order(s) ...
            return(0);
        }
        // No open orders(s)
        ...

Reason: