Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1337

 
sibiriyak73:

Where is the mistake?

If there are a lot of characters, then there are a lot of "opens".

struct Orders
   {
   string symbol;
   bool open;
   } orders[];

int OnInit()
   {
   ArrayResize(orders,2);
   orders[0].symbol="EURUSD";
   orders[0].open=false;
   orders[1].symbol="USDJPY";
   orders[1].open=false;
   return(INIT_SUCCEEDED);
   }

void OnTick()
   {
   for(int i=0; i<2; i++)
      {
      if(!orders[i].open)
         {
         int ticket1=OrderSend(orders[i].symbol,OP_SELLSTOP,1.5,limit1,10,SL1,TP1,NULL,0,0,clrRed);  //Здесь открываем
         orders[i].open=true;
         }
      }
........


 
Aleksei Stepanenko:

If there are a lot of characters, then there are a lot of "opens".

Thanks, buddy. How do I make it so that it one transaction a day to tear off on the current instrument and disregard other instruments (EA stands on several instruments)
 
sibiriyak73:
Thank you buddy. How to make it one trade per day to tear off at the current symbol and disregard other instruments (advisor stands on several symbols)

- look in the order history by its magic number and symbol, if the time of opening (or closing?) of the order corresponds to the current date, then exit OnTick()

- control the "new bar" on TF D1


the first way - more writing, but in the tester, MT4 it will work very fast; the second way - 3 lines of code, but in the tester it will "chew" the hard drive and the testing time will be longer

 
sibiriyak73:
(EA stands on several instruments)

Ah, I think I get it, if you have an EA standing on multiple instruments, then you just need to add a check that the order belongs to the EA.

int OPEN=1;

int total=OrdersTotal();
for(int i=0; i<total; i++)
   {
   if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderCloseTime()==0 && OrderSymbol()==Symbol())
      {
      OPEN=0;
      break;
      }
   }

if(OPEN==1)
   {
   int ticket1=OrderSend(Symbol(),OP_SELLSTOP,1.5,limit1,10,SL1,TP1,NULL,0,0,clrRed);  //Здесь открываем
   }
 
Aleksei Stepanenko:

Ah, I think I get it, if you have an EA on multiple instruments, then you just need to add a check that the order belongs to the EA.

This code opens a huge number of deals. The idea is correct, but how to implement it correctly.

 
sibiriyak73:

This code opens a huge number of trades. The idea is correct but how to implement it correctly.

You need this

Function isTradeToDay().

This function returns the trade flag for today

Только "Полезные функции от KimIV".
Только "Полезные функции от KimIV".
  • 2011.02.18
  • www.mql5.com
Все функции взяты из этой ветки - http://forum.mql4...
 

What can you guess from a piece of code here?

Put a check on it, no more than once a day.

datetime last_time=0;   
MqlDateTime date1, date2; 

void OnTick()
   {
   int OPEN=1;

   int total=OrdersTotal();
   for(int i=0; i<total; i++)
      {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderCloseTime()==0 && OrderSymbol()==Symbol())
         {
         OPEN=0;
         break;
         }
      }
      
   TimeToStruct(last_time,date1);
   TimeToStruct(iTime(Symbol(),0,0),date2);
   if(date1.day==date2.day)
      {
      OPEN=0;
      }

   if(OPEN==1)
      {

      int ticket1=OrderSend(Symbol(),OP_SELLSTOP,1.5,limit1,10,SL1,TP1,NULL,0,0,clrRed);  //Здесь открываем
      if(ticket1>=0 && OrderSelect(ticket1,SELECT_BY_TICKET,MODE_TRADES))
         {
         last_time=OrderOpenTime();
         }
      }

 
sibiriyak73:

Guys help in the advisor need to open one trade a day ON THE TOOL.

You could try this

datetime dt = iTime(_Symbol,PERIOD_D1,0);

   for(int i = 0; i < OrdersHistoryTotal(); i++) 
    {
     if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
      {
       if((OrderSymbol() != _Symbol) && (dt < OrderCloseTime())
        {
         Открываем ордер;
        }
      }
    }
 
MakarFX:

You could try this.

All written a long time ago ... a very long time ago.

This code checks what's already in the market and what's in the history in case it's already closed today.

//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 19.02.2008                                                     |
//|  Описание : Возвращает флаг торгов сегодня.                                |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
bool isTradeToDay(string sy="", int op=-1, int mn=-1) {
  int i, k=OrdersHistoryTotal();

  if (sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {
      if (OrderSymbol()==sy || sy=="") {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (op<0 || OrderType()==op) {
            if (mn<0 || OrderMagicNumber()==mn) {
              if (TimeDay  (OrderOpenTime())==Day()
              &&  TimeMonth(OrderOpenTime())==Month()
              &&  TimeYear (OrderOpenTime())==Year()) return(True);
            }
          }
        }
      }
    }
  }
  k=OrdersTotal();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (OrderSymbol()==sy || sy=="") {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (op<0 || OrderType()==op) {
            if (mn<0 || OrderMagicNumber()==mn) {
              if (TimeDay  (OrderOpenTime())==Day()
              &&  TimeMonth(OrderOpenTime())==Month()
              &&  TimeYear (OrderOpenTime())==Year()) return(True);
            }
          }
        }
      }
    }
  }
  return(False);
}
 
Aleksei Stepanenko:

What can you guess from a piece of code here?

Put a check on it, no more than once a day.

Greetings. The problem is that if the EA sees an open trade in the account, it does not open any more trades. It's like "you have open trades in your account why do you want me to do this". I have to explain him that no more than one trade should be opened on THIS TOOL(EUR USD for example) because this EA is open on few symbols. I think it would be wrong to show all code (it's a big code with declared variables, etc.).