Download MetaTrader 5
To add comments, please log in or register
Publish your application in Cobe Base. Millions of traders will see it!
Laurentiu Sorin Tene
692
Laurentiu Sorin Tene 2014.05.08 16:15 

Hello,

Below is my simple code that generate 2 orders instead of one

//+------------------------------------------------------------------+
//|                                                         TEST.mq5 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
input double Lots               = 0.01;
input double StopLoss           = 25;
input double TakeProfit         = 0;
input double TrailingStop       = 2;
double price=0;
int OrdinTrimis=0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(PositionSelect(_Symbol)==false)
     {
      if(OrdinTrimis==0)
        {
         BuyPendingOrder1();
         Alert(OrdinTrimis);
        }
     }
  }
//+------------------------------------------------------------------+

void BuyPendingOrder1()
  {

//    double price = NormalizeDouble(Ask(), NDigits) - PriceOffset1*PipValue*Point();
   price=SymbolInfoDouble(Symbol(),SYMBOL_ASK)+0.00015;
   double SL=price-StopLoss*0.00001;
   if(StopLoss==0) SL=0;
   double TP=price+TakeProfit*0.00001;
   if(TakeProfit==0) TP=0;

//--- prepare the request
   MqlTradeRequest request;
   MqlTradeResult result;
   ZeroMemory(request);
   ZeroMemory(result);
   request.action=TRADE_ACTION_PENDING;         //  pending order
   request.magic=1;                  // Magic number
   request.symbol = Symbol();                      // Trading symbol
   request.volume = Lots;                          // volume in 0.1 lots
   request.sl = SL;                                // Stop Loss is not specified
   request.tp = TP;                                // Take Profit is not specified
   request.deviation=4;                         // deviation in 5 points
   request.price=price;
//--- form the order type
   request.type=ORDER_TYPE_BUY_STOP;           // order type ORDER_TYPE_BUY_LIMIT, ORDER_TYPE_SELL_LIMIT, ORDER_TYPE_BUY_STOP, ORDER_TYPE_SELL_STOP
//--- form the price for the pending order
   request.type_filling=ORDER_FILLING_RETURN;
//    request.type_time = ORDER_TIME_GTC;
   request.expiration=ORDER_TIME_GTC;
   OrderSend(request,result);
// check the result
   if(!OrderSend(request,result))
      Print("error OrderSend = ",__FUNCTION__,": ",result.comment," answer code ",result.retcode);//    }
   else
      OrdinTrimis=OrdinTrimis+1;
  }
//+------------------------------------------------------------------+

 As you can see in the picture below the OrderSend variable is 1, so the code should not be executed twice

 

 Any suggestions?

Rodrigo Malacarne
Moderator
9853
Rodrigo Malacarne 2014.05.08 17:12  
tenlau:

Hello,

Below is my simple code that generate 2 orders instead of one

 As you can see in the picture below the OrderSend variable is 1, so the code should not be executed twice

 

 Any suggestions?

Please take a look at this post.
Laurentiu Sorin Tene
692
Laurentiu Sorin Tene 2014.05.08 21:58  
Malacarne:
Please take a look at this post.

I read that post. Seem that it is not solved the issue and the Service Desk did not answer. Also there was a problem with detection of PositionSelect, on my case is about detection of the value of a variable OrdinTrimis. Seems to be a bug since the behaviour is the same in two different cases. In my case I test the code on a demo account about 30 times with the same result.

Pasi Hakamaki
4414
Pasi Hakamaki 2014.05.08 22:19  

You are sending the order twice.

   OrderSend(request,result);
// check the result
   if(!OrderSend(request,result))
      Print("error OrderSend = ",__FUNCTION__,": ",result.comment," answer code ",result.retcode);
Laurentiu Sorin Tene
692
Laurentiu Sorin Tene 2014.05.08 22:40  
Candles:

You are sending the order twice.

Sorry it is an if statement not an instruction. And as you see on my first post picture OrderTrimis variable is 1 and OrderSend should be done only one time according to my code
Pasi Hakamaki
4414
Pasi Hakamaki 2014.05.08 23:10  
tenlau:
Sorry it is an if statement not an instruction. And as you see on my first post picture OrderTrimis variable is 1 and OrderSend should be done only one time according to my code

The OrderSend() function is executed no matter if it is inside if statement or not. Thus you send the order twice. Try this and it will work:

//+------------------------------------------------------------------+
//|                                                         TEST.mq5 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
input double Lots               = 0.01;
input double StopLoss           = 25;
input double TakeProfit         = 0;
input double TrailingStop       = 2;
double price=0;
int OrdinTrimis=0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(PositionSelect(_Symbol)==false)
     {
      if(OrdinTrimis==0)
        {
         BuyPendingOrder1();
         Alert(OrdinTrimis);
        }
     }
  }
//+------------------------------------------------------------------+

void BuyPendingOrder1()
  {

//    double price = NormalizeDouble(Ask(), NDigits) - PriceOffset1*PipValue*Point();
   price=SymbolInfoDouble(Symbol(),SYMBOL_ASK)+0.0010;
   double SL=price-StopLoss*0.00001;
   if(StopLoss==0) SL=0;
   double TP=price+TakeProfit*0.00001;
   if(TakeProfit==0) TP=0;

//--- prepare the request
   MqlTradeRequest request;
   MqlTradeResult result;
   ZeroMemory(request);
   ZeroMemory(result);
   request.action=TRADE_ACTION_PENDING;         //  pending order
   request.magic=1;                  // Magic number
   request.symbol = Symbol();                      // Trading symbol
   request.volume = Lots;                          // volume in 0.1 lots
   request.sl = SL;                                // Stop Loss is not specified
   request.tp = TP;                                // Take Profit is not specified
   request.deviation=4;                         // deviation in 5 points
   request.price=price;
//--- form the order type
   request.type=ORDER_TYPE_BUY_STOP;           // order type ORDER_TYPE_BUY_LIMIT, ORDER_TYPE_SELL_LIMIT, ORDER_TYPE_BUY_STOP, ORDER_TYPE_SELL_STOP
//--- form the price for the pending order
   request.type_filling=ORDER_FILLING_RETURN;
//    request.type_time = ORDER_TIME_GTC;
   request.expiration=ORDER_TIME_GTC;
   bool res=OrderSend(request,result);
// check the result
   if(!res)
      Print("error OrderSend = ",__FUNCTION__,": ",result.comment," answer code ",result.retcode);//    }
   else
      OrdinTrimis=OrdinTrimis+1;
  }
//+------------------------------------------------------------------+


But note that:

In case of a successful basic check of structures (index checking) returns true. However, this is not a sign of successful execution of a trade operation.

Jinsong Zhang
15358
Jinsong Zhang 2014.05.09 07:46  
tenlau:
Sorry it is an if statement not an instruction. And as you see on my first post picture OrderTrimis variable is 1 and OrderSend should be done only one time according to my code
Candles is right.
Laurentiu Sorin Tene
692
Laurentiu Sorin Tene 2014.05.09 14:54  
Candles:

The OrderSend() function is executed no matter if it is inside if statement or not. Thus you send the order twice. Try this and it will work:


But note that:

In case of a successful basic check of structures (index checking) returns true. However, this is not a sign of successful execution of a trade operation.

Many thanks, it works ! Though I do not understand why checking if a function was executed makes to be executed again.
Pasi Hakamaki
4414
Pasi Hakamaki 2014.05.09 15:16  
tenlau:
Many thanks, it works ! Though I do not understand why checking if a function was executed makes to be executed again.

Good that it is working. Just remember that when you have function inside if statement what you actually got is just the return value of this function.

/
To add comments, please log in or register