0
votes

I'm using this code to check if orders are existing for the price buy1 and sell1 in my code. Somehow some orders get executed twice. Shouldn't happen because i'm checking if there is any open order with the same takeprofit. Anyone able to see whats wrong?

bool CheckBuyOrder1(double buy1_tp){
for( int i = 0 ; i < OrdersTotal() - 1 ; i++ ) {
  OrderSelect( i, SELECT_BY_POS, MODE_TRADES );
  if( OrderTakeProfit() == buy1_tp && OrderComment() == "buy" ) 
return(true);
}
return(false);
}

bool CheckSellOrder1(double sell1_tp){
for( int i = 0 ; i < OrdersTotal() - 1 ; i++ ) {
  OrderSelect( i, SELECT_BY_POS, MODE_TRADES );
  if( OrderTakeProfit() == sell1_tp && OrderComment() == "sell" ) 
return(true);
}
return(false);
}

int totalOrders = OrdersTotal();

void OnTick()
{
if(totalOrders != OrdersTotal()){
double vbid    = MarketInfo("EURUSD",MODE_BID);
double bid = NormalizeDouble(vbid, 3);
double market_buy_tp = bid;
double buy1= bid + 0.002;
double buy1_tp= bid + 0.003;
if(CheckOpenOrders1(market_buy_tp)==false && CheckBuyOrder1(buy1_tp)==false){
int ticket9=OrderSend(Symbol(),OP_BUYSTOP,Lots,buy1,MaxSlippage,0,buy1_tp,"buy",16380,0,clrGreen);
}

double market_sell_tp = bid;
double sell1 = bid - 0.003;
double sell1_tp= bid - 0.004;
if(CheckOpenOrdersSell1(market_sell_tp)==false && CheckSellOrder1(sell1_tp)==false ){
int ticket19=OrderSend(Symbol(),OP_SELLSTOP,Lots,sell1,MaxSlippage,0,sell1_tp,"sell",16380,0,clrGreen);
}
totalOrders = OrdersTotal();
}}
1

1 Answers

0
votes

Whenever you try to compare an OrderTakeProfit() value with some other double - keep in mind rounding and float representation.

E.g. if you need to compare 0.10 with another double that you believe is 0.10 - you might be surprised that 0.10 is 0.09(9)6 or 0.10(0)4

That is why sometimes you might not find such an order.

double o = Point / 2;    // initialize in OnInit(), declare in global vars
...

bool CheckOrderBuy( double tp ){
   for ( int i = OrdersTotal() - 1; i >= 0; i-- ){
         if ( !OrderSelect( i, SELECT_BY_POS ) )continue;
         if (  fabs( OrderTakeProfit() - tp ) <  o
            &&       OrderType()              == OP_BUY
               ) return true;
   }
   return false;
}