anyone can help me with the logic? - page 3

 
Yes, tons of ways => for sure, but not the least answer to the question till now. Just curses from seasoned user to the first person who dare suggest a solution. charity begins at home. thanks for the tips, i have no problem with it as far as it is in a constructive manner. your suggestion is nice but all these functions don't exist, (? half a question). The last order in history is not necessarily the last closed order. But i would be glad to be wrong since i find it horribly complicate to implement. you are right about storing arrays in global variable, it is suitable for state variable and may be the very last closed order. "my efficiant manner", only targeted the state variable and when well coded it is use even in air craft and all sorts of real time system. It is far much more resiliant, i would never start an automat not defined in a determinist manner. => it is a fair definition of "bullt-proof" from my point of view. You can pass the state variable values by file also => for a state variable, why not sending it by email ? ;-) Don't be paranoid ubzen, i'm obnoxious when it is deserved.
 

your suggestion is nice but all these functions don't exist, (? half a question)

To the half which is a question, these functions do exist (imo), here for example. Allot of us have posted functions which select the last order in history multiple times. We've also part toke in discussions upon which circumstances such code may break. Now it's up to newer members to search for those posts.

The last order in history is not necessarily the last closed order.

And thats why you ask for the last closed order. With && OrderType()==.

for a state variable, why not sending it by email ?

Cute ;) I see where you're going with this. Don't worry I'm too un-obnoxious to play anonymous smart games. I just try to be humble online, thats all.

 
ubzen:

Allot of us have posted functions which select the last order in history multiple times.

One which I haven't seen for a while goes as follows:

   // Build a list of historic ticket numbers, plus something to order them by such as close-time
   int HistoricTradeCount = 0;
   double HistoricTrades[][2];
   ArrayResize(HistoricTrades, OrdersHistoryTotal());
   for (int i = 0; i < OrdersHistoryTotal(); i++) {
      if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {
         if (true) { // Replace with any desired conditions, e.g. OrderSymbol() == Symbol()
            HistoricTrades[HistoricTradeCount][0] = OrderCloseTime();
            HistoricTrades[HistoricTradeCount][1] = OrderTicket();
            HistoricTradeCount++;
         }
      } else {
         // WTF?
      }
   }
   
   if (HistoricTradeCount <= 0) {
      // No (matching) historic orders   

   } else {
      // The array was initially made large enough for all orders in the history, but we may
      // only be using a subset of that space if restrictions are added for only looking 
      // at particular orders. We need to trim the array down to the number of members actually used.
      ArrayResize(HistoricTrades, HistoricTradeCount);

      // Sort the array - which operates on [x][0] - and therefore puts it into descending order of close-time 
      ArraySort(HistoricTrades, WHOLE_ARRAY, 0, MODE_DESCEND);
      
      // Ticket of the most recent order is in HistoricTrades[0][1]...
      MessageBox("Most recent close: #" + HistoricTrades[0][1]);
   }
 
far much more interresting discussion. i'll dig it later, i have to make more tests, but i'm not sure the algo works if you were short, ubzen. the one from jjc is interesting to put in a script for a one-shot use and from a logic perspective. But for an EA, each time it is executed, the most part of a same history is loaded, parsed and sorted out, the memory is constently allocated, initialized, desallocated and so on and so on. It's just a kind remark, i strongly respect people who contribute constructively. For the sake of performance, i have the feeling that storing the last closed order in a global variable seems to be quite a decent solution. I guess lucif is get back to hell ? ;-)
 
ebal:
For the sake of performance, i have the feeling that storing the last closed order in a global variable seems to be quite a decent solution. I guess lucif is get back to hell ? ;-)
Well, yes, but doing a full scan via an array is probably quicker than you think. The processing time worsens exponentially as the history gets longer, but I've got it processing a history of 1000 orders in less than 1 millisecond [based on the average time taken for 1000 consecutive calls; any isolated operation will tend to be slower than this]. That shouldn't be too much of a burden given that this kind of check only needs to be done occasionally, and/or the result can be cached and only updated when OrdersHistoryTotal() changes.

( If you want something fun and related to consider... Everyone on this forum - including me - who says that you should close open positions by looping down from OrdersTotal() to 0 is, strictly speaking, wrong. It doesn't work universally. Oanda's proprietary bridge implements a very pedantic interpretation of the FIFO rules and requires that open orders are closed oldest-first. Therefore, looping from OrdersTotal() to zero doesn't work. Instead, you have to do something like building an array of the orders, sorting them by open time to make sure that they're in order, and then closing them using the array; e.g https://www.mql5.com/en/forum/123595 / https://www.mql5.com/en/forum/118657. )
 

but i'm not sure the algo works if you were short, It does. OP_SELL is a constant which equals 1. OP_BUY is a constant which equals 0.

OrderType()         <= OP_SELL // Avoid cr/bal forum.mql4.com/32363#325360

Check the Documents and the link provided above for a list of Constants and their numeric values.

But for an EA, each time it is executed, the most part of a same history is loaded, parsed and sorted out...

JJC's code is great for someone who wants to be on the safer side. This would be the case if MetaQuotes (or possibly the Broker) ever changes the default sort of the history pool. It provides a good example for sorting arrays. You could save this array to file and create your own backup copy of the trade history.

Currently I believe it goes from first 0 to last OrderHistoryTotal-1 according to OrderCloseTime. So when you do OrderHistoryTotal-1 you get Last-Orders-First. Buy you don't care about 'Balance' statement: OrderType()==6, 'Credit' statement: OrderType()==7 or Canceled Pending Orders. Therefore you tell it to filter those until you find the Last market order which was closed.

Anyways, should your broker start messing with your history or mt4 update and change the sort, you'd wish you'd been using an array and saving to file. But like I said, what you impliment depends on your skills and paranoia. JJC has the skills.

I guess lucif is get back to hell ..... I guess lucif don't care ;)))