Order Not Closing Fast Enough

 

I wanted to test out a simple closing condition

if(accountbalance()<accountequity()) (then I use the close trade close after order select ect, I also used the for loop counting down)

I used a guaranteed calling condition, if(60==60) because I am calling the function which contains the if statement above.

My orders are closing but they are taking way too long to close sometimes more than 20 periods, during which my account equity was greater than the balance on 2 or three bars, I tried using orderprofit() instead but I got the exact same result.

I'm using this condition to minimize risk so it's ability to close a trade is not enough, I need it to close the trade as soon as possible (if certain conditions are not met, which is what the if(60==60) is for) .

I'll post a broken up section of the code.

for(int l=OrdersTotal()-1;l>=0;l--)

{

if(OrderSelect(l,SELECT_BY_POS,MODE_TRADES))

if(OrderMagicNumber()==MagicNumber)

if(OrderSymbol()==Symbol())

if(60==60)OrderEntry(4);

.

.

.

.

.

.

.

.

.

if(direction==4)

{

for(int O=OrdersTotal()-1;O>=0;O--)

{

if(OrderSelect(O,SELECT_BY_POS,MODE_TRADES))

if(OrderMagicNumber()==MagicNumber)

if(OrderSymbol()==Symbol())

if(OrderType()==OP_BUY)

{

if(AccountBalace()<AccountEquity())

OrderClose(OrderTicket(), OrderLots(), bid,3,Yellow);

}

else

{

if(OrderProfit()+OrderCommission()>0)

OrderClose(OrderTicket(), OrderLots(), ask,3,Yellow);

Print("Hi");

}

}

}

Help would be greatly appreciated

 

I just changed the code to if(OrderOpenPrice < bid) (for a buying order), it closed earlier and with profit.

Is there more lag associated with the earlier statement?

Also there was lag with this one too but I think in both cases the slippage may be to blame.

It's still not fast enough, might lag be the issue?

 
MetaNt:
Are you sure OrderClose() is called just 1 time?
Check also its return value.

A too old "preferred closing price" or too tight "slippage" may be the cause.
Maybe RefreshRates() can help you.

 

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. OrderClose(OrderTicket(), OrderLots(), bid,3,Yellow); 
    Is your close even being called? What are Function return values ? How do I use them ? - MQL4 forum
  3. Not adjusting slippage for 4/5 digit brokers.
 
if(direction==4)

How does the variable direction get the value 4?

 
It's actually called multiple times, which is an issue I have dealt with. My slippage is adjusted with (pips) a variable based upon an expression in the init function . I haven't had issues with stoploss values in ordersend and my ordersend used the same slippage as the orderclose function; in the end I managed to deal with the issue but found that I had simply made a static stop loss coded in a cumbersome way (I had already made it so that my lot size depended on my account balance). I have started using a different kind of stoploss based on price action, but I have issues with it that are getting in the way of an otherwise good EA. I will upload the code, and help really would be greatly appreciated.
 

So this code below will gives information to a void OrderEntry(int direction) function and the if(direction==3) is fulfilled, or at least that was is meant to happen, it is for a sell order and links to the order close for a sell order.

if(ask>=OrderOpenPrice()+NormalizeDouble((iHigh(Symbol(),0,1)-iOpen(Symbol(),0,1)),4) && HighPrice > OpenPrice)OrderEntry(3);

I'm getting stopped out way beyond the what is expected.

Hopefully from the picture it can be seen that the stop is way out, now I tried adding some pips to the NormalizeDouble((iHigh(Symbol(),0,1)-iOpen(Symbol(),0,1)),4) in order to avoid and orderclose error 138.

My attempt to avoid such an error has failed.

This is a semidynamic stoploss in that it does not change during the order like a trialing stop but is different for each order, I've constructed it so that my order criteria may become suited to market fluctuations.

 

Lol fixed it, once again I made my coding too cumbersome.

OrderSend(Symbol(),OP_BUY,LotSize,ask,5,ask-(Open[1]-Low[1]),.....

Seems to do the trick and so does the correctly adjusted sell version.

 
MetaNt:

Lol fixed it, once again I made my coding too cumbersome.

OrderSend(Symbol(),OP_BUY,LotSize,ask,5,ask-(Open[1]-Low[1]),.....

Seems to do the trick and so does the correctly adjusted sell version.

Please start using the SRC button . . . or do I have to start removing your code ? How to use the SRC button.

 
Sorry I'm new here, I saw people's posts with coded given in a similar fashion, and so I thought it was the correct process.
 
MetaNt: Sorry I'm new here,
And apparently hard of reading