Need help on CloseOrder at SAR cross - page 2

 

Hy phy,

In the MACD EA, OrderSelect() is used with ticket, and this ticket is taken from the previous OrderSend command.

My issue is, I enter trade manually, so I do not have a EA-returned ticket. I only attach the EA to close the order. Moreover, I clearly understand the use of ticket in the OrderSelect() function, my question is understanding the meaning of the index & pool in the OrderSelect(index,SELECT_BY_POS,MODE_TRADES) command.

Cheers

 

for(int i = OrdersTotal()-1; i >= 0; i--){ /// loop through the list of all open trades and pending orders

OrderSelect(i, SEL_BY_POSITION, MODE_TRADES); // select the order at position "i" in the list. and the convention is to use MODE_TRADES for postions and pending orders

After an order is selected, you can find out everything else about it, price, time, stops, and so on, including Ticket number.

"pool" is the list of open trades / pending orders

 

Thanks phy, particularly for your patience. I hope not to cause you headache, however I'm more retarded than your answer... -:), I'm still confused by the basics:

- Is the "pool" the list of all open/ pending orders that appear chronologically in the Trade tab in Terminal window, or is it the list of open/ pending orders of a specific pair on a specific chart that the EA is launched on?

- Same problem with the OrdersTotal(): when launching the EA on a single chart of any pair, will the OrdersTotal() count all the open/ pending orders or just the open/ pending orders of the pair (the chart) that the EA is launched on?

- If it is the list of all open/ pending orders in the Trade tab (i.e of all pairs), then does index 0 refer to the order in the top line, 1 to the one in second line. .. and so on? Or index 0 to the one in last line, index 1 to the one in second to last... and so on?

- Again, if it is the list of all open / pending orders in the Trade tab (of all pairs) when the EA is launched on a single chart, will it be executed on all orders on all other charts? If so, this is dangerous since I may want to exit at SAR cross on EURUSD, but exit at 20-EMA cross at GBPUSD etc.

...

So far, with such level of confusion, from my inference I conclude that "pool" or OrdersTotal() will take into account only the chart/ the pair that the EA is launched on. If this is correct, then I have the following question:

- I can try with the loop through all the open orders on that single chart/ pair that the EA is launched, but say on a single chart at any time of launching the EA, I always have exactly 1 open order, is there any short cut to coding instead of using the for() above? E.g, does the OrderSelect(0,SEL_BY_POS, MODE_TRADES) work?

Would be really thankful to having your replies phy,

cheers

 

- Is the "pool" the list of all open/ pending orders that appear chronologically in the Trade tab in Terminal window, or is it the list of open/ pending orders of a specific pair on a specific chart that the EA is launched on?

----------- pool is all open/pending orders for your account

- Same problem with the OrdersTotal(): when launching the EA on a single chart of any pair, will the OrdersTotal() count all the open/ pending orders or just the open/ pending orders of the pair (the chart) that the EA is launched on?

----------- all open/pending orders for your account

- If it is the list of all open/ pending orders in the Trade tab (i.e of all pairs), then does index 0 refer to the order in the top line, 1 to the one in second line. .. and so on? Or index 0 to the one in last line, index 1 to the one in second to last... and so on?

---------- don't know how it is ordered specifically.

- Again, if it is the list of all open / pending orders in the Trade tab (of all pairs) when the EA is launched on a single chart, will it be executed on all orders on all other charts? If so, this is dangerous since I may want to exit at SAR cross on EURUSD, but exit at 20-EMA cross at GBPUSD etc.

---------- Orders don't know anything about charts. From one EA on one chart you can trade all pairs

...

So far, with such level of confusion, from my inference I conclude that "pool" or OrdersTotal() will take into account only the chart/ the pair that the EA is launched on. If this is correct, then I have the following question:

---------- not correct

- I can try with the loop through all the open orders on that single chart/ pair that the EA is launched, but say on a single chart at any time of launching the EA, I always have exactly 1 open order, is there any short cut to coding instead of using the for() above? E.g, does the OrderSelect(0,SEL_BY_POS, MODE_TRADES) work?

---------- should work, if there is only one open trade or pending order in your ACCOUNT

 

Hi phy,

finally I have come up with the following EA (God bless me!):

//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+

int start()
{
bool result1,result2;
double var1=iMA(NULL,0,20,0,MODE_EMA,PRICE_CLOSE,0);
double var2=iMA(NULL,0,20,0,MODE_EMA,PRICE_CLOSE,1);

//----
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
if(OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{
if((var2<Low[1] && var1>=Bid))
{
result1=OrderClose(OrderTicket(), OrderLots(), Bid, 5, CLR_NONE);
if(result1!=TRUE) Print("OrderClose error ", GetLastError());
}
}
else if(OrderType()==OP_SELL)
{
if((var2>High[1] && var1<=Bid))
{
result2=OrderClose(OrderTicket(), OrderLots(), Ask, 5, CLR_NONE);
if(result2!=TRUE) Print("OrderClose error ", GetLastError());
}
}
}
else Print("OrderSelect error ",GetLastError());
}
//----
return(0);
}
//+------------------------------------------------------------------+

Not sure if the "for -> if(OrderSelect..) -> If(OrderSymbol..)" used that way is correct? However, it did work!

I have a few more issues that need your wisdom -:)

- Given my case (I enter trades manually, then attach the above EA on every chart if there's at least one order open), should I do anything with the init() and deinit() functions? These two remain weird to me.

- After the above EA closes all orders on the chart it is attached, it returns the "OrderSelect error 0" on every tick. If I'm not there to manually remove the EA, how do I get rid of this message?

- Should this EA be fine for real trades? I mean, should I try to add other things like RefreshRates... etc?

Many thanks for your help, phy

Cheers

 

Change

for(int i=0;i<OrdersTotal();i++)
to
for(int i = OrdersTotal()-1; i >= 0; i--)

-----

init() is run before the code starts for the first time or is restarted
deinit() runs as the code is removed from the chart. See uninitialization codes

----

After the above EA closes all orders on the chart it is attached, it returns the "OrderSelect error 0" on every tick. If I'm not there to manually remove the EA, how do I get rid of this message?

Change

else Print("OrderSelect error ",GetLastError());
to something like
else{
int error = GetLastError();
if(error != 0) Print("OrderSelect error ", error);
}

----
Should this EA be fine for real trades? I mean, should I try to add other things like RefreshRates... etc

Try it out on demo, see what happens...

 

Hy phy,

Is error 0 an error after all? I observed with my code (before revising as per your instruction) that the "OrderSelect error 0" message appeared not only after all the orders are closed while the EA still runs, but it also appeared when there are open orders of one pair and you open another order of a different pair.

Ironically (to me) also, when no orders at all are open and I intentionally launch the EA on any chart, it returns no error message! I thought there should be some error with the OrderSelct, because there's no order at all to select. Am I wrong?

Then I changed the coding as your instruction. It worked just fine, one of the pair returned the 146 (trade thread is busy) error but still processed the exit command afterwards. The mq4 documentation says on error 146 "Retry only after the IsTradeContextBusy function has returned FALSE". Does this mean the EA automatically reprocess the exit command on next ticks when the trade context is no longer busy, or it means I have to include some "IsTradeContextBusy()==false" function in the EA?

Thanks a lot

cheers

 

Is error 0 an error after all? I observed with my code (before revising as per your instruction) that the "OrderSelect error 0" message appeared not only after all the orders are closed while the EA still runs, but it also appeared when there are open orders of one pair and you open another order of a different pair.

GetLastError() = 0 = no error

Ironically (to me) also, when no orders at all are open and I intentionally launch the EA on any chart, it returns no error message! I thought there should be some error with the OrderSelct, because there's no order at all to select. Am I wrong?

Your "for" loop does not execute with 0 orders

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

Then I changed the coding as your instruction. It worked just fine, one of the pair returned the 146 (trade thread is busy) error but still processed the exit command afterwards. The mq4 documentation says on error 146 "Retry only after the IsTradeContextBusy function has returned FALSE". Does this mean the EA automatically reprocess the exit command on next ticks when the trade context is no longer busy, or it means I have to include some "IsTradeContextBusy()==false" function in the EA?

Your code just notes the error and continues to the end. Next tick it runs again, if it is an Expert. It does not automatically retry within one tick run, although you can code it.

 

Hi phy,

Could you please explain what does the error 145 "order is too close to market" mean?

Thank you very much,

cheers

 

https://docs.mql4.com/trading/errors

Metaquotes gives the dealers nasty little tools to prevent you from setting orders where you want,
apparently they can also lock in a previous order when price gets close enough to it that it "might" be executed.

See also MarketInfo() MODE_STOPLEVEL and MODE_FREEZELEVEL

You are probably running into the "freeze level" setting at your friendly Dealer.

"Modifying has been denied since the order is too close to market and locked for possible soon execution.
The data can be refreshed after more than 15 seconds using the RefreshRates function, and a retry can be made."