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

 

Hello!

Me Simon. I have CTRL-C -> CTRL-V coded EA. Then I have bit recoded it, since I have learned bit on the go. Had problem with 'lastIsBE' function. So recoded it again, especialy this mentioned function. But... still have same problem as before - although last open order is break even, EA says it is not. I have tried to persuade this EA of mine, we have had looong conversations, many a nights. He is still stubborn (must have got this after me LOL), and insist that some open orders are not break even, no matter that is obvious from chart that they are. It sometimes work, sometimes not. Using Alpari UK.

The code is from mql4Book. You know, I did my homework. But my home does not work. LOL!

#include file has this:

/*static*/ double  OpenOrdersNew [30][9];
/*static*/ double  OpenOrdersOld [30][9];
/*static*/ int     OpenOrdersType [6];
...


// ===== ORDER COUNTING AND PROCESSING FUNCTIONS ================================================================================
// ----- PUT OPEN ORDERS AND THEIR VARIABLESE 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 ++) // all open and pending orders¸ original
//   for (int i = OrdersTotal() - 1; i >= 0; i --) // last order
      {
      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;
   }

EA has this:

...

int init()
   {
   checkRisk();
   IS_ECN_BROKER = IsECNBroker;
   magic = makeMagicNumber (WindowExpertName() + Symbol());
   // ----- LOT CALCULATION -----  
   CurrentSymbolType = SymbolType();
   CurrentBasePairForCross = BasePairForCross();
   CurrentCounterPairForCross = CounterPairForCross();
   // ----- LOT CALCULATION END -----
   if (IsTesting()) {Test = "_test";}
   readGV();
   {Print (name + " " + Symbol() + " GV loaded! " + TimeToStr (TimeCurrent(), TIME_DATE | TIME_MINUTES));} // time is Broker time !
   OpenOrdersArray();
   ShowLineOrdersOnChart();
   return (0);
   }
...

// ----- START FUNCTION -----
int start()
   {
   label ("LastIsBE_XX", 70, 25, 1, "LastIsBE: " + LastIsBE, 10, font, White); // testing
   LastTicket = OpenOrdersNew [1,1];
 //  if (!BreakEven && WaitForBE && getNumOpenOrders (-1, magic) == 0) {TradeGrid = false;}
 //  if (!BreakEven && WaitForBE && getNumOpenOrders (-1, magic) > 0) {TradeGrid = true;}
 //  if (getNumOpenOrders (-1, magic) < 1) /*LastIsBE = false;*/ TrailLast = false; TrailBuy = false; TrailSell = false;
   OpenOrdersArray();
 //  displayOpenOrdersNew(); // prints last 2 open orders info - testing purpose
   lastIsBE();
   breakEven();
   trailLast();
...

...

// ----- LAST IS BREAK EVEN -----
void lastIsBE ()
   {
//   LastIsBE = false; //disable ? - instead of declaring at start function - NOT WORKING
   int total = OrdersTotal();
   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);
               }
            }
         }
      }
   //return (0); // enable this? it just returns to start()
   }

Now, as I said, sometimes it works, then not. It works flawless on Tester, then I have put EA on multiple charts, iiiiikkk!

"I can not do this for you!", said Agent SukrChi EA. "Why not?", I replied. "I am too old for this!", replied EA. "No you are not! You have just come into existence!". And the conversation went on...

As many before me, I have exhausted my tweak-the-code ideas.

Thank you for your help,

best regards,

Simon

 

You need to read the Book and the Documentation . . .

  for (int i = total - 1; i >= 0; i --)
      {
      if (OrderMagicNumber() == magic)   //  <-------------- for which order ? ?
         {

Read OrderMagicNumber() and OrderSelect()

 

Hi!

Have attached some more pictures (combined):

LEFT - same as above - I say YES BE, EA says NO BE | MIDDLE - I say YES BE, EA says YES BE (at least once a while we agree LOL) | RIGHT - I say NO BE, EA says YES BE ("No way!", I add. "Not possible, no trades!". "Never mind, yes break even.", replies calmly EA. Must be some distant relative of Zaphod Bebbleebrox LOL).

 

Raptor, Raptor, Raptor!

You are da MAN!

Works now:

// ----- LAST IS BREAK EVEN -----
void lastIsBE ()
   {
//   LastIsBE = false; //disable ? - instead of declaring at start function - NOT WORKING
   int total = OrdersTotal();
   for (int i = total - 1; i >= 0; i --)
      {
      OrderSelect (i, SELECT_BY_POS, MODE_TRADES); // <-- Noughty noughty line, where were you all this time? Shame on you, you made me sooo worried!
      if (Symbol() == OrderSymbol() && 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; //continue;//setGV ("LastIsBE" + Test, 1);
               }
            else
               {
               LastIsBE = false; //setGV ("LastIsBE" + Test, 0);
               }
       //     continue;
            }
         // ----- 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);
               }
            }
         }
      }
   return (0); // enable this? it just returns to start()
   }

Now, do I need to have 'return(0);' at user-defined functions, at end, or not? Most of user-defined I have seen do not have it!

And, if I would write:

OrderSelect ( >1<, SELECT_BY_POS, MODE_TRADES);

would that select last order? Considering that I would count from last toward first.

RaptorUK, muchas gracias, shall never forget you!

Best regards,

Simon

 

it's RaptorUK not Raptor. https://www.mql5.com/en/forum/139661

Carefully look at OrderSelect() of first code on your first post.

That's how to correctly use OrderSelect() .

 
Chistabo:

Now, do I need to have 'return(0);' at user-defined functions, at end, or not? Most of user-defined I have seen do not have it!

Not for type void . . void means the Function doesn't return anything . . . read this thread, it should help: What are Function return values ? How do I use them ?
 

In any case, "return;" can be used inside "void" function in the point where it needs to be stopped.

 

Dear fellows!

Anybody willing to take a peek into my code? It seem to be not working properly after all.

Must be catch somewhere else, since I had same troubles (EA lying to me directly into my eyes!) with previous code.

Run out of ideas... and will...

Thank you for your support along this dirty "wanna-be-coder" road.

Frankly, I was thinking to rent a coder, but am currently (read - last 2 years) unemployed, and therefore bit short.

Nevermind,

Have fun,

Simon

S love nia

P.S: Sorry, RaptorUK, those protesting boards were bit small, I didn't know that picture would be so big. But, I think you got the idea of me thanking you. Beside, I was constantly beeing distracted by black & white lady, especially white! LOL!

 
RaptorUK:
Not for type void . . void means the Function doesn't return anything . . . read this thread, it should help: What are Function return values ? How do I use them ?


Not sure what exactly do you mean. I have heard this statment often, but...

int count = 0;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
   {
   Alert ("Initialized!");
   return(0);
   }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
   {
   return(0);
   }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
   {
   double price = Bid;
   count();
   Alert ("New tick no. " + count + ", price is " + price);
   return(0);
   }
//+------------------------------------------------------------------+
//| expert custom functions                                          |
//+------------------------------------------------------------------+
void count() // <-- void
   {
   count++;
   }

... returns a value of int count.

Regards,

May the Pips fall into your account,

Simon

S love nia

 

Hi ho!

Could it be that declaring (assigning)

LastIsBE = false;

on global scope (head part) makes troubles to this 'lastIsBE()' function? I have removed '= false' part, now waiting for orders to open...

Thanky,

Simon

 
Chistabo:


Not sure what exactly do you mean. I have heard this statment often, but...

... returns a value of int count.

No it doesn't . . read the thread I gave you a link to.

Count is declared globally, the function called count() increments the variable count . . . it returns nothing . . .

Reason: