Help needed - How can I check if ticket exists (running or pending) at specific price ?

 

Hello,

I would like to be able to check with my EA,
before opening a new pending order / ticket
if at that price / value another order, running
or pending exists.

Is there an MQL function that does this or
how else would I be able to do this ?

Thank you for your help on advance,
I will be looking forward for your replies!

 
FxTRance:

Hello,

I would like to be able to check with my EA,
before opening a new pending order / ticket
if at that price / value another order, running
or pending exists.

Is there an MQL function that does this or
how else would I be able to do this ?

Thank you for your help on advance,
I will be looking forward for your replies!

There is no function for this in MQL4. You can use a for-loop similar to this:

double trigger_price = Bid; //-- Modify this variable to your prefered method of detecting new order open price, ie price when your ea trigger new order.
bool   orderopen = 0;

for (int o=OrdersTotal(); o>=0; o--)
{
   OrderSelect(o,SELECT_BY_POS,MODE_TRADES);

   if (trigger_price == OrderOpenPrice())
   {
      orderopen = 1;
}  }

if (orderopen == 0)
{
   OrderSend(Symbol(),OP_BUY,0.1,Bid,3,0,0,Symbol(),0,0,Blue); //-- Put your OrderSend-function here.
}

/ McKeen

 

Dear McKeen,

thanx a million pips for your help,
it's indeed priceless, exactly what
I was looking for!

Cheers!

 
FxTRance:

Dear McKeen,

thanx a million pips for your help,
it's indeed priceless, exactly what
I was looking for!

Cheers!

You are very much welcome!

/ McKeen

 

You might want to add an allowance for slippage in your loop. The market order might not have been executed at the desired price.

double trigger_price_A = Bid+Slippage;
double trigger_price_B = Bid-Slippage;

Then change the if statement to this.

if (OrderOpenPrice()>trigger_price_B && OrderOpenPrice()<trigger_price_A)

Chris

 

Dear Chris,

Excellent point, the true is that the meaning of slippage is confusing me a little
but now I can say that I have a better understanding of what it is, your code is really
helping me with that. But I have some questions:

We define the slippage? And if we not how can I take the price of slippage for each order?
The pending orders have slippage?

I mean slippage (The difference between the expected price of a trade, and the price the
trade actually executes) occur due to volatility of market, right? So, we don't define slippage!
Is something that might be happen..

Thank you so much Chris,
Pips with us :D
Mary

PS. I can't speak so well, so excuse me for my English! Also, I am new in mql community
I count only 5 days and in Forex trading a month, so please be patient!! But I really want
to learn...I love the feeling of freedom that forex giving to us. At least we have a chance...
 
for (int o=OrdersTotal(); o>=0; o--)
{
   OrderSelect(o,SELECT_BY_POS,MODE_TRADES);

must be OrdersTotal()-1

Should always check return value from OrderSelect

What about same price, different pair?

    for(int pos = OrdersTotal() - 1; pos >= 0; pos--) if (
        OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
    &&  OrderMagicNumber() == magic.number              // my magic number
    &&  OrderSymbol()      == Symbol() ){               // and period and symbol
        if (MathAbs(trigger_price - OrderOpenPrice()) < 3*Point) ...
 

Many people when they create an EA use Slippage an an external integer.

When you use the OrderSend function one of the parameters is the maximum slippage, you can either set it to the variable Slippage, or you can set it to a number. E.g:

OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,0,0,"Comment",NULL,NULL,Green);
OrderSend(Symbol(),OP_BUY,LotSize,Ask,Slippage,0,0,"Comment",NULL,NULL,Green);

I have shown the slippage in red.

We do not need to know the exact value of the slippage of the order, because we know the maximum value of slippage and the minimum value, so we can use the if statement I mentioned in my other post to find out if the order price lies within the maximum and minimum slippage.

I think we can also simply the if statement to this:

if (trigger_price_A>OrderOpenPrice()>trigger_price_B)

Which I think means if OrderOpenPrice<trigger_price_A and >trigger_price_B.

Chris

Edit: The one WHRoeder said would be better, I forgot about that:

if (MathAbs(trigger_price - OrderOpenPrice()) < 3*Point) 

Where 3 is the slippage and MathAbs returns the absolute value. E.g. if trigger_price - OrderOpenPrice() is -2 it returns 2.

 
Thank you so much guys,

with your help I wrote my first EA :). I am backtesting now and I am waiting to open the market to try it in a demo account.

See you soon,
Mary