[ARCHIVE] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 3. - page 287

 
So will I be able to see the correct code?
 
sergeev:
on the first non-buy order you will flood the account with buy orders.


Thank you! Found another mistake!

//+------------------------------------------------------------------+
//|                                                      OnlyBuy.mq4 |
//|                      Copyright © 2011, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

extern double lots=0.1;
extern int SlipPage=3;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
  if (OrdersTotal()==0)
   OrderSend(Symbol(),OP_BUY,lots,NormalizeDouble(Ask,Digits),SlipPage,0,0,NULL,0,0,Red);
  for (int j = 0; j < OrdersTotal(); j++)
   {
    OrderSelect(j, SELECT_BY_POS, MODE_TRADES);
    if (OrderSymbol() == Symbol())
     {
      if (OrderType() != OP_BUY)
       OrderSend(Symbol(),OP_BUY,lots,NormalizeDouble(Ask,Digits),SlipPage,0,0,NULL,0,0,Red);
     }
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
 
icas:


Thank you! Found another error!


Mm-hmm.

but the previous one hasn't been fixed.

You can't put an order opening immediately into a loop. You have to do a full loop on the orders first and find out exactly whether there is a Buy or not.

And only after that do you put a Buy, after that cycle.

 
sergeev:

Mm-hmm.

but the previous one wasn't fixed.

You can't put an order opening immediately into a loop. You must first do a full loop on the orders and find out exactly whether there is a Buy or a No.

And only after that should you place a Buy if it is not there yet.



Right, I just took a piece from my programme posti without re-doing it. I'll do it now - five minutes...
 
But this code is not correct either! It will only open a buy order if there are no other orders for any other financial instrument, and if there are any other orders, it will not open a buy order.
 
icas:

Right, I just took a piece from my programme posti without redoing it. I'll do it now - five minutes...
//+------------------------------------------------------------------+
//|                                                      OnlyBuy.mq4 |
//|                      Copyright © 2011, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

extern double lots=0.1;
extern int SlipPage=3;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
  for (int j = 0; j < OrdersTotal(); j++)
   {
    OrderSelect(j, SELECT_BY_POS, MODE_TRADES);
    if (OrderSymbol() == Symbol())
     {
      if (OrderType() == OP_BUY) return(0);
     }
   }
  OrderSend(Symbol(),OP_BUY,lots,NormalizeDouble(Ask,Digits),SlipPage,0,0,NULL,0,0,Red);
  Sleep(10000); //Пауза 10 сек. для исполнениея ордера
//----
   return(0);
  }
//+------------------------------------------------------------------+
Right now, right?
 
icas:
Is that right now?
Sleep(10000); //Пауза 10 сек. для исполнениея ордера

What's the point of this pause?
 
kolyango:

Why the pause?

The order is not executed immediately. Without a pause on the next tick, the order may not be opened yet, and the program will send another command to open it. In the MT5 tester, you can already enter the execution delay.
 
icas:
Is it correct now?

Have you tried making it a separate function to search for the presence/absence of items?

For example:

bool ExistPositions(int mn, string sy="", int op=-1) {
   if (sy=="") sy=Symbol();
   for (int i=0; i<OrdersTotal(); i++) {
      if (OrderSelect(i, SELECT_BY_POS)) {
         if (OrderMagicNumber()!=mn)   continue;
         if (OrderSymbol()!=sy)        continue;
         if (OrderType()>1)            continue;
         if (op<0 || OrderType()==op)  
            return(True);
         }
      }
   return(False);
}

Magic - magic number of EA (set in settings, or generated by EA automatically). Then to check the absence of position Buy by current symbol should be written:

if (!ExistPositions(Magic, Symbol(), OP_BUY)) {
   // ... Тут код для вызова функции открытия позиций ... 
   }

Note - not just a command to open a Buy order, but a full function with handling all order opening parameters and errors returned by the server.

However, for a tester, we can do without checking the entered parameters for valid values...

 
artmedia70:

Have you tried making it a separate function to search for the presence/absence of items?

For example:

Then to check for absence of Buy position by current symbol should be written:

Note - not just a command to open a Buy order, but a full function with processing all parameters of the order opening and errors returned by the server.

However, the tester may do without checking the entered parameters for permissible values...



In the first post I gave the functions, kolyango wasn't happy with that. Also, I don't like working with magicians.
Reason: