Open a pending order

 

Hi all,

I'm looking to put a pending order opposite to a buy already open. For that I though to make use if this code. Nevertheless is not working as expected.

Also I receive the error Invalid lots amount for OrderSend function

Could you show me the right way to do it ?

This is the code to get the LotSize, 

{
       lotmin = MarketInfo(Symbol(),MODE_MINLOT);
       lotmax = MarketInfo(Symbol(),MODE_MAXLOT);
       lotstep = MarketInfo(Symbol(),MODE_LOTSTEP);
             
       LotSize = MathFloor(LotSize/lotstep) * lotstep;
     
    if(LotSize < lotmin)
        LotSize = lotmin;
    if(LotSize > lotmax)
        LotSize = lotmax;             
     }  

 

This is the code to get the first order open

 if(BuyAllowed )    
          {//5  
                 RefreshRates();                               
       BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,Slippage*pips2points,0,0,"Initial Buy Order",MagicNumber,0,Green);                
       if(BuyTicket > -1)     
        {//6
          AddLimitsBuy();
          BuyAllowed = false;SellAllowed = true; 
          Print(" Initial Buy Order Placed - BuyTicket: ",BuyTicket," Open Buy Price: ",DoubleToStr(OrderOpenPrice(),Digits)," Spread: ",DoubleToStr(Spread,Digits));                                                                                                
        }//6

 

 Here is the code to get MLots,

 MLots = 0;
   
   for(int OrdersPool = OrdersTotal()-1; OrdersPool >= 0; OrdersPool--)
      {//11
      if(!OrderSelect(OrdersPool,SELECT_BY_POS,MODE_TRADES))continue;
         {//12
         if(OrderSymbol() == Symbol()&& OrderMagicNumber() == MagicNumber)
            {//13
            LastLot = OrderLots();
            }//13                                                 
          if(MLots <= LastLot)MLots = MathFloor((LastLot*Multiplier)/lotstep)* lotstep;       
         }//12      
      }//11 
   return(0);
  }//0 

Here is the code to get the pending order

void OpenOppositeOrder()
    {//0     
     int Op; 
    
     for(int Counter = OrdersTotal()-1; Counter >= 0; Counter--)     
      {//1
      if(!OrderSelect(Counter,SELECT_BY_POS,MODE_TRADES))continue;          
         {//2
         if(OrderSymbol() == Symbol()&& OrderMagicNumber() == MagicNumber)           
            {//3
             Op = OrderType();            
           
             if (Op == OP_BUY && SellAllowed)                           
               {//4                              
                  RefreshRates();                             
                  SellTicket = OrderSend(Symbol(), OP_SELLSTOP, MLots, Bid - OppositeDistance * pips2dbl, Slippage*pips2points, 0, 0, "Sell Opposite Order", MagicNumber, 0, Red);
                  if(SellTicket > -1)                   
                   {//5
                     AddLimitsSell();
                     BuyAllowed=true;SellAllowed=false;                                                                                
                     Print(" Opposite Sell Order Placed: ","Order Ticket: ",SellTicket," Spread: ",DoubleToStr(Spread,Digits)," Open Price: ",DoubleToStr(OrderOpenPrice(),Digits));                 
                  return;

Thank you in advance for help provided.

Luis 

 

@luisneves:

1> I don't see any lotsize specified. Make sure the lotsize is not zero.

2> Are you copy & pasting codes from the book?

-if you are, I strongly recommend that you don't.

-You need to understand why you're doing something.

 
ubzen:

@luisneves:

1> I don't see any lotsize specified. Make sure the lotsize is not zero.

2> Are you copy & pasting codes from the book?

-if you are, I strongly recommend that you don't.

-You need to understand why you're doing something.


Hi ubzen,

Thank you for your attention to my issue. 

Regarding your first observation are you say that Lotsize should be FixedLotSize as below ?

 

extern double FixedLotSize           = 0.01;

 and then,

LotSize = MathFloor(FixedLotSize/lotstep) * lotstep;

 Regarding your second observation, am not copy paste from the book, tol this moment I've received support from several people and going from there trial and error and so on.

Last from your third observation, this is the way that am seen the code; 

When an order open the code look for the orders that are from the ea making use of filters for symbol and magic number and last for order type.

If order type is a buy, then put an pending order below the open of the buy order  by OppositeDistance, which in this case is as, 

extern int OppositeDistance = 2;

Mlots is the last lot encountered by the  third block of code make use of filters for symbol and magic  number.

As  understood so far an pending order in this case an OP_SELLSTOP (only one) should open, but it hasn't. Of course that something is wrong and that's why I appreciate help here.

I'm learning from the results that I get from testing and then discussing that behaviour with people.

Best regards

Luis 

 

In that case make extensive use of the Print(), Alert() and Comment().

- Solve the problem with the error message you're getting first.

- Before your OrderSend(...) command for the hedge order. Print the OrderLots.

RefreshRates();
Print("MLots="+MLots);
                  SellTicket = OrderSend(...);
                  
 
ubzen:

In that case make extensive use of the Print(), Alert() and Comment().

- Solve the problem with the error message you're getting first.

- Before your OrderSend(...) command for the hedge order. Print the OrderLots.


Hi ubzen,

Thank you for time spent in support me.

So, as per your advise I've done the print and the result is as follow,

2013.04.09 12:53:35     2012.08.13 23:32  market_order_ecn_80_800_v7_v3 EURUSDi,M1:  Opposite Sell Order Placed: Order Ticket: 2 Spread: 1.00000 Open Price: 1.23323
2013.04.09 12:53:35     2012.08.13 23:32  market_order_ecn_80_800_v7_v3 EURUSDi,M1: modify #2 sell stop 0.03 EURUSDi at 1.23343 sl: 1.23843 tp: 1.22843 ok
2013.04.09 12:53:35     2012.08.13 23:32  market_order_ecn_80_800_v7_v3 EURUSDi,M1: open #2 sell stop 0.03 EURUSDi at 1.23323 ok
2013.04.09 12:53:35     2012.08.13 23:32  market_order_ecn_80_800_v7_v3 EURUSDi,M1: MLots=0.03000000
2013.04.09 12:53:19     2012.08.13 23:32  market_order_ecn_80_800_v7_v3 EURUSDi,M1:  Open Opposite Order - Bid: 1.23342 Open Price: 1.23342 Ticket: -1 Spread: 1.00000 Lots: 0 Stop Level: 0 Freeze Level: 0
2013.04.09 12:53:19     2012.08.13 23:32  market_order_ecn_80_800_v7_v3 EURUSDi,M1: Alert:  Open Opposite Order - Opposite Sell Failed: 4051: invalid function parameter value
2013.04.09 12:53:19     2012.08.13 23:32  stdlib EURUSDi,M1: loaded successfully

 In fact on tester I see the pending order (arrow), but seems that multiple orders are keeping getting open.

For sure that the reason could be seen from the log above, but I can't see where...

Luis 

 
luisneves:


Hi ubzen,

Thank you for time spent in support me.

So, as per your advise I've done the print and the result is as follow,

 In fact on tester I see the pending order (arrow), but seems that multiple orders are keeping getting open.

For sure that the reason could be seen from the log above, but I can't see where...

Luis 


Lots: 0
 

Hi RaptorUK,

Thank you for your point of "view". Definitely I'm in need for a pair of glasses.... 

Nevertheless I can't see how Lots (MLots) could be 0 if MLots come from the initial lots which was present in the first buy order and now is LastLot times multiplier...

And if I make use of OP_SELL instead of OP_SELLSTOP MlLots is ok. 

Any help, please.

Luis 

 

@luisneves: I strongly recommend going through the book chapter by chapter. As always, I don't recommend coding by trial and error.

1st> Your codes are hard to read because allot of the code is hidden.

2nd> I'm not asking you to paste your entire system here, however, that means you'll have to detect most of your errors and solve them.

3rd> The reason you could be getting Lots=0 is because of this:

MLots = 0;
_________
_________ 
return(0);

- You set MLots to Zero. And then you return Zero. Is that thing returning from a function? I cannot tell.

4> I recommend re-writing your codes in smaller functions because de-bugging like this will cost you allot of time.

 
luisneves:

Hi RaptorUK,

Thank you for your point of "view". Definitely I'm in need for a pair of glasses.... 

Nevertheless I can't see how Lots (MLots) could be 0 if MLots come from the initial lots which was present in the first buy order and now is LastLot times multiplier...

And if I make use of OP_SELL instead of OP_SELLSTOP MlLots is ok. 

Any help, please.

Luis 

You can't place an Order with a position size of zero,  even if you use OP_SELL.  Mlots was zero . . .  track it back to it's source,  add print debug messages along the way and find out why it was zero.  It can be a long laborious task but the pain of doing it help you avoid the need in the future.
 
@sapto: start your own thread and explain what failed the test. Don't hyjack other people threads.
Reason: