A question for MQL experts - page 8

 
Rita:
Thank you, abolk, for the clarification.


It looks like several functions need to be combined into one. The logic is the same everywhere

 

-Good afternoon.

In our old Expert Advisor we used this opening of positions (I. Kim's function):

if (NumberOfPositions(NULL,OP_BUY,Magic1)<1 ) {//если нет откр. позиций
  ti=OpenPosition(NULL,OP_BUY, Lots,0 ,0,Magic1);
                                              } 
} }//если условия соответствуют заданным      

Now I'm writing another EA and I'm copying this bit of code

and suddenly I get the following message when I try to compile it

I cannot figure out why I get errors! The code is strictly balanced.

What's more! When I remove the parameter ti(which is declared in global variables), the compilation errors disappear:

 if (NumberOfPositions(Symbol_1,-1, MagicNumber) ==1  && 
     NumberOfPositions(Symbol_2,-1, MagicNumber) <1 ) {
//если есть открытая позиция 1 символа, - открываем позицию второго символа: 
//ti=
  OpenPosition(Symbol_2,Positions_Symbol_2, Lots_1,0 ,0,MagicNumber);
                                                          }

 //if ( ti>0 )  
 //MessageBox("Позиции открыты! Не забудьте отключить Start_Trade",0,1 );

But I need exactly with a ticket (ti), in order to realize MessageBox's execution afterwards.

I cannot figure it out since this morning. Please advise where it is inaccurate?

Why all of a sudden I get errors when adding ti= before OpenPosition( - why?


 

OpenPosition() must be declared as void and should be int.

int OpenPosition(....){

....

}
 

Thank you, Integer!

Yes, - that's exactly what happened! Now I've corrected it and it all compiled properly!

 

Afternoon.

The EA suddenly started to print on every tick for no apparent reason:

2012.01.11 22:28:19 Complex_Common_01mod EURCHF,H1: unknown subwindow number -1 for ObjectCreate function

I am calling the custom indicator in the code (see fig.):

//-------------- задаем значения индикатора Complex_Common по первому символу  --------
  double Complex_1_1=iCustom(NULL,Tf,"Complex_Common_01mod",Period_low,Period_fast,m,0); 
 //---------------задаем значения индикатора Complex_Common по второму символу ------------ 
  double Complex_1_2=iCustom(NULL,Tf,"Complex_Common_01mod",Period_low,Period_fast,n,0); 

Could you please tell me where the error is coming from? It wasn't there before. I have changed the code a bit, without even touching the indicator part!

And the journal on every tick started giving me this message...

ObjectCreate function - do not use in code at all

 
The indicator searches for its window name and does not find it. seehttps://docs.mql4.com/ru/customind/IndicatorShortName
 
Rita:

Thank you, Integer!

Yes, - that's exactly what happened! Now I've corrected it and it all compiled properly!

Only, to get the ticket, you need to refine Kim's function.
 
FAQ:
Indicator searches for its window name and doesn't find it. seehttps://docs.mql4.com/ru/customind/IndicatorShortName


So it turns out that this is a defect of the indicator? And not a glitch in the Expert Advisor? I am not sure what to do with it? I don't require in the code of the EA to call the window of this indicator!

Please advise how to fix this defect, if possible. (- unknown subwindow number -1 for ObjectCreate function ) ?

Here is indicator code:

#property copyright "SemSemFX@rambler.ru"
#property link      "http://onix-trade.net/forum/index.php?showtopic=107"
//индикатор  Complex_Common_01mod 

#property indicator_separate_window
#property indicator_buffers 5
#property indicator_color1 Lime
#property indicator_color2 DodgerBlue
#property indicator_color3 Red
#property indicator_color4 Magenta
#property indicator_color5 Aqua
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
#property indicator_width4 2
#property indicator_width5 2

extern int Period_low = 21;
extern int Period_fast = 8;
//---- buffers
double USD[];
double EUR[];
double GBP[];
double CHF[];
double JPY[];

int wndNum;                                // Номер подокна индикатора
string wndName;                            // Наименование окна

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,USD);
   SetIndexLabel(0, "USD"); 
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,EUR);
   SetIndexLabel(1, "EUR"); 
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,GBP);
   SetIndexLabel(2, "GBP"); 
   SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(3,CHF);
   SetIndexLabel(3, "CHF"); 
   SetIndexStyle(4,DRAW_LINE);
   SetIndexBuffer(4,JPY);
   SetIndexLabel(4, "JPY"); 
 
//----
   return(0);
  }

int deinit()  {
  // Удаляем все графические объекты
  DeleteObject("USD"); 
  DeleteObject("EUR");
  DeleteObject("GBP");
  DeleteObject("CHF");
  DeleteObject("JPY"); 
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
     int limit;
     int counted_bars=IndicatorCounted();
  //---- проверка на возможные ошибки
     if(counted_bars<0) return(-1);
  //---- последний посчитанный бар будет пересчитан
     if(counted_bars>0) counted_bars-=10;
     limit=Bars-counted_bars;
  //---- основной цикл
      int Price=6; int Mode=2;
    
     for(int i=0; i<limit; i++)
       {
        USD[i]=
            (iMA("EURUSD",0,Period_low,0,Mode,Price,i)-
            iMA("EURUSD",0,Period_fast,0,Mode,Price,i))*10000
            +
            (iMA("GBPUSD",0,Period_low,0,Mode,Price,i)-
            iMA("GBPUSD",0,Period_fast,0,Mode,Price,i))*10000
            +
            (iMA("USDCHF",0,Period_fast,0,Mode,Price,i)-
            iMA("USDCHF",0,Period_low,0,Mode,Price,i))*10000
            +
            (iMA("USDJPY",0,Period_fast,0,Mode,Price,i)-
            iMA("USDJPY",0,Period_low,0,Mode,Price,i))*100
            ;
        EUR[i]=
            (iMA("EURUSD",0,Period_fast,0,Mode,Price,i)-
            iMA("EURUSD",0,Period_low,0,Mode,Price,i))*10000
            +
            (iMA("EURGBP",0,Period_fast,0,Mode,Price,i)-
            iMA("EURGBP",0,Period_low,0,Mode,Price,i))*10000
            +
            (iMA("EURCHF",0,Period_fast,0,Mode,Price,i)-
            iMA("EURCHF",0,Period_low,0,Mode,Price,i))*10000
            +
            (iMA("EURJPY",0,Period_fast,0,Mode,Price,i)-
            iMA("EURJPY",0,Period_low,0,Mode,Price,i))*100
            ;
        GBP[i]=
            (iMA("GBPUSD",0,Period_fast,0,Mode,Price,i)-
            iMA("GBPUSD",0,Period_low,0,Mode,Price,i))*10000
            +
            (iMA("EURGBP",0,Period_low,0,Mode,Price,i)-
            iMA("EURGBP",0,Period_fast,0,Mode,Price,i))*10000
            +
            (iMA("GBPCHF",0,Period_fast,0,Mode,Price,i)-
            iMA("GBPCHF",0,Period_low,0,Mode,Price,i))*10000
            +
            (iMA("GBPJPY",0,Period_fast,0,Mode,Price,i)-
            iMA("GBPJPY",0,Period_low,0,Mode,Price,i))*100
            ;
        CHF[i]=
            (iMA("USDCHF",0,Period_low,0,Mode,Price,i)-
            iMA("USDCHF",0,Period_fast,0,Mode,Price,i))*10000
            +
            (iMA("EURCHF",0,Period_low,0,Mode,Price,i)-
            iMA("EURCHF",0,Period_fast,0,Mode,Price,i))*10000
            +
            (iMA("GBPCHF",0,Period_low,0,Mode,Price,i)-
            iMA("GBPCHF",0,Period_fast,0,Mode,Price,i))*10000
            +
            (iMA("CHFJPY",0,Period_fast,0,Mode,Price,i)-
            iMA("CHFJPY",0,Period_low,0,Mode,Price,i))*100
            ;
        JPY[i]=
            (iMA("USDJPY",0,Period_low,0,Mode,Price,i)-
            iMA("USDJPY",0,Period_fast,0,Mode,Price,i))*100
            +
            (iMA("EURJPY",0,Period_low,0,Mode,Price,i)-
            iMA("EURJPY",0,Period_fast,0,Mode,Price,i))*100
            +
            (iMA("GBPJPY",0,Period_low,0,Mode,Price,i)-
            iMA("GBPJPY",0,Period_fast,0,Mode,Price,i))*100
            +
            (iMA("CHFJPY",0,Period_low,0,Mode,Price,i)-
            iMA("CHFJPY",0,Period_fast,0,Mode,Price,i))*100
            ;
       }

   object("USD", "USD", 10, indicator_color1, 5);
   object("EUR", "EUR", 10, indicator_color2,18);
   object("GBP", "GBP", 10, indicator_color3,31);
   object("CHF", "CHF", 10, indicator_color4,44);
   object("JPY", "JPY", 10, indicator_color5,57);
  //----
    return(0);
  }
//+------------------------------------------------------------------+


// Удаляем графический объект
void DeleteObject(string name) {
  ObjectDelete(name+WindowExpertName());
}
// Рисуем метку
void object(string name, string _text, int fontsize, color color_, int value) {
 string objName = name+WindowExpertName();
 ObjectCreate(objName, OBJ_LABEL, WindowFind(WindowExpertName()), 0, 0);
 ObjectSetText(objName, _text, fontsize, "Verdana", color_);
 ObjectSet(objName, OBJPROP_CORNER, 1);
 ObjectSet(objName, OBJPROP_XDISTANCE, 4);
 ObjectSet(objName, OBJPROP_YDISTANCE, value); 
}
 
int wndNum;                                // Номер подокна индикатора  /// И ЭТО ТОЖЕ НЕ ИСПОЛЬЗУЕТЕ ВИДИМО ОСТАЛОСЬ ОТ ПРЕДЫДУЩЕГО ПИСАТЕЛЯ

string wndName=????;                            // Наименование окна /// ЧТО ЭТО ?????

// Удаляем графический объект
void DeleteObject(string name) {
  ObjectDelete(name+WindowExpertName());// Вы нашли окно предварительно? 
}
// Рисуем метку
void object(string name, string _text, int fontsize, color color_, int value) {
 string objName = name+WindowExpertName();
 ObjectCreate(objName, OBJ_LABEL, WindowFind(WindowExpertName()), 0, 0);// То же самое
 ObjectSetText(objName, _text, fontsize, "Verdana", color_);
 ObjectSet(objName, OBJPROP_CORNER, 1);
 ObjectSet(objName, OBJPROP_XDISTANCE, 4);
 ObjectSet(objName, OBJPROP_YDISTANCE, value); 
}
Don't tie the window name to the object name, just name the objects with unique names.
 
FAQ:
Don't tie the window name to the object name, just name the objects with unique names.

Thank you, I think I got it - I'll try naming and finding the window now.