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

 
hoz:



 double lastOrderCloseTime = -1,                   // Время закрытия последнего ордера
        lastOrderOpenTime = -1;                     // Время открытия последнего ордера
What is it?

Type, int or datetime
 
artmedia70:
I once made a function that outputs messages to an empty indicator window. You can adjust the colours of the displayed lines. It's already debugged. I can share it if you need it.


I will gladly look at your code Artem. For testing print, not the most convenient option...
 

r772ra:

 double lastOrderCloseTime = -1,                   // Время закрытия последнего ордера
        lastOrderOpenTime = -1;                     // Время открытия последнего ордера

What's this?


Type, int or datetime

I rewrote the whole mess and it came out like this:

//+-------------------------------------------------------------------------------------+
//| Получаем состояние последней позиции (Открыта или закрыта)                          |
//+-------------------------------------------------------------------------------------+
datetime GetLastOrderState()
{
   datetime lastOrderCloseTime = -1,                   // Время закрытия последнего ордера
            lastOrderOpenTime = -1;                     // Время открытия последнего ордера
   
   for (int i=OrdersTotal()-1; i>=0; i--)
   {
      if (!OrderSelect(i, SELECT_BY_POS)) continue;
      if (OrderMagicNumber() != i_magic) continue;
      if (OrderSymbol() != Symbol()) continue;
  
      if (lastOrderOpenTime < OrderOpenTime())
      {
          lastOrderOpenTime = OrderOpenTime();
          lastOrderCloseTime = OrderCloseTime();
      }
   }
   Comment("Время закрытия последнего открытого ордера = ", lastOrderCloseTime);
   return (lastOrderCloseTime);
}

For some reason, even when the orders are closed and the last order closing time should not be zero.

We got it in the comment all the time:

Время закрытия последнего открытого ордера = 0

That is why my code does not work further, because this function does not work.

 
hoz:

So I rewrote the whole mess and it came out like this:

For some reason even when the orders are closed and the last order closing time should be correspondingly not zero.

I was able to get it in my comment all the time:

That is why nothing works according to my code, because this function does not let me in.

This EA considers pending orders in your code. Well, it will look through the open orders accordingly. Naturally, it will return zero close time.

If you want to view the closed orders, you have to view them in the list of closed orders:

for (int i=OrdersHistoryTotal()-1; i>=0; i--)

и

if (!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) continue;

But... we need to make two functions - one is looking for open orders and the other is looking for closed orders.

 
artmedia70:

It also takes your pending orders into account. And accordingly it looks through the open orders. Naturally, it will return zero close time.

To view the closed orders, you have to view them in the list of the closed ones:

и

But... we have to make two functions - one is looking for open orders and the other is looking for closed orders.


Thank you, Artyom. Exactly... What's next is a question of code optimisation. There are questions on code structure, but it will have to be written on a fresh head.
 
hoz:

I will gladly look at your code, Artem. For testing the print, it is not the most convenient variant...

Here's an EA to test the function. It contains the function itself and its call. Before calling it, you have to prepare a message, which can be up to four lines. The length of each line, if I remember correctly, should not exceed 64 characters. That's why I've split one message into several lines. Each line of one message can be output in a different colour. It's kind of clear in the code there - first we prepare message lines, then we call the function. I haven't commented much there - I wrote it in one go long ago. Now I have already forgotten what is there for. If I have any questions, I'll remember, but I remember only necessary things now.

//+------------------------------------------------------------------+
//|                                                Test iPrint().mq4 |
//|                             Copyright © 2013, Artyom A. Trishkin |
//|                                                skype: artmedia70 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2013, Artyom A. Trishkin"
#property link      "skype: artmedia70"
//+------------------------------------------------------------------+
//| expert variables                                                 |
//+------------------------------------------------------------------+
int            a,b,c;
string         Exp_Name, Prefix, pref,
               message1, message2, message3, message4, 
               Mass_Name_Message[10];
//+----------------------------------------------------------------------------+
int init() {
   Exp_Name=WindowExpertName(); 
   pref="_r";
   if (IsDemo()) pref="_d";
   if (IsTesting()) pref="_t";
   if (IsVisualMode()) pref="_v";
   Prefix=Exp_Name+"_"+Symbol()+pref;  
//----------------------------
   return;                                         // Выход из init() 
}
//+----------------------------------------------------------------------------+
int deinit() {
   if (!IsTesting()) {
      Comment("");
// -------- Блок удаления всех объектов, построенных на графике --------
      string Name_Del[1]; 
      int Quant_Del=0;                    
      int Quant_Objects=ObjectsTotal();   
      int LenPref=StringLen(Prefix);
      ArrayResize(Name_Del,Quant_Objects);
      for(int k=0; k<Quant_Objects; k++) {
         string Obj_Name=ObjectName(k);   
         string Head=StringSubstr(Obj_Name,0,LenPref);
         if (Head==Prefix) {                              
            Quant_Del+=1;        
            Name_Del[Quant_Del-1]=Obj_Name;
            }
        }
      for(int i=0; i<Quant_Del; i++)    
         ObjectDelete(Name_Del[i]); 
// ----- Конец блока удаления всех объектов, построенных на графике -----
      }
   return;                                // Выход из deinit()
}
//+----------------------------------------------------------------------------+
int start()
  {
//-------------------------------------------------------
// Подготавливаем первое сообщение
   message1=StringConcatenate("Первое сообщение из двух строк: Тик: ",GetTickCount()," ");
   message2=StringConcatenate("Цена Bid: ", DoubleToStr(Bid,Digits));
   iPrint(false, message1, message2, "", "", 9, Aqua, DarkOrange);   // Выводим первое
   Sleep(3000);
//-------------------------------------------------------
// Подготавливаем второе сообщение
   b=4; c=7;
   a=b+c;
   message1=StringConcatenate("Второе сообщение из трёх строк: a=",a," ");
   message2=StringConcatenate("b=",b," ");
   message3=StringConcatenate("c=",c);
   iPrint(false, message1, message2, message3, "", 9, Aqua, LimeGreen, DarkOrange); // Выводим второе
   Sleep(3000);
//-------------------------------------------------------
// Подготавливаем третье сообщение
   message1=StringConcatenate("Третье сообщение из четырёх строк: Время: ", TimeToStr(TimeCurrent())," ");
   message2=StringConcatenate("Тик: ",GetTickCount()," ");
   message3=StringConcatenate("Время бара: ",TimeToStr(Time[0])," ");
   message4=StringConcatenate("Цена Bid: ", DoubleToStr(Bid,Digits));
   iPrint(false, message1, message2, message3, message4, 9, Aqua, Aqua, Aqua, DarkOrange);// Выводим третье
   Sleep(3000);
//-------------------------------------------------------
   return(0);
  }
//+----------------------------------------------------------------------------+
// Если bool print = true или нет окна индикатора, то функция будет выводить сообщения в журнал
void iPrint(bool print, string mess1, string mess2="", string mess3="", string mess4="", 
            int sz=9, color cl1=Aqua, color cl2=Aqua, color cl3=Aqua, color cl4=Aqua) {
   string   NameGrafText, message, nm;
   int      i, y, k, LenStr, shift, Win_Num=-1, num=0;
   color    cl;
   if (mess1=="") {
      Print("Func iPrint: Передана пустая строка, выходим");
      return;
      }
   Win_Num=WindowFind("Win_Inform");
   if (print || 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=ObjectGet(NameGrafText, OBJPROP_YDISTANCE);               // координата Y
            ObjectSet  (NameGrafText, OBJPROP_YDISTANCE, y+(sz+1)*num); // координата Y
            ObjectSet  (NameGrafText, OBJPROP_COLOR, DimGray);          // цвет
            }
         }
      }
   shift=num;
   int v=GetTickCount();
   for (i=0; i<num; i++) {
      shift--;
      NameGrafText=Prefix+"_Graf_Text_"+i+"_"+Symbol()+"_"+v;  // Уникальное имя объекта
      int app=0;
      while (ObjectFind(NameGrafText)==Win_Num) {
         app++;
         NameGrafText=Prefix+"_Graf_Text_"+i+"_"+Symbol()+"_"+v+"_"+app;
         }
      Mass_Name_Message[num-1-i]=NameGrafText;
      switch (i) {
         case 0: message=mess1; cl=cl1; break;
         case 1: message=mess2; cl=cl2; break;
         case 2: message=mess3; cl=cl3; break;
         case 3: message=mess4; cl=cl4; break;
         default:message=mess1; cl=cl1; 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();
      }
//---------------------------------      
   nm=Prefix+"_Balance_txt";
   message="Баланс :";
   cl=Yellow;
   SetText(message, cl, nm, 2, 4, 20, "Arial", 9, Win_Num);
   nm=Prefix+"_Balance";
   message=DoubleToStr(AccountBalance(),2);
   cl=DarkTurquoise;
   SetText(message, cl, nm, 2, 70, 20, "Arial", 9, Win_Num);
//---------------------------------      
   nm=Prefix+"_Equity_txt";
   message="Средства :";
   cl=Yellow;
   SetText(message, cl, nm, 2, 4, 8, "Arial", 9, Win_Num);
   nm=Prefix+"_Equity";
   message=DoubleToStr(AccountEquity(),2);
   cl=DarkTurquoise;
   SetText(message, cl, nm, 2, 70, 8, "Arial", 9, Win_Num);
//---------------------------------      
}
//+----------------------------------------------------------------------------+
void SetText(string Text, color cl, string nm, int angle, int x, int y, string font, int sz=0, int wnd=0) {
   if (ObjectFind(nm)<0) ObjectCreate(nm, OBJ_LABEL, wnd, 0, 0);
   ObjectSet(nm, OBJPROP_CORNER   , angle);
   ObjectSet(nm, OBJPROP_XDISTANCE, x);
   ObjectSet(nm, OBJPROP_YDISTANCE, y);
   ObjectSet(nm, OBJPROP_WIDTH    , sz);
   ObjectSetText(nm, Text, sz, font, cl);
   }
//+----------------------------------------------------------------------------+

And a turkey:

//+------------------------------------------------------------------+
//|                                                   Win_Inform.mq4 |
//|                                Copyright © 2012, Artyom Trishkin |
//|                                                skype: artmedia70 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012, Artyom Trishkin"
#property link      "skype: artmedia70"

#property indicator_separate_window
int start()
  {
  }
//+------------------------------------------------------------------+

That's the way it is... I hope you'll figure it out.

Of course, you can make an underlay instead of outputting it to the main chart, but I am too lazy to do it... :)

 
artmedia70:

Here's an EA to test the function. It contains the function itself and its call. Before calling it, you have to prepare a message, which can be up to four lines. The length of each line, if I remember correctly, should not exceed 64 characters. That's why I've split one message into several lines. Each line of one message can be output in a different colour. It's kind of clear in the code there - first we prepare message lines, then we call the function. I haven't commented much there - I wrote it in one go long ago. Now I have already forgotten what is there for. If I have any questions, I'll remember, but I remember only necessary things now.

And a turkey:

That's the way it is... I hope you'll figure it out.

Of course, you can make an underlay instead of outputting it to the main chart, but I am too lazy to do it... :)


 

Good people! I've written an EA, everything seems to be working fine in real life. But I check it on the tester - it glitches. The gist of it is as follows. The Expert Advisor should open orders by horizontal lines drawn on the chart. In the tester, it opens them at various levels all the time, as if a lot of lines have been set, though there are none! Can you explain what's wrong?

A part of the code responsible for lines:

int New_gorizont()

{

int tip_o_buy;

int tip_o_sell;

int obj_total=ObjectsTotal();

// if no new lines, exit

if(obj_total==0)

return;

// if(there are...

if(obj_total!=0)

string name=ObjectName(0);

double pr=NormalizeDouble(ObjectGet(name,1),Digits);

ObjectDelete(name); // delete and set again

if(pr>Bid+(Stop_level+1)*Point) // determine the order type

{

tip_o_sell=OP_SELLLIMIT;

tip_o_buy=OP_BUYSTOP;

}

if(pr<Bid-(Stop_level+1)*Point)

{

tip_o_sell=OP_SELLSTOP;

tip_o_buy=OP_BUYLIMIT;

}

// set orders

OrderSend(Symb,tip_o_sell,Value,pr,3,pr+StopLoss*Point,pr-TakeProfit*Point, "Order set");

Prov_oshibok();

................................

}

In other words, obj_total==0 and that's it. But it sets and sets everything to infinity for some reason.

 
Hi all.
Is it normal that the log does not show that a pending order has triggered? It happens in the tester.
The account is real.
 
Dozol:

Good people! I have written an EA, everything seems to be working fine in real life, all is normal. But I check it on the tester and it glitches.


I do not believe it, this EA cannot work either on the real site or in the tester.
Reason: