How put each new order in no repetitive price with previous ones?

 

Hi.

In first place, congratulations for Meta Quotes team for excellent work!

I need to put each new order in non-repetitive price with previous ones. I can do easy with last order using "OrderOpenPrice()", but not with several orders.

I want each new order to be in a price at least x pips distant from any previous price of open orders and pending orders. But there is no problem if it is to the same price (or minus of x pips) of orders that were already closed.

Thank you.

 
You need to loop on all orders in the trade pool, select them using OrderSelect() and verify that their OrderOpenPrice() is far enough from your target price.
 
Thank you, but i never used it. Please, can you explain anymore?
 
Can put an example?
 

I'll explain the basics... There are 2 'order pools' that are held server-side and are accessible via code (as long as the Terminal is connected the pools will be updated by the server):

  1. Trade pool - contains all open positions and pending orders.
  2. History pool - contains all closed positions as well as balance and credit statements (note that the accessible part of the history pool relies on user preferences -> https://www.mql5.com/en/forum/123269).


For each and every order in the trade/history pool, any one of the following properties can be read:

To do this the order must be selected first via the function OrderSelect(). Orders can be selected via their index in the pools or via their ticket number (if it is known).


One of the most common processing tasks in MQL4 is to loop on all orders (in one of the pools), select each one by index, read whatever properties are needed and do whatever processing needs to be done with those properties. The basic structure of this loop looks like this:

int total = OrdersTotal();                 // or OrdersHistoryTotal() if the history pool is the target
for (int i=0; i<OrdersTotal(); i++) {      // note that in certain cases we should loop backwards
   if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;  // or MODE_HISTORY if the history pool is the target
   
   // if (OrderMagicNumber()==MAGIC) {...    // these are the 3 most common criterions for filtering the orders we are interested in 
   // if (OrderSymbol()==SYMBOL) {...
   // if (OrderType()==TYPE) {...
   
   // read whatever information is needed and do whatever processing needs to be done
}

In your case you would filter by order type, read the order's open price and calculate how far it is from the price u r interested in.

 
gordon:

I'll explain the basics... There are 2 'order pools' that are held server-side and are accessible via code (as long as the Terminal is connected the pools will be updated by the server):

  1. Trade pool - contains all open positions and pending orders.
  2. History pool - contains all closed positions as well as balance and credit statements (note that the accessible part of the history pool relies on user preferences -> https://www.mql5.com/en/forum/123269).

For each and every order in the trade/history pool, any one of the following can be read:

To do this the order must be selected first via the function OrderSelect(). Orders can be selected via their index in the pools or via their ticket number (if it is known).


One of the most common processing tasks in MQL4 is to loop on all orders (in one of the pools), select each one by index, read whatever properties are needed and do whatever processing needs to be done with them. The basic structure of this loop looks like this:

In your case you would filter by order type, read the order's open price and calculate how far it is from the price u r interested in.

Dear Gordon

In your answer to "Help needed" you say "You should loop down and not up in your order loop", yet here you are looping up.

Shouldn't it be...

for (int i<OrdersTotal(); i>=0; i--) {

? We love you, anyway,

Regards, Helmut

 
engcomp:

Dear Gordon

In your answer to "Help needed" you say "You should loop down and not up in your order loop", yet here you are looping up.

Shouldn't it be...

? We love you, anyway,

Regards, Helmut

May I eat humble pie?

It should be...

for (int i=OrdersTotal()-1;, i>=0; i++) {
 
engcomp:

May I eat humble pie?

It should be...

Take it all back

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

I shouldn't have opened my mouth. Sorry, Gordon

 
engcomp:

Take it all back

I shouldn't have opened my mouth. Sorry, Gordon


This stuff drives you nuts sometimes doesn't it...:)

Both are correct depending on what you are doing (as Gordon comments in the code).... if you intend to close the order then you need to loop backwards. This is because when an order is closed, it affects the indexing of the order pool. Looping up and closing means all the orders with higher indexes and yet to be checked have there index reduced (while your loop count gets increased). You will therefore miss an order. Looping down, all the indexes with a higher number (that get reduced) have already been checked so you won't miss any. If you are not closing, then whether you loop up or down doesn't matter

 

I specifically commented:

for (int i=0; i<OrdersTotal(); i++) {      // note that in certain cases we should loop backwards

Those certain cases are when the loop contains code that affects the indexes themselves, specifically if positions are being closed in the loop...


p.s. engcomp, you can edit your posts... No reason to post those 2 corrections.

 
Thank you very much for your so kind attention and detailed explanation. :-)
Reason: