Download MetaTrader 5
To add comments, please log in or register
Have you installed MetaTrader 5? Find out more about it!
DeanDeV
330
DeanDeV 2015.03.28 12:00 

Hi,

I have a MA EA which should only place a single buy/sell order on a cross over. Any way to stop it from placing 2, sometimes 3, on a single crossover?

void OrderEntry(int direction)
   {
   if((Hour()>=StartTimeMA && Hour()<=FinishTimeMA))  //Preferred trading hours
      {
      if(direction==0)
         {
         double MABSL = Ask-(MAStopLoss*Point);
         double MABTP = Ask+(MATakeProfit*Point);

         int buyticket = OrderSend(Symbol(),OP_BUY,MALotSize,Ask,5,0,0,"MA Buy Order",MAMagicNumber,0,Aqua);     //placing order with ecn broker
                     if(buyticket>0)OrderModify(buyticket,OrderOpenPrice(),MABSL,MABTP,0,CLR_NONE);
         }

      if(direction==1)
         {
         double MASSL = Bid+(MAStopLoss*Point);
         double MASTP = Bid-(MATakeProfit*Point);

         int sellticket = OrderSend(Symbol(),OP_SELL,MALotSize,Bid,5,0,0,"MA Sell Order",MAMagicNumber,0,Aqua);
                     if(sellticket>0)OrderModify(sellticket,OrderOpenPrice(),MASSL,MASTP,0,CLR_NONE); 
         }
       }
   }
whroeder1
13610
whroeder1 2015.03.28 12:20  
DeanDeV: Any way to stop it from placing 2, sometimes 3, on a single crossover?
      if(direction==0)
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Look for open orders and which bar the order was opened on. Or don't look for a signal, look for a change in signal:
    static int direction = -1;
    int dirPrev = direction;
    direction = ...;
    if(dirPrev != direction) OrderEntry(direction);
  3. Don't use int 0 and 1 when you mean bool. Don't use int 0 and 1 when you mean OP_BUY and OP_SELL.
  4. Check your return codes (OrderSend and OrderModify) What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  5. if(buyticket>0)OrderModify(buyticket,OrderOpenPrice(),MABSL,MABTP,0,CLR_NONE);
    You can not use any Trade Functions - MQL4 Documentation unless you select the order first.
DeanDeV
330
DeanDeV 2015.03.28 13:39  
I used this:
int ThisBarTrade           =  0;

if (Bars != ThisBarTrade ) 
   {
   ThisBarTrade = Bars; 
   

   }
Seems to be working perfectly now. Thanks!
/
To add comments, please log in or register