Need a Code Master's help - selecting last order function - page 2

 

Hello!


for (int i = total - 1; i >= 0; i --)
      {
      if (OrderMagicNumber() == magic)
         {
         // ----- BUY ORDER - HAS OrderStopLoss() >= OrderOpenPrice() + MoveSLTo * Point -----
         if (OpenOrdersNew [1,3] == 0) // last buy order
            {
            if (OpenOrdersNew [1,6] >= OpenOrdersNew [1,5] + (MoveSLTo * Point)) // last OrderStopLoss() >= OrderOpenPrice()
               {
               LastIsBE = true; //setGV ("LastIsBE" + Test, 1);
               }
            else
               {
               LastIsBE = false; //setGV ("LastIsBE" + Test, 0);
               }
            }
         // ----- SELL ORDER - HAS OrderStopLoss() <= OrderOpenPrice() - MoveSLTo * Point -----
         if (OpenOrdersNew [1,3] == 1) // last sell order
            {
            if (OpenOrdersNew [1,6] <= OpenOrdersNew [1,5] - (MoveSLTo * Point))
               {
               LastIsBE = true; //setGV ("LastIsBE" + Test, 1);
               }
            else
               {
               LastIsBE = false; //setGV ("LastIsBE" + Test, 0);
               }
            }
         }
      }

In this code you check OpenOrdersNew[x][x] - for each order the ea has made

ie:

Order 1 - Check OpenOrdersNew[1][x]
Order 2 - Check OpenOrdersNew[1][x]
Order 3 - Check OpenOrdersNew[1][x]
Order n - Check OpenOrdersNew[1][x]

in:

OpenOrdersArray()

you save data from the olders order towards the newest (from 0 to n)


Either change the iteration in OpenOrdersArray()....

Or preferably Check OrderOpenTime() for each order and use data from the newest.

Example:

datetime lastordertime;

if(OrderType() == OP_BUY && OrderOpenTime() > lastordertime){

   Buy_SL = OrderStoploss();
   Buy_Ticket = OrderTicket();
   // Whatever you want for buy orders.

   lastordertime = OrderOpenTime();
}
 

Aloha, Compania!

Yes, this is what I am revising right now. I confess, do not know what is the difference btw

for (int i = 0; i < OrdersTotal(); i++) // https://www.mql5.com/en/articles/1390 = Terminal.mqh - this counts all orders, but starts from last?

for (int i = OrdersTotal() ; i >= 0; i--) // code used in my EA - this counts all orders, but starts from first (oldest)? 'i--' means count backwards > toward newest orders?

for (int i = OrdersTotal() - 1; i >= 0; i--) // tried this too - this counts all arders, but starts from first - 1 (second?)

Or preferably Check OrderOpenTime() for each order and use data from the newest. - I used this in my previously code, had same problems as now, not working correctly.

Thank you for your precious help,

Regards,

Simon

 

So...

OpenOrdersArray(); function selects orders and puts them in array. I have recoded into original (int Terminal()) version:

// ----- PUT OPEN ORDERS AND THEIR VARIABLES INTO ARRAY -----
double OpenOrdersArray() // https://book.mql4.com/build/orders (int Terminal())
   {
   ArrayCopy (OpenOrdersOld, OpenOrdersNew, 0); // saves from OpenOrdersNew into OpenOrdersOld array
   int CountOrders = 0;
   ArrayInitialize (OpenOrdersNew, 0);
   ArrayInitialize (OpenOrdersType, 0);
// ----- count open orders -----
   for (int i = 0; i < OrdersTotal(); i++) // https://www.mql5.com/en/articles/1390 = Terminal.mqh
//   for (int i = OrdersTotal() - 1; i >= 0; i --) // last order - this works better than i++
      {
      if ((OrderSelect (i, SELECT_BY_POS) == true) && OrderSymbol() == Symbol() && OrderMagicNumber() == magic)
         {
         CountOrders ++;
         OpenOrdersNew [CountOrders][1] = OrderTicket();
         OpenOrdersNew [CountOrders][2] = OrderOpenTime();
         OpenOrdersNew [CountOrders][3] = OrderType();
         OpenOrdersNew [CountOrders][4] = NormalizeDouble (OrderLots(), 2);
         OpenOrdersNew [CountOrders][5] = NormalizeDouble (OrderOpenPrice(), Digits);
         OpenOrdersNew [CountOrders][6] = NormalizeDouble (OrderStopLoss(), Digits);
         OpenOrdersNew [CountOrders][7] = OrderMagicNumber();
         if (OrderComment() == "") OpenOrdersNew [CountOrders][8] = 0; else OpenOrdersNew [CountOrders][8] = 1;
         OpenOrdersType [OrderType()] ++;
         }
      }
   OpenOrdersNew [0][0] = CountOrders;
   return;
   }

Using my 'off-the-attick' funcion 'displayOpenOrdersNew()', the openOrdersArray() seem to work properly. It does select last open order as first, order opened before last as second. So far, so good.

- MT4 log file -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

12:22:25 Agent SukrChi 007 EA EURUSD,H1: Alert: Total no. of open orders: EURUSD - 2

12:22:25 Agent SukrChi 007 EA EURUSD,H1: Alert: Open orders buy: EURUSD - 2

12:22:25 Agent SukrChi 007 EA EURUSD,H1: Alert: #1 order ticket: EURUSD - 152528163, date 2012.06.19, time 13:01, Buy, lots 0.14 at 1.26189, SL at 1.26591

// if (OpenOrdersNew [1,1] > 0) {Alert ("#1 order ticket: "... - order ticket is > than 0, so there is order...

12:22:25 Agent SukrChi 007 EA EURUSD,H1: Alert: #2 order ticket: EURUSD - 152590200, date 2012.06.21, time 12:00, Buy, lots 0.22 at 1.26721, SL at 1.26449

// if (OpenOrdersNew [2,1] > 0) {Alert ("#2 order ticket: "...

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

So, it seems that I am selecting wrong order (or coding) in function 'lastIsBE'. How can I select (with OrderSelect()) only array order that I am interested in, that is last one (OpenOrdersNew [1])?

After selecting right order, it should not matter if I check via (OP_BUY):

if (OpenOrdersNew [1,6] >= OpenOrdersNew [1,5] + (MoveSLTo * Point))

or

if (OrderStopLoss() >= OrderOpenPrice() + (MoveSLTo * Point))

Btw, I have not used OrderSelect() (as I have been pointed out) because (by my logic, not necessarily correct) the correct order is already selected by OpenOrdersNew [1,x]. Seems I am missing something here! I mean, do I have to select order from TRADE pool, when OpenOrdersArray() function has already done this? Brrrrrrrr... LOL.

OK, took a look to my code - I do have to select via OrderSelect(), because I want to know if selected order is of correct Symbol() (hey, where is Auto list names function here! LOL) and OrderMagicNumber(). But, maybe if I just condition...

Back to tweaking.

Have fun,

Simon

 

Regarding this...

datetime lastordertime;

if(OrderType() == OP_BUY && OrderOpenTime() > lastordertime){

   Buy_SL = OrderStoploss();
   Buy_Ticket = OrderTicket();
   // Whatever you want for buy orders.

   lastordertime = OrderOpenTime();
}

order ticket #108, open time = today 11:40 -> lastordertime = today 11:40 - order #108 opened

order ticket #109, open time = today 16:20 -> lastordertime = today 16:20 - order #109 opened

order ticket #109, close time = today 18:15 -> lastordertime = today 16:20 - order #109 closed (time stays the same?)

last order is order #108 and OrderOpenTime() < lastordertime! So it would not select it. Or am I, again!, missing something?

Besta regarda,

Simon

 
Chistabo:

Regarding this...

order ticket #108, open time = today 11:40 -> lastordertime = today 11:40 - order #108 opened

order ticket #109, open time = today 16:20 -> lastordertime = today 16:20 - order #109 opened

order ticket #109, close time = today 18:15 -> lastordertime = today 16:20 - order #109 closed (time stays the same?)

last order is order #108 and OrderOpenTime() < lastordertime! So it would not select it. Or am I, again!, missing something?

Your loop is selecting open orders so it will never see #109

