Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6. - page 759

 
//+------------------------------------------------------------------+
//|                                                        Ozero.mq4 |
//|                                            Copyright 2014, Vinin |
//|                                             http://vinin.ucoz.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, Vinin"
#property link      "http://vinin.ucoz.ru"
#property version   "1.00"
#property strict
// -------- переменные --- Н -----
extern double gLot=0.1;            // размер лота для покупки 
extern double gPoint=0.001;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
// -------- переменные --- К ----- 
int init()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {

// Открытие позиции
   if(OrdersTotal()==0)
     {
      Print(" Открытие первой позиции ");
      double _Ask=MarketInfo("GBPUSD",MODE_ASK);
      int ticket=OrderSend("GBPUSD",OP_BUY,gLot,_Ask,15,0,0);
      if(ticket>0)
        {
         Print(" ОРДЕР ОТКРЫТ ",ticket);
        }
     }

   double _Bid=MarketInfo("GBPUSD",MODE_BID); //для цены
   for(int pos=OrdersTotal()-1;pos>=0;pos--)
     {
      if(OrderSelect(pos,SELECT_BY_POS))
        {
         if(OrderOpenPrice()+gPoint<_Bid)
           {
            OrderClose(OrderTicket(),OrderLots(),_Bid,15);
            
ResetLastError();
if(!OrderClose(OrderTicket(),OrderLots(),_Bid,15)) Print("Чё-та не закрылася позиция. Фигня вот такая происходит: "+GetLastError());

           }
        }
     }
   return(0);
  }

Good morning.

The program looks like this.

When compiling it gives out:

Ozero.mq4'Ozero.mq4 1 1
return value of 'OrderClose' should be checked Ozero.mq4 46 13
implicit conversion from 'number' to 'string' Ozero.mq4 49 117
0 error(s), 2 warning(s) 1 3


The "results" tab shows that orders are opening and closing.

1 2014.08.25 00:00 buy 1 0.10 1.6550 0.0000 0.0000 0.00 10000.00
2 2014.08.25 06:30 close 1 0.10 1.6562 0.0000 0.0000 0.12 10000.12
3 2014.08.25 06:31 buy 2 0.10 1.6564 0.0000 0.0000 0.00 10000.12
4 2014.08.25 10:00 close 2 0.10 1.6574 0.0000 0.0000 0.10 10000.22
5 2014.08.25 10:00 buy 3 0.10 1.6576 0.0000 0.0000 0.00 10000.22
6 2014.08.25 12:58 close 3 0.10 1.6586 0.0000 0.0000 0.10 10000.32
7 2014.08.25 12:58 buy 4 0.10 1.6590 0.0000 0.0000 0.00 10000.32
8 2014.08.26 13:13 close at stop 4 0.10 1.6576 0.0000 0.0000 -0.14 10000.18


8 - I interrupted the work by closing the tester.


Next, we look at the "Journal":

2014.10.31 07:46:44.837 GBPUSD,M15: 4453 tick events (5608 bars, 340128 bar states) processed within 312876 ms (total time 324436 ms)
2014.10.31 07:46:44.837 2014.08.26 13:13 Tester: order #4 is closed
2014.10.31 07:46:44.798 2014.08.26 13:13 Visual tester stopped
2014.10.31 07:43:15.315 2014.08.25 12:58 Ozero GBPUSD,M15: ORDER OPEN 4
2014.10.31 07:43:15.315 2014.08.25 12:58 Ozero GBPUSD,M15: open #4 buy 0.10 GBPUSD at 1.6590 ok
2014.10.31 07:43:15.315 2014.08.25 12:58 Ozero GBPUSD,M15: Open #4 buy 0.10 GBPUSD at 1.6590 ok
2014.10.31 07:43:15.245 2014.08.25 12:58 Ozero GBPUSD,M15: I don't think it closed. Here we go: 4108
2014.10.31 07:43:15.245 2014.08.25 12:58 Ozero GBPUSD,M15: OrderClose error 4108
2014.10.31 07:43:15.245 2014.08.25 12:58 Ozero GBPUSD,M15: unknown ticket 3 for OrderClose function
2014.10.31 07:43:15.245 2014.08.25 12:58 Ozero GBPUSD,M15: close #3 buy 0.10 GBPUSD at 1.6576 at price 1.6586
2014.10.31 07:42:51.123 2014.08.25 10:00 Ozero GBPUSD,M15: HOLD OPEN #3
2014.10.31 07:42:51.123 2014.08.25 10:00 Ozero GBPUSD,M15: open #3 buy 0.10 GBPUSD at 1.6576 ok
2014.10.31 07:42:51.123 2014.08.25 10:00 Ozero GBPUSD,M15: Open #1 position
2014.10.31 07:42:51.059 2014.08.25 10:00 Ozero GBPUSD,M15: What's up? Here we go: 4108
2014.10.31 07:42:51.059 2014.08.25 10:00 Ozero GBPUSD,M15: OrderClose error 4108
2014.10.31 07:42:51.059 2014.08.25 10:00 Ozero GBPUSD,M15: unknown ticket 2 for OrderClose function
2014.10.31 07:42:51.059 2014.08.25 10:00 Ozero GBPUSD,M15: close #2 buy 0.10 GBPUSD at 1.6564 at price 1.6574
2014.10.31 07:42:31.473 2014.08.25 06:31 Ozero GBPUSD,M15: HOLD OPEN 2
2014.10.31 07:42:31.473 2014.08.25 06:31 Ozero GBPUSD,M15: open #2 buy 0.10 GBPUSD at 1.6564 ok
2014.10.31 07:42:31.473 2014.08.25 06:31 Ozero GBPUSD,M15: Opening of the first position
2014.10.31 07:42:31.331 2014.08.25 06:30 Ozero GBPUSD,M15: What's up? Here we go: 4108
2014.10.31 07:42:31.331 2014.08.25 06:30 Ozero GBPUSD,M15: OrderClose error 4108
2014.10.31 07:42:31.331 2014.08.25 06:30 Ozero GBPUSD,M15: unknown ticket 1 for OrderClose function
2014.10.31 07:42:31.331 2014.08.25 06:30 Ozero GBPUSD,M15: close #1 buy 0.10 GBPUSD at 1.6550 at price 1.6562
2014.10.31 07:41:31.966 2014.08.25 00:00 Ozero GBPUSD,M15: ORDER OPEN 1
2014.10.31 07:41:31.966 2014.08.25 00:00 Ozero GBPUSD,M15: open #1 buy 0.10 GBPUSD at 1.6550 ok
2014.10.31 07:41:31.966 2014.08.25 00:00 Ozero GBPUSD,M15: open #1 buy 0.10 GBPUSD at 1.6550 ok
2014.10.31 07:41:31.958 Ozero inputs: gLot=0.1; gPoint=0.001;

Now I'm at a loss altogether. On one hand the orders are being closed as you can see in the chart and in the results, on the other hand the code is being executed with errors as seen in the log: (((((((((((

Ozero.

 
You are trying to close the same order twice. Remove the top one.
 
Roger:
You are trying to close the same order twice. Remove the top one.
Preferably the lower one. The order has to be selected beforehand.
 
            
//ResetLastError();
//if(!OrderClose(OrderTicket(),OrderLots(),_Bid,15)) Print("Чё-та не закрылася позиция. Фигня вот такая происходит: "+GetLastError());

Good morning.

Eliminated two lines. Everything works. And everything is displayed correctly on the graph, and in the results and in the logs.

Question: when compiling, it gives out


'Ozero.mq4'Ozero.mq4 1 1
Return value of 'OrderClose' should be checked Ozero.mq4 46 13
0 error(s), 1 warning(s) 1 2



What does the compiler warn me about? The 46th line is OrderClose(OrderTicket(),OrderLots(),_Bid,15);


Ozero.

 
tuner:

Can you guys tell me what might be causing the glitch that occurred today?

The EA has an option to stop trading 15 minutes before market close on Friday.

When a new day appears the EA checks if it is Friday, then today we stop trading in StringToTime("23:59")-15*60

In the strategy tester, this function works correctly. However, in real trading, I see a totally different situation - the Expert Advisor stops trading on the first tick when Friday arrives.

Here are the prints from Expert Advisor's journal:

Broker #1, demo:

0 05:59:47.731 Scalper GBPAUDpt,M1: Finish In Friday = 2014.10.23 23:44:00

Broker 2, real:

0 03:00:11.999 Scalper EURUSD,M1: Finish In Friday = 2014.10.23 23:44:00

That is, when today's daily bar appears (Friday, 24th October) on the first tick, the EA

will convert the line "23:59" to yesterday's time, instead of assigning today's day.

However, if the Expert Advisor is restarted on Friday in the middle of the day, it will correctly determine the time of completion.

I've found the reason of this problem: https://forum.mql4.com/33023

As expected, it is practically a bug in the StringToTime function. All the symptoms are similar. This function converts string "23:59" to datetime with date of local PC but not with date of MT4. And in the tester, as we know, the date of the local PC is emulated and equated to the terminal time. That's why everything works properly in the tester, but the demo/real time results in glitches and losses. The documentation, of course, does not say where the date is coming from and the link above shows that this problem was reported 4 years ago, but apparently the methaquotes do not care.

 
      RefreshRates();                     // Обновим данные
      _Bid=MarketInfo( "GBPUSD",MODE_BID);

      if (_Bid>=gPoint1)
        {
           for(pos=OrdersTotal();pos>=0;pos--)
             {
                OrderClose(OrderTicket()-1,OrderLots(),_Bid,15);
             }      
       
        }

Another question. Do I understand correctly that:

1. the order numbering starts from "0".

2. This code will close absolutely all orders, regardless of whether I open them manually or programmatically? I.e., we will never find an order this part of the program cannot close?


Ozero.

 

Has anyone done scrolling text on the screen? For example - order so-and-so opened/closed/modified, etc... News there so-and-so... ?

If you don't mind, please show me a piece of code.

Thanks!

 
Vinin:
The lower one is better. The order must be chosen beforehand.

It's better this way:

//+------------------------------------------------------------------+
//|                                                        Ozero.mq4 |
//|                                            Copyright 2014, Vinin |
//|                                         http://bomzh_inc.ucoz.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, Bomzh Inc"
#property link      "http://bomzh_inc.ucoz.ru"
#property version   "1.00"
#property strict
// -------- переменные --- Н -----
input double gLot=0.1;           // Размер лота для покупки 
input double gPoint=0.001;       // Дельта для закрытия 
input string symbol="GBPUSD";    // Рабочая валютная пара
void OnTick() {
   // Открытие позиции
   if(OrdersTotal()==0) {
      int ticket=0;
      double _Ask=SymbolInfoDouble(symbol,SYMBOL_ASK);
      Print(" Открытие первой позиции ");
      ticket=OrderSend(symbol,OP_BUY,gLot,_Ask,15,0,0);
      if(ticket>0) Print("ОТКРЫТ ОРДЕР #"+IntegerToString(ticket));
      }
   //--- Закрытие позиций
   double _Bid=SymbolInfoDouble(symbol,SYMBOL_BID); //для цены
   for(int pos=OrdersTotal()-1;pos>=0;pos--) {
      if(OrderSelect(pos,SELECT_BY_POS)) {
         if(OrderSymbol()!=symbol) continue;
         if(OrderType()!=OP_BUY)   continue;
         if(OrderOpenPrice()+gPoint<_Bid) {
            ResetLastError();
            if(!OrderClose(OrderTicket(),OrderLots(),_Bid,15)) 
               Print("Чё-та не закрылася позиция. Фигня вот такая происходит: "+IntegerToString(GetLastError()));
            }
         }
      }
   //--- End OnTick()
}
//+------------------------------------------------------------------+
 
_new-rena:

Has anyone done scrolling text on the screen? For example - order so-and-so opened/closed/modified, etc... News there so-and-so... ?

If you don't mind, please show me a piece of code.

Senx!

Was doing vertical scrolling in empty indicator window. Here's the function:

//+------------------------------------------------------------------+
// Если bool print = true или нет окна индикатора, то функция будет выводить сообщения в журнал
void iPrint(string mess1, int sz=9, color color1=clrDarkGray, 
            string mess2="", color color2=clrDarkGray, 
            string mess3="", color color3=clrDarkGray, 
            string mess4="", color color4=clrDarkGray, 
            bool draws=true) {
   string   NameGrafText, message, nm;
   int      i, y, k, shift, Win_Num=-1, num=0;
   color    cl;
   Win_Num=WindowFind("Win_Inform");
   if (mess1=="") {
      //Print("Func iPrint: Передана пустая строка, выходим");
      return;
      }
   if (!draws || Win_Num<0) {
      message=mess1+mess2+mess3+mess4;
      Print(message); 
      return;
      }
   k=ArraySize(Mass_Name_Message)-1;
   if (StringLen(mess1)>0) num++;
   if (StringLen(mess2)>0) num++;
   if (StringLen(mess3)>0) num++;
   if (StringLen(mess4)>0) num++;
// Смещение и перекрашивание старых сообщений   
   for (i=k; i>=0; i--) {                             
      NameGrafText=Mass_Name_Message[i];           
      if (StringLen(NameGrafText)>0) {
         if (ObjectFind(NameGrafText)==Win_Num) {
            if (i+num>k) {                   // Удаляем самую верхнюю строчку
               ObjectDelete(NameGrafText);  
               Mass_Name_Message[i]="";
               }
            else if (i+num<=k) {             // Сдвигаем и перекрашиваем старые сообщения
               Mass_Name_Message[i+num]=Mass_Name_Message[i];
               y=(int)ObjectGet(NameGrafText, OBJPROP_YDISTANCE);          // старая координата Y
               ObjectSet  (NameGrafText, OBJPROP_YDISTANCE, y+(sz+1)*num); // новая координата Y
               ObjectSet  (NameGrafText, OBJPROP_COLOR, clrDimGray);       // новый цвет
               }
            }
         }
      }
// Вывод новых сообщений
   shift=num;
   uint v=GetTickCount();
   for (i=0; i<num; i++) {
      shift--;
      NameGrafText=Prefix+"_GT_"+IntegerToString(i)+"_"+IntegerToString(v);   // Уникальное имя объекта
      int app=0;
      while (ObjectFind(NameGrafText)==Win_Num) {  // Если всё-таки такой объект есть
         app++;
         NameGrafText=Prefix+"_GT_"+IntegerToString(i)+"_"+IntegerToString(v)+"_"+IntegerToString(app); // добавим к его имени "хвостик"
         }
      Mass_Name_Message[num-1-i]=NameGrafText;
      switch (i) {
         case 0: message=mess1; cl=color1; break;
         case 1: message=mess2; cl=color2; break;
         case 2: message=mess3; cl=color3; break;
         case 3: message=mess4; cl=color4; break;
         default:message=mess1; cl=color1; break;
         }
      ObjectCreate (NameGrafText, OBJ_LABEL, Win_Num, 0, 0);
      ObjectSetText(NameGrafText, message, sz, "Courier New", cl);
      ObjectSet    (NameGrafText, OBJPROP_COLOR, cl);                   // цвет
      ObjectSet    (NameGrafText, OBJPROP_CORNER,    2);                // угол
      ObjectSet    (NameGrafText, OBJPROP_XDISTANCE, 150);              // координата Х
      ObjectSet    (NameGrafText, OBJPROP_YDISTANCE, 2+(sz+1)*shift);   // координата Y
      WindowRedraw();
      }
}
//+------------------------------------------------------------------+

As an example of using a single colour message (you can have 4 colours in one line for different words):

if(get.BarOpenLastPos(sy,PERIOD_H4,OP_SELL,mn)!=0) {
   string mess1="На D1 вниз, на H4 ниже SAR, M15 сигнал. Продаём";
   iPrint(mess1,9,clrBlue);
   trade.OpenS(sy,lots,mn,0.0,0.0,co);
   }

There should be an empty indicator named Win_Inform on the chart.

 
_new-rena:

Has anyone done scrolling text on the screen? For example - order so-and-so opened/closed/modified, etc... News there so-and-so... ?

If you don't mind, please show me a piece of code.

Thanks!

https://book.mql4.com/ru/build/info