Open opposite order

 

Hi all,

 

Would like to get from you help to deal with this issue;

When an order is open and the price is under of the order open price I want to open an opposite order.

In this particular case  I'm try to use this code  but without success. Could anyone give a help here? 

Thanks in advance

Luis

double OpenOppositeOrder() 
 {
  int Op;  
 // for(int Counter=0;Counter<=OrdersTotal()-1;Counter++)
  for(int Counter=OrdersTotal()-1;Counter>=0;Counter--)
  {
   OrderSelect(Counter,SELECT_BY_POS,MODE_TRADES);
   {
    if(OrderSymbol()==Symbol()&& OrderMagicNumber()==MagicNumber)
    {
     Op=OrderType();
     if(Op==OP_BUY && NormalizeDouble(Bid,Digits)*UsePoint<OpenPrice)
     {      
     SellTicket = OrderSend(Symbol(),OP_SELL,LotSize,NormalizeDouble(Bid,Digits),UseSlippage,0,0,"Sell Order",MagicNumber,0,Red);
     if(SellTicket>0)AddLimitsSell();
      }
     if(Op==OP_SELL && NormalizeDouble(Ask,Digits)*UsePoint>OpenPrice)
     {      
     BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,NormalizeDouble(Ask,Digits),UseSlippage,0,0,"Buy Order",MagicNumber,0,Green);
     if(BuyTicket>0)AddLimitsBuy();
      }
     }
    }
   }
  }
 
luisneves:

Hi all,

 

Would like to get from you help to deal with this issue;

When an order is open and the price is under of the order open price I want to open an opposite order.

In this particular case  I'm try to use this code  but without success. Could anyone give a help here? 

Thanks in advance

Luis

Could you answer this question, it will help you a lot :

1. What is the return value of OrderSelect() - read the documentation of OrderSelect will help you answer this.

2. What error when you fail to send order ? 

 
luisneves:

Hi all,

 

Would like to get from you help to deal with this issue;

When an order is open and the price is under of the order open price I want to open an opposite order.

In this particular case  I'm try to use this code  but without success. Could anyone give a help here? 

What is the purpose of the variable called  UsePoint  ?  if you can explain why you are using it you will probably understand what your issue is . . .  also . . .  where does the value of  OpenPrice  come from ?

 
Don't you want to know if your OrderSend() works or not ?  and if it fails don't you want to know why ?  don't you want to know what the relevant variables were when it failed ?

Read this:  What are Function return values ? How do I use them ?

 

Hi RaptorUK;

Thank you for your prompt response;

UsePoint is to adjust for 5 decimal points so is Point*10 

OpenPrice is OrderOpenPrice() 

 

Thank you

Luis 

 
luisneves:

Hi RaptorUK;

Thank you for your prompt response;

UsePoint is to adjust for 5 decimal points so is Point*10 

Can you explain why you are using it in this situation ?

 

Hi RaptorUK,

You are right. I just need to use that for TakeProfit and Stoploss (I think...)

 
luisneves:

Hi RaptorUK,

You are right. I just need to use that for TakeProfit and Stoploss (I think...)

Thank you for answering and thinking about it :-)
 

Hi RaptorUK,

I've take a look on the link you provided and put on the code the error routine and the error that just returned says 2012.11.06 06:40  market order ecn v1 EURUSDi,M5: Order Send failed, error # 4051.

Then on the code I've done some changes on OrderSelect  and use Ticket, but things still not working. Also I done a search for the meaning and what I found is  ERR_INVALID_FUNCTION_PARAMVALUE (4051) and unfortunately I don't know

how to get from here... 

double OpenOppositeOrder() 
 {
  int Op;  
 // for(int Counter=0;Counter<=OrdersTotal()-1;Counter++)
  for(int Counter=OrdersTotal()-1;Counter>=0;Counter--)
  {
   //OrderSelect(Counter,SELECT_BY_POS,MODE_TRADES);
   OrderSelect(Ticket,SELECT_BY_TICKET);
   {
    if(OrderSymbol()==Symbol()&& OrderMagicNumber()==MagicNumber)
    {
     Op=OrderType();
     if(Op==OP_BUY && NormalizeDouble(Bid,Digits)<OpenPrice)
     {      
     Ticket = OrderSend(Symbol(),OP_SELL,LotSize,NormalizeDouble(Bid,Digits),UseSlippage,0,0,"Sell Order",MagicNumber,0,Red);
     if(Ticket>0)AddLimitsSell();
      } 
      if( Ticket > 0 )
   {
   Print("Order placed # ", Ticket);
   }
else
   {
   Print("Order Send failed, error # ", GetLastError() );
   }
     if(Op==OP_SELL && NormalizeDouble(Ask,Digits)>OpenPrice)
     {      
     Ticket = OrderSend(Symbol(),OP_BUY,LotSize,NormalizeDouble(Ask,Digits),UseSlippage,0,0,"Buy Order",MagicNumber,0,Green);
     if(Ticket>0)AddLimitsBuy();
      }
      if( Ticket > 0 )
   {
   Print("Order placed # ", Ticket);
   }
else
   {
   Print("Order Send failed, error # ", GetLastError() );
   }
      
      
     }
    }
   }
  }

 


 
luisneves:

Hi RaptorUK,

I've take a look on the link you provided and put on the code the error routine and the error that just returned says 2012.11.06 06:40  market order ecn v1 EURUSDi,M5: Order Send failed, error # 4051.

Then on the code I've done some changes on OrderSelect  and use Ticket, but things still not working. Also I done a search for the meaning and what I found is  ERR_INVALID_FUNCTION_PARAMVALUE (4051) and unfortunately I don't know

how to get from here... 

You need to check the values you are sending to the OrderSend() function . . .  are they the correct types ?  for example,  UseSlippage must be an int , it sounds like a bool . . .   is LotSize a double ?  is it in the correct range ?  greater than Min Lot and less than Max Lot and a multiple of Lot Step ?
 
RaptorUK:
You need to check the values you are sending to the OrderSend() function . . .  are they the correct types ?  for example,  UseSlippage must be an int , it sounds like a bool . . .   is LotSize a double ?  is it in the correct range ?  greater than Min Lot and less than Max Lot and a multiple of Lot Step ?


Hi RaptorUK,

Sorry to take your time, probably you start to get tired of me. So Slippage is an int and LotSize is a double....and things still bad....

I really still try hard to understand this but seems that my ignorance is taking advantage(...) 

For times seems that could be some issue with the Tester and then I try it on Demo. And here I can't have an opposite order opening if price comes under (for Buy) the OrderOpenPrice. 

Please, could you take a look in the code that is attached ?

I don't want soever that you code for me but just a little help.

thank you in advance for any help provided and fo your patience as well 

Luis 

Files:
 
luisneves:


Hi RaptorUK,

Sorry to take your time, probably you start to get tired of me. So Slippage is an int and LotSize is a double....and things still bad....

I really still try hard to understand this but seems that my ignorance is taking advantage(...) 

For times seems that could be some issue with the Tester and then I try it on Demo. And here I can't have an opposite order opening if price comes under (for Buy) the OrderOpenPrice. 

Please, could you take a look in the code that is attached ?

I don't want soever that you code for me but just a little help.

thank you in advance for any help provided and fo your patience as well 

Why did you change correct code to incorrect code ?

You loop was looping through all the orders that in the Trades Pool  (open and pending orders)  . . .  now this code makes no sense,  where does ticket come from on the first pass in the loop ?

 for(int Counter=OrdersTotal()-1; Counter>=0; Counter--)
  {
   //OrderSelect(Counter,SELECT_BY_POS,MODE_TRADES);
   OrderSelect( Ticket,  SELECT_BY_TICKET);

 
Another question for you . . . .  why is this Function type double when it doesn't return anything ?