If you were selecting history OrderOpenTime would be unchanged, and OrderCloseTime will be now be nonzero.

 
// ----- LAST IS BREAK EVEN FUNCTION -----
void lastIsBE()
   {
   // ----- BUY ORDER - HAS OrderStopLoss() >= OrderOpenPrice() + MoveSLTo * Point -----
   if (OpenOrdersNew [1,3] == 0) // last buy order
      { 
      // if (OrderStopLoss() >= OrderOpenPrice() + (MoveSLTo * Point))
      if (OpenOrdersNew [1,6] >= OpenOrdersNew [1,5] + (MoveSLTo * Point))
         {
         LastIsBE = true; //continue;
         }
      // if (OrderStopLoss() < OrderOpenPrice() + (MoveSLTo * Point))
      if (OpenOrdersNew [1,6] < OpenOrdersNew [1,5] + (MoveSLTo * Point))
         {
         LastIsBE = false; //continue;
         }
      }
   // ----- SELL ORDER - HAS OrderStopLoss() <= OrderOpenPrice() - MoveSLTo * Point -----
   if (OpenOrdersNew [1,3] == 1) // sell order
      {
      //  if (OrderStopLoss() <= OrderOpenPrice() - (MoveSLTo * Point))
      if (OpenOrdersNew [1,6] <= OpenOrdersNew [1,5] - (MoveSLTo * Point))
         {
         LastIsBE = true; //continue;
         }
      //  if (OrderStopLoss() > OrderOpenPrice() - (MoveSLTo * Point))
      if (OpenOrdersNew [1,6] > OpenOrdersNew [1,5] - (MoveSLTo * Point))
         {
         LastIsBE = false; //continue;
         }
      }
   }

This should do the trick, shouldn't? I think I do not have to select orders again, when they are selected in OpenOrdersArray() function already. Just condition... and pray to the Mighty Lord, cause now I am realy out of ideas. It looks like working on Demo, but not on Tester. Is there any catch with arrays and Texter?

Best regards,

Simon

 

Now this is my old LastIsBE() function (which I will test again):

// ----- last is break even function - old -----
// ----- LAST IS BE FUNCTION -----
void lastIsBE()
   {
   LastOrderTime = 0;
   for (int i = OrdersTotal() - 1; i >= 0; i --)
      {
      OrderSelect (i, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == magic)
         {
         int curOpenTime = OrderOpenTime();
         if (curOpenTime > LastOrderTime)
            {
            if (OrderType() == OP_BUY)
               {
		// ----- LAST IS BREAK EVEN - BUY -----
               if (OrderStopLoss() >= (OrderOpenPrice() + (MoveSLTo * Point)))
                  {
                  LastIsBE = true; //GlobalVariableSet ("LastIsBE_" + Symbol(), 1);
                  }
               else
                  {
                  LastIsBE = false; //GlobalVariableSet ("LastIsBE_" + Symbol(), 0);
                  }
               }

            if (OrderType() == OP_SELL)
               {
		// ----- LAST IS BREAK EVEN - SELL -----
               if (OrderStopLoss() <= (OrderOpenPrice() - (MoveSLTo * Point)))
                  {
                  LastIsBE = true; //GlobalVariableSet ("LastIsBE_" + Symbol(), 1);
                  }
               else
                  {
                  LastIsBE = false;// GlobalVariableSet ("LastIsBE_" + Symbol(), 0);
                  }
               }
            }
         LastOrderTime = curOpenTime;
         }
      }
   }

And this is my old & current trailLast() function:

// ----- TRAIL LAST ONLY FUNCTION -----
void trailLast()
   {
   if (TrailLast)
      {
      for (int i = OrdersTotal(); i >= 0; i --)
         {
         OrderSelect (i, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == magic)
            {
            // ----- TRAILING STOP - LAST ONLY - BUY -----
            if (OrderType() == OP_BUY && OrderOpenTime() > LastOrderTime)
               {
               if (BuyTSLast > OrderStopLoss() || OrderStopLoss() == 0)
                  {
                  if (BreakEven == true && OrderStopLoss() < (OrderOpenPrice() + Point * MoveSLTo))
                     {
                     OrderModify (LastTicket, 0, BuyTSLast, OrderTakeProfit(), 0, CLR_BUY_ARROW);
                     }
                  if (BreakEven == false)
                     {
                     OrderModify (LastTicket, 0, BuyTSLast, OrderTakeProfit(), 0, CLR_BUY_ARROW);
                     }
                  }
               }
            // ----- TRAILING STOP - LAST ONLY - SELL -----
            if (OrderType() == OP_SELL && OrderOpenTime() > LastOrderTime)
               {
               if (SellTSLast < OrderStopLoss() || OrderStopLoss() == 0)
                  {
                  if (BreakEven == true && OrderStopLoss() > (OrderOpenPrice() - Point * MoveSLTo))
                     {
                     OrderModify (LastTicket, 0, SellTSLast, OrderTakeProfit(), 0, CLR_SELL_ARROW);
                     }
                  if (BreakEven == false)
                     {
                     OrderModify (LastTicket, 0, SellTSLast, OrderTakeProfit(), 0, CLR_SELL_ARROW);
                     }
                  }
               }
            }
         LastOrderTime = OrderOpenTime();
         }
      }
   }

... both similar as Master enotrek (bow) suggestion.

I was avoiding learning arrays like El Diabolo is avoiding a Holly Cross. I tought it would be cool to learn those el D arrays.

Btw, there is some strange white foam coming out of my mouth. I am afraid... LOL.

Hope this will work now. Hoping for last 2 years...


I, Simon the Wanna-be-coder, humbly bow to all Code Masters helping me! Thank you.

Besta regarda,

Simon

Now back to learning... LOL.

 
WHRoeder:

Your loop is selecting open orders so it will never see #109

If you were selecting history OrderOpenTime would be unchanged, and OrderCloseTime will be now be nonzero.

... if I zeroize it, right? This is exactly what I did - at beginning of lastIsBE() custom function set value LastOrderTime = 0;

Now, it seem this to be working properly - trailLast() function:

// ----- TRAIL LAST ONLY FUNCTION -----
void trailLast()
   {
   if (TrailLast)
      {
      LastOrderTime = 0;
      for (int i = OrdersTotal() - 1; i >= 0; i --)
         {
         OrderSelect (i, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == magic)
            {
            int curOpenTime = OrderOpenTime();
            if (curOpenTime > LastOrderTime)
               {
               // ----- TRAILING STOP - LAST ONLY - BUY -----
               if (OrderType() == OP_BUY)// && OrderOpenTime() > LastOrderTime)
                  {
                  if (BuyTSLast > OrderStopLoss() || OrderStopLoss() == 0)
                     {
                     if (BreakEven == true && OrderStopLoss() < (OrderOpenPrice() + Point * MoveSLTo))
                        {
                        OrderModify (OrderTicket(), 0, BuyTSLast, OrderTakeProfit(), 0, CLR_BUY_ARROW);
                        }
                     if (BreakEven == false)
                        {
                        OrderModify (OrderTicket(), 0, BuyTSLast, OrderTakeProfit(), 0, CLR_BUY_ARROW);
                        }
                     }
                  }
               // ----- TRAILING STOP - LAST ONLY - SELL -----
               if (OrderType() == OP_SELL)// && OrderOpenTime() > LastOrderTime)
                  {
                  if (SellTSLast < OrderStopLoss() || OrderStopLoss() == 0)
                     {
                     if (BreakEven == true && OrderStopLoss() > (OrderOpenPrice() - Point * MoveSLTo))
                        {
                        OrderModify (OrderTicket(), 0, SellTSLast, OrderTakeProfit(), 0, CLR_SELL_ARROW);
                        }
                     if (BreakEven == false)
                        {
                        OrderModify (OrderTicket(), 0, SellTSLast, OrderTakeProfit(), 0, CLR_SELL_ARROW);
                        }
                     }
                  }
               }
            LastOrderTime = OrderOpenTime();
            }
         }
      }
   }

... and lastIsBE() function:

// ----- LAST IS BE FUNCTION ----- working
void lastIsBE()
   {
   LastOrderTime = 0;
   for (int i = OrdersTotal() - 1; i >= 0; i --)
      {
      OrderSelect (i, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == magic)
         {
         int curOpenTime = OrderOpenTime();
         if (curOpenTime > LastOrderTime)
            {
            if (OrderType() == OP_BUY)
               {
               // ----- LAST IS BREAK EVEN - BUY -----
               if (OrderStopLoss() >= (OrderOpenPrice() + (MoveSLTo * Point)))
                  {
                  LastIsBE = true;
                  }
               else
                  {
                  LastIsBE = false;
                  }
               }
            if (OrderType() == OP_SELL)
               {
               // ----- LAST IS BREAK EVEN - SELL -----
               if (OrderStopLoss() <= (OrderOpenPrice() - (MoveSLTo * Point)))
                  {
                  LastIsBE = true;
                  }
               else
                  {
                  LastIsBE = false;
                  }
               }
            }
         LastOrderTime = curOpenTime;
         }
      }
   }

No more foam from my mouth. Hooraay!

Best regards,

Have fun,

Simon

S love nia

Reason: