How to check if last three orders were in loss? - page 2

 
nicholishen:

2. It should be implicit that Sort take an integer param to select a sorting mode. Haven't you worked with lists in MQL?

3. You have to add the objects to the list before you can sort them, again, haven't you worked with lists in MQL?

It's this mandatory ?

No I don't work with any code of the not so "standard" library. I study it, I know how to use it (don't worry about me), and I learnt the good ideas when there are. But I am using my own code to manage list, when I need list.

By the way you still didn't answer the question (2 AND 3).

Why are you answering to question by questions ?

Ok forget question 2, it's not so important, I can check the documentation (hopefully there is an answer) if you don't know.

About 3, let me ask it an other way :  Is it really not possible to use only 1 loop ? It's faster, don't you think ?

4. Orderselect would only return false if you fed in an invalid ticket. Like I said, bigger issues at play.

Invalid ticket, even when you select by pos ? (as you also stop in this case).

If I have an invalid ticket, why stopping ? Are you assuming there is a bug in your code or what ? Or maybe you mean a ticket could never be invalid using your code ?

It seems to me you could miss a "match" (3 loss in a row on a given symbol) if you quit the loop as soon as OrderSelect() is false. Or maybe you are right to quit, but then a message error should be logged, not ?

Are you really believing all what you read without checking ?
 
nicholishen:

When did you sort them??

I didn't sort them and I never said I did.

Ok, Alain, my specification is I want to know the sum of the profit on the last three orders sorted by close time not open time. What do you do? Go.

Happy you said it...going to bed, it's 4:30 AM here. I had fun, thanks. 
 
Alain Verleyen:

I didn't sort them and I never said I did.

Happy you said it...going to bed, it's 4:30 AM here. I had fun, thanks. 

No, that is a saying in English that means your turn. Show me how you'd do it since my way is wrong.


Back to the other post, you can't use just one loop. First you need a loop to add all the orders to the list. Next you sort the list by your criteria. Finally you loop through your sorted orders for analysis. This is the most efficient way to work with the history pool when you need to programmatically sort the pool. 

 
Alain Verleyen: Are you really believing all what you read without checking ?

What in the links provided do you beleave is false? You keep making statements without facts.

 
whroeder1:

What in the links provided do you beleave is false? You keep making statements without facts.

Where do you read I believe something is false ? I asked nicholishen to explain this statement "Mql history pool is generally sorted by open time, but not always guaranteed." He obviously can't explain it, just repeating what you wrote. It could be right or wrong, doesn't matter, he just repeated it.

By the way, you should better quote the documentation than links to user experience  :

Consecutive selection of orders using the SELECT_BY_POS parameter returns information in the sequence in which it was received from the trading server. Sorting of the resulting list of orders cannot be guaranteed.

So the sorting comes from the trading server. If for some reason, information about an order is delayed, it can be placed after an order opened later.

OrderSelect - Trade Functions - MQL4 Reference
OrderSelect - Trade Functions - MQL4 Reference
  • docs.mql4.com
To find out from what list the order has been selected, its close time must be analyzed. If the order close time equals to 0, the order is open or pending and taken from the terminal open orders list. One can distinguish an opened order from a pending order by the order type. If the order close time does not equal to 0, the order is a closed...
 
Alain Verleyen:

Where do you read I believe something is false ? I asked nicholishen to explain this statement "Mql history pool is generally sorted by open time, but not always guaranteed.".

My own short answer: you have to sort the history. You can never rely on it being in a particular order.

Longer answer: when MT4 starts up and downloads the history from the server, the history is in order of open time, not close time. But when you then close trades while MT4 is running, they are added to and appear in the history in close-time order. So, it's then an inconsistent mix.

Even longer answer: the order used to depend on what you had selected in the user interface. If you sorted the UI history grid by trade volume, that's what you used to get when you queried the list programmatically.

 
JC:

My own short answer: you have to sort the history. You can never rely on it being in a particular order.

Longer answer: when MT4 starts up and downloads the history from the server, the history is in order of open time, not close time. But when you then close trades while MT4 is running, they are added to and appear in the history in close-time order. So, it's then an inconsistent mix.

Even longer answer: the order used to depend on what you had selected in the user interface. If you sorted the UI history grid by trade volume, that's what you used to get when you queried the list programmatically.

Thanks for the precision. We know have a comprehensive explanation.
 

Here is my version, not using OOP, using 1 loop only. No filter on symbol (you can enable it by removing the '//').

void OnStart()
  {
   int last_3_orders=0;
   datetime last1=0,last2=0,last3=0;
   bool last1Inloss=false,last2Inloss=false,last3Inloss=false;

   for(int i=OrdersHistoryTotal()-1;i>=0;i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
      //if(OrderSymbol()==Symbol())
        {
         bool orderInloss=(OrderProfit()<0);
         datetime oct=OrderCloseTime();
         if(oct>last3)
           {
            if(oct>last2)
              {
               if(oct>last1)
                 {
                  if(last1!=0)
                    {
                     if(last2!=0)
                       {
                        last3=last2;
                        last3Inloss=last2Inloss;
                       }
                     last2=last1;
                     last2Inloss=last1Inloss;
                    }
                  last1=oct;
                  last1Inloss=orderInloss;
                 }
               else
                 {
                  if(last2!=0)
                    {
                     last3=last2;
                     last3Inloss=last2Inloss;
                    }
                  last2=oct;
                  last2Inloss=orderInloss;
                 }
              }
            else
              {
               last3=oct;
               last3Inloss=orderInloss;
              }
           }
        }
     }
//---
   last_3_orders=(last1Inloss && last2Inloss && last3Inloss) ? 1 : 0;

   Print("No OOP: last 1 ",last1,"(",last1Inloss,") last 2 ",last2,"(",last2Inloss,") last3 ",last3,"(",last3Inloss,") last 3 in loss ? ",last_3_orders);
  }

And a little bench mark :

2017.10.03 17:02:28.227 Tested on 90 history orders, all symbols
2017.10.03 17:02:28.235 Bench no OOP: 8132 µs 1000 iterations. last 1 2017.09.27 16:45:10(true) last 2 2017.09.27 16:30:01(true) last3 2017.09.27 16:00:10(false) last 3 in loss ? 0
2017.10.03 17:02:28.402 Bench nisholishen (fixed): 166805 µs 1000 iterations. loss count 2

20 times faster ?

Please note that I have nothing against OOP. A good OOP version could be coded, I let it as an exercise to nicholishen.
 
Alain Verleyen:

Here is my version, not using OOP, using 1 loop only. No filter on symbol (you can enable it by removing the '//').

And a little bench mark :

20 times faster ?

Please note that I have nothing against OOP. A good OOP version could be coded, I let it as an exercise to nicholishen.

So you're going to completely rewrite multi-layer nesting blocks any time you need to work with sorted history? Have fun with that. ... let's see you analyse the last 10 orders......

{
   {
      {
         {
            {
               {
                  {
                     {
                        {
                           {
                              {
                                 {
                                    {
                                       {
                                          {
                                             {
 
nicholishen:

So you're going to completely rewrite multi-layer nesting blocks any time you need to work with sorted history? Have fun with that.

Again you assume...don't assume. It's just an example. I am going to write efficient code for me and my customers, don't worry.

If you can live with a code 20 times slower, it's ok, you have your own expectations, but don't assume everyone have the same.

Reason: