How to run ordersend just once?

 

Hi. 

i'm trying to wrote a simple expert based on indicator buffers. but when indicator send a Sell or Buy signal , my expert make many buy or Sell Orders! while i want to Run the order just once , not dozens of times. also i want to close the past order when a new order runs. can anybody help me with that? thanks in advance if someone help me and edit the code. here is the code :


void start()

  {
   
   
   Modify();
  
  
  
  

   double up_arrow = iCustom(Symbol(), 0, "indicator", 0 , 0);

   double down_arrow = iCustom(Symbol(), 0, "indicator", 1 , 0);


     
      if(up_arrow != EMPTY_VALUE)

        {

            OrderSend(Symbol(), OP_BUY, Lot, Ask, 0, 0, 0, 0 , 0 , 0, Green);

        }
         
        if(down_arrow != EMPTY_VALUE)
         
        {

            OrderSend(Symbol(), OP_SELL, Lot, Bid, 0, 0, 0 , 0, 0 , 0, Red);

        }
         
         
    
}

 

1)try to get iCustom datas on every new bar instead of every tick

2)also get its datas from candle 1 instead of candle 0

-----------------------------------

use same magic number to close previous order and open a new one 

 
idealist72:

Hi. 

i'm trying to wrote a simple expert based on indicator buffers. but when indicator send a Sell or Buy signal , my expert make many buy or Sell Orders! while i want to Run the order just once , not dozens of times. also i want to close the past order when a new order runs. can anybody help me with that? thanks in advance if someone help me and edit the code. here is the code :


Please check your iCustom function, you are looking for an arrow each and every tick, where as it's advisable to look for arrow on new candle and you must be looking at candle 1 not candle 0. 


You need a control a function that counts number of open trades per Symbol() per OrderMagicNumber() 

Also a function that is capable to OrderClose() Ann open order. Based on Symbol() and OrderType() and OrderMagicNumber()
 

Looking at it another way, WHY is your code generating multiple orders?

Your start() function, which I think should be OnTick(), generates the code every tick, so there is nothing to stop orders being placed on every tick? 

It's unlikely you want orders placed on every tick, so, as others have suggested, create a function to identify if an order already exists for chart symbol. You might do this by looping through existing orders and seeing if the OrderSymbol() matches the chart symbol.

This function handily returns the ticket# in case you want to close the order. If it returns -1 there is no existing order and you can carry on.

int ticket(){
  for (int i=0;i<OrdersTotal();i++){
    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      if(OrderSymbol()==Symbol())return OrderTicket();
  }
 return -1;
 }

So, that should prevent multiple orders and the next step would be to run the code not every tick, but every x bars....if you do a search for "mql4 identifying new bar" that will get you going.....(50,000 hits on google!)

Basic Principles - Trading Operations - MetaTrader 5 Help
Basic Principles - Trading Operations - MetaTrader 5 Help
  • www.metatrader5.com
is an instruction given to a broker to buy or sell a financial instrument. There are two main types of orders: Market and Pending. In addition, there are special Take Profit and Stop Loss levels. is the commercial exchange (buying or selling) of a financial security. Buying is executed at the demand price (Ask), and Sell is performed at the...
Reason